blob: cff5483ea4282cf38cdda7702177cf43490ac4d7 [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:
17 raise Exception, ("Version number mismatch", __version__, _ctypes_version)
18
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":
24 import gestalt
25
26 # gestalt.gestalt("sysv") returns the version number of the
27 # currently active system file as BCD.
28 # On OS X 10.4.6 -> 0x1046
29 # On OS X 10.2.8 -> 0x1028
30 # See also http://www.rgaros.nl/gestalt/
31 #
32 # On OS X 10.3, we use RTLD_GLOBAL as default mode
33 # because RTLD_LOCAL does not work at least on some
34 # libraries.
35
36 if gestalt.gestalt("sysv") < 0x1040:
37 DEFAULT_MODE = RTLD_GLOBAL
38
Thomas Hellerbabddfc2006-03-08 19:56:54 +000039from _ctypes import FUNCFLAG_CDECL as _FUNCFLAG_CDECL, \
40 FUNCFLAG_PYTHONAPI as _FUNCFLAG_PYTHONAPI
41
Thomas Hellerbabddfc2006-03-08 19:56:54 +000042"""
43WINOLEAPI -> HRESULT
44WINOLEAPI_(type)
45
46STDMETHODCALLTYPE
47
48STDMETHOD(name)
49STDMETHOD_(type, name)
50
51STDAPICALLTYPE
52"""
53
54def create_string_buffer(init, size=None):
55 """create_string_buffer(aString) -> character array
56 create_string_buffer(anInteger) -> character array
57 create_string_buffer(aString, anInteger) -> character array
58 """
Walter Dörwaldaa97f042007-05-03 21:05:51 +000059 if isinstance(init, str):
Thomas Hellerbabddfc2006-03-08 19:56:54 +000060 if size is None:
61 size = len(init)+1
62 buftype = c_char * size
63 buf = buftype()
64 buf.value = init
65 return buf
Walter Dörwaldaa97f042007-05-03 21:05:51 +000066 elif isinstance(init, int):
Thomas Hellerbabddfc2006-03-08 19:56:54 +000067 buftype = c_char * init
68 buf = buftype()
69 return buf
70 raise TypeError, init
71
72def c_buffer(init, size=None):
73## "deprecated, use create_string_buffer instead"
74## import warnings
75## warnings.warn("c_buffer is deprecated, use create_string_buffer instead",
76## DeprecationWarning, stacklevel=2)
77 return create_string_buffer(init, size)
78
79_c_functype_cache = {}
80def CFUNCTYPE(restype, *argtypes):
81 """CFUNCTYPE(restype, *argtypes) -> function prototype.
Tim Peterse8d09e52006-03-09 01:15:05 +000082
Thomas Hellerbabddfc2006-03-08 19:56:54 +000083 restype: the result type
84 argtypes: a sequence specifying the argument types
Tim Peterse8d09e52006-03-09 01:15:05 +000085
Thomas Wouters0e3f5912006-08-11 14:57:12 +000086 The function prototype can be called in different ways to create a
Thomas Hellerbabddfc2006-03-08 19:56:54 +000087 callable object:
Tim Peterse8d09e52006-03-09 01:15:05 +000088
Thomas Wouters477c8d52006-05-27 19:21:47 +000089 prototype(integer address) -> foreign function
90 prototype(callable) -> create and return a C callable function from callable
91 prototype(integer index, method name[, paramflags]) -> foreign function calling a COM method
92 prototype((ordinal number, dll object)[, paramflags]) -> foreign function exported by ordinal
93 prototype((function name, dll object)[, paramflags]) -> foreign function exported by name
Thomas Hellerbabddfc2006-03-08 19:56:54 +000094 """
95 try:
96 return _c_functype_cache[(restype, argtypes)]
97 except KeyError:
98 class CFunctionType(_CFuncPtr):
99 _argtypes_ = argtypes
100 _restype_ = restype
101 _flags_ = _FUNCFLAG_CDECL
102 _c_functype_cache[(restype, argtypes)] = CFunctionType
103 return CFunctionType
104
105if _os.name in ("nt", "ce"):
106 from _ctypes import LoadLibrary as _dlopen
107 from _ctypes import FUNCFLAG_STDCALL as _FUNCFLAG_STDCALL
108 if _os.name == "ce":
109 # 'ce' doesn't have the stdcall calling convention
110 _FUNCFLAG_STDCALL = _FUNCFLAG_CDECL
111
112 _win_functype_cache = {}
113 def WINFUNCTYPE(restype, *argtypes):
114 # docstring set later (very similar to CFUNCTYPE.__doc__)
115 try:
116 return _win_functype_cache[(restype, argtypes)]
117 except KeyError:
118 class WinFunctionType(_CFuncPtr):
119 _argtypes_ = argtypes
120 _restype_ = restype
121 _flags_ = _FUNCFLAG_STDCALL
122 _win_functype_cache[(restype, argtypes)] = WinFunctionType
123 return WinFunctionType
124 if WINFUNCTYPE.__doc__:
125 WINFUNCTYPE.__doc__ = CFUNCTYPE.__doc__.replace("CFUNCTYPE", "WINFUNCTYPE")
126
127elif _os.name == "posix":
128 from _ctypes import dlopen as _dlopen
129
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000130from _ctypes import sizeof, byref, addressof, alignment, resize
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000131from _ctypes import _SimpleCData
132
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000133def _check_size(typ, typecode=None):
134 # Check if sizeof(ctypes_type) against struct.calcsize. This
135 # should protect somewhat against a misconfigured libffi.
136 from struct import calcsize
137 if typecode is None:
138 # Most _type_ codes are the same as used in struct
139 typecode = typ._type_
140 actual, required = sizeof(typ), calcsize(typecode)
141 if actual != required:
142 raise SystemError("sizeof(%s) wrong: %d instead of %d" % \
143 (typ, actual, required))
144
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000145class py_object(_SimpleCData):
146 _type_ = "O"
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000147 def __repr__(self):
148 try:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000149 return super().__repr__()
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000150 except ValueError:
151 return "%s(<NULL>)" % type(self).__name__
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000152_check_size(py_object, "P")
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000153
154class c_short(_SimpleCData):
155 _type_ = "h"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000156_check_size(c_short)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000157
158class c_ushort(_SimpleCData):
159 _type_ = "H"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000160_check_size(c_ushort)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000161
162class c_long(_SimpleCData):
163 _type_ = "l"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000164_check_size(c_long)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000165
166class c_ulong(_SimpleCData):
167 _type_ = "L"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000168_check_size(c_ulong)
Tim Peterse8d09e52006-03-09 01:15:05 +0000169
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000170if _calcsize("i") == _calcsize("l"):
171 # if int and long have the same size, make c_int an alias for c_long
172 c_int = c_long
173 c_uint = c_ulong
174else:
175 class c_int(_SimpleCData):
176 _type_ = "i"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000177 _check_size(c_int)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000178
179 class c_uint(_SimpleCData):
180 _type_ = "I"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000181 _check_size(c_uint)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000182
183class c_float(_SimpleCData):
184 _type_ = "f"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000185_check_size(c_float)
Tim Peterse8d09e52006-03-09 01:15:05 +0000186
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000187class c_double(_SimpleCData):
188 _type_ = "d"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000189_check_size(c_double)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000190
191if _calcsize("l") == _calcsize("q"):
192 # if long and long long have the same size, make c_longlong an alias for c_long
193 c_longlong = c_long
194 c_ulonglong = c_ulong
195else:
196 class c_longlong(_SimpleCData):
197 _type_ = "q"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000198 _check_size(c_longlong)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000199
200 class c_ulonglong(_SimpleCData):
201 _type_ = "Q"
202 ## def from_param(cls, val):
203 ## return ('d', float(val), val)
204 ## from_param = classmethod(from_param)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000205 _check_size(c_ulonglong)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000206
207class c_ubyte(_SimpleCData):
208 _type_ = "B"
209c_ubyte.__ctype_le__ = c_ubyte.__ctype_be__ = c_ubyte
210# backward compatibility:
211##c_uchar = c_ubyte
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000212_check_size(c_ubyte)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000213
214class c_byte(_SimpleCData):
215 _type_ = "b"
216c_byte.__ctype_le__ = c_byte.__ctype_be__ = c_byte
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000217_check_size(c_byte)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000218
219class c_char(_SimpleCData):
220 _type_ = "c"
221c_char.__ctype_le__ = c_char.__ctype_be__ = c_char
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000222_check_size(c_char)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000223
224class c_char_p(_SimpleCData):
225 _type_ = "z"
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000226 if _os.name == "nt":
227 def __repr__(self):
228 if not windll.kernel32.IsBadStringPtrA(self, -1):
229 return "%s(%r)" % (self.__class__.__name__, self.value)
230 return "%s(%s)" % (self.__class__.__name__, cast(self, c_void_p).value)
231 else:
232 def __repr__(self):
233 return "%s(%s)" % (self.__class__.__name__, cast(self, c_void_p).value)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000234_check_size(c_char_p, "P")
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000235
236class c_void_p(_SimpleCData):
237 _type_ = "P"
238c_voidp = c_void_p # backwards compatibility (to a bug)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000239_check_size(c_void_p)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000240
Guido van Rossumd8faa362007-04-27 19:54:29 +0000241class c_bool(_SimpleCData):
242 _type_ = "t"
243
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000244# This cache maps types to pointers to them.
245_pointer_type_cache = {}
246
247def POINTER(cls):
248 try:
249 return _pointer_type_cache[cls]
250 except KeyError:
251 pass
252 if type(cls) is str:
253 klass = type(_Pointer)("LP_%s" % cls,
254 (_Pointer,),
255 {})
256 _pointer_type_cache[id(klass)] = klass
257 return klass
258 else:
259 name = "LP_%s" % cls.__name__
260 klass = type(_Pointer)(name,
261 (_Pointer,),
262 {'_type_': cls})
263 _pointer_type_cache[cls] = klass
264 return klass
265
266try:
267 from _ctypes import set_conversion_mode
268except ImportError:
269 pass
270else:
271 if _os.name in ("nt", "ce"):
272 set_conversion_mode("mbcs", "ignore")
273 else:
274 set_conversion_mode("ascii", "strict")
275
276 class c_wchar_p(_SimpleCData):
277 _type_ = "Z"
278
279 class c_wchar(_SimpleCData):
280 _type_ = "u"
281
282 POINTER(c_wchar).from_param = c_wchar_p.from_param #_SimpleCData.c_wchar_p_from_param
283
284 def create_unicode_buffer(init, size=None):
285 """create_unicode_buffer(aString) -> character array
286 create_unicode_buffer(anInteger) -> character array
287 create_unicode_buffer(aString, anInteger) -> character array
288 """
Thomas Heller60831312007-07-12 19:06:25 +0000289 if isinstance(init, (str, bytes)):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000290 if size is None:
291 size = len(init)+1
292 buftype = c_wchar * size
293 buf = buftype()
294 buf.value = init
295 return buf
Walter Dörwaldaa97f042007-05-03 21:05:51 +0000296 elif isinstance(init, int):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000297 buftype = c_wchar * init
298 buf = buftype()
299 return buf
300 raise TypeError, init
301
302POINTER(c_char).from_param = c_char_p.from_param #_SimpleCData.c_char_p_from_param
303
304# XXX Deprecated
305def SetPointerType(pointer, cls):
306 if _pointer_type_cache.get(cls, None) is not None:
307 raise RuntimeError, \
308 "This type already exists in the cache"
Guido van Rossum1b01e5c2006-08-19 02:45:06 +0000309 if id(pointer) not in _pointer_type_cache:
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000310 raise RuntimeError, \
311 "What's this???"
312 pointer.set_type(cls)
313 _pointer_type_cache[cls] = pointer
314 del _pointer_type_cache[id(pointer)]
315
316
317def pointer(inst):
318 return POINTER(type(inst))(inst)
319
320# XXX Deprecated
321def ARRAY(typ, len):
322 return typ * len
323
324################################################################
325
326
327class CDLL(object):
328 """An instance of this class represents a loaded dll/shared
329 library, exporting functions using the standard C calling
330 convention (named 'cdecl' on Windows).
331
332 The exported functions can be accessed as attributes, or by
333 indexing with the function name. Examples:
334
335 <obj>.qsort -> callable object
336 <obj>['qsort'] -> callable object
337
338 Calling the functions releases the Python GIL during the call and
Thomas Woutersed03b412007-08-28 21:37:11 +0000339 reacquires it afterwards.
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000340 """
341 class _FuncPtr(_CFuncPtr):
342 _flags_ = _FUNCFLAG_CDECL
343 _restype_ = c_int # default, can be overridden in instances
344
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000345 def __init__(self, name, mode=DEFAULT_MODE, handle=None):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000346 self._name = name
347 if handle is None:
348 self._handle = _dlopen(self._name, mode)
349 else:
350 self._handle = handle
351
352 def __repr__(self):
353 return "<%s '%s', handle %x at %x>" % \
354 (self.__class__.__name__, self._name,
355 (self._handle & (_sys.maxint*2 + 1)),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000356 id(self) & (_sys.maxint*2 + 1))
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000357
358 def __getattr__(self, name):
359 if name.startswith('__') and name.endswith('__'):
360 raise AttributeError, name
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000361 func = self.__getitem__(name)
362 setattr(self, name, func)
363 return func
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000364
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000365 def __getitem__(self, name_or_ordinal):
366 func = self._FuncPtr((name_or_ordinal, self))
Walter Dörwaldaa97f042007-05-03 21:05:51 +0000367 if not isinstance(name_or_ordinal, int):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000368 func.__name__ = name_or_ordinal
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000369 return func
370
371class PyDLL(CDLL):
372 """This class represents the Python library itself. It allows to
373 access Python API functions. The GIL is not released, and
374 Python exceptions are handled correctly.
375 """
376 class _FuncPtr(_CFuncPtr):
377 _flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI
378 _restype_ = c_int # default, can be overridden in instances
379
380if _os.name in ("nt", "ce"):
Tim Peterse8d09e52006-03-09 01:15:05 +0000381
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000382 class WinDLL(CDLL):
383 """This class represents a dll exporting functions using the
384 Windows stdcall calling convention.
385 """
386 class _FuncPtr(_CFuncPtr):
387 _flags_ = _FUNCFLAG_STDCALL
388 _restype_ = c_int # default, can be overridden in instances
389
390 # XXX Hm, what about HRESULT as normal parameter?
391 # Mustn't it derive from c_long then?
392 from _ctypes import _check_HRESULT, _SimpleCData
393 class HRESULT(_SimpleCData):
394 _type_ = "l"
395 # _check_retval_ is called with the function's result when it
396 # is used as restype. It checks for the FAILED bit, and
397 # raises a WindowsError if it is set.
398 #
399 # The _check_retval_ method is implemented in C, so that the
400 # method definition itself is not included in the traceback
401 # when it raises an error - that is what we want (and Python
402 # doesn't have a way to raise an exception in the caller's
403 # frame).
404 _check_retval_ = _check_HRESULT
Tim Peterse8d09e52006-03-09 01:15:05 +0000405
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000406 class OleDLL(CDLL):
407 """This class represents a dll exporting functions using the
408 Windows stdcall calling convention, and returning HRESULT.
409 HRESULT error values are automatically raised as WindowsError
410 exceptions.
411 """
412 class _FuncPtr(_CFuncPtr):
413 _flags_ = _FUNCFLAG_STDCALL
414 _restype_ = HRESULT
415
Thomas Wouters477c8d52006-05-27 19:21:47 +0000416class LibraryLoader(object):
417 def __init__(self, dlltype):
418 self._dlltype = dlltype
419
420 def __getattr__(self, name):
421 if name[0] == '_':
422 raise AttributeError(name)
423 dll = self._dlltype(name)
424 setattr(self, name, dll)
425 return dll
426
427 def __getitem__(self, name):
428 return getattr(self, name)
429
430 def LoadLibrary(self, name):
431 return self._dlltype(name)
432
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000433cdll = LibraryLoader(CDLL)
434pydll = LibraryLoader(PyDLL)
435
436if _os.name in ("nt", "ce"):
437 pythonapi = PyDLL("python dll", None, _sys.dllhandle)
438elif _sys.platform == "cygwin":
439 pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
440else:
441 pythonapi = PyDLL(None)
442
443
444if _os.name in ("nt", "ce"):
445 windll = LibraryLoader(WinDLL)
446 oledll = LibraryLoader(OleDLL)
447
448 if _os.name == "nt":
449 GetLastError = windll.kernel32.GetLastError
450 else:
451 GetLastError = windll.coredll.GetLastError
452
453 def WinError(code=None, descr=None):
454 if code is None:
455 code = GetLastError()
456 if descr is None:
457 descr = FormatError(code).strip()
458 return WindowsError(code, descr)
459
460_pointer_type_cache[None] = c_void_p
461
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000462if sizeof(c_uint) == sizeof(c_void_p):
463 c_size_t = c_uint
464elif sizeof(c_ulong) == sizeof(c_void_p):
465 c_size_t = c_ulong
Thomas Wouters89f507f2006-12-13 04:49:30 +0000466elif sizeof(c_ulonglong) == sizeof(c_void_p):
467 c_size_t = c_ulonglong
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000468
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000469# functions
470
471from _ctypes import _memmove_addr, _memset_addr, _string_at_addr, _cast_addr
472
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000473## void *memmove(void *, const void *, size_t);
474memmove = CFUNCTYPE(c_void_p, c_void_p, c_void_p, c_size_t)(_memmove_addr)
475
476## void *memset(void *, int, size_t)
477memset = CFUNCTYPE(c_void_p, c_void_p, c_int, c_size_t)(_memset_addr)
478
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000479def PYFUNCTYPE(restype, *argtypes):
480 class CFunctionType(_CFuncPtr):
481 _argtypes_ = argtypes
482 _restype_ = restype
483 _flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI
484 return CFunctionType
Thomas Wouters477c8d52006-05-27 19:21:47 +0000485
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000486_cast = PYFUNCTYPE(py_object, c_void_p, py_object, py_object)(_cast_addr)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000487def cast(obj, typ):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000488 return _cast(obj, obj, typ)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000489
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000490_string_at = CFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000491def string_at(ptr, size=-1):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000492 """string_at(addr[, size]) -> string
493
494 Return the string at addr."""
495 return _string_at(ptr, size)
496
497try:
498 from _ctypes import _wstring_at_addr
499except ImportError:
500 pass
501else:
502 _wstring_at = CFUNCTYPE(py_object, c_void_p, c_int)(_wstring_at_addr)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000503 def wstring_at(ptr, size=-1):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000504 """wstring_at(addr[, size]) -> string
505
506 Return the string at addr."""
507 return _wstring_at(ptr, size)
Tim Peterse8d09e52006-03-09 01:15:05 +0000508
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000509
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000510if _os.name in ("nt", "ce"): # COM stuff
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000511 def DllGetClassObject(rclsid, riid, ppv):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000512 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000513 ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*'])
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000514 except ImportError:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000515 return -2147221231 # CLASS_E_CLASSNOTAVAILABLE
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000516 else:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000517 return ccom.DllGetClassObject(rclsid, riid, ppv)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000518
519 def DllCanUnloadNow():
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000520 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000521 ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*'])
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000522 except ImportError:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000523 return 0 # S_OK
524 return ccom.DllCanUnloadNow()
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000525
526from ctypes._endian import BigEndianStructure, LittleEndianStructure
527
528# Fill in specifically-sized types
529c_int8 = c_byte
530c_uint8 = c_ubyte
531for kind in [c_short, c_int, c_long, c_longlong]:
532 if sizeof(kind) == 2: c_int16 = kind
533 elif sizeof(kind) == 4: c_int32 = kind
534 elif sizeof(kind) == 8: c_int64 = kind
535for kind in [c_ushort, c_uint, c_ulong, c_ulonglong]:
536 if sizeof(kind) == 2: c_uint16 = kind
537 elif sizeof(kind) == 4: c_uint32 = kind
538 elif sizeof(kind) == 8: c_uint64 = kind
539del(kind)