blob: f7d0970f94bca8d82d3c347f023f0b6e11d763a9 [file] [log] [blame]
Thomas Hellerbabddfc2006-03-08 19:56:54 +00001"""create and manipulate C data types in Python"""
2
Thomas Hellerbabddfc2006-03-08 19:56:54 +00003import os as _os, sys as _sys
Thomas Hellerbabddfc2006-03-08 19:56:54 +00004
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00005__version__ = "1.1.0"
Thomas Hellerbabddfc2006-03-08 19:56:54 +00006
7from _ctypes import Union, Structure, Array
8from _ctypes import _Pointer
9from _ctypes import CFuncPtr as _CFuncPtr
10from _ctypes import __version__ as _ctypes_version
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011from _ctypes import RTLD_LOCAL, RTLD_GLOBAL
Thomas Hellerbabddfc2006-03-08 19:56:54 +000012from _ctypes import ArgumentError
13
14from struct import calcsize as _calcsize
15
16if __version__ != _ctypes_version:
Collin Wintera73bfee2007-08-30 03:47:13 +000017 raise Exception("Version number mismatch", __version__, _ctypes_version)
Thomas Hellerbabddfc2006-03-08 19:56:54 +000018
19if _os.name in ("nt", "ce"):
20 from _ctypes import FormatError
21
Thomas Wouters0e3f5912006-08-11 14:57:12 +000022DEFAULT_MODE = RTLD_LOCAL
23if _os.name == "posix" and _sys.platform == "darwin":
Thomas Wouters0e3f5912006-08-11 14:57:12 +000024 # On OS X 10.3, we use RTLD_GLOBAL as default mode
25 # because RTLD_LOCAL does not work at least on some
Guido van Rossum8ce8a782007-11-01 19:42:39 +000026 # libraries. OS X 10.3 is Darwin 7, so we check for
27 # that.
Thomas Wouters0e3f5912006-08-11 14:57:12 +000028
Guido van Rossum8ce8a782007-11-01 19:42:39 +000029 if int(_os.uname()[2].split('.')[0]) < 8:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000030 DEFAULT_MODE = RTLD_GLOBAL
31
Thomas Hellerbabddfc2006-03-08 19:56:54 +000032from _ctypes import FUNCFLAG_CDECL as _FUNCFLAG_CDECL, \
33 FUNCFLAG_PYTHONAPI as _FUNCFLAG_PYTHONAPI
34
Thomas Hellerbabddfc2006-03-08 19:56:54 +000035"""
36WINOLEAPI -> HRESULT
37WINOLEAPI_(type)
38
39STDMETHODCALLTYPE
40
41STDMETHOD(name)
42STDMETHOD_(type, name)
43
44STDAPICALLTYPE
45"""
46
47def create_string_buffer(init, size=None):
Guido van Rossum97f9d4f2007-10-24 18:41:19 +000048 """create_string_buffer(aBytes) -> character array
Thomas Hellerbabddfc2006-03-08 19:56:54 +000049 create_string_buffer(anInteger) -> character array
50 create_string_buffer(aString, anInteger) -> character array
51 """
Guido van Rossum97f9d4f2007-10-24 18:41:19 +000052 if isinstance(init, (str, bytes)):
Thomas Hellerbabddfc2006-03-08 19:56:54 +000053 if size is None:
54 size = len(init)+1
55 buftype = c_char * size
56 buf = buftype()
57 buf.value = init
58 return buf
Walter Dörwaldaa97f042007-05-03 21:05:51 +000059 elif isinstance(init, int):
Thomas Hellerbabddfc2006-03-08 19:56:54 +000060 buftype = c_char * init
61 buf = buftype()
62 return buf
Collin Wintera73bfee2007-08-30 03:47:13 +000063 raise TypeError(init)
Thomas Hellerbabddfc2006-03-08 19:56:54 +000064
65def c_buffer(init, size=None):
66## "deprecated, use create_string_buffer instead"
67## import warnings
68## warnings.warn("c_buffer is deprecated, use create_string_buffer instead",
69## DeprecationWarning, stacklevel=2)
70 return create_string_buffer(init, size)
71
72_c_functype_cache = {}
73def CFUNCTYPE(restype, *argtypes):
74 """CFUNCTYPE(restype, *argtypes) -> function prototype.
Tim Peterse8d09e52006-03-09 01:15:05 +000075
Thomas Hellerbabddfc2006-03-08 19:56:54 +000076 restype: the result type
77 argtypes: a sequence specifying the argument types
Tim Peterse8d09e52006-03-09 01:15:05 +000078
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079 The function prototype can be called in different ways to create a
Thomas Hellerbabddfc2006-03-08 19:56:54 +000080 callable object:
Tim Peterse8d09e52006-03-09 01:15:05 +000081
Thomas Wouters477c8d52006-05-27 19:21:47 +000082 prototype(integer address) -> foreign function
83 prototype(callable) -> create and return a C callable function from callable
84 prototype(integer index, method name[, paramflags]) -> foreign function calling a COM method
85 prototype((ordinal number, dll object)[, paramflags]) -> foreign function exported by ordinal
86 prototype((function name, dll object)[, paramflags]) -> foreign function exported by name
Thomas Hellerbabddfc2006-03-08 19:56:54 +000087 """
88 try:
89 return _c_functype_cache[(restype, argtypes)]
90 except KeyError:
91 class CFunctionType(_CFuncPtr):
92 _argtypes_ = argtypes
93 _restype_ = restype
94 _flags_ = _FUNCFLAG_CDECL
95 _c_functype_cache[(restype, argtypes)] = CFunctionType
96 return CFunctionType
97
98if _os.name in ("nt", "ce"):
99 from _ctypes import LoadLibrary as _dlopen
100 from _ctypes import FUNCFLAG_STDCALL as _FUNCFLAG_STDCALL
101 if _os.name == "ce":
102 # 'ce' doesn't have the stdcall calling convention
103 _FUNCFLAG_STDCALL = _FUNCFLAG_CDECL
104
105 _win_functype_cache = {}
106 def WINFUNCTYPE(restype, *argtypes):
107 # docstring set later (very similar to CFUNCTYPE.__doc__)
108 try:
109 return _win_functype_cache[(restype, argtypes)]
110 except KeyError:
111 class WinFunctionType(_CFuncPtr):
112 _argtypes_ = argtypes
113 _restype_ = restype
114 _flags_ = _FUNCFLAG_STDCALL
115 _win_functype_cache[(restype, argtypes)] = WinFunctionType
116 return WinFunctionType
117 if WINFUNCTYPE.__doc__:
118 WINFUNCTYPE.__doc__ = CFUNCTYPE.__doc__.replace("CFUNCTYPE", "WINFUNCTYPE")
119
120elif _os.name == "posix":
121 from _ctypes import dlopen as _dlopen
122
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000123from _ctypes import sizeof, byref, addressof, alignment, resize
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000124from _ctypes import _SimpleCData
125
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000126def _check_size(typ, typecode=None):
127 # Check if sizeof(ctypes_type) against struct.calcsize. This
128 # should protect somewhat against a misconfigured libffi.
129 from struct import calcsize
130 if typecode is None:
131 # Most _type_ codes are the same as used in struct
132 typecode = typ._type_
133 actual, required = sizeof(typ), calcsize(typecode)
134 if actual != required:
135 raise SystemError("sizeof(%s) wrong: %d instead of %d" % \
136 (typ, actual, required))
137
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000138class py_object(_SimpleCData):
139 _type_ = "O"
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000140 def __repr__(self):
141 try:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000142 return super().__repr__()
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000143 except ValueError:
144 return "%s(<NULL>)" % type(self).__name__
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000145_check_size(py_object, "P")
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000146
147class c_short(_SimpleCData):
148 _type_ = "h"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000149_check_size(c_short)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000150
151class c_ushort(_SimpleCData):
152 _type_ = "H"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000153_check_size(c_ushort)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000154
155class c_long(_SimpleCData):
156 _type_ = "l"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000157_check_size(c_long)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000158
159class c_ulong(_SimpleCData):
160 _type_ = "L"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000161_check_size(c_ulong)
Tim Peterse8d09e52006-03-09 01:15:05 +0000162
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000163if _calcsize("i") == _calcsize("l"):
164 # if int and long have the same size, make c_int an alias for c_long
165 c_int = c_long
166 c_uint = c_ulong
167else:
168 class c_int(_SimpleCData):
169 _type_ = "i"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000170 _check_size(c_int)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000171
172 class c_uint(_SimpleCData):
173 _type_ = "I"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000174 _check_size(c_uint)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000175
176class c_float(_SimpleCData):
177 _type_ = "f"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000178_check_size(c_float)
Tim Peterse8d09e52006-03-09 01:15:05 +0000179
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000180class c_double(_SimpleCData):
181 _type_ = "d"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000182_check_size(c_double)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000183
Thomas Wouters89d996e2007-09-08 17:39:28 +0000184class c_longdouble(_SimpleCData):
185 _type_ = "D"
186if sizeof(c_longdouble) == sizeof(c_double):
187 c_longdouble = c_double
188
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000189if _calcsize("l") == _calcsize("q"):
190 # if long and long long have the same size, make c_longlong an alias for c_long
191 c_longlong = c_long
192 c_ulonglong = c_ulong
193else:
194 class c_longlong(_SimpleCData):
195 _type_ = "q"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000196 _check_size(c_longlong)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000197
198 class c_ulonglong(_SimpleCData):
199 _type_ = "Q"
200 ## def from_param(cls, val):
201 ## return ('d', float(val), val)
202 ## from_param = classmethod(from_param)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000203 _check_size(c_ulonglong)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000204
205class c_ubyte(_SimpleCData):
206 _type_ = "B"
207c_ubyte.__ctype_le__ = c_ubyte.__ctype_be__ = c_ubyte
208# backward compatibility:
209##c_uchar = c_ubyte
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000210_check_size(c_ubyte)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000211
212class c_byte(_SimpleCData):
213 _type_ = "b"
214c_byte.__ctype_le__ = c_byte.__ctype_be__ = c_byte
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000215_check_size(c_byte)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000216
217class c_char(_SimpleCData):
218 _type_ = "c"
219c_char.__ctype_le__ = c_char.__ctype_be__ = c_char
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000220_check_size(c_char)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000221
222class c_char_p(_SimpleCData):
223 _type_ = "z"
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000224 if _os.name == "nt":
225 def __repr__(self):
226 if not windll.kernel32.IsBadStringPtrA(self, -1):
227 return "%s(%r)" % (self.__class__.__name__, self.value)
228 return "%s(%s)" % (self.__class__.__name__, cast(self, c_void_p).value)
229 else:
230 def __repr__(self):
231 return "%s(%s)" % (self.__class__.__name__, cast(self, c_void_p).value)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000232_check_size(c_char_p, "P")
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000233
234class c_void_p(_SimpleCData):
235 _type_ = "P"
236c_voidp = c_void_p # backwards compatibility (to a bug)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000237_check_size(c_void_p)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000238
Guido van Rossumd8faa362007-04-27 19:54:29 +0000239class c_bool(_SimpleCData):
240 _type_ = "t"
241
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000242# This cache maps types to pointers to them.
243_pointer_type_cache = {}
244
245def POINTER(cls):
246 try:
247 return _pointer_type_cache[cls]
248 except KeyError:
249 pass
250 if type(cls) is str:
251 klass = type(_Pointer)("LP_%s" % cls,
252 (_Pointer,),
253 {})
254 _pointer_type_cache[id(klass)] = klass
255 return klass
256 else:
257 name = "LP_%s" % cls.__name__
258 klass = type(_Pointer)(name,
259 (_Pointer,),
260 {'_type_': cls})
261 _pointer_type_cache[cls] = klass
262 return klass
263
264try:
265 from _ctypes import set_conversion_mode
266except ImportError:
267 pass
268else:
269 if _os.name in ("nt", "ce"):
270 set_conversion_mode("mbcs", "ignore")
271 else:
272 set_conversion_mode("ascii", "strict")
273
274 class c_wchar_p(_SimpleCData):
275 _type_ = "Z"
276
277 class c_wchar(_SimpleCData):
278 _type_ = "u"
279
280 POINTER(c_wchar).from_param = c_wchar_p.from_param #_SimpleCData.c_wchar_p_from_param
281
282 def create_unicode_buffer(init, size=None):
283 """create_unicode_buffer(aString) -> character array
284 create_unicode_buffer(anInteger) -> character array
285 create_unicode_buffer(aString, anInteger) -> character array
286 """
Thomas Heller60831312007-07-12 19:06:25 +0000287 if isinstance(init, (str, bytes)):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000288 if size is None:
289 size = len(init)+1
290 buftype = c_wchar * size
291 buf = buftype()
292 buf.value = init
293 return buf
Walter Dörwaldaa97f042007-05-03 21:05:51 +0000294 elif isinstance(init, int):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000295 buftype = c_wchar * init
296 buf = buftype()
297 return buf
Collin Wintera73bfee2007-08-30 03:47:13 +0000298 raise TypeError(init)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000299
300POINTER(c_char).from_param = c_char_p.from_param #_SimpleCData.c_char_p_from_param
301
302# XXX Deprecated
303def SetPointerType(pointer, cls):
304 if _pointer_type_cache.get(cls, None) is not None:
Collin Wintera73bfee2007-08-30 03:47:13 +0000305 raise RuntimeError("This type already exists in the cache")
Guido van Rossum1b01e5c2006-08-19 02:45:06 +0000306 if id(pointer) not in _pointer_type_cache:
Collin Wintera73bfee2007-08-30 03:47:13 +0000307 raise RuntimeError("What's this???")
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000308 pointer.set_type(cls)
309 _pointer_type_cache[cls] = pointer
310 del _pointer_type_cache[id(pointer)]
311
312
313def pointer(inst):
314 return POINTER(type(inst))(inst)
315
316# XXX Deprecated
317def ARRAY(typ, len):
318 return typ * len
319
320################################################################
321
322
323class CDLL(object):
324 """An instance of this class represents a loaded dll/shared
325 library, exporting functions using the standard C calling
326 convention (named 'cdecl' on Windows).
327
328 The exported functions can be accessed as attributes, or by
329 indexing with the function name. Examples:
330
331 <obj>.qsort -> callable object
332 <obj>['qsort'] -> callable object
333
334 Calling the functions releases the Python GIL during the call and
Thomas Woutersed03b412007-08-28 21:37:11 +0000335 reacquires it afterwards.
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000336 """
337 class _FuncPtr(_CFuncPtr):
338 _flags_ = _FUNCFLAG_CDECL
339 _restype_ = c_int # default, can be overridden in instances
340
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000341 def __init__(self, name, mode=DEFAULT_MODE, handle=None):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000342 self._name = name
343 if handle is None:
344 self._handle = _dlopen(self._name, mode)
345 else:
346 self._handle = handle
347
348 def __repr__(self):
349 return "<%s '%s', handle %x at %x>" % \
350 (self.__class__.__name__, self._name,
Christian Heimesa37d4c62007-12-04 23:02:19 +0000351 (self._handle & (_sys.maxsize*2 + 1)),
352 id(self) & (_sys.maxsize*2 + 1))
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000353
354 def __getattr__(self, name):
355 if name.startswith('__') and name.endswith('__'):
Collin Wintera73bfee2007-08-30 03:47:13 +0000356 raise AttributeError(name)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000357 func = self.__getitem__(name)
358 setattr(self, name, func)
359 return func
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000360
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000361 def __getitem__(self, name_or_ordinal):
362 func = self._FuncPtr((name_or_ordinal, self))
Walter Dörwaldaa97f042007-05-03 21:05:51 +0000363 if not isinstance(name_or_ordinal, int):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000364 func.__name__ = name_or_ordinal
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000365 return func
366
367class PyDLL(CDLL):
368 """This class represents the Python library itself. It allows to
369 access Python API functions. The GIL is not released, and
370 Python exceptions are handled correctly.
371 """
372 class _FuncPtr(_CFuncPtr):
373 _flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI
374 _restype_ = c_int # default, can be overridden in instances
375
376if _os.name in ("nt", "ce"):
Tim Peterse8d09e52006-03-09 01:15:05 +0000377
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000378 class WinDLL(CDLL):
379 """This class represents a dll exporting functions using the
380 Windows stdcall calling convention.
381 """
382 class _FuncPtr(_CFuncPtr):
383 _flags_ = _FUNCFLAG_STDCALL
384 _restype_ = c_int # default, can be overridden in instances
385
386 # XXX Hm, what about HRESULT as normal parameter?
387 # Mustn't it derive from c_long then?
388 from _ctypes import _check_HRESULT, _SimpleCData
389 class HRESULT(_SimpleCData):
390 _type_ = "l"
391 # _check_retval_ is called with the function's result when it
392 # is used as restype. It checks for the FAILED bit, and
393 # raises a WindowsError if it is set.
394 #
395 # The _check_retval_ method is implemented in C, so that the
396 # method definition itself is not included in the traceback
397 # when it raises an error - that is what we want (and Python
398 # doesn't have a way to raise an exception in the caller's
399 # frame).
400 _check_retval_ = _check_HRESULT
Tim Peterse8d09e52006-03-09 01:15:05 +0000401
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000402 class OleDLL(CDLL):
403 """This class represents a dll exporting functions using the
404 Windows stdcall calling convention, and returning HRESULT.
405 HRESULT error values are automatically raised as WindowsError
406 exceptions.
407 """
408 class _FuncPtr(_CFuncPtr):
409 _flags_ = _FUNCFLAG_STDCALL
410 _restype_ = HRESULT
411
Thomas Wouters477c8d52006-05-27 19:21:47 +0000412class LibraryLoader(object):
413 def __init__(self, dlltype):
414 self._dlltype = dlltype
415
416 def __getattr__(self, name):
417 if name[0] == '_':
418 raise AttributeError(name)
419 dll = self._dlltype(name)
420 setattr(self, name, dll)
421 return dll
422
423 def __getitem__(self, name):
424 return getattr(self, name)
425
426 def LoadLibrary(self, name):
427 return self._dlltype(name)
428
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000429cdll = LibraryLoader(CDLL)
430pydll = LibraryLoader(PyDLL)
431
432if _os.name in ("nt", "ce"):
433 pythonapi = PyDLL("python dll", None, _sys.dllhandle)
434elif _sys.platform == "cygwin":
435 pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
436else:
437 pythonapi = PyDLL(None)
438
439
440if _os.name in ("nt", "ce"):
441 windll = LibraryLoader(WinDLL)
442 oledll = LibraryLoader(OleDLL)
443
444 if _os.name == "nt":
445 GetLastError = windll.kernel32.GetLastError
446 else:
447 GetLastError = windll.coredll.GetLastError
448
449 def WinError(code=None, descr=None):
450 if code is None:
451 code = GetLastError()
452 if descr is None:
453 descr = FormatError(code).strip()
454 return WindowsError(code, descr)
455
456_pointer_type_cache[None] = c_void_p
457
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000458if sizeof(c_uint) == sizeof(c_void_p):
459 c_size_t = c_uint
460elif sizeof(c_ulong) == sizeof(c_void_p):
461 c_size_t = c_ulong
Thomas Wouters89f507f2006-12-13 04:49:30 +0000462elif sizeof(c_ulonglong) == sizeof(c_void_p):
463 c_size_t = c_ulonglong
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000464
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000465# functions
466
467from _ctypes import _memmove_addr, _memset_addr, _string_at_addr, _cast_addr
468
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000469## void *memmove(void *, const void *, size_t);
470memmove = CFUNCTYPE(c_void_p, c_void_p, c_void_p, c_size_t)(_memmove_addr)
471
472## void *memset(void *, int, size_t)
473memset = CFUNCTYPE(c_void_p, c_void_p, c_int, c_size_t)(_memset_addr)
474
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000475def PYFUNCTYPE(restype, *argtypes):
476 class CFunctionType(_CFuncPtr):
477 _argtypes_ = argtypes
478 _restype_ = restype
479 _flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI
480 return CFunctionType
Thomas Wouters477c8d52006-05-27 19:21:47 +0000481
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000482_cast = PYFUNCTYPE(py_object, c_void_p, py_object, py_object)(_cast_addr)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000483def cast(obj, typ):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000484 return _cast(obj, obj, typ)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000485
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000486_string_at = CFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000487def string_at(ptr, size=-1):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000488 """string_at(addr[, size]) -> string
489
490 Return the string at addr."""
491 return _string_at(ptr, size)
492
493try:
494 from _ctypes import _wstring_at_addr
495except ImportError:
496 pass
497else:
498 _wstring_at = CFUNCTYPE(py_object, c_void_p, c_int)(_wstring_at_addr)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000499 def wstring_at(ptr, size=-1):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000500 """wstring_at(addr[, size]) -> string
501
502 Return the string at addr."""
503 return _wstring_at(ptr, size)
Tim Peterse8d09e52006-03-09 01:15:05 +0000504
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000505
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000506if _os.name in ("nt", "ce"): # COM stuff
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000507 def DllGetClassObject(rclsid, riid, ppv):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000508 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000509 ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*'])
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000510 except ImportError:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000511 return -2147221231 # CLASS_E_CLASSNOTAVAILABLE
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000512 else:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000513 return ccom.DllGetClassObject(rclsid, riid, ppv)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000514
515 def DllCanUnloadNow():
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000516 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000517 ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*'])
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000518 except ImportError:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000519 return 0 # S_OK
520 return ccom.DllCanUnloadNow()
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000521
522from ctypes._endian import BigEndianStructure, LittleEndianStructure
523
524# Fill in specifically-sized types
525c_int8 = c_byte
526c_uint8 = c_ubyte
527for kind in [c_short, c_int, c_long, c_longlong]:
528 if sizeof(kind) == 2: c_int16 = kind
529 elif sizeof(kind) == 4: c_int32 = kind
530 elif sizeof(kind) == 8: c_int64 = kind
531for kind in [c_ushort, c_uint, c_ulong, c_ulonglong]:
532 if sizeof(kind) == 2: c_uint16 = kind
533 elif sizeof(kind) == 4: c_uint32 = kind
534 elif sizeof(kind) == 8: c_uint64 = kind
535del(kind)
Thomas Heller674e9382007-08-31 13:06:44 +0000536
537# XXX for whatever reasons, creating the first instance of a callback
538# function is needed for the unittests on Win64 to succeed. This MAY
539# be a compiler bug, since the problem occurs only when _ctypes is
540# compiled with the MS SDK compiler. Or an uninitialized variable?
541CFUNCTYPE(c_int)(lambda: None)