Exemplo n.º 1
0
 def setcontents(self, value):
     if not isinstance(value, self._type_):
         raise TypeError("expected %s instead of %s" % (self._type_.__name__, type(value).__name__))
     self._objects = {keepalive_key(1): value}
     if value._ensure_objects() is not None:
         self._objects[keepalive_key(0)] = value._objects
     value = value._buffer
     self._buffer[0] = value
Exemplo n.º 2
0
 def setcontents(self, value):
     if not isinstance(value, self._type_):
         raise TypeError("expected %s instead of %s" % (
             self._type_.__name__, type(value).__name__))
     self._objects = {keepalive_key(1):value}
     if value._ensure_objects() is not None:
         self._objects[keepalive_key(0)] = value._objects
     value = value._buffer
     self._buffer[0] = value
Exemplo n.º 3
0
 def __init__(self, argument=None):
     self.callable = None
     self.name = None
     self._objects = {keepalive_key(0): self}
     if isinstance(argument, int):
         self._buffer = _rawffi.Array('P').fromaddress(argument, 1)
         # XXX finish this one, we need to be able to jump there somehow
     elif callable(argument):
         self.callable = argument
         argtypes = [arg._ffiargshape for arg in self._argtypes_]
         restype = self._restype_._ffiletter
         self._ptr = _rawffi.CallbackPtr(argument, argtypes, restype)
         self._needs_free = True
         self._buffer = self._ptr.byptr()
     elif isinstance(argument, tuple) and len(argument) == 2:
         import ctypes
         self.name, self.dll = argument
         if isinstance(self.dll, str):
             self.dll = ctypes.CDLL(self.dll)
         # we need to check dll anyway
         self._getfuncptr([], ctypes.c_int)
     elif argument is None:
         return  # needed for test..
     else:
         raise TypeError("Unknown constructor %s" % (argument, ))
Exemplo n.º 4
0
 def __init__(self, argument=None):
     self.callable = None
     self.name = None
     self._objects = {keepalive_key(0):self}
     if isinstance(argument, int):
         self._buffer = _rawffi.Array('P').fromaddress(argument, 1)
         # XXX finish this one, we need to be able to jump there somehow
     elif callable(argument):
         self.callable = argument
         argtypes = [arg._ffiargshape for arg in self._argtypes_]
         restype = self._restype_._ffiletter
         self._ptr = _rawffi.CallbackPtr(argument, argtypes, restype)
         self._needs_free = True
         self._buffer = self._ptr.byptr()
     elif isinstance(argument, tuple) and len(argument) == 2:
         import ctypes
         self.name, self.dll = argument
         if isinstance(self.dll, str):
             self.dll = ctypes.CDLL(self.dll)
         # we need to check dll anyway
         self._getfuncptr([], ctypes.c_int)
     elif argument is None:
         return # needed for test..
     else:
         raise TypeError("Unknown constructor %s" % (argument,))
Exemplo n.º 5
0
 def __init__(self, argument=None):
     self.name = None
     self._objects = {keepalive_key(0): self}
     self._needs_free = True
     if isinstance(argument, (int, long)):
         ffiargs, ffires = self._ffishapes(self._argtypes_, self._restype_)
         self._ptr = _rawffi.FuncPtr(argument, ffiargs, ffires)
         self._buffer = self._ptr.byptr()
     elif callable(argument):
         self.callable = argument
         ffiargs, ffires = self._ffishapes(self._argtypes_, self._restype_)
         self._ptr = _rawffi.CallbackPtr(
             self._wrap_callable(argument, self.argtypes), ffiargs, ffires)
         self._buffer = self._ptr.byptr()
     elif isinstance(argument, tuple) and len(argument) == 2:
         import ctypes
         self.name, self.dll = argument
         if isinstance(self.dll, str):
             self.dll = ctypes.CDLL(self.dll)
         # we need to check dll anyway
         ptr = self._getfuncptr([], ctypes.c_int)
         self._buffer = ptr.byptr()
     elif argument is None:
         # this is needed for casts
         self._buffer = _rawffi.Array('P')(1)
         return
     else:
         raise TypeError("Unknown constructor %s" % (argument, ))
 def __set__(self, obj, value):
     fieldtype = self.ctype
     cobj = fieldtype.from_param(value)
     if ensure_objects(cobj) is not None:
         key = keepalive_key(self.num)
         store_reference(obj, key, cobj._objects)
     arg = cobj._get_buffer_value()
     if fieldtype._fficompositesize is not None:
         from ctypes import memmove
         dest = obj._buffer.fieldaddress(self.name)
         memmove(dest, arg, fieldtype._fficompositesize)
     else:
         obj._buffer.__setattr__(self.name, arg)
