| 1 |
import sys |
| 2 |
from pypy.interpreter.baseobjspace import W_Root |
| 3 |
from pypy.interpreter.error import OperationError, oefmt |
| 4 |
from pypy.interpreter.gateway import interp2app |
| 5 |
from pypy.interpreter.typedef import TypeDef, make_weakref_descr, GetSetProperty |
| 6 |
|
| 7 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 8 |
from rpython.rlib.objectmodel import we_are_translated |
| 9 |
|
| 10 |
from pypy.module._cffi_backend import cdataobj |
| 11 |
|
| 12 |
|
| 13 |
class W_CType(W_Root): |
| 14 |
_attrs_ = ['space', 'size', 'name', 'name_position', '_lifeline_', |
| 15 |
'_pointer_type'] |
| 16 |
_immutable_fields_ = ['size?', 'name', 'name_position'] |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
is_primitive_integer = False |
| 23 |
is_nonfunc_pointer_or_array = False |
| 24 |
is_indirect_arg_for_call_python = False |
| 25 |
kind = "?" |
| 26 |
|
| 27 |
def __init__(self, space, size, name, name_position): |
| 28 |
self.space = space |
| 29 |
self.size = size |
| 30 |
self.name = name |
| 31 |
self.name_position = name_position |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
def repr(self): |
| 36 |
space = self.space |
| 37 |
return space.newtext("<ctype '%s'>" % (self.name,)) |
| 38 |
|
| 39 |
def extra_repr(self, cdata): |
| 40 |
if cdata: |
| 41 |
return '0x%x' % rffi.cast(lltype.Unsigned, cdata) |
| 42 |
else: |
| 43 |
return 'NULL' |
| 44 |
|
| 45 |
def is_unichar_ptr_or_array(self): |
| 46 |
return False |
| 47 |
|
| 48 |
def unpack_list_of_int_items(self, ptr, length): |
| 49 |
return None |
| 50 |
|
| 51 |
def unpack_list_of_float_items(self, ptr, length): |
| 52 |
return None |
| 53 |
|
| 54 |
def pack_list_of_items(self, cdata, w_ob, expected_length): |
| 55 |
return False |
| 56 |
|
| 57 |
def _within_bounds(self, actual_length, expected_length): |
| 58 |
return expected_length < 0 or actual_length <= expected_length |
| 59 |
|
| 60 |
def newp(self, w_init, allocator): |
| 61 |
space = self.space |
| 62 |
raise oefmt(space.w_TypeError, |
| 63 |
"expected a pointer or array ctype, got '%s'", self.name) |
| 64 |
|
| 65 |
def cast(self, w_ob): |
| 66 |
space = self.space |
| 67 |
raise oefmt(space.w_TypeError, "cannot cast to '%s'", self.name) |
| 68 |
|
| 69 |
def cast_to_int(self, cdata): |
| 70 |
space = self.space |
| 71 |
raise oefmt(space.w_TypeError, "int() not supported on cdata '%s'", |
| 72 |
self.name) |
| 73 |
|
| 74 |
def float(self, cdata): |
| 75 |
space = self.space |
| 76 |
raise oefmt(space.w_TypeError, "float() not supported on cdata '%s'", |
| 77 |
self.name) |
| 78 |
|
| 79 |
def complex(self, cdata): |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
space = self.space |
| 84 |
raise oefmt(space.w_TypeError, "complex() not supported on cdata '%s'", |
| 85 |
self.name) |
| 86 |
|
| 87 |
def convert_to_object(self, cdata): |
| 88 |
space = self.space |
| 89 |
raise oefmt(space.w_TypeError, "cannot return a cdata '%s'", self.name) |
| 90 |
|
| 91 |
def convert_from_object(self, cdata, w_ob): |
| 92 |
space = self.space |
| 93 |
raise oefmt(space.w_TypeError, "cannot initialize cdata '%s'", |
| 94 |
self.name) |
| 95 |
|
| 96 |
def convert_argument_from_object(self, cdata, w_ob, keepalives, i): |
| 97 |
self.convert_from_object(cdata, w_ob) |
| 98 |
return False |
| 99 |
|
| 100 |
def _convert_error(self, expected, w_got): |
| 101 |
space = self.space |
| 102 |
if isinstance(w_got, cdataobj.W_CData): |
| 103 |
if self.name == w_got.ctype.name: |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
if self is w_got.ctype: |
| 109 |
raise oefmt(space.w_SystemError, |
| 110 |
"initializer for ctype '%s' is correct, but we get " |
| 111 |
"an internal mismatch--please report a bug", |
| 112 |
self.name) |
| 113 |
return oefmt(space.w_TypeError, |
| 114 |
"initializer for ctype '%s' appears indeed to " |
| 115 |
"be '%s', but the types are different (check " |
| 116 |
"that you are not e.g. mixing up different ffi " |
| 117 |
"instances)", self.name, w_got.ctype.name) |
| 118 |
return oefmt(space.w_TypeError, |
| 119 |
"initializer for ctype '%s' must be a %s, not cdata " |
| 120 |
"'%s'", self.name, expected, w_got.ctype.name) |
| 121 |
else: |
| 122 |
return oefmt(space.w_TypeError, |
| 123 |
"initializer for ctype '%s' must be a %s, not %T", |
| 124 |
self.name, expected, w_got) |
| 125 |
|
| 126 |
def _cannot_index(self): |
| 127 |
space = self.space |
| 128 |
raise oefmt(space.w_TypeError, "cdata of type '%s' cannot be indexed", |
| 129 |
self.name) |
| 130 |
|
| 131 |
def _check_subscript_index(self, w_cdata, i): |
| 132 |
raise self._cannot_index() |
| 133 |
|
| 134 |
def _check_slice_index(self, w_cdata, start, stop): |
| 135 |
raise self._cannot_index() |
| 136 |
|
| 137 |
def string(self, cdataobj, maxlen): |
| 138 |
space = self.space |
| 139 |
raise oefmt(space.w_TypeError, |
| 140 |
"string(): unexpected cdata '%s' argument", self.name) |
| 141 |
|
| 142 |
def unpack_ptr(self, w_ctypeptr, ptr, length): |
| 143 |
|
| 144 |
|
| 145 |
space = self.space |
| 146 |
itemsize = self.size |
| 147 |
if itemsize < 0: |
| 148 |
raise oefmt(space.w_ValueError, |
| 149 |
"'%s' points to items of unknown size", |
| 150 |
w_ctypeptr.name) |
| 151 |
result_w = [None] * length |
| 152 |
for i in range(length): |
| 153 |
result_w[i] = self.convert_to_object(ptr) |
| 154 |
ptr = rffi.ptradd(ptr, itemsize) |
| 155 |
return space.newlist(result_w) |
| 156 |
|
| 157 |
def add(self, cdata, i): |
| 158 |
space = self.space |
| 159 |
raise oefmt(space.w_TypeError, "cannot add a cdata '%s' and a number", |
| 160 |
self.name) |
| 161 |
|
| 162 |
def nonzero(self, cdata): |
| 163 |
return bool(cdata) |
| 164 |
|
| 165 |
def insert_name(self, extra, extra_position): |
| 166 |
name = '%s%s%s' % (self.name[:self.name_position], |
| 167 |
extra, |
| 168 |
self.name[self.name_position:]) |
| 169 |
name_position = self.name_position + extra_position |
| 170 |
return name, name_position |
| 171 |
|
| 172 |
def alignof(self): |
| 173 |
align = self._alignof() |
| 174 |
if not we_are_translated(): |
| 175 |
|
| 176 |
if isinstance(align, llmemory.FieldOffset): |
| 177 |
align = rffi.sizeof(align.TYPE.y) |
| 178 |
if sys.platform != 'win32' and (1 << (8*align-2)) > sys.maxint: |
| 179 |
align /= 2 |
| 180 |
else: |
| 181 |
|
| 182 |
|
| 183 |
align = llmemory.raw_malloc_usage(align) |
| 184 |
return align |
| 185 |
|
| 186 |
def _alignof(self): |
| 187 |
space = self.space |
| 188 |
raise oefmt(space.w_ValueError, "ctype '%s' is of unknown alignment", |
| 189 |
self.name) |
| 190 |
|
| 191 |
def direct_typeoffsetof(self, w_field_or_index, following=0): |
| 192 |
space = self.space |
| 193 |
try: |
| 194 |
fieldname = space.text_w(w_field_or_index) |
| 195 |
except OperationError as e: |
| 196 |
if not e.match(space, space.w_TypeError): |
| 197 |
raise |
| 198 |
try: |
| 199 |
index = space.int_w(w_field_or_index) |
| 200 |
except OperationError as e: |
| 201 |
if not e.match(space, space.w_TypeError): |
| 202 |
raise |
| 203 |
raise oefmt(space.w_TypeError, |
| 204 |
"field name or array index expected") |
| 205 |
return self.typeoffsetof_index(index) |
| 206 |
else: |
| 207 |
return self.typeoffsetof_field(fieldname, following) |
| 208 |
|
| 209 |
def typeoffsetof_field(self, fieldname, following): |
| 210 |
raise oefmt(self.space.w_TypeError, |
| 211 |
"with a field name argument, expected a struct or union " |
| 212 |
"ctype") |
| 213 |
|
| 214 |
def typeoffsetof_index(self, index): |
| 215 |
raise oefmt(self.space.w_TypeError, |
| 216 |
"with an integer argument, expected an array or pointer " |
| 217 |
"ctype") |
| 218 |
|
| 219 |
def rawaddressof(self, cdata, offset): |
| 220 |
raise oefmt(self.space.w_TypeError, "expected a pointer ctype") |
| 221 |
|
| 222 |
def call(self, funcaddr, args_w): |
| 223 |
space = self.space |
| 224 |
raise oefmt(space.w_TypeError, "cdata '%s' is not callable", self.name) |
| 225 |
|
| 226 |
def iter(self, cdata): |
| 227 |
space = self.space |
| 228 |
raise oefmt(space.w_TypeError, |
| 229 |
"cdata '%s' does not support iteration", self.name) |
| 230 |
|
| 231 |
def unpackiterable_int(self, cdata): |
| 232 |
return None |
| 233 |
|
| 234 |
def get_vararg_type(self): |
| 235 |
return self |
| 236 |
|
| 237 |
def getcfield(self, attr): |
| 238 |
space = self.space |
| 239 |
raise oefmt(space.w_AttributeError, |
| 240 |
"cdata '%s' has no attribute '%s'", self.name, attr) |
| 241 |
|
| 242 |
def copy_and_convert_to_object(self, source): |
| 243 |
return self.convert_to_object(source) |
| 244 |
|
| 245 |
|
| 246 |
def dir(self): |
| 247 |
space = self.space |
| 248 |
lst = [space.newtext(name) |
| 249 |
for name in _name_of_attributes |
| 250 |
if space.findattr(self, space.newtext(name)) is not None] |
| 251 |
return space.newlist(lst) |
| 252 |
|
| 253 |
def _fget(self, attrchar): |
| 254 |
space = self.space |
| 255 |
if attrchar == 'k': |
| 256 |
return space.newtext(self.kind) |
| 257 |
if attrchar == 'c': |
| 258 |
return space.newtext(self.name) |
| 259 |
raise oefmt(space.w_AttributeError, |
| 260 |
"ctype '%s' has no such attribute", self.name) |
| 261 |
|
| 262 |
def fget_kind(self, space): return self._fget('k') |
| 263 |
def fget_cname(self, space): return self._fget('c') |
| 264 |
def fget_item(self, space): return self._fget('i') |
| 265 |
def fget_length(self, space): return self._fget('l') |
| 266 |
def fget_fields(self, space): return self._fget('f') |
| 267 |
def fget_args(self, space): return self._fget('a') |
| 268 |
def fget_result(self, space): return self._fget('r') |
| 269 |
def fget_ellipsis(self, space): return self._fget('E') |
| 270 |
def fget_abi(self, space): return self._fget('A') |
| 271 |
def fget_elements(self, space): return self._fget('e') |
| 272 |
def fget_relements(self, space):return self._fget('R') |
| 273 |
|
| 274 |
def cdata_dir(self): |
| 275 |
return [] |
| 276 |
|
| 277 |
|
| 278 |
W_CType.typedef = TypeDef( |
| 279 |
'_cffi_backend.CType', |
| 280 |
__repr__ = interp2app(W_CType.repr), |
| 281 |
__weakref__ = make_weakref_descr(W_CType), |
| 282 |
kind = GetSetProperty(W_CType.fget_kind, doc="kind"), |
| 283 |
cname = GetSetProperty(W_CType.fget_cname, doc="C name"), |
| 284 |
item = GetSetProperty(W_CType.fget_item, doc="pointer to, or array of"), |
| 285 |
length = GetSetProperty(W_CType.fget_length, doc="array length or None"), |
| 286 |
fields = GetSetProperty(W_CType.fget_fields, doc="struct or union fields"), |
| 287 |
args = GetSetProperty(W_CType.fget_args, doc="function argument types"), |
| 288 |
result = GetSetProperty(W_CType.fget_result, doc="function result type"), |
| 289 |
ellipsis = GetSetProperty(W_CType.fget_ellipsis, doc="function has '...'"), |
| 290 |
abi = GetSetProperty(W_CType.fget_abi, doc="function ABI"), |
| 291 |
elements = GetSetProperty(W_CType.fget_elements, doc="enum elements"), |
| 292 |
relements = GetSetProperty(W_CType.fget_relements, |
| 293 |
doc="enum elements, reversed"), |
| 294 |
__dir__ = interp2app(W_CType.dir), |
| 295 |
) |
| 296 |
W_CType.typedef.acceptable_as_base_class = False |
| 297 |
|
| 298 |
_name_of_attributes = [name for name in W_CType.typedef.rawdict |
| 299 |
if not name.startswith('_')] |
| 300 |
_name_of_attributes.sort() |