| 1 |
|
| 2 |
import _rawffi |
| 3 |
from _rawffi import alt as _ffi |
| 4 |
from _ctypes.basics import _CData, _CDataMeta, cdata_from_address, ArgumentError |
| 5 |
from _ctypes.basics import keepalive_key, store_reference, ensure_objects |
| 6 |
from _ctypes.basics import sizeof, byref, as_ffi_pointer |
| 7 |
from _ctypes.array import Array, array_get_slice_params, array_slice_getitem,\ |
| 8 |
array_slice_setitem |
| 9 |
|
| 10 |
from __pypy__ import builtinify |
| 11 |
|
| 12 |
|
| 13 |
_pointer_type_cache = {} |
| 14 |
|
| 15 |
DEFAULT_VALUE = object() |
| 16 |
|
| 17 |
class PointerType(_CDataMeta): |
| 18 |
def __new__(self, name, cls, typedict): |
| 19 |
d = dict( |
| 20 |
size = _rawffi.sizeof('P'), |
| 21 |
align = _rawffi.alignment('P'), |
| 22 |
length = 1, |
| 23 |
_ffiargshape_ = 'P', |
| 24 |
_ffishape_ = 'P', |
| 25 |
_fficompositesize_ = None, |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
obj = type.__new__(self, name, cls, typedict) |
| 30 |
for k, v in d.items(): |
| 31 |
setattr(obj, k, v) |
| 32 |
if '_type_' in typedict: |
| 33 |
self.set_type(obj, typedict['_type_']) |
| 34 |
else: |
| 35 |
def __init__(self, value=None): |
| 36 |
raise TypeError("%s has no type" % obj) |
| 37 |
obj.__init__ = __init__ |
| 38 |
return obj |
| 39 |
|
| 40 |
def from_param(self, value): |
| 41 |
if value is None: |
| 42 |
return self(None) |
| 43 |
if isinstance(value, self): |
| 44 |
return value |
| 45 |
if hasattr(self, '_type_'): |
| 46 |
|
| 47 |
|
| 48 |
if isinstance(value, self._type_): |
| 49 |
return byref(value) |
| 50 |
|
| 51 |
if isinstance(value, (_Pointer, Array)): |
| 52 |
if issubclass(type(value)._type_, self._type_): |
| 53 |
return value |
| 54 |
return _CDataMeta.from_param(self, value) |
| 55 |
|
| 56 |
def _sizeofinstances(self): |
| 57 |
return _rawffi.sizeof('P') |
| 58 |
|
| 59 |
def _alignmentofinstances(self): |
| 60 |
return _rawffi.alignment('P') |
| 61 |
|
| 62 |
def _is_pointer_like(self): |
| 63 |
return True |
| 64 |
|
| 65 |
def set_type(self, TP): |
| 66 |
if self._is_abstract(): |
| 67 |
raise TypeError('abstract class') |
| 68 |
ffiarray = _rawffi.Array('P') |
| 69 |
def __init__(self, value=None): |
| 70 |
if not hasattr(self, '_buffer'): |
| 71 |
self._buffer = ffiarray(1, autofree=True) |
| 72 |
if value is not None: |
| 73 |
self.contents = value |
| 74 |
def _init_no_arg_(self): |
| 75 |
self._buffer = ffiarray(1, autofree=True) |
| 76 |
self._ffiarray = ffiarray |
| 77 |
self.__init__ = __init__ |
| 78 |
self._init_no_arg_ = _init_no_arg_ |
| 79 |
self._type_ = TP |
| 80 |
|
| 81 |
def _build_ffiargtype(self): |
| 82 |
return _ffi.types.Pointer(self._type_.get_ffi_argtype()) |
| 83 |
|
| 84 |
def _deref_ffiargtype(self): |
| 85 |
return self._type_.get_ffi_argtype() |
| 86 |
|
| 87 |
from_address = cdata_from_address |
| 88 |
|
| 89 |
def _getformat(self): |
| 90 |
return '&' + self._type_._getformat() |
| 91 |
|
| 92 |
class _Pointer(_CData, metaclass=PointerType): |
| 93 |
|
| 94 |
def getcontents(self): |
| 95 |
addr = self._buffer[0] |
| 96 |
if addr == 0: |
| 97 |
raise ValueError("NULL pointer access") |
| 98 |
instance = self._type_.from_address(addr) |
| 99 |
instance.__dict__['_base'] = self |
| 100 |
return instance |
| 101 |
|
| 102 |
def setcontents(self, value): |
| 103 |
if not (isinstance(value, _CData) or isinstance(value, self._type_)): |
| 104 |
raise TypeError("expected %s instead of %s" % ( |
| 105 |
self._type_.__name__, type(value).__name__)) |
| 106 |
self._objects = {keepalive_key(1):value} |
| 107 |
if value._ensure_objects() is not None: |
| 108 |
self._objects[keepalive_key(0)] = value._objects |
| 109 |
value = value._buffer |
| 110 |
self._buffer[0] = value |
| 111 |
|
| 112 |
_get_slice_params = array_get_slice_params |
| 113 |
_slice_getitem = array_slice_getitem |
| 114 |
|
| 115 |
def _subarray(self, index=0): |
| 116 |
"""Return a _rawffi array of length 1 whose address is the same as |
| 117 |
the index'th item to which self is pointing.""" |
| 118 |
address = self._buffer[0] |
| 119 |
address += index * sizeof(self._type_) |
| 120 |
return self._type_.from_address(address)._buffer |
| 121 |
|
| 122 |
def __getitem__(self, index): |
| 123 |
if isinstance(index, slice): |
| 124 |
return self._slice_getitem(index) |
| 125 |
return self._type_._CData_output(self._subarray(index), self, index) |
| 126 |
|
| 127 |
def __setitem__(self, index, value): |
| 128 |
cobj = self._type_.from_param(value) |
| 129 |
if ensure_objects(cobj) is not None: |
| 130 |
store_reference(self, index, cobj._objects) |
| 131 |
address = self._buffer[0] |
| 132 |
address += index * sizeof(self._type_) |
| 133 |
cobj._copy_to(address) |
| 134 |
|
| 135 |
def __bool__(self): |
| 136 |
return self._buffer[0] != 0 |
| 137 |
|
| 138 |
contents = property(getcontents, setcontents) |
| 139 |
_obj = property(getcontents) |
| 140 |
|
| 141 |
def _as_ffi_pointer_(self, ffitype): |
| 142 |
return as_ffi_pointer(self, ffitype) |
| 143 |
|
| 144 |
def _cast_addr(ptr, obj, tp): |
| 145 |
if not (isinstance(tp, _CDataMeta) and tp._is_pointer_like()): |
| 146 |
raise TypeError("cast() argument 2 must be a pointer type, not %s" |
| 147 |
% (tp,)) |
| 148 |
result = tp._newowninstance_() |
| 149 |
if isinstance(obj, int): |
| 150 |
result._buffer[0] = obj |
| 151 |
return result |
| 152 |
elif obj is None: |
| 153 |
return result |
| 154 |
elif isinstance(obj, Array): |
| 155 |
result._buffer[0] = obj._buffer |
| 156 |
elif isinstance(obj, bytes): |
| 157 |
result._buffer[0] = memoryview(obj)._pypy_raw_address() |
| 158 |
return result |
| 159 |
elif not (isinstance(obj, _CData) and type(obj)._is_pointer_like()): |
| 160 |
raise TypeError("cast() argument 1 must be a pointer, not %s" |
| 161 |
% (type(obj),)) |
| 162 |
else: |
| 163 |
result._buffer[0] = obj._buffer[0] |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
if obj._ensure_objects() is not None: |
| 170 |
result._objects = obj._objects |
| 171 |
if isinstance(obj._objects, dict): |
| 172 |
result._objects[id(obj)] = obj |
| 173 |
|
| 174 |
return result |
| 175 |
|
| 176 |
@builtinify |
| 177 |
def POINTER(cls): |
| 178 |
try: |
| 179 |
return _pointer_type_cache[cls] |
| 180 |
except KeyError: |
| 181 |
pass |
| 182 |
if type(cls) is str: |
| 183 |
klass = type(_Pointer)("LP_%s" % cls, |
| 184 |
(_Pointer,), |
| 185 |
{}) |
| 186 |
klass._type_ = 'P' |
| 187 |
_pointer_type_cache[id(klass)] = klass |
| 188 |
return klass |
| 189 |
else: |
| 190 |
name = "LP_%s" % cls.__name__ |
| 191 |
klass = type(_Pointer)(name, |
| 192 |
(_Pointer,), |
| 193 |
{'_type_': cls}) |
| 194 |
_pointer_type_cache[cls] = klass |
| 195 |
return klass |
| 196 |
|
| 197 |
@builtinify |
| 198 |
def pointer(inst): |
| 199 |
return POINTER(type(inst))(inst) |