| 1 |
import types |
| 2 |
import weakref |
| 3 |
|
| 4 |
from .error import CDefError, VerificationError, VerificationMissing |
| 5 |
|
| 6 |
|
| 7 |
Q_CONST = 0x01 |
| 8 |
Q_RESTRICT = 0x02 |
| 9 |
Q_VOLATILE = 0x04 |
| 10 |
|
| 11 |
def qualify(quals, replace_with): |
| 12 |
if quals & Q_CONST: |
| 13 |
replace_with = ' const ' + replace_with.lstrip() |
| 14 |
if quals & Q_VOLATILE: |
| 15 |
replace_with = ' volatile ' + replace_with.lstrip() |
| 16 |
if quals & Q_RESTRICT: |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
replace_with = ' __restrict ' + replace_with.lstrip() |
| 21 |
return replace_with |
| 22 |
|
| 23 |
|
| 24 |
class BaseTypeByIdentity(object): |
| 25 |
is_array_type = False |
| 26 |
is_raw_function = False |
| 27 |
|
| 28 |
def get_c_name(self, replace_with='', context='a C file', quals=0): |
| 29 |
result = self.c_name_with_marker |
| 30 |
assert result.count('&') == 1 |
| 31 |
|
| 32 |
replace_with = replace_with.strip() |
| 33 |
if replace_with: |
| 34 |
if replace_with.startswith('*') and '&[' in result: |
| 35 |
replace_with = '(%s)' % replace_with |
| 36 |
elif not replace_with[0] in '[(': |
| 37 |
replace_with = ' ' + replace_with |
| 38 |
replace_with = qualify(quals, replace_with) |
| 39 |
result = result.replace('&', replace_with) |
| 40 |
if '$' in result: |
| 41 |
raise VerificationError( |
| 42 |
"cannot generate '%s' in %s: unknown type name" |
| 43 |
% (self._get_c_name(), context)) |
| 44 |
return result |
| 45 |
|
| 46 |
def _get_c_name(self): |
| 47 |
return self.c_name_with_marker.replace('&', '') |
| 48 |
|
| 49 |
def has_c_name(self): |
| 50 |
return '$' not in self._get_c_name() |
| 51 |
|
| 52 |
def is_integer_type(self): |
| 53 |
return False |
| 54 |
|
| 55 |
def get_cached_btype(self, ffi, finishlist, can_delay=False): |
| 56 |
try: |
| 57 |
BType = ffi._cached_btypes[self] |
| 58 |
except KeyError: |
| 59 |
BType = self.build_backend_type(ffi, finishlist) |
| 60 |
BType2 = ffi._cached_btypes.setdefault(self, BType) |
| 61 |
assert BType2 is BType |
| 62 |
return BType |
| 63 |
|
| 64 |
def __repr__(self): |
| 65 |
return '<%s>' % (self._get_c_name(),) |
| 66 |
|
| 67 |
def _get_items(self): |
| 68 |
return [(name, getattr(self, name)) for name in self._attrs_] |
| 69 |
|
| 70 |
|
| 71 |
class BaseType(BaseTypeByIdentity): |
| 72 |
|
| 73 |
def __eq__(self, other): |
| 74 |
return (self.__class__ == other.__class__ and |
| 75 |
self._get_items() == other._get_items()) |
| 76 |
|
| 77 |
def __ne__(self, other): |
| 78 |
return not self == other |
| 79 |
|
| 80 |
def __hash__(self): |
| 81 |
return hash((self.__class__, tuple(self._get_items()))) |
| 82 |
|
| 83 |
|
| 84 |
class VoidType(BaseType): |
| 85 |
_attrs_ = () |
| 86 |
|
| 87 |
def __init__(self): |
| 88 |
self.c_name_with_marker = 'void&' |
| 89 |
|
| 90 |
def build_backend_type(self, ffi, finishlist): |
| 91 |
return global_cache(self, ffi, 'new_void_type') |
| 92 |
|
| 93 |
void_type = VoidType() |
| 94 |
|
| 95 |
|
| 96 |
class BasePrimitiveType(BaseType): |
| 97 |
def is_complex_type(self): |
| 98 |
return False |
| 99 |
|
| 100 |
|
| 101 |
class PrimitiveType(BasePrimitiveType): |
| 102 |
_attrs_ = ('name',) |
| 103 |
|
| 104 |
ALL_PRIMITIVE_TYPES = { |
| 105 |
'char': 'c', |
| 106 |
'short': 'i', |
| 107 |
'int': 'i', |
| 108 |
'long': 'i', |
| 109 |
'long long': 'i', |
| 110 |
'signed char': 'i', |
| 111 |
'unsigned char': 'i', |
| 112 |
'unsigned short': 'i', |
| 113 |
'unsigned int': 'i', |
| 114 |
'unsigned long': 'i', |
| 115 |
'unsigned long long': 'i', |
| 116 |
'float': 'f', |
| 117 |
'double': 'f', |
| 118 |
'long double': 'f', |
| 119 |
'float _Complex': 'j', |
| 120 |
'double _Complex': 'j', |
| 121 |
'_Bool': 'i', |
| 122 |
|
| 123 |
'wchar_t': 'c', |
| 124 |
'char16_t': 'c', |
| 125 |
'char32_t': 'c', |
| 126 |
'int8_t': 'i', |
| 127 |
'uint8_t': 'i', |
| 128 |
'int16_t': 'i', |
| 129 |
'uint16_t': 'i', |
| 130 |
'int32_t': 'i', |
| 131 |
'uint32_t': 'i', |
| 132 |
'int64_t': 'i', |
| 133 |
'uint64_t': 'i', |
| 134 |
'int_least8_t': 'i', |
| 135 |
'uint_least8_t': 'i', |
| 136 |
'int_least16_t': 'i', |
| 137 |
'uint_least16_t': 'i', |
| 138 |
'int_least32_t': 'i', |
| 139 |
'uint_least32_t': 'i', |
| 140 |
'int_least64_t': 'i', |
| 141 |
'uint_least64_t': 'i', |
| 142 |
'int_fast8_t': 'i', |
| 143 |
'uint_fast8_t': 'i', |
| 144 |
'int_fast16_t': 'i', |
| 145 |
'uint_fast16_t': 'i', |
| 146 |
'int_fast32_t': 'i', |
| 147 |
'uint_fast32_t': 'i', |
| 148 |
'int_fast64_t': 'i', |
| 149 |
'uint_fast64_t': 'i', |
| 150 |
'intptr_t': 'i', |
| 151 |
'uintptr_t': 'i', |
| 152 |
'intmax_t': 'i', |
| 153 |
'uintmax_t': 'i', |
| 154 |
'ptrdiff_t': 'i', |
| 155 |
'size_t': 'i', |
| 156 |
'ssize_t': 'i', |
| 157 |
} |
| 158 |
|
| 159 |
def __init__(self, name): |
| 160 |
assert name in self.ALL_PRIMITIVE_TYPES |
| 161 |
self.name = name |
| 162 |
self.c_name_with_marker = name + '&' |
| 163 |
|
| 164 |
def is_char_type(self): |
| 165 |
return self.ALL_PRIMITIVE_TYPES[self.name] == 'c' |
| 166 |
def is_integer_type(self): |
| 167 |
return self.ALL_PRIMITIVE_TYPES[self.name] == 'i' |
| 168 |
def is_float_type(self): |
| 169 |
return self.ALL_PRIMITIVE_TYPES[self.name] == 'f' |
| 170 |
def is_complex_type(self): |
| 171 |
return self.ALL_PRIMITIVE_TYPES[self.name] == 'j' |
| 172 |
|
| 173 |
def build_backend_type(self, ffi, finishlist): |
| 174 |
return global_cache(self, ffi, 'new_primitive_type', self.name) |
| 175 |
|
| 176 |
|
| 177 |
class UnknownIntegerType(BasePrimitiveType): |
| 178 |
_attrs_ = ('name',) |
| 179 |
|
| 180 |
def __init__(self, name): |
| 181 |
self.name = name |
| 182 |
self.c_name_with_marker = name + '&' |
| 183 |
|
| 184 |
def is_integer_type(self): |
| 185 |
return True |
| 186 |
|
| 187 |
def build_backend_type(self, ffi, finishlist): |
| 188 |
raise NotImplementedError("integer type '%s' can only be used after " |
| 189 |
"compilation" % self.name) |
| 190 |
|
| 191 |
class UnknownFloatType(BasePrimitiveType): |
| 192 |
_attrs_ = ('name', ) |
| 193 |
|
| 194 |
def __init__(self, name): |
| 195 |
self.name = name |
| 196 |
self.c_name_with_marker = name + '&' |
| 197 |
|
| 198 |
def build_backend_type(self, ffi, finishlist): |
| 199 |
raise NotImplementedError("float type '%s' can only be used after " |
| 200 |
"compilation" % self.name) |
| 201 |
|
| 202 |
class DefinedType(BaseType): |
| 203 |
_attrs_ = ('name', ) |
| 204 |
|
| 205 |
def __init__(self, name, realtype, quals): |
| 206 |
self.name = name |
| 207 |
self.realtype = realtype |
| 208 |
self.quals = quals |
| 209 |
self.c_name_with_marker = name + '&' |
| 210 |
|
| 211 |
|
| 212 |
class BaseFunctionType(BaseType): |
| 213 |
_attrs_ = ('args', 'result', 'ellipsis', 'abi') |
| 214 |
|
| 215 |
def __init__(self, args, result, ellipsis, abi=None): |
| 216 |
self.args = args |
| 217 |
self.result = result |
| 218 |
self.ellipsis = ellipsis |
| 219 |
self.abi = abi |
| 220 |
|
| 221 |
reprargs = [arg._get_c_name() for arg in self.args] |
| 222 |
if self.ellipsis: |
| 223 |
reprargs.append('...') |
| 224 |
reprargs = reprargs or ['void'] |
| 225 |
replace_with = self._base_pattern % (', '.join(reprargs),) |
| 226 |
if abi is not None: |
| 227 |
replace_with = replace_with[:1] + abi + ' ' + replace_with[1:] |
| 228 |
self.c_name_with_marker = ( |
| 229 |
self.result.c_name_with_marker.replace('&', replace_with)) |
| 230 |
|
| 231 |
|
| 232 |
class RawFunctionType(BaseFunctionType): |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
_base_pattern = '(&)(%s)' |
| 237 |
is_raw_function = True |
| 238 |
|
| 239 |
def build_backend_type(self, ffi, finishlist): |
| 240 |
raise CDefError("cannot render the type %r: it is a function " |
| 241 |
"type, not a pointer-to-function type" % (self,)) |
| 242 |
|
| 243 |
def as_function_pointer(self): |
| 244 |
return FunctionPtrType(self.args, self.result, self.ellipsis, self.abi) |
| 245 |
|
| 246 |
|
| 247 |
class FunctionPtrType(BaseFunctionType): |
| 248 |
_base_pattern = '(*&)(%s)' |
| 249 |
|
| 250 |
def build_backend_type(self, ffi, finishlist): |
| 251 |
result = self.result.get_cached_btype(ffi, finishlist) |
| 252 |
args = [] |
| 253 |
for tp in self.args: |
| 254 |
args.append(tp.get_cached_btype(ffi, finishlist)) |
| 255 |
abi_args = () |
| 256 |
if self.abi == "__stdcall": |
| 257 |
if not self.ellipsis: |
| 258 |
try: |
| 259 |
abi_args = (ffi._backend.FFI_STDCALL,) |
| 260 |
except AttributeError: |
| 261 |
pass |
| 262 |
return global_cache(self, ffi, 'new_function_type', |
| 263 |
tuple(args), result, self.ellipsis, *abi_args) |
| 264 |
|
| 265 |
def as_raw_function(self): |
| 266 |
return RawFunctionType(self.args, self.result, self.ellipsis, self.abi) |
| 267 |
|
| 268 |
|
| 269 |
class PointerType(BaseType): |
| 270 |
_attrs_ = ('totype', 'quals') |
| 271 |
|
| 272 |
def __init__(self, totype, quals=0): |
| 273 |
self.totype = totype |
| 274 |
self.quals = quals |
| 275 |
extra = qualify(quals, " *&") |
| 276 |
if totype.is_array_type: |
| 277 |
extra = "(%s)" % (extra.lstrip(),) |
| 278 |
self.c_name_with_marker = totype.c_name_with_marker.replace('&', extra) |
| 279 |
|
| 280 |
def build_backend_type(self, ffi, finishlist): |
| 281 |
BItem = self.totype.get_cached_btype(ffi, finishlist, can_delay=True) |
| 282 |
return global_cache(self, ffi, 'new_pointer_type', BItem) |
| 283 |
|
| 284 |
voidp_type = PointerType(void_type) |
| 285 |
|
| 286 |
def ConstPointerType(totype): |
| 287 |
return PointerType(totype, Q_CONST) |
| 288 |
|
| 289 |
const_voidp_type = ConstPointerType(void_type) |
| 290 |
|
| 291 |
|
| 292 |
class NamedPointerType(PointerType): |
| 293 |
_attrs_ = ('totype', 'name') |
| 294 |
|
| 295 |
def __init__(self, totype, name, quals=0): |
| 296 |
PointerType.__init__(self, totype, quals) |
| 297 |
self.name = name |
| 298 |
self.c_name_with_marker = name + '&' |
| 299 |
|
| 300 |
|
| 301 |
class ArrayType(BaseType): |
| 302 |
_attrs_ = ('item', 'length') |
| 303 |
is_array_type = True |
| 304 |
|
| 305 |
def __init__(self, item, length): |
| 306 |
self.item = item |
| 307 |
self.length = length |
| 308 |
|
| 309 |
if length is None: |
| 310 |
brackets = '&[]' |
| 311 |
elif length == '...': |
| 312 |
brackets = '&[/*...*/]' |
| 313 |
else: |
| 314 |
brackets = '&[%s]' % length |
| 315 |
self.c_name_with_marker = ( |
| 316 |
self.item.c_name_with_marker.replace('&', brackets)) |
| 317 |
|
| 318 |
def length_is_unknown(self): |
| 319 |
return isinstance(self.length, str) |
| 320 |
|
| 321 |
def resolve_length(self, newlength): |
| 322 |
return ArrayType(self.item, newlength) |
| 323 |
|
| 324 |
def build_backend_type(self, ffi, finishlist): |
| 325 |
if self.length_is_unknown(): |
| 326 |
raise CDefError("cannot render the type %r: unknown length" % |
| 327 |
(self,)) |
| 328 |
self.item.get_cached_btype(ffi, finishlist) |
| 329 |
BPtrItem = PointerType(self.item).get_cached_btype(ffi, finishlist) |
| 330 |
return global_cache(self, ffi, 'new_array_type', BPtrItem, self.length) |
| 331 |
|
| 332 |
char_array_type = ArrayType(PrimitiveType('char'), None) |
| 333 |
|
| 334 |
|
| 335 |
class StructOrUnionOrEnum(BaseTypeByIdentity): |
| 336 |
_attrs_ = ('name',) |
| 337 |
forcename = None |
| 338 |
|
| 339 |
def build_c_name_with_marker(self): |
| 340 |
name = self.forcename or '%s %s' % (self.kind, self.name) |
| 341 |
self.c_name_with_marker = name + '&' |
| 342 |
|
| 343 |
def force_the_name(self, forcename): |
| 344 |
self.forcename = forcename |
| 345 |
self.build_c_name_with_marker() |
| 346 |
|
| 347 |
def get_official_name(self): |
| 348 |
assert self.c_name_with_marker.endswith('&') |
| 349 |
return self.c_name_with_marker[:-1] |
| 350 |
|
| 351 |
|
| 352 |
class StructOrUnion(StructOrUnionOrEnum): |
| 353 |
fixedlayout = None |
| 354 |
completed = 0 |
| 355 |
partial = False |
| 356 |
packed = 0 |
| 357 |
|
| 358 |
def __init__(self, name, fldnames, fldtypes, fldbitsize, fldquals=None): |
| 359 |
self.name = name |
| 360 |
self.fldnames = fldnames |
| 361 |
self.fldtypes = fldtypes |
| 362 |
self.fldbitsize = fldbitsize |
| 363 |
self.fldquals = fldquals |
| 364 |
self.build_c_name_with_marker() |
| 365 |
|
| 366 |
def anonymous_struct_fields(self): |
| 367 |
if self.fldtypes is not None: |
| 368 |
for name, type in zip(self.fldnames, self.fldtypes): |
| 369 |
if name == '' and isinstance(type, StructOrUnion): |
| 370 |
yield type |
| 371 |
|
| 372 |
def enumfields(self, expand_anonymous_struct_union=True): |
| 373 |
fldquals = self.fldquals |
| 374 |
if fldquals is None: |
| 375 |
fldquals = (0,) * len(self.fldnames) |
| 376 |
for name, type, bitsize, quals in zip(self.fldnames, self.fldtypes, |
| 377 |
self.fldbitsize, fldquals): |
| 378 |
if (name == '' and isinstance(type, StructOrUnion) |
| 379 |
and expand_anonymous_struct_union): |
| 380 |
|
| 381 |
for result in type.enumfields(): |
| 382 |
yield result |
| 383 |
else: |
| 384 |
yield (name, type, bitsize, quals) |
| 385 |
|
| 386 |
def force_flatten(self): |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
names = [] |
| 391 |
types = [] |
| 392 |
bitsizes = [] |
| 393 |
fldquals = [] |
| 394 |
for name, type, bitsize, quals in self.enumfields(): |
| 395 |
names.append(name) |
| 396 |
types.append(type) |
| 397 |
bitsizes.append(bitsize) |
| 398 |
fldquals.append(quals) |
| 399 |
self.fldnames = tuple(names) |
| 400 |
self.fldtypes = tuple(types) |
| 401 |
self.fldbitsize = tuple(bitsizes) |
| 402 |
self.fldquals = tuple(fldquals) |
| 403 |
|
| 404 |
def get_cached_btype(self, ffi, finishlist, can_delay=False): |
| 405 |
BType = StructOrUnionOrEnum.get_cached_btype(self, ffi, finishlist, |
| 406 |
can_delay) |
| 407 |
if not can_delay: |
| 408 |
self.finish_backend_type(ffi, finishlist) |
| 409 |
return BType |
| 410 |
|
| 411 |
def finish_backend_type(self, ffi, finishlist): |
| 412 |
if self.completed: |
| 413 |
if self.completed != 2: |
| 414 |
raise NotImplementedError("recursive structure declaration " |
| 415 |
"for '%s'" % (self.name,)) |
| 416 |
return |
| 417 |
BType = ffi._cached_btypes[self] |
| 418 |
|
| 419 |
self.completed = 1 |
| 420 |
|
| 421 |
if self.fldtypes is None: |
| 422 |
pass |
| 423 |
|
| 424 |
elif self.fixedlayout is None: |
| 425 |
fldtypes = [tp.get_cached_btype(ffi, finishlist) |
| 426 |
for tp in self.fldtypes] |
| 427 |
lst = list(zip(self.fldnames, fldtypes, self.fldbitsize)) |
| 428 |
extra_flags = () |
| 429 |
if self.packed: |
| 430 |
if self.packed == 1: |
| 431 |
extra_flags = (8,) |
| 432 |
else: |
| 433 |
extra_flags = (0, self.packed) |
| 434 |
ffi._backend.complete_struct_or_union(BType, lst, self, |
| 435 |
-1, -1, *extra_flags) |
| 436 |
|
| 437 |
else: |
| 438 |
fldtypes = [] |
| 439 |
fieldofs, fieldsize, totalsize, totalalignment = self.fixedlayout |
| 440 |
for i in range(len(self.fldnames)): |
| 441 |
fsize = fieldsize[i] |
| 442 |
ftype = self.fldtypes[i] |
| 443 |
|
| 444 |
if isinstance(ftype, ArrayType) and ftype.length_is_unknown(): |
| 445 |
|
| 446 |
BItemType = ftype.item.get_cached_btype(ffi, finishlist) |
| 447 |
nlen, nrest = divmod(fsize, ffi.sizeof(BItemType)) |
| 448 |
if nrest != 0: |
| 449 |
self._verification_error( |
| 450 |
"field '%s.%s' has a bogus size?" % ( |
| 451 |
self.name, self.fldnames[i] or '{}')) |
| 452 |
ftype = ftype.resolve_length(nlen) |
| 453 |
self.fldtypes = (self.fldtypes[:i] + (ftype,) + |
| 454 |
self.fldtypes[i+1:]) |
| 455 |
|
| 456 |
BFieldType = ftype.get_cached_btype(ffi, finishlist) |
| 457 |
if isinstance(ftype, ArrayType) and ftype.length is None: |
| 458 |
assert fsize == 0 |
| 459 |
else: |
| 460 |
bitemsize = ffi.sizeof(BFieldType) |
| 461 |
if bitemsize != fsize: |
| 462 |
self._verification_error( |
| 463 |
"field '%s.%s' is declared as %d bytes, but is " |
| 464 |
"really %d bytes" % (self.name, |
| 465 |
self.fldnames[i] or '{}', |
| 466 |
bitemsize, fsize)) |
| 467 |
fldtypes.append(BFieldType) |
| 468 |
|
| 469 |
lst = list(zip(self.fldnames, fldtypes, self.fldbitsize, fieldofs)) |
| 470 |
ffi._backend.complete_struct_or_union(BType, lst, self, |
| 471 |
totalsize, totalalignment) |
| 472 |
self.completed = 2 |
| 473 |
|
| 474 |
def _verification_error(self, msg): |
| 475 |
raise VerificationError(msg) |
| 476 |
|
| 477 |
def check_not_partial(self): |
| 478 |
if self.partial and self.fixedlayout is None: |
| 479 |
raise VerificationMissing(self._get_c_name()) |
| 480 |
|
| 481 |
def build_backend_type(self, ffi, finishlist): |
| 482 |
self.check_not_partial() |
| 483 |
finishlist.append(self) |
| 484 |
|
| 485 |
return global_cache(self, ffi, 'new_%s_type' % self.kind, |
| 486 |
self.get_official_name(), key=self) |
| 487 |
|
| 488 |
|
| 489 |
class StructType(StructOrUnion): |
| 490 |
kind = 'struct' |
| 491 |
|
| 492 |
|
| 493 |
class UnionType(StructOrUnion): |
| 494 |
kind = 'union' |
| 495 |
|
| 496 |
|
| 497 |
class EnumType(StructOrUnionOrEnum): |
| 498 |
kind = 'enum' |
| 499 |
partial = False |
| 500 |
partial_resolved = False |
| 501 |
|
| 502 |
def __init__(self, name, enumerators, enumvalues, baseinttype=None): |
| 503 |
self.name = name |
| 504 |
self.enumerators = enumerators |
| 505 |
self.enumvalues = enumvalues |
| 506 |
self.baseinttype = baseinttype |
| 507 |
self.build_c_name_with_marker() |
| 508 |
|
| 509 |
def force_the_name(self, forcename): |
| 510 |
StructOrUnionOrEnum.force_the_name(self, forcename) |
| 511 |
if self.forcename is None: |
| 512 |
name = self.get_official_name() |
| 513 |
self.forcename = '$' + name.replace(' ', '_') |
| 514 |
|
| 515 |
def check_not_partial(self): |
| 516 |
if self.partial and not self.partial_resolved: |
| 517 |
raise VerificationMissing(self._get_c_name()) |
| 518 |
|
| 519 |
def build_backend_type(self, ffi, finishlist): |
| 520 |
self.check_not_partial() |
| 521 |
base_btype = self.build_baseinttype(ffi, finishlist) |
| 522 |
return global_cache(self, ffi, 'new_enum_type', |
| 523 |
self.get_official_name(), |
| 524 |
self.enumerators, self.enumvalues, |
| 525 |
base_btype, key=self) |
| 526 |
|
| 527 |
def build_baseinttype(self, ffi, finishlist): |
| 528 |
if self.baseinttype is not None: |
| 529 |
return self.baseinttype.get_cached_btype(ffi, finishlist) |
| 530 |
|
| 531 |
if self.enumvalues: |
| 532 |
smallest_value = min(self.enumvalues) |
| 533 |
largest_value = max(self.enumvalues) |
| 534 |
else: |
| 535 |
import warnings |
| 536 |
try: |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
__warningregistry__.clear() |
| 541 |
except NameError: |
| 542 |
pass |
| 543 |
warnings.warn("%r has no values explicitly defined; " |
| 544 |
"guessing that it is equivalent to 'unsigned int'" |
| 545 |
% self._get_c_name()) |
| 546 |
smallest_value = largest_value = 0 |
| 547 |
if smallest_value < 0: |
| 548 |
sign = 1 |
| 549 |
candidate1 = PrimitiveType("int") |
| 550 |
candidate2 = PrimitiveType("long") |
| 551 |
else: |
| 552 |
sign = 0 |
| 553 |
candidate1 = PrimitiveType("unsigned int") |
| 554 |
candidate2 = PrimitiveType("unsigned long") |
| 555 |
btype1 = candidate1.get_cached_btype(ffi, finishlist) |
| 556 |
btype2 = candidate2.get_cached_btype(ffi, finishlist) |
| 557 |
size1 = ffi.sizeof(btype1) |
| 558 |
size2 = ffi.sizeof(btype2) |
| 559 |
if (smallest_value >= ((-1) << (8*size1-1)) and |
| 560 |
largest_value < (1 << (8*size1-sign))): |
| 561 |
return btype1 |
| 562 |
if (smallest_value >= ((-1) << (8*size2-1)) and |
| 563 |
largest_value < (1 << (8*size2-sign))): |
| 564 |
return btype2 |
| 565 |
raise CDefError("%s values don't all fit into either 'long' " |
| 566 |
"or 'unsigned long'" % self._get_c_name()) |
| 567 |
|
| 568 |
def unknown_type(name, structname=None): |
| 569 |
if structname is None: |
| 570 |
structname = '$%s' % name |
| 571 |
tp = StructType(structname, None, None, None) |
| 572 |
tp.force_the_name(name) |
| 573 |
tp.origin = "unknown_type" |
| 574 |
return tp |
| 575 |
|
| 576 |
def unknown_ptr_type(name, structname=None): |
| 577 |
if structname is None: |
| 578 |
structname = '$$%s' % name |
| 579 |
tp = StructType(structname, None, None, None) |
| 580 |
return NamedPointerType(tp, name) |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
_typecache_cffi_backend = weakref.WeakValueDictionary() |
| 585 |
|
| 586 |
def get_typecache(backend): |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
if isinstance(backend, types.ModuleType): |
| 591 |
return _typecache_cffi_backend |
| 592 |
with global_lock: |
| 593 |
if not hasattr(type(backend), '__typecache'): |
| 594 |
type(backend).__typecache = weakref.WeakValueDictionary() |
| 595 |
return type(backend).__typecache |
| 596 |
|
| 597 |
def global_cache(srctype, ffi, funcname, *args, **kwds): |
| 598 |
key = kwds.pop('key', (funcname, args)) |
| 599 |
assert not kwds |
| 600 |
try: |
| 601 |
return ffi._typecache[key] |
| 602 |
except KeyError: |
| 603 |
pass |
| 604 |
try: |
| 605 |
res = getattr(ffi._backend, funcname)(*args) |
| 606 |
except NotImplementedError as e: |
| 607 |
raise NotImplementedError("%s: %r: %s" % (funcname, srctype, e)) |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
cache = ffi._typecache |
| 612 |
with global_lock: |
| 613 |
res1 = cache.get(key) |
| 614 |
if res1 is None: |
| 615 |
cache[key] = res |
| 616 |
return res |
| 617 |
else: |
| 618 |
return res1 |
| 619 |
|
| 620 |
def pointer_cache(ffi, BType): |
| 621 |
return global_cache('?', ffi, 'new_pointer_type', BType) |
| 622 |
|
| 623 |
def attach_exception_info(e, name): |
| 624 |
if e.args and type(e.args[0]) is str: |
| 625 |
e.args = ('%s: %s' % (name, e.args[0]),) + e.args[1:] |