blob: cdf6d36e47a1c0032b3c0dca8eebc026b3270f69 [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":
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
Collin Wintera73bfee2007-08-30 03:47:13 +000070 raise TypeError(init)
Thomas Hellerbabddfc2006-03-08 19:56:54 +000071
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
Collin Wintera73bfee2007-08-30 03:47:13 +0000300 raise TypeError(init)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000301
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:
Collin Wintera73bfee2007-08-30 03:47:13 +0000307 raise RuntimeError("This type already exists in the cache")
Guido van Rossum1b01e5c2006-08-19 02:45:06 +0000308 if id(pointer) not in _pointer_type_cache:
Collin Wintera73bfee2007-08-30 03:47:13 +0000309 raise RuntimeError("What's this???")
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000310 pointer.set_type(cls)
311 _pointer_type_cache[cls] = pointer
312 del _pointer_type_cache[id(pointer)]
313
314
315def pointer(inst):
316 return POINTER(type(inst))(inst)
317
318# XXX Deprecated
319def ARRAY(typ, len):
320 return typ * len
321
322################################################################
323
324
325class CDLL(object):
326 """An instance of this class represents a loaded dll/shared
327 library, exporting functions using the standard C calling
328 convention (named 'cdecl' on Windows).
329
330 The exported functions can be accessed as attributes, or by
331 indexing with the function name. Examples:
332
333 <obj>.qsort -> callable object
334 <obj>['qsort'] -> callable object
335
336 Calling the functions releases the Python GIL during the call and
Thomas Woutersed03b412007-08-28 21:37:11 +0000337 reacquires it afterwards.
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000338 """
339 class _FuncPtr(_CFuncPtr):
340 _flags_ = _FUNCFLAG_CDECL
341 _restype_ = c_int # default, can be overridden in instances
342
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000343 def __init__(self, name, mode=DEFAULT_MODE, handle=None):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000344 self._name = name
345 if handle is None:
346 self._handle = _dlopen(self._name, mode)
347 else:
348 self._handle = handle
349
350 def __repr__(self):
351 return "<%s '%s', handle %x at %x>" % \
352 (self.__class__.__name__, self._name,
353 (self._handle & (_sys.maxint*2 + 1)),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000354 id(self) & (_sys.maxint*2 + 1))
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000355
356 def __getattr__(self, name):
357 if name.startswith('__') and name.endswith('__'):
Collin Wintera73bfee2007-08-30 03:47:13 +0000358 raise AttributeError(name)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000359 func = self.__getitem__(name)
360 setattr(self, name, func)
361 return func
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000362
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000363 def __getitem__(self, name_or_ordinal):
364 func = self._FuncPtr((name_or_ordinal, self))
Walter Dörwaldaa97f042007-05-03 21:05:51 +0000365 if not isinstance(name_or_ordinal, int):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000366 func.__name__ = name_or_ordinal
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000367 return func
368
369class PyDLL(CDLL):
370 """This class represents the Python library itself. It allows to
371 access Python API functions. The GIL is not released, and
372 Python exceptions are handled correctly.
373 """
374 class _FuncPtr(_CFuncPtr):
375 _flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI
376 _restype_ = c_int # default, can be overridden in instances
377
378if _os.name in ("nt", "ce"):
Tim Peterse8d09e52006-03-09 01:15:05 +0000379
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000380 class WinDLL(CDLL):
381 """This class represents a dll exporting functions using the
382 Windows stdcall calling convention.
383 """
384 class _FuncPtr(_CFuncPtr):
385 _flags_ = _FUNCFLAG_STDCALL
386 _restype_ = c_int # default, can be overridden in instances
387
388 # XXX Hm, what about HRESULT as normal parameter?
389 # Mustn't it derive from c_long then?
390 from _ctypes import _check_HRESULT, _SimpleCData
391 class HRESULT(_SimpleCData):
392 _type_ = "l"
393 # _check_retval_ is called with the function's result when it
394 # is used as restype. It checks for the FAILED bit, and
395 # raises a WindowsError if it is set.
396 #
397 # The _check_retval_ method is implemented in C, so that the
398 # method definition itself is not included in the traceback
399 # when it raises an error - that is what we want (and Python
400 # doesn't have a way to raise an exception in the caller's
401 # frame).
402 _check_retval_ = _check_HRESULT
Tim Peterse8d09e52006-03-09 01:15:05 +0000403
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000404 class OleDLL(CDLL):
405 """This class represents a dll exporting functions using the
406 Windows stdcall calling convention, and returning HRESULT.
407 HRESULT error values are automatically raised as WindowsError
408 exceptions.
409 """
410 class _FuncPtr(_CFuncPtr):
411 _flags_ = _FUNCFLAG_STDCALL
412 _restype_ = HRESULT
413
Thomas Wouters477c8d52006-05-27 19:21:47 +0000414class LibraryLoader(object):
415 def __init__(self, dlltype):
416 self._dlltype = dlltype
417
418 def __getattr__(self, name):
419 if name[0] == '_':
420 raise AttributeError(name)
421 dll = self._dlltype(name)
422 setattr(self, name, dll)
423 return dll
424
425 def __getitem__(self, name):
426 return getattr(self, name)
427
428 def LoadLibrary(self, name):
429 return self._dlltype(name)
430
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000431cdll = LibraryLoader(CDLL)
432pydll = LibraryLoader(PyDLL)
433
434if _os.name in ("nt", "ce"):
435 pythonapi = PyDLL("python dll", None, _sys.dllhandle)
436elif _sys.platform == "cygwin":
437 pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
438else:
439 pythonapi = PyDLL(None)
440
441
442if _os.name in ("nt", "ce"):
443 windll = LibraryLoader(WinDLL)
444 oledll = LibraryLoader(OleDLL)
445
446 if _os.name == "nt":
447 GetLastError = windll.kernel32.GetLastError
448 else:
449 GetLastError = windll.coredll.GetLastError
450
451 def WinError(code=None, descr=None):
452 if code is None:
453 code = GetLastError()
454 if descr is None:
455 descr = FormatError(code).strip()
456 return WindowsError(code, descr)
457
458_pointer_type_cache[None] = c_void_p
459
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000460if sizeof(c_uint) == sizeof(c_void_p):
461 c_size_t = c_uint
462elif sizeof(c_ulong) == sizeof(c_void_p):
463 c_size_t = c_ulong
Thomas Wouters89f507f2006-12-13 04:49:30 +0000464elif sizeof(c_ulonglong) == sizeof(c_void_p):
465 c_size_t = c_ulonglong
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000466
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000467# functions
468
469from _ctypes import _memmove_addr, _memset_addr, _string_at_addr, _cast_addr
470
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000471## void *memmove(void *, const void *, size_t);
472memmove = CFUNCTYPE(c_void_p, c_void_p, c_void_p, c_size_t)(_memmove_addr)
473
474## void *memset(void *, int, size_t)
475memset = CFUNCTYPE(c_void_p, c_void_p, c_int, c_size_t)(_memset_addr)
476
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000477def PYFUNCTYPE(restype, *argtypes):
478 class CFunctionType(_CFuncPtr):
479 _argtypes_ = argtypes
480 _restype_ = restype
481 _flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI
482 return CFunctionType
Thomas Wouters477c8d52006-05-27 19:21:47 +0000483
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000484_cast = PYFUNCTYPE(py_object, c_void_p, py_object, py_object)(_cast_addr)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000485def cast(obj, typ):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000486 return _cast(obj, obj, typ)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000487
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000488_string_at = CFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000489def string_at(ptr, size=-1):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000490 """string_at(addr[, size]) -> string
491
492 Return the string at addr."""
493 return _string_at(ptr, size)
494
495try:
496 from _ctypes import _wstring_at_addr
497except ImportError:
498 pass
499else:
500 _wstring_at = CFUNCTYPE(py_object, c_void_p, c_int)(_wstring_at_addr)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000501 def wstring_at(ptr, size=-1):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000502 """wstring_at(addr[, size]) -> string
503
504 Return the string at addr."""
505 return _wstring_at(ptr, size)
Tim Peterse8d09e52006-03-09 01:15:05 +0000506
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000507
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000508if _os.name in ("nt", "ce"): # COM stuff
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000509 def DllGetClassObject(rclsid, riid, ppv):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000510 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000511 ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*'])
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000512 except ImportError:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000513 return -2147221231 # CLASS_E_CLASSNOTAVAILABLE
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000514 else:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000515 return ccom.DllGetClassObject(rclsid, riid, ppv)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000516
517 def DllCanUnloadNow():
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000518 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000519 ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*'])
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000520 except ImportError:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000521 return 0 # S_OK
522 return ccom.DllCanUnloadNow()
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000523
524from ctypes._endian import BigEndianStructure, LittleEndianStructure
525
526# Fill in specifically-sized types
527c_int8 = c_byte
528c_uint8 = c_ubyte
529for kind in [c_short, c_int, c_long, c_longlong]:
530 if sizeof(kind) == 2: c_int16 = kind
531 elif sizeof(kind) == 4: c_int32 = kind
532 elif sizeof(kind) == 8: c_int64 = kind
533for kind in [c_ushort, c_uint, c_ulong, c_ulonglong]:
534 if sizeof(kind) == 2: c_uint16 = kind
535 elif sizeof(kind) == 4: c_uint32 = kind
536 elif sizeof(kind) == 8: c_uint64 = kind
537del(kind)