Exemplo n.º 7
0
 def __set__(self, obj, value):
     fieldtype = self.ctype
     cobj = fieldtype.from_param(value)
     if ensure_objects(cobj) is not None:
         key = keepalive_key(self.num)
         store_reference(obj, key, cobj._objects)
     arg = cobj._get_buffer_value()
     if fieldtype._fficompositesize is not None:
         from ctypes import memmove
         dest = obj._buffer.fieldaddress(self.name)
         memmove(dest, arg, fieldtype._fficompositesize)
     else:
         obj._buffer.__setattr__(self.name, arg)
Exemplo n.º 8
0
    def __init__(self, *args):
        self.name = None
        self._objects = {keepalive_key(0): self}
        self._needs_free = True
        argument = None
        if len(args) == 1:
            argument = args[0]

        if isinstance(argument, (int, long)):
            # direct construction from raw address
            ffiargs, ffires = self._ffishapes(self._argtypes_, self._restype_)
            self._ptr = _rawffi.FuncPtr(argument, ffiargs, ffires,
                                        self._flags_)
            self._buffer = self._ptr.byptr()
        elif callable(argument):
            # A callback into python
            self.callable = argument
            ffiargs, ffires = self._ffishapes(self._argtypes_, self._restype_)
            self._ptr = _rawffi.CallbackPtr(
                self._wrap_callable(argument, self.argtypes), ffiargs, ffires,
                self._flags_)
            self._buffer = self._ptr.byptr()
        elif isinstance(argument, tuple) and len(argument) == 2:
            # function exported from a shared library
            import ctypes
            self.name, self.dll = argument
            if isinstance(self.dll, str):
                self.dll = ctypes.CDLL(self.dll)
            # we need to check dll anyway
            ptr = self._getfuncptr([], ctypes.c_int)
            self._buffer = ptr.byptr()

        elif (sys.platform == 'win32' and len(args) >= 2
              and isinstance(args[0], (int, long))):
            # A COM function call, by index
            ffiargs, ffires = self._ffishapes(self._argtypes_, self._restype_)
            self._com_index = args[0] + 0x1000
            self.name = args[1]
            if len(args) > 2:
                self._paramflags = args[2]
            # XXX ignored iid = args[3]

        elif len(args) == 0:
            # Empty function object.
            # this is needed for casts
            self._buffer = _rawffi.Array('P')(1)
            return
        else:
            raise TypeError("Unknown constructor %s" % (args, ))
Exemplo n.º 9
0
    def __init__(self, *args):
        self.name = None
        self._objects = {keepalive_key(0):self}
        self._needs_free = True
        argument = None
        if len(args) == 1:
            argument = args[0]

        if isinstance(argument, (int, long)):
            # direct construction from raw address
            ffiargs, ffires = self._ffishapes(self._argtypes_, self._restype_)
            self._ptr = _rawffi.FuncPtr(argument, ffiargs, ffires,
                                        self._flags_)
            self._buffer = self._ptr.byptr()
        elif callable(argument):
            # A callback into python
            self.callable = argument
            ffiargs, ffires = self._ffishapes(self._argtypes_, self._restype_)
            self._ptr = _rawffi.CallbackPtr(self._wrap_callable(argument,
                                                                self.argtypes),
                                            ffiargs, ffires, self._flags_)
            self._buffer = self._ptr.byptr()
        elif isinstance(argument, tuple) and len(argument) == 2:
            # function exported from a shared library
            import ctypes
            self.name, self.dll = argument
            if isinstance(self.dll, str):
                self.dll = ctypes.CDLL(self.dll)
            # we need to check dll anyway
            ptr = self._getfuncptr([], ctypes.c_int)
            self._buffer = ptr.byptr()

        elif (sys.platform == 'win32' and
              len(args) >= 2 and isinstance(args[0], (int, long))):
            # A COM function call, by index
            ffiargs, ffires = self._ffishapes(self._argtypes_, self._restype_)
            self._com_index =  args[0] + 0x1000
            self.name = args[1]
            if len(args) > 2:
                self._paramflags = args[2]
            # XXX ignored iid = args[3]

        elif len(args) == 0:
            # Empty function object.
            # this is needed for casts
            self._buffer = _rawffi.Array('P')(1)
            return
        else:
            raise TypeError("Unknown constructor %s" % (args,))
