Debian Codegraph @

src/cffi/commontypes.py

raw
1 import sys
2 from . import model
3 from .error import FFIError
4
5
6 COMMON_TYPES = {}
7
8 try:
9 # fetch "bool" and all simple Windows types
10 from _cffi_backend import _get_common_types
11 _get_common_types(COMMON_TYPES)
12 except ImportError:
13 pass
14
15 COMMON_TYPES['FILE'] = model.unknown_type('FILE', '_IO_FILE')
16 COMMON_TYPES['bool'] = '_Bool' # in case we got ImportError above
17 COMMON_TYPES['float _Complex'] = '_cffi_float_complex_t'
18 COMMON_TYPES['double _Complex'] = '_cffi_double_complex_t'
19
20 for _type in model.PrimitiveType.ALL_PRIMITIVE_TYPES:
21 if _type.endswith('_t'):
22 COMMON_TYPES[_type] = _type
23 del _type
24
25 _CACHE = {}
26
27 def resolve_common_type(parser, commontype):
28 try:
29 return _CACHE[commontype]
30 except KeyError:
31 cdecl = COMMON_TYPES.get(commontype, commontype)
32 if not isinstance(cdecl, str):
33 result, quals = cdecl, 0 # cdecl is already a BaseType
34 elif cdecl in model.PrimitiveType.ALL_PRIMITIVE_TYPES:
35 result, quals = model.PrimitiveType(cdecl), 0
36 elif cdecl == 'set-unicode-needed':
37 raise FFIError("The Windows type %r is only available after "
38 "you call ffi.set_unicode()" % (commontype,))
39 else:
40 if commontype == cdecl:
41 raise FFIError(
42 "Unsupported type: %r. Please look at "
43 "http://cffi.readthedocs.io/en/latest/cdef.html#ffi-cdef-limitations "
44 "and file an issue if you think this type should really "
45 "be supported." % (commontype,))
46 result, quals = parser.parse_type_and_quals(cdecl) # recursive
47
48 assert isinstance(result, model.BaseTypeByIdentity)
49 _CACHE[commontype] = result, quals
50 return result, quals
51
52
53 # ____________________________________________________________
54 # extra types for Windows (most of them are in commontypes.c)
55
56
57 def win_common_types():
58 return {
59 "UNICODE_STRING": model.StructType(
60 "_UNICODE_STRING",
61 ["Length",
62 "MaximumLength",
63 "Buffer"],
64 [model.PrimitiveType("unsigned short"),
65 model.PrimitiveType("unsigned short"),
66 model.PointerType(model.PrimitiveType("wchar_t"))],
67 [-1, -1, -1]),
68 "PUNICODE_STRING": "UNICODE_STRING *",
69 "PCUNICODE_STRING": "const UNICODE_STRING *",
70
71 "TBYTE": "set-unicode-needed",
72 "TCHAR": "set-unicode-needed",
73 "LPCTSTR": "set-unicode-needed",
74 "PCTSTR": "set-unicode-needed",
75 "LPTSTR": "set-unicode-needed",
76 "PTSTR": "set-unicode-needed",
77 "PTBYTE": "set-unicode-needed",
78 "PTCHAR": "set-unicode-needed",
79 }
80
81 if sys.platform == 'win32':
82 COMMON_TYPES.update(win_common_types())