Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 1 | """create and manipulate C data types in Python""" |
| 2 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 3 | import os as _os, sys as _sys |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 4 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 5 | __version__ = "1.1.0" |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 6 | |
| 7 | from _ctypes import Union, Structure, Array |
| 8 | from _ctypes import _Pointer |
| 9 | from _ctypes import CFuncPtr as _CFuncPtr |
| 10 | from _ctypes import __version__ as _ctypes_version |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11 | from _ctypes import RTLD_LOCAL, RTLD_GLOBAL |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 12 | from _ctypes import ArgumentError |
| 13 | |
| 14 | from struct import calcsize as _calcsize |
| 15 | |
| 16 | if __version__ != _ctypes_version: |
| 17 | raise Exception, ("Version number mismatch", __version__, _ctypes_version) |
| 18 | |
| 19 | if _os.name in ("nt", "ce"): |
| 20 | from _ctypes import FormatError |
| 21 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 22 | DEFAULT_MODE = RTLD_LOCAL |
| 23 | if _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 Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 39 | from _ctypes import FUNCFLAG_CDECL as _FUNCFLAG_CDECL, \ |
| 40 | FUNCFLAG_PYTHONAPI as _FUNCFLAG_PYTHONAPI |
| 41 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 42 | """ |
| 43 | WINOLEAPI -> HRESULT |
| 44 | WINOLEAPI_(type) |
| 45 | |
| 46 | STDMETHODCALLTYPE |
| 47 | |
| 48 | STDMETHOD(name) |
| 49 | STDMETHOD_(type, name) |
| 50 | |
| 51 | STDAPICALLTYPE |
| 52 | """ |
| 53 | |
| 54 | def 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örwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 59 | if isinstance(init, str): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 60 | 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örwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 66 | elif isinstance(init, int): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 67 | buftype = c_char * init |
| 68 | buf = buftype() |
| 69 | return buf |
| 70 | raise TypeError, init |
| 71 | |
| 72 | def 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 = {} |
| 80 | def CFUNCTYPE(restype, *argtypes): |
| 81 | """CFUNCTYPE(restype, *argtypes) -> function prototype. |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 82 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 83 | restype: the result type |
| 84 | argtypes: a sequence specifying the argument types |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 85 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 86 | The function prototype can be called in different ways to create a |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 87 | callable object: |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 88 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 89 | 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 Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 94 | """ |
| 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 | |
| 105 | if _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 | |
| 127 | elif _os.name == "posix": |
| 128 | from _ctypes import dlopen as _dlopen |
| 129 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 130 | from _ctypes import sizeof, byref, addressof, alignment, resize |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 131 | from _ctypes import _SimpleCData |
| 132 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 133 | def _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 Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 145 | class py_object(_SimpleCData): |
| 146 | _type_ = "O" |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 147 | def __repr__(self): |
| 148 | try: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 149 | return super().__repr__() |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 150 | except ValueError: |
| 151 | return "%s(<NULL>)" % type(self).__name__ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 152 | _check_size(py_object, "P") |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 153 | |
| 154 | class c_short(_SimpleCData): |
| 155 | _type_ = "h" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 156 | _check_size(c_short) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 157 | |
| 158 | class c_ushort(_SimpleCData): |
| 159 | _type_ = "H" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 160 | _check_size(c_ushort) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 161 | |
| 162 | class c_long(_SimpleCData): |
| 163 | _type_ = "l" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 164 | _check_size(c_long) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 165 | |
| 166 | class c_ulong(_SimpleCData): |
| 167 | _type_ = "L" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 168 | _check_size(c_ulong) |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 169 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 170 | if _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 |
| 174 | else: |
| 175 | class c_int(_SimpleCData): |
| 176 | _type_ = "i" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 177 | _check_size(c_int) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 178 | |
| 179 | class c_uint(_SimpleCData): |
| 180 | _type_ = "I" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 181 | _check_size(c_uint) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 182 | |
| 183 | class c_float(_SimpleCData): |
| 184 | _type_ = "f" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 185 | _check_size(c_float) |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 186 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 187 | class c_double(_SimpleCData): |
| 188 | _type_ = "d" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 189 | _check_size(c_double) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 190 | |
| 191 | if _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 |
| 195 | else: |
| 196 | class c_longlong(_SimpleCData): |
| 197 | _type_ = "q" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 198 | _check_size(c_longlong) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 199 | |
| 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 Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 205 | _check_size(c_ulonglong) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 206 | |
| 207 | class c_ubyte(_SimpleCData): |
| 208 | _type_ = "B" |
| 209 | c_ubyte.__ctype_le__ = c_ubyte.__ctype_be__ = c_ubyte |
| 210 | # backward compatibility: |
| 211 | ##c_uchar = c_ubyte |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 212 | _check_size(c_ubyte) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 213 | |
| 214 | class c_byte(_SimpleCData): |
| 215 | _type_ = "b" |
| 216 | c_byte.__ctype_le__ = c_byte.__ctype_be__ = c_byte |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 217 | _check_size(c_byte) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 218 | |
| 219 | class c_char(_SimpleCData): |
| 220 | _type_ = "c" |
| 221 | c_char.__ctype_le__ = c_char.__ctype_be__ = c_char |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 222 | _check_size(c_char) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 223 | |
| 224 | class c_char_p(_SimpleCData): |
| 225 | _type_ = "z" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 226 | 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 Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 234 | _check_size(c_char_p, "P") |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 235 | |
| 236 | class c_void_p(_SimpleCData): |
| 237 | _type_ = "P" |
| 238 | c_voidp = c_void_p # backwards compatibility (to a bug) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 239 | _check_size(c_void_p) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 241 | class c_bool(_SimpleCData): |
| 242 | _type_ = "t" |
| 243 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 244 | # This cache maps types to pointers to them. |
| 245 | _pointer_type_cache = {} |
| 246 | |
| 247 | def 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 | |
| 266 | try: |
| 267 | from _ctypes import set_conversion_mode |
| 268 | except ImportError: |
| 269 | pass |
| 270 | else: |
| 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 Heller | 6083131 | 2007-07-12 19:06:25 +0000 | [diff] [blame] | 289 | if isinstance(init, (str, bytes)): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 290 | 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örwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 296 | elif isinstance(init, int): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 297 | buftype = c_wchar * init |
| 298 | buf = buftype() |
| 299 | return buf |
| 300 | raise TypeError, init |
| 301 | |
| 302 | POINTER(c_char).from_param = c_char_p.from_param #_SimpleCData.c_char_p_from_param |
| 303 | |
| 304 | # XXX Deprecated |
| 305 | def 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 Rossum | 1b01e5c | 2006-08-19 02:45:06 +0000 | [diff] [blame] | 309 | if id(pointer) not in _pointer_type_cache: |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 310 | 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 | |
| 317 | def pointer(inst): |
| 318 | return POINTER(type(inst))(inst) |
| 319 | |
| 320 | # XXX Deprecated |
| 321 | def ARRAY(typ, len): |
| 322 | return typ * len |
| 323 | |
| 324 | ################################################################ |
| 325 | |
| 326 | |
| 327 | class 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 Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame^] | 339 | reacquires it afterwards. |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 340 | """ |
| 341 | class _FuncPtr(_CFuncPtr): |
| 342 | _flags_ = _FUNCFLAG_CDECL |
| 343 | _restype_ = c_int # default, can be overridden in instances |
| 344 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 345 | def __init__(self, name, mode=DEFAULT_MODE, handle=None): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 346 | 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 356 | id(self) & (_sys.maxint*2 + 1)) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 357 | |
| 358 | def __getattr__(self, name): |
| 359 | if name.startswith('__') and name.endswith('__'): |
| 360 | raise AttributeError, name |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 361 | func = self.__getitem__(name) |
| 362 | setattr(self, name, func) |
| 363 | return func |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 364 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 365 | def __getitem__(self, name_or_ordinal): |
| 366 | func = self._FuncPtr((name_or_ordinal, self)) |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 367 | if not isinstance(name_or_ordinal, int): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 368 | func.__name__ = name_or_ordinal |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 369 | return func |
| 370 | |
| 371 | class 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 | |
| 380 | if _os.name in ("nt", "ce"): |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 381 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 382 | 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 Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 405 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 406 | 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 Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 416 | class 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 Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 433 | cdll = LibraryLoader(CDLL) |
| 434 | pydll = LibraryLoader(PyDLL) |
| 435 | |
| 436 | if _os.name in ("nt", "ce"): |
| 437 | pythonapi = PyDLL("python dll", None, _sys.dllhandle) |
| 438 | elif _sys.platform == "cygwin": |
| 439 | pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2]) |
| 440 | else: |
| 441 | pythonapi = PyDLL(None) |
| 442 | |
| 443 | |
| 444 | if _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 Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 462 | if sizeof(c_uint) == sizeof(c_void_p): |
| 463 | c_size_t = c_uint |
| 464 | elif sizeof(c_ulong) == sizeof(c_void_p): |
| 465 | c_size_t = c_ulong |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 466 | elif sizeof(c_ulonglong) == sizeof(c_void_p): |
| 467 | c_size_t = c_ulonglong |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 468 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 469 | # functions |
| 470 | |
| 471 | from _ctypes import _memmove_addr, _memset_addr, _string_at_addr, _cast_addr |
| 472 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 473 | ## void *memmove(void *, const void *, size_t); |
| 474 | memmove = CFUNCTYPE(c_void_p, c_void_p, c_void_p, c_size_t)(_memmove_addr) |
| 475 | |
| 476 | ## void *memset(void *, int, size_t) |
| 477 | memset = CFUNCTYPE(c_void_p, c_void_p, c_int, c_size_t)(_memset_addr) |
| 478 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 479 | def PYFUNCTYPE(restype, *argtypes): |
| 480 | class CFunctionType(_CFuncPtr): |
| 481 | _argtypes_ = argtypes |
| 482 | _restype_ = restype |
| 483 | _flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI |
| 484 | return CFunctionType |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 485 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 486 | _cast = PYFUNCTYPE(py_object, c_void_p, py_object, py_object)(_cast_addr) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 487 | def cast(obj, typ): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 488 | return _cast(obj, obj, typ) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 489 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 490 | _string_at = CFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 491 | def string_at(ptr, size=-1): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 492 | """string_at(addr[, size]) -> string |
| 493 | |
| 494 | Return the string at addr.""" |
| 495 | return _string_at(ptr, size) |
| 496 | |
| 497 | try: |
| 498 | from _ctypes import _wstring_at_addr |
| 499 | except ImportError: |
| 500 | pass |
| 501 | else: |
| 502 | _wstring_at = CFUNCTYPE(py_object, c_void_p, c_int)(_wstring_at_addr) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 503 | def wstring_at(ptr, size=-1): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 504 | """wstring_at(addr[, size]) -> string |
| 505 | |
| 506 | Return the string at addr.""" |
| 507 | return _wstring_at(ptr, size) |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 508 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 509 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 510 | if _os.name in ("nt", "ce"): # COM stuff |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 511 | def DllGetClassObject(rclsid, riid, ppv): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 512 | try: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 513 | ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*']) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 514 | except ImportError: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 515 | return -2147221231 # CLASS_E_CLASSNOTAVAILABLE |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 516 | else: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 517 | return ccom.DllGetClassObject(rclsid, riid, ppv) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 518 | |
| 519 | def DllCanUnloadNow(): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 520 | try: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 521 | ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*']) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 522 | except ImportError: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 523 | return 0 # S_OK |
| 524 | return ccom.DllCanUnloadNow() |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 525 | |
| 526 | from ctypes._endian import BigEndianStructure, LittleEndianStructure |
| 527 | |
| 528 | # Fill in specifically-sized types |
| 529 | c_int8 = c_byte |
| 530 | c_uint8 = c_ubyte |
| 531 | for 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 |
| 535 | for 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 |
| 539 | del(kind) |