Exemplo n.º 10
0
 def __setattr__(self, name, value):
     try:
         fieldtype = self._fieldtypes[name].ctype
     except KeyError:
         return _CData.__setattr__(self, name, value)
     if ensure_objects(value) is not None:
         key = keepalive_key(getattr(self.__class__, name).num)
         store_reference(self, key, value._objects)
     arg = fieldtype._CData_value(value)
     if fieldtype._fficompositesize is not None:
         from ctypes import memmove
         dest = self._buffer.fieldaddress(name)
         memmove(dest, arg, fieldtype._fficompositesize)
     else:
         self._buffer.__setattr__(name, arg)
Exemplo n.º 11
0
 def __setattr__(self, name, value):
     try:
         fieldtype = self._fieldtypes[name].ctype
     except KeyError:
         return _CData.__setattr__(self, name, value)
     if ensure_objects(value) is not None:
         key = keepalive_key(getattr(self.__class__, name).num)
         store_reference(self, key, value._objects)
     arg = fieldtype._CData_value(value)
     if fieldtype._fficompositesize is not None:
         from ctypes import memmove
         dest = self._buffer.fieldaddress(name)
         memmove(dest, arg, fieldtype._fficompositesize)
     else:
         self._buffer.__setattr__(name, arg)
Exemplo n.º 12
0
 def __setattr__(self, name, value):
     try:
         fieldtype = self._fieldtypes[name].ctype
     except KeyError:
         raise AttributeError(name)
     if getattr(value, '_objects', None):
         key = keepalive_key(getattr(self.__class__, name).offset)
         store_reference(self, key, value._objects)
     arg = fieldtype._CData_value(value)
     if isinstance(fieldtype._ffishape, tuple):
         from ctypes import memmove
         dest = self._buffer.fieldaddress(name)
         memmove(dest, arg._buffer.buffer, fieldtype._ffishape[0])
     else:
         self._buffer.__setattr__(name, arg)
Exemplo n.º 13
0
 def __setattr__(self, name, value):
     try:
         fieldtype = self._fieldtypes[name].ctype
     except KeyError:
         raise AttributeError(name)
     if getattr(value, '_objects', None):
         key = keepalive_key(getattr(self.__class__, name).offset)
         store_reference(self, key, value._objects)
     arg = fieldtype._CData_value(value)
     if isinstance(fieldtype._ffishape, tuple):
         from ctypes import memmove
         dest = self._buffer.fieldaddress(name)
         memmove(dest, arg._buffer.buffer, fieldtype._ffishape[0])
     else:
         self._buffer.__setattr__(name, arg)
Exemplo n.º 14
0
 def __set__(self, obj, value):
     fieldtype = self.ctype
     cobj = fieldtype.from_param(value)
     key = keepalive_key(self.num)
     if issubclass(fieldtype, _Pointer) and isinstance(cobj, Array):
         # if our value is an Array we need the whole thing alive
         store_reference(obj, key, cobj)
     elif ensure_objects(cobj) is not None:
         store_reference(obj, key, cobj._objects)
     arg = cobj._get_buffer_value()
     if fieldtype._fficompositesize is not None:
         from ctypes import memmove
         dest = obj._buffer.fieldaddress(self.name)
         memmove(dest, arg, fieldtype._fficompositesize)
     else:
         obj._buffer.__setattr__(self.name, arg)
Exemplo n.º 15
0
 def __set__(self, obj, value):
     fieldtype = self.ctype
     cobj = fieldtype.from_param(value)
     key = keepalive_key(self.num)
     if issubclass(fieldtype, _Pointer) and isinstance(cobj, Array):
         # if our value is an Array we need the whole thing alive
         store_reference(obj, key, cobj)
     elif ensure_objects(cobj) is not None:
         store_reference(obj, key, cobj._objects)
     arg = cobj._get_buffer_value()
     if fieldtype._fficompositesize is not None:
         from ctypes import memmove
         dest = obj._buffer.fieldaddress(self.name)
         memmove(dest, arg, fieldtype._fficompositesize)
     else:
         obj._buffer.__setattr__(self.name, arg)
