def populate(): from _expat import ffi, lib for name, value in lib.__dict__.items(): if name.startswith('XML_ERROR_'): c_str = lib.XML_ErrorString(value) globals()[name] = ffi.string(c_str) __all__.append(name)
def GetBase(self): """GetBase() -> url Return base URL string for the parser. """ base = lib.XML_GetBase(self._parser) if base == ffi.NULL: return None return ffi.string(base)
def _features(): features = [] f = lib.XML_GetFeatureList() for i in itertools.count(): if f[i].feature == 0: break features.append((ffi.string(f[i].name), f[i].value)) return features
def convert_model(model): """Convert a XML_Content struct into python tuples""" children = [] for i in range(model.numchildren): children.append(convert_model(model.children[i])) if model.name == ffi.NULL: name = None else: name = ffi.string(model.name) return (model.type, model.quant, name, tuple(children))
def _set_error(self, code): lineno = lib.XML_GetErrorLineNumber(self._parser) column = lib.XML_GetErrorColumnNumber(self._parser) err_string = ffi.string(lib.XML_ErrorString(code)) message = '%s: line %i, column %i' % (err_string, lineno, column) e = ExpatError(message) e.code = code e.lineno = lineno e.offset = column raise e
def my_UnknownEncodingHandler(userdata, name, info): parser = ffi.from_handle(userdata) name = ffi.string(name) try: parser.UnknownEncodingHandler(name, info) return lib.XML_STATUS_OK except Exception as e: if not parser._exc_info: parser._exc_info = sys.exc_info() lib.XML_StopParser(parser._parser, lib.XML_FALSE) return lib.XML_STATUS_ERROR
def GetInputContext(self): """GetInputContext() -> string Return the untranslated text of the input that caused the current event. If the event was generated by a large amount of text (such as a start tag for an element with many attributes), not all of the text may be available. """ offset = ffi.new('int *') size = ffi.new('int *') context = lib.XML_GetInputContext(self._parser, offset, size) offset = offset[0] size = size[0] if context == ffi.NULL: return None return ffi.string(context[offset:size])
def _string(self, cdata, maxlen=-1, intern_=False): """Turn cdata (up to maxlen bytes) into a Python string. If we're in unicode mode, decode. If requested, intern the result. We can't really intern unicode strings with pure Python, but we can achieve the same result, quite easily. """ str_ = ffi.string(cdata, maxlen) if intern_: str_ = intern(str_) if str_ in self._intern: return self._intern[str_] value = str_ if self.returns_unicode: value = value.decode(native_encoding) if intern_: self._intern[str_] = value return value
def my_ExternalEntityRefHandler(parser, context, base, systemId, publicId): if context != ffi.NULL: context = ffi.string(context) return (context, base, systemId, publicId)
def ErrorString(errno): """ErrorString(errno) -> string Returns string error for given number.""" return ffi.string(lib.XML_ErrorString(errno))
'error', 'errors', 'features', 'model', 'native_encoding', 'version_info', ] def _version_info(): info = lib.XML_ExpatVersionInfo() return (info.major, info.minor, info.micro) version_info = _version_info() EXPAT_VERSION = ffi.string(lib.XML_ExpatVersion()) __version__ = sys.version.split()[0] # When Expat supports some way of figuring out how it was # compiled, this should check and set native_encoding # appropriately. native_encoding = 'UTF-8' # Explicitly passing None means no interning is desired. # Not passing anything means that a new dictionary is used. INTERN_NEWDICT = object() def _features(): features = [] f = lib.XML_GetFeatureList()