1
  $   ˆl–ß=¿J*j"TåÀ·p.©‹FÿÄ…e¦ÛeÿÃÂ ?÷     import libxml2mod
import types
import sys

# The root of all libxml2 errors.
class libxmlError(Exception): pass

# Type of the wrapper class for the C objects wrappers
def checkWrapper(obj):
    try:
        n = type(_obj).__name__
        if n != 'PyCObject' and n != 'PyCapsule':
            return 1
    except:
        return 0
    return 0

#
# id() is sometimes negative ...
#
def pos_id(o):
    i = id(o)
    if (i < 0):
        return (sys.maxsize - i)
    return i

#
# Errors raised by the wrappers when some tree handling failed.
#
class treeError(libxmlError):
    def __init__(self, msg):
        self.msg = msg
    def __str__(self):
        return self.msg

class parserError(libxmlError):
    def __init__(self, msg):
        self.msg = msg
    def __str__(self):
        return self.msg

class uriError(libxmlError):
    def __init__(self, msg):
        self.msg = msg
    def __str__(self):
        return self.msg

class xpathError(libxmlError):
    def __init__(self, msg):
        self.msg = msg
    def __str__(self):
        return self.msg

class ioWrapper:
    def __init__(self, _obj):
        self.__io = _obj
        self._o = None

    def io_close(self):
        if self.__io == None:
            return(-1)
        self.__io.close()
        self.__io = None
        return(0)

    def io_flush(self):
        if self.__io == None:
            return(-1)
        self.__io.flush()
        return(0)

    def io_read(self, len = -1):
        if self.__io == None:
            return(-1)
        try:
            if len < 0:
                ret = self.__io.read()
            else:
                ret = self.__io.read(len)
        except Exception:
            import sys
            e = sys.exc_info()[1]
            print("failed to read from Python:", type(e))
            print("on IO:", self.__io)
            self.__io == None
            return(-1)

        return(ret)

    def io_write(self, str, len = -1):
        if self.__io == None:
            return(-1)
        if len < 0:
            return(self.__io.write(str))
        return(self.__io.write(str, len))

class ioReadWrapper(ioWrapper):
    def __init__(self, _obj, enc = ""):
        ioWrapper.__init__(self, _obj)
        self._o = libxml2mod.xmlCreateInputBuffer(self, enc)

    def __del__(self):
        print("__del__")
        self.io_close()
        if self._o != None:
            libxml2mod.xmlFreeParserInputBuffer(self._o)
        self._o = None

    def close(self):
        self.io_close()
        if self._o != None:
            libxml2mod.xmlFreeParserInputBuffer(self._o)
        self._o = None

class ioWriteWrapper(ioWrapper):
    def __init__(self, _obj, enc = ""):
#        print "ioWriteWrapper.__init__", _obj
        if type(_obj) == type(''):
            print("write io from a string")
            self.o = None
        elif type(_obj).__name__ == 'PyCapsule':
            file = libxml2mod.outputBufferGetPythonFile(_obj)
            if file != None:
                ioWrapper.__init__(self, file)
            else:
                ioWrapper.__init__(self, _obj)
            self._o = _obj
#        elif type(_obj) == types.InstanceType:
#            print(("write io from instance of %s" % (_obj.__class__)))
#            ioWrapper.__init__(self, _obj)
#            self._o = libxml2mod.xmlCreateOutputBuffer(self, enc)
        else:
            file = libxml2mod.outputBufferGetPythonFile(_obj)
            if file != None:
                ioWrapper.__init__(self, file)
            else:
                ioWrapper.__init__(self, _obj)
            self._o = _obj

    def __del__(self):
#        print "__del__"
        self.io_close()
        if self._o != None:
            libxml2mod.xmlOutputBufferClose(self._o)
        self._o = None

    def flush(self):
        self.io_flush()
        if self._o != None:
            libxml2mod.xmlOutputBufferClose(self._o)
        self._o = None

    def close(self):
        self.io_flush()
        if self._o != None:
            libxml2mod.xmlOutputBufferClose(self.