NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

__init__.py (12584B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\stdlib\ctypes\__init__.py
      2 import os as _os, sys as _sys
      3 __version__ = '1.1.0'
      4 from _ctypes import Union, Structure, Array
      5 from _ctypes import _Pointer
      6 from _ctypes import CFuncPtr as _CFuncPtr
      7 from _ctypes import __version__ as _ctypes_version
      8 from _ctypes import RTLD_LOCAL, RTLD_GLOBAL
      9 from _ctypes import ArgumentError
     10 from struct import calcsize as _calcsize
     11 if __version__ != _ctypes_version:
     12     raise Exception('Version number mismatch', __version__, _ctypes_version)
     13 if _os.name in ('nt', 'ce'):
     14     from _ctypes import FormatError
     15 DEFAULT_MODE = RTLD_LOCAL
     16 if _os.name == 'posix' and _sys.platform == 'darwin':
     17     if int(_os.uname()[2].split('.')[0]) < 8:
     18         DEFAULT_MODE = RTLD_GLOBAL
     19 from _ctypes import FUNCFLAG_CDECL as _FUNCFLAG_CDECL, FUNCFLAG_PYTHONAPI as _FUNCFLAG_PYTHONAPI, FUNCFLAG_USE_ERRNO as _FUNCFLAG_USE_ERRNO, FUNCFLAG_USE_LASTERROR as _FUNCFLAG_USE_LASTERROR
     20 
     21 def create_string_buffer(init, size = None):
     22     if isinstance(init, (str, unicode)):
     23         if size is None:
     24             size = len(init) + 1
     25         buftype = c_char * size
     26         buf = buftype()
     27         buf.value = init
     28         return buf
     29     if isinstance(init, (int, long)):
     30         buftype = c_char * init
     31         buf = buftype()
     32         return buf
     33     raise TypeError(init)
     34 
     35 
     36 def c_buffer(init, size = None):
     37     return create_string_buffer(init, size)
     38 
     39 
     40 _c_functype_cache = {}
     41 
     42 def CFUNCTYPE(restype, *argtypes, **kw):
     43     flags = _FUNCFLAG_CDECL
     44     if kw.pop('use_errno', False):
     45         flags |= _FUNCFLAG_USE_ERRNO
     46     if kw.pop('use_last_error', False):
     47         flags |= _FUNCFLAG_USE_LASTERROR
     48     if kw:
     49         raise ValueError('unexpected keyword argument(s) %s' % kw.keys())
     50     try:
     51         return _c_functype_cache[restype, argtypes, flags]
     52     except KeyError:
     53 
     54         class CFunctionType(_CFuncPtr):
     55             _argtypes_ = argtypes
     56             _restype_ = restype
     57             _flags_ = flags
     58 
     59         _c_functype_cache[restype, argtypes, flags] = CFunctionType
     60         return CFunctionType
     61 
     62 
     63 if _os.name in ('nt', 'ce'):
     64     from _ctypes import LoadLibrary as _dlopen
     65     from _ctypes import FUNCFLAG_STDCALL as _FUNCFLAG_STDCALL
     66     if _os.name == 'ce':
     67         _FUNCFLAG_STDCALL = _FUNCFLAG_CDECL
     68     _win_functype_cache = {}
     69 
     70     def WINFUNCTYPE(restype, *argtypes, **kw):
     71         flags = _FUNCFLAG_STDCALL
     72         if kw.pop('use_errno', False):
     73             flags |= _FUNCFLAG_USE_ERRNO
     74         if kw.pop('use_last_error', False):
     75             flags |= _FUNCFLAG_USE_LASTERROR
     76         if kw:
     77             raise ValueError('unexpected keyword argument(s) %s' % kw.keys())
     78         try:
     79             return _win_functype_cache[restype, argtypes, flags]
     80         except KeyError:
     81 
     82             class WinFunctionType(_CFuncPtr):
     83                 _argtypes_ = argtypes
     84                 _restype_ = restype
     85                 _flags_ = flags
     86 
     87             _win_functype_cache[restype, argtypes, flags] = WinFunctionType
     88             return WinFunctionType
     89 
     90 
     91     if WINFUNCTYPE.__doc__:
     92         WINFUNCTYPE.__doc__ = CFUNCTYPE.__doc__.replace('CFUNCTYPE', 'WINFUNCTYPE')
     93 elif _os.name == 'posix':
     94     from _ctypes import dlopen as _dlopen
     95 from _ctypes import sizeof, byref, addressof, alignment, resize
     96 from _ctypes import get_errno, set_errno
     97 from _ctypes import _SimpleCData
     98 
     99 def _check_size(typ, typecode = None):
    100     from struct import calcsize
    101     if typecode is None:
    102         typecode = typ._type_
    103     actual, required = sizeof(typ), calcsize(typecode)
    104     if actual != required:
    105         raise SystemError('sizeof(%s) wrong: %d instead of %d' % (typ, actual, required))
    106 
    107 
    108 class py_object(_SimpleCData):
    109     _type_ = 'O'
    110 
    111     def __repr__(self):
    112         try:
    113             return super(py_object, self).__repr__()
    114         except ValueError:
    115             return '%s(<NULL>)' % type(self).__name__
    116 
    117 
    118 _check_size(py_object, 'P')
    119 
    120 class c_short(_SimpleCData):
    121     _type_ = 'h'
    122 
    123 
    124 _check_size(c_short)
    125 
    126 class c_ushort(_SimpleCData):
    127     _type_ = 'H'
    128 
    129 
    130 _check_size(c_ushort)
    131 
    132 class c_long(_SimpleCData):
    133     _type_ = 'l'
    134 
    135 
    136 _check_size(c_long)
    137 
    138 class c_ulong(_SimpleCData):
    139     _type_ = 'L'
    140 
    141 
    142 _check_size(c_ulong)
    143 if _calcsize('i') == _calcsize('l'):
    144     c_int = c_long
    145     c_uint = c_ulong
    146 else:
    147 
    148     class c_int(_SimpleCData):
    149         _type_ = 'i'
    150 
    151 
    152     _check_size(c_int)
    153 
    154     class c_uint(_SimpleCData):
    155         _type_ = 'I'
    156 
    157 
    158     _check_size(c_uint)
    159 
    160 class c_float(_SimpleCData):
    161     _type_ = 'f'
    162 
    163 
    164 _check_size(c_float)
    165 
    166 class c_double(_SimpleCData):
    167     _type_ = 'd'
    168 
    169 
    170 _check_size(c_double)
    171 
    172 class c_longdouble(_SimpleCData):
    173     _type_ = 'g'
    174 
    175 
    176 if sizeof(c_longdouble) == sizeof(c_double):
    177     c_longdouble = c_double
    178 if _calcsize('l') == _calcsize('q'):
    179     c_longlong = c_long
    180     c_ulonglong = c_ulong
    181 else:
    182 
    183     class c_longlong(_SimpleCData):
    184         _type_ = 'q'
    185 
    186 
    187     _check_size(c_longlong)
    188 
    189     class c_ulonglong(_SimpleCData):
    190         _type_ = 'Q'
    191 
    192 
    193     _check_size(c_ulonglong)
    194 
    195 class c_ubyte(_SimpleCData):
    196     _type_ = 'B'
    197 
    198 
    199 c_ubyte.__ctype_le__ = c_ubyte.__ctype_be__ = c_ubyte
    200 _check_size(c_ubyte)
    201 
    202 class c_byte(_SimpleCData):
    203     _type_ = 'b'
    204 
    205 
    206 c_byte.__ctype_le__ = c_byte.__ctype_be__ = c_byte
    207 _check_size(c_byte)
    208 
    209 class c_char(_SimpleCData):
    210     _type_ = 'c'
    211 
    212 
    213 c_char.__ctype_le__ = c_char.__ctype_be__ = c_char
    214 _check_size(c_char)
    215 
    216 class c_char_p(_SimpleCData):
    217     _type_ = 'z'
    218     if _os.name == 'nt':
    219 
    220         def __repr__(self):
    221             if not windll.kernel32.IsBadStringPtrA(self, -1):
    222                 return '%s(%r)' % (self.__class__.__name__, self.value)
    223             return '%s(%s)' % (self.__class__.__name__, cast(self, c_void_p).value)
    224 
    225     else:
    226 
    227         def __repr__(self):
    228             return '%s(%s)' % (self.__class__.__name__, cast(self, c_void_p).value)
    229 
    230 
    231 _check_size(c_char_p, 'P')
    232 
    233 class c_void_p(_SimpleCData):
    234     _type_ = 'P'
    235 
    236 
    237 c_voidp = c_void_p
    238 _check_size(c_void_p)
    239 
    240 class c_bool(_SimpleCData):
    241     _type_ = '?'
    242 
    243 
    244 from _ctypes import POINTER, pointer, _pointer_type_cache
    245 try:
    246     from _ctypes import set_conversion_mode
    247 except ImportError:
    248     pass
    249 else:
    250     if _os.name in ('nt', 'ce'):
    251         set_conversion_mode('mbcs', 'ignore')
    252     else:
    253         set_conversion_mode('ascii', 'strict')
    254 
    255     class c_wchar_p(_SimpleCData):
    256         _type_ = 'Z'
    257 
    258 
    259     class c_wchar(_SimpleCData):
    260         _type_ = 'u'
    261 
    262 
    263     POINTER(c_wchar).from_param = c_wchar_p.from_param
    264 
    265     def create_unicode_buffer(init, size = None):
    266         if isinstance(init, (str, unicode)):
    267             if size is None:
    268                 size = len(init) + 1
    269             buftype = c_wchar * size
    270             buf = buftype()
    271             buf.value = init
    272             return buf
    273         if isinstance(init, (int, long)):
    274             buftype = c_wchar * init
    275             buf = buftype()
    276             return buf
    277         raise TypeError(init)
    278 
    279 
    280 POINTER(c_char).from_param = c_char_p.from_param
    281 
    282 def SetPointerType(pointer, cls):
    283     if _pointer_type_cache.get(cls, None) is not None:
    284         raise RuntimeError('This type already exists in the cache')
    285     if id(pointer) not in _pointer_type_cache:
    286         raise RuntimeError("What's this???")
    287     pointer.set_type(cls)
    288     _pointer_type_cache[cls] = pointer
    289     del _pointer_type_cache[id(pointer)]
    290 
    291 
    292 def ARRAY(typ, len):
    293     return typ * len
    294 
    295 
    296 class CDLL(object):
    297     _func_flags_ = _FUNCFLAG_CDECL
    298     _func_restype_ = c_int
    299 
    300     def __init__(self, name, mode = DEFAULT_MODE, handle = None, use_errno = False, use_last_error = False):
    301         self._name = name
    302         flags = self._func_flags_
    303         if use_errno:
    304             flags |= _FUNCFLAG_USE_ERRNO
    305         if use_last_error:
    306             flags |= _FUNCFLAG_USE_LASTERROR
    307 
    308         class _FuncPtr(_CFuncPtr):
    309             _flags_ = flags
    310             _restype_ = self._func_restype_
    311 
    312         self._FuncPtr = _FuncPtr
    313         if handle is None:
    314             self._handle = _dlopen(self._name, mode)
    315         else:
    316             self._handle = handle
    317 
    318     def __repr__(self):
    319         return "<%s '%s', handle %x at %x>" % (self.__class__.__name__,
    320          self._name,
    321          self._handle & _sys.maxint * 2 + 1,
    322          id(self) & _sys.maxint * 2 + 1)
    323 
    324     def __getattr__(self, name):
    325         if name.startswith('__') and name.endswith('__'):
    326             raise AttributeError(name)
    327         func = self.__getitem__(name)
    328         setattr(self, name, func)
    329         return func
    330 
    331     def __getitem__(self, name_or_ordinal):
    332         func = self._FuncPtr((name_or_ordinal, self))
    333         if not isinstance(name_or_ordinal, (int, long)):
    334             func.__name__ = name_or_ordinal
    335         return func
    336 
    337 
    338 class PyDLL(CDLL):
    339     _func_flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI
    340 
    341 
    342 if _os.name in ('nt', 'ce'):
    343 
    344     class WinDLL(CDLL):
    345         _func_flags_ = _FUNCFLAG_STDCALL
    346 
    347 
    348     from _ctypes import _check_HRESULT, _SimpleCData
    349 
    350     class HRESULT(_SimpleCData):
    351         _type_ = 'l'
    352         _check_retval_ = _check_HRESULT
    353 
    354 
    355     class OleDLL(CDLL):
    356         _func_flags_ = _FUNCFLAG_STDCALL
    357         _func_restype_ = HRESULT
    358 
    359 
    360 class LibraryLoader(object):
    361 
    362     def __init__(self, dlltype):
    363         self._dlltype = dlltype
    364 
    365     def __getattr__(self, name):
    366         if name[0] == '_':
    367             raise AttributeError(name)
    368         dll = self._dlltype(name)
    369         setattr(self, name, dll)
    370         return dll
    371 
    372     def __getitem__(self, name):
    373         return getattr(self, name)
    374 
    375     def LoadLibrary(self, name):
    376         return self._dlltype(name)
    377 
    378 
    379 cdll = LibraryLoader(CDLL)
    380 pydll = LibraryLoader(PyDLL)
    381 if _os.name in ('nt', 'ce') and hasattr(_sys, 'dllhandle'):
    382     pythonapi = PyDLL('python dll', None, _sys.dllhandle)
    383 elif _sys.platform == 'cygwin':
    384     pythonapi = PyDLL('libpython%d.%d.dll' % _sys.version_info[:2])
    385 else:
    386     pythonapi = None
    387 if _os.name in ('nt', 'ce'):
    388     windll = LibraryLoader(WinDLL)
    389     oledll = LibraryLoader(OleDLL)
    390     if _os.name == 'nt':
    391         GetLastError = windll.kernel32.GetLastError
    392     else:
    393         GetLastError = windll.coredll.GetLastError
    394     from _ctypes import get_last_error, set_last_error
    395 
    396     def WinError(code = None, descr = None):
    397         if code is None:
    398             code = GetLastError()
    399         if descr is None:
    400             descr = FormatError(code).strip()
    401         return WindowsError(code, descr)
    402 
    403 
    404 _pointer_type_cache[None] = c_void_p
    405 if sizeof(c_uint) == sizeof(c_void_p):
    406     c_size_t = c_uint
    407     c_ssize_t = c_int
    408 elif sizeof(c_ulong) == sizeof(c_void_p):
    409     c_size_t = c_ulong
    410     c_ssize_t = c_long
    411 elif sizeof(c_ulonglong) == sizeof(c_void_p):
    412     c_size_t = c_ulonglong
    413     c_ssize_t = c_longlong
    414 from _ctypes import _memmove_addr, _memset_addr, _string_at_addr, _cast_addr
    415 memmove = CFUNCTYPE(c_void_p, c_void_p, c_void_p, c_size_t)(_memmove_addr)
    416 memset = CFUNCTYPE(c_void_p, c_void_p, c_int, c_size_t)(_memset_addr)
    417 
    418 def PYFUNCTYPE(restype, *argtypes):
    419 
    420     class CFunctionType(_CFuncPtr):
    421         _argtypes_ = argtypes
    422         _restype_ = restype
    423         _flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI
    424 
    425     return CFunctionType
    426 
    427 
    428 _cast = PYFUNCTYPE(py_object, c_void_p, py_object, py_object)(_cast_addr)
    429 
    430 def cast(obj, typ):
    431     return _cast(obj, obj, typ)
    432 
    433 
    434 _string_at = PYFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr)
    435 
    436 def string_at(ptr, size = -1):
    437     return _string_at(ptr, size)
    438 
    439 
    440 try:
    441     from _ctypes import _wstring_at_addr
    442 except ImportError:
    443     pass
    444 else:
    445     _wstring_at = PYFUNCTYPE(py_object, c_void_p, c_int)(_wstring_at_addr)
    446 
    447     def wstring_at(ptr, size = -1):
    448         return _wstring_at(ptr, size)
    449 
    450 
    451 if _os.name in ('nt', 'ce'):
    452 
    453     def DllGetClassObject(rclsid, riid, ppv):
    454         try:
    455             ccom = __import__('comtypes.server.inprocserver', globals(), locals(), ['*'])
    456         except ImportError:
    457             return -2147221231
    458 
    459         return ccom.DllGetClassObject(rclsid, riid, ppv)
    460 
    461 
    462     def DllCanUnloadNow():
    463         try:
    464             ccom = __import__('comtypes.server.inprocserver', globals(), locals(), ['*'])
    465         except ImportError:
    466             return 0
    467 
    468         return ccom.DllCanUnloadNow()
    469 
    470 
    471 from ctypes._endian import BigEndianStructure, LittleEndianStructure
    472 c_int8 = c_byte
    473 c_uint8 = c_ubyte
    474 for kind in [c_short,
    475  c_int,
    476  c_long,
    477  c_longlong]:
    478     if sizeof(kind) == 2:
    479         c_int16 = kind
    480     elif sizeof(kind) == 4:
    481         c_int32 = kind
    482     elif sizeof(kind) == 8:
    483         c_int64 = kind
    484 
    485 for kind in [c_ushort,
    486  c_uint,
    487  c_ulong,
    488  c_ulonglong]:
    489     if sizeof(kind) == 2:
    490         c_uint16 = kind
    491     elif sizeof(kind) == 4:
    492         c_uint32 = kind
    493     elif sizeof(kind) == 8:
    494         c_uint64 = kind
    495 
    496 del kind
    497 CFUNCTYPE(c_int)(lambda : None)