You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

commontypes.py 2.6KB

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