Exemplo n.º 16
0
 def __setattr__(self, name, value):
     try:
         fieldtype = self._fieldtypes[name].ctype
     except KeyError:
         raise AttributeError(name)
     if ensure_objects(value) is not None:
         key = keepalive_key(getattr(self.__class__, name).num)
         store_reference(self, key, value._objects)
     arg = fieldtype._CData_value(value)
     if fieldtype._fficompositesize is not None:
         from ctypes import memmove
         dest = self._buffer.buffer
         memmove(dest, arg, fieldtype._fficompositesize)
     else:
         buf = self._ffiarrays[name].fromaddress(self._buffer.buffer, 1)
         buf[0] = arg
Exemplo n.º 17
0
 def __setattr__(self, name, value):
     try:
         field = self._fieldtypes[name]
     except KeyError:
         return _CData.__setattr__(self, name, value)
     fieldtype = field.ctype
     cobj = fieldtype.from_param(value)
     if ensure_objects(cobj) is not None:
         key = keepalive_key(field.num)
         store_reference(self, key, cobj._objects)
     arg = cobj._get_buffer_value()
     if fieldtype._fficompositesize is not None:
         from ctypes import memmove
         dest = self._buffer.fieldaddress(name)
         memmove(dest, arg, fieldtype._fficompositesize)
     else:
         self._buffer.__setattr__(name, arg)
Exemplo n.º 18
0
 def __setattr__(self, name, value):
     try:
         field = self._fieldtypes[name]
     except KeyError:
         return _CData.__setattr__(self, name, value)
     fieldtype = field.ctype
     cobj = fieldtype.from_param(value)
     if ensure_objects(cobj) is not None:
         key = keepalive_key(field.num)
         store_reference(self, key, cobj._objects)
     arg = cobj._get_buffer_value()
     if fieldtype._fficompositesize is not None:
         from ctypes import memmove
         dest = self._buffer.fieldaddress(name)
         memmove(dest, arg, fieldtype._fficompositesize)
     else:
         self._buffer.__setattr__(name, arg)
Exemplo n.º 19
0
 def __setattr__(self, name, value):
     try:
         field = self._fieldtypes[name]
     except KeyError:
         raise AttributeError(name)
     fieldtype = field.ctype
     cobj = fieldtype.from_param(value)
     if ensure_objects(cobj) is not None:
         key = keepalive_key(field.num)
         store_reference(self, key, cobj._objects)
     arg = cobj._get_buffer_value()
     if fieldtype._fficompositesize is not None:
         from ctypes import memmove
         dest = self._buffer.buffer
         memmove(dest, arg, fieldtype._fficompositesize)
     else:
         buf = self._ffiarrays[name].fromaddress(self._buffer.buffer, 1)
         buf[0] = arg
Exemplo n.º 20
0
 def __setattr__(self, name, value):
     try:
         field = self._fieldtypes[name]
     except KeyError:
         raise AttributeError(name)
     fieldtype = field.ctype
     cobj = fieldtype.from_param(value)
     if ensure_objects(cobj) is not None:
         key = keepalive_key(field.num)
         store_reference(self, key, cobj._objects)
     arg = cobj._get_buffer_value()
     if fieldtype._fficompositesize is not None:
         from ctypes import memmove
         dest = self._buffer.buffer
         memmove(dest, arg, fieldtype._fficompositesize)
     else:
         buf = self._ffiarrays[name].fromaddress(self._buffer.buffer, 1)
         buf[0] = arg
Exemplo n.º 21
0
 def __set__(self, obj, value):
     if self.inside_anon_field is not None:
         setattr(self.inside_anon_field.__get__(obj), self.name, value)
         return
     fieldtype = self.ctype
     cobj = fieldtype.from_param(value)
     key = keepalive_key(self.num)
     if issubclass(fieldtype, _Pointer) and isinstance(cobj, Array):
         # if our value is an Array we need the whole thing alive
         store_reference(obj, key, cobj)
     elif ensure_objects(cobj) is not None:
         store_reference(obj, key, cobj._objects)
     arg = cobj._get_buffer_value()
     if fieldtype._fficompositesize_ is not None:
         from ctypes import memmove
         dest = obj._buffer.fieldaddress(self.name)
         memmove(dest, arg, fieldtype._fficompositesize_)
     elif not isinstance(obj, _CData):
         raise (TypeError, 'not a ctype instance')
     else:
         obj._buffer.__setattr__(self.name, arg)
Exemplo n.º 22
0
    def __init__(self, *args):
        self.name = None
        self._objects = {keepalive_key(0):self}
        self._needs_free = True

        # Empty function object -- this is needed for casts
        if not args:
            self._set_address(0)
            return

        argsl = list(args)
        argument = argsl.pop(0)

        # Direct construction from raw address
        if isinstance(argument, (int, long)) and not argsl:
            self._set_address(argument)
            restype = self._restype_
            if restype is None:
                import ctypes
                restype = ctypes.c_int
            if self._argtypes_ is None:
                self._argtypes_ = []
            self._ptr = self._getfuncptr_fromaddress(self._argtypes_, restype)
            self._check_argtypes_for_fastpath()
            return


        # A callback into python
        if callable(argument) and not argsl:
            self.callable = argument
            ffiargs, ffires = self._ffishapes(self._argtypes_, self._restype_)
            if self._restype_ is None:
                ffires = None
            self._ptr = _rawffi.CallbackPtr(self._wrap_callable(argument,
                                                                self.argtypes),
                                            ffiargs, ffires, self._flags_)
            self._buffer = self._ptr.byptr()
            return

        # Function exported from a shared library
        if isinstance(argument, tuple) and len(argument) == 2:
            import ctypes
            self.name, dll = argument
            if isinstance(dll, str):
                self.dll = ctypes.CDLL(self.dll)
            else:
                self.dll = dll
            if argsl:
                self.paramflags = argsl.pop(0)
                if argsl:
                    raise TypeError("Unknown constructor %s" % (args,))
            # We need to check dll anyway
            ptr = self._getfuncptr([], ctypes.c_int)
            self._set_address(ptr.getaddr())
            return

        # A COM function call, by index
        if (sys.platform == 'win32' and isinstance(argument, (int, long))
            and argsl):
            ffiargs, ffires = self._ffishapes(self._argtypes_, self._restype_)
            self._com_index =  argument + 0x1000
            self.name = argsl.pop(0)
            if argsl:
                self.paramflags = argsl.pop(0)
                if argsl:
                    self._com_iid = argsl.pop(0)
                    if argsl:
                        raise TypeError("Unknown constructor %s" % (args,))
            return

        raise TypeError("Unknown constructor %s" % (args,))
Exemplo n.º 23
0
    def __init__(self, *args):
        self.name = None
        self._objects = {keepalive_key(0): self}
        self._needs_free = True

        # Empty function object -- this is needed for casts
        if not args:
            self._set_address(0)
            return

        argsl = list(args)
        argument = argsl.pop(0)

        # Direct construction from raw address
        if isinstance(argument, int) and not argsl:
            self._set_address(argument)
            restype = self._restype_
            if restype is None:
                import ctypes
                restype = ctypes.c_int
            if self._argtypes_ is None:
                self._argtypes_ = []
            self._ptr = self._getfuncptr_fromaddress(self._argtypes_, restype)
            return

        # A callback into python
        if callable(argument) and not argsl:
            self.callable = argument
            ffiargs, ffires = self._ffishapes(self._argtypes_, self._restype_)
            if self._restype_ is None:
                ffires = None
            self._ptr = _rawffi.CallbackPtr(
                self._wrap_callable(argument, self.argtypes), ffiargs, ffires,
                self._flags_)
            self._buffer = self._ptr.byptr()
            return

        # Function exported from a shared library
        if isinstance(argument, tuple) and len(argument) == 2:
            import ctypes
            self.name, dll = argument
            if isinstance(dll, str):
                self.dll = ctypes.CDLL(self.dll)
            else:
                self.dll = dll
            if argsl:
                self.paramflags = argsl.pop(0)
                if argsl:
                    raise TypeError("Unknown constructor %s" % (args, ))
            # We need to check dll anyway
            ptr = self._getfuncptr([], ctypes.c_int)
            self._set_address(ptr.getaddr())
            return

        # A COM function call, by index
        if (sys.platform == 'win32' and isinstance(argument, int) and argsl):
            ffiargs, ffires = self._ffishapes(self._argtypes_, self._restype_)
            self._com_index = argument + 0x1000
            self.name = argsl.pop(0)
            if argsl:
                self.paramflags = argsl.pop(0)
                if argsl:
                    self._com_iid = argsl.pop(0)
                    if argsl:
                        raise TypeError("Unknown constructor %s" % (args, ))
            return

        raise TypeError("Unknown constructor %s" % (args, ))