Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1 | ###################################################################### |
| 2 | # This file should be kept compatible with Python 2.3, see PEP 291. # |
| 3 | ###################################################################### |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 4 | """create and manipulate C data types in Python""" |
| 5 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 6 | import os as _os, sys as _sys |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 7 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 8 | __version__ = "1.1.0" |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 9 | |
| 10 | from _ctypes import Union, Structure, Array |
| 11 | from _ctypes import _Pointer |
| 12 | from _ctypes import CFuncPtr as _CFuncPtr |
| 13 | from _ctypes import __version__ as _ctypes_version |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14 | from _ctypes import RTLD_LOCAL, RTLD_GLOBAL |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 15 | from _ctypes import ArgumentError |
| 16 | |
| 17 | from struct import calcsize as _calcsize |
| 18 | |
| 19 | if __version__ != _ctypes_version: |
| 20 | raise Exception, ("Version number mismatch", __version__, _ctypes_version) |
| 21 | |
| 22 | if _os.name in ("nt", "ce"): |
| 23 | from _ctypes import FormatError |
| 24 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 25 | DEFAULT_MODE = RTLD_LOCAL |
| 26 | if _os.name == "posix" and _sys.platform == "darwin": |
| 27 | import gestalt |
| 28 | |
| 29 | # gestalt.gestalt("sysv") returns the version number of the |
| 30 | # currently active system file as BCD. |
| 31 | # On OS X 10.4.6 -> 0x1046 |
| 32 | # On OS X 10.2.8 -> 0x1028 |
| 33 | # See also http://www.rgaros.nl/gestalt/ |
| 34 | # |
| 35 | # On OS X 10.3, we use RTLD_GLOBAL as default mode |
| 36 | # because RTLD_LOCAL does not work at least on some |
| 37 | # libraries. |
| 38 | |
| 39 | if gestalt.gestalt("sysv") < 0x1040: |
| 40 | DEFAULT_MODE = RTLD_GLOBAL |
| 41 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 42 | from _ctypes import FUNCFLAG_CDECL as _FUNCFLAG_CDECL, \ |
| 43 | FUNCFLAG_PYTHONAPI as _FUNCFLAG_PYTHONAPI |
| 44 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 45 | """ |
| 46 | WINOLEAPI -> HRESULT |
| 47 | WINOLEAPI_(type) |
| 48 | |
| 49 | STDMETHODCALLTYPE |
| 50 | |
| 51 | STDMETHOD(name) |
| 52 | STDMETHOD_(type, name) |
| 53 | |
| 54 | STDAPICALLTYPE |
| 55 | """ |
| 56 | |
| 57 | def create_string_buffer(init, size=None): |
| 58 | """create_string_buffer(aString) -> character array |
| 59 | create_string_buffer(anInteger) -> character array |
| 60 | create_string_buffer(aString, anInteger) -> character array |
| 61 | """ |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 62 | if isinstance(init, str): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 63 | if size is None: |
| 64 | size = len(init)+1 |
| 65 | buftype = c_char * size |
| 66 | buf = buftype() |
| 67 | buf.value = init |
| 68 | return buf |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 69 | elif isinstance(init, int): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 70 | buftype = c_char * init |
| 71 | buf = buftype() |
| 72 | return buf |
| 73 | raise TypeError, init |
| 74 | |
| 75 | def c_buffer(init, size=None): |
| 76 | ## "deprecated, use create_string_buffer instead" |
| 77 | ## import warnings |
| 78 | ## warnings.warn("c_buffer is deprecated, use create_string_buffer instead", |
| 79 | ## DeprecationWarning, stacklevel=2) |
| 80 | return create_string_buffer(init, size) |
| 81 | |
| 82 | _c_functype_cache = {} |
| 83 | def CFUNCTYPE(restype, *argtypes): |
| 84 | """CFUNCTYPE(restype, *argtypes) -> function prototype. |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 85 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 86 | restype: the result type |
| 87 | argtypes: a sequence specifying the argument types |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 88 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 89 | The function prototype can be called in different ways to create a |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 90 | callable object: |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 91 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 92 | prototype(integer address) -> foreign function |
| 93 | prototype(callable) -> create and return a C callable function from callable |
| 94 | prototype(integer index, method name[, paramflags]) -> foreign function calling a COM method |
| 95 | prototype((ordinal number, dll object)[, paramflags]) -> foreign function exported by ordinal |
| 96 | prototype((function name, dll object)[, paramflags]) -> foreign function exported by name |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 97 | """ |
| 98 | try: |
| 99 | return _c_functype_cache[(restype, argtypes)] |
| 100 | except KeyError: |
| 101 | class CFunctionType(_CFuncPtr): |
| 102 | _argtypes_ = argtypes |
| 103 | _restype_ = restype |
| 104 | _flags_ = _FUNCFLAG_CDECL |
| 105 | _c_functype_cache[(restype, argtypes)] = CFunctionType |
| 106 | return CFunctionType |
| 107 | |
| 108 | if _os.name in ("nt", "ce"): |
| 109 | from _ctypes import LoadLibrary as _dlopen |
| 110 | from _ctypes import FUNCFLAG_STDCALL as _FUNCFLAG_STDCALL |
| 111 | if _os.name == "ce": |
| 112 | # 'ce' doesn't have the stdcall calling convention |
| 113 | _FUNCFLAG_STDCALL = _FUNCFLAG_CDECL |
| 114 | |
| 115 | _win_functype_cache = {} |
| 116 | def WINFUNCTYPE(restype, *argtypes): |
| 117 | # docstring set later (very similar to CFUNCTYPE.__doc__) |
| 118 | try: |
| 119 | return _win_functype_cache[(restype, argtypes)] |
| 120 | except KeyError: |
| 121 | class WinFunctionType(_CFuncPtr): |
| 122 | _argtypes_ = argtypes |
| 123 | _restype_ = restype |
| 124 | _flags_ = _FUNCFLAG_STDCALL |
| 125 | _win_functype_cache[(restype, argtypes)] = WinFunctionType |
| 126 | return WinFunctionType |
| 127 | if WINFUNCTYPE.__doc__: |
| 128 | WINFUNCTYPE.__doc__ = CFUNCTYPE.__doc__.replace("CFUNCTYPE", "WINFUNCTYPE") |
| 129 | |
| 130 | elif _os.name == "posix": |
| 131 | from _ctypes import dlopen as _dlopen |
| 132 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 133 | from _ctypes import sizeof, byref, addressof, alignment, resize |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 134 | from _ctypes import _SimpleCData |
| 135 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 136 | def _check_size(typ, typecode=None): |
| 137 | # Check if sizeof(ctypes_type) against struct.calcsize. This |
| 138 | # should protect somewhat against a misconfigured libffi. |
| 139 | from struct import calcsize |
| 140 | if typecode is None: |
| 141 | # Most _type_ codes are the same as used in struct |
| 142 | typecode = typ._type_ |
| 143 | actual, required = sizeof(typ), calcsize(typecode) |
| 144 | if actual != required: |
| 145 | raise SystemError("sizeof(%s) wrong: %d instead of %d" % \ |
| 146 | (typ, actual, required)) |
| 147 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 148 | class py_object(_SimpleCData): |
| 149 | _type_ = "O" |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 150 | def __repr__(self): |
| 151 | try: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame^] | 152 | return super().__repr__() |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 153 | except ValueError: |
| 154 | return "%s(<NULL>)" % type(self).__name__ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 155 | _check_size(py_object, "P") |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 156 | |
| 157 | class c_short(_SimpleCData): |
| 158 | _type_ = "h" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 159 | _check_size(c_short) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 160 | |
| 161 | class c_ushort(_SimpleCData): |
| 162 | _type_ = "H" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 163 | _check_size(c_ushort) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 164 | |
| 165 | class c_long(_SimpleCData): |
| 166 | _type_ = "l" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 167 | _check_size(c_long) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 168 | |
| 169 | class c_ulong(_SimpleCData): |
| 170 | _type_ = "L" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 171 | _check_size(c_ulong) |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 172 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 173 | if _calcsize("i") == _calcsize("l"): |
| 174 | # if int and long have the same size, make c_int an alias for c_long |
| 175 | c_int = c_long |
| 176 | c_uint = c_ulong |
| 177 | else: |
| 178 | class c_int(_SimpleCData): |
| 179 | _type_ = "i" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 180 | _check_size(c_int) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 181 | |
| 182 | class c_uint(_SimpleCData): |
| 183 | _type_ = "I" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 184 | _check_size(c_uint) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 185 | |
| 186 | class c_float(_SimpleCData): |
| 187 | _type_ = "f" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 188 | _check_size(c_float) |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 189 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 190 | class c_double(_SimpleCData): |
| 191 | _type_ = "d" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 192 | _check_size(c_double) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 193 | |
| 194 | if _calcsize("l") == _calcsize("q"): |
| 195 | # if long and long long have the same size, make c_longlong an alias for c_long |
| 196 | c_longlong = c_long |
| 197 | c_ulonglong = c_ulong |
| 198 | else: |
| 199 | class c_longlong(_SimpleCData): |
| 200 | _type_ = "q" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 201 | _check_size(c_longlong) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 202 | |
| 203 | class c_ulonglong(_SimpleCData): |
| 204 | _type_ = "Q" |
| 205 | ## def from_param(cls, val): |
| 206 | ## return ('d', float(val), val) |
| 207 | ## from_param = classmethod(from_param) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 208 | _check_size(c_ulonglong) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 209 | |
| 210 | class c_ubyte(_SimpleCData): |
| 211 | _type_ = "B" |
| 212 | c_ubyte.__ctype_le__ = c_ubyte.__ctype_be__ = c_ubyte |
| 213 | # backward compatibility: |
| 214 | ##c_uchar = c_ubyte |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 215 | _check_size(c_ubyte) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 216 | |
| 217 | class c_byte(_SimpleCData): |
| 218 | _type_ = "b" |
| 219 | c_byte.__ctype_le__ = c_byte.__ctype_be__ = c_byte |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 220 | _check_size(c_byte) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 221 | |
| 222 | class c_char(_SimpleCData): |
| 223 | _type_ = "c" |
| 224 | c_char.__ctype_le__ = c_char.__ctype_be__ = c_char |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 225 | _check_size(c_char) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 226 | |
| 227 | class c_char_p(_SimpleCData): |
| 228 | _type_ = "z" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 229 | _check_size(c_char_p, "P") |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 230 | |
| 231 | class c_void_p(_SimpleCData): |
| 232 | _type_ = "P" |
| 233 | c_voidp = c_void_p # backwards compatibility (to a bug) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 234 | _check_size(c_void_p) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 236 | class c_bool(_SimpleCData): |
| 237 | _type_ = "t" |
| 238 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 239 | # This cache maps types to pointers to them. |
| 240 | _pointer_type_cache = {} |
| 241 | |
| 242 | def POINTER(cls): |
| 243 | try: |
| 244 | return _pointer_type_cache[cls] |
| 245 | except KeyError: |
| 246 | pass |
| 247 | if type(cls) is str: |
| 248 | klass = type(_Pointer)("LP_%s" % cls, |
| 249 | (_Pointer,), |
| 250 | {}) |
| 251 | _pointer_type_cache[id(klass)] = klass |
| 252 | return klass |
| 253 | else: |
| 254 | name = "LP_%s" % cls.__name__ |
| 255 | klass = type(_Pointer)(name, |
| 256 | (_Pointer,), |
| 257 | {'_type_': cls}) |
| 258 | _pointer_type_cache[cls] = klass |
| 259 | return klass |
| 260 | |
| 261 | try: |
| 262 | from _ctypes import set_conversion_mode |
| 263 | except ImportError: |
| 264 | pass |
| 265 | else: |
| 266 | if _os.name in ("nt", "ce"): |
| 267 | set_conversion_mode("mbcs", "ignore") |
| 268 | else: |
| 269 | set_conversion_mode("ascii", "strict") |
| 270 | |
| 271 | class c_wchar_p(_SimpleCData): |
| 272 | _type_ = "Z" |
| 273 | |
| 274 | class c_wchar(_SimpleCData): |
| 275 | _type_ = "u" |
| 276 | |
| 277 | POINTER(c_wchar).from_param = c_wchar_p.from_param #_SimpleCData.c_wchar_p_from_param |
| 278 | |
| 279 | def create_unicode_buffer(init, size=None): |
| 280 | """create_unicode_buffer(aString) -> character array |
| 281 | create_unicode_buffer(anInteger) -> character array |
| 282 | create_unicode_buffer(aString, anInteger) -> character array |
| 283 | """ |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 284 | if isinstance(init, str): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 285 | if size is None: |
| 286 | size = len(init)+1 |
| 287 | buftype = c_wchar * size |
| 288 | buf = buftype() |
| 289 | buf.value = init |
| 290 | return buf |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 291 | elif isinstance(init, int): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 292 | buftype = c_wchar * init |
| 293 | buf = buftype() |
| 294 | return buf |
| 295 | raise TypeError, init |
| 296 | |
| 297 | POINTER(c_char).from_param = c_char_p.from_param #_SimpleCData.c_char_p_from_param |
| 298 | |
| 299 | # XXX Deprecated |
| 300 | def SetPointerType(pointer, cls): |
| 301 | if _pointer_type_cache.get(cls, None) is not None: |
| 302 | raise RuntimeError, \ |
| 303 | "This type already exists in the cache" |
Guido van Rossum | 1b01e5c | 2006-08-19 02:45:06 +0000 | [diff] [blame] | 304 | if id(pointer) not in _pointer_type_cache: |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 305 | raise RuntimeError, \ |
| 306 | "What's this???" |
| 307 | pointer.set_type(cls) |
| 308 | _pointer_type_cache[cls] = pointer |
| 309 | del _pointer_type_cache[id(pointer)] |
| 310 | |
| 311 | |
| 312 | def pointer(inst): |
| 313 | return POINTER(type(inst))(inst) |
| 314 | |
| 315 | # XXX Deprecated |
| 316 | def ARRAY(typ, len): |
| 317 | return typ * len |
| 318 | |
| 319 | ################################################################ |
| 320 | |
| 321 | |
| 322 | class CDLL(object): |
| 323 | """An instance of this class represents a loaded dll/shared |
| 324 | library, exporting functions using the standard C calling |
| 325 | convention (named 'cdecl' on Windows). |
| 326 | |
| 327 | The exported functions can be accessed as attributes, or by |
| 328 | indexing with the function name. Examples: |
| 329 | |
| 330 | <obj>.qsort -> callable object |
| 331 | <obj>['qsort'] -> callable object |
| 332 | |
| 333 | Calling the functions releases the Python GIL during the call and |
| 334 | reaquires it afterwards. |
| 335 | """ |
| 336 | class _FuncPtr(_CFuncPtr): |
| 337 | _flags_ = _FUNCFLAG_CDECL |
| 338 | _restype_ = c_int # default, can be overridden in instances |
| 339 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 340 | def __init__(self, name, mode=DEFAULT_MODE, handle=None): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 341 | self._name = name |
| 342 | if handle is None: |
| 343 | self._handle = _dlopen(self._name, mode) |
| 344 | else: |
| 345 | self._handle = handle |
| 346 | |
| 347 | def __repr__(self): |
| 348 | return "<%s '%s', handle %x at %x>" % \ |
| 349 | (self.__class__.__name__, self._name, |
| 350 | (self._handle & (_sys.maxint*2 + 1)), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 351 | id(self) & (_sys.maxint*2 + 1)) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 352 | |
| 353 | def __getattr__(self, name): |
| 354 | if name.startswith('__') and name.endswith('__'): |
| 355 | raise AttributeError, name |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 356 | func = self.__getitem__(name) |
| 357 | setattr(self, name, func) |
| 358 | return func |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 359 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 360 | def __getitem__(self, name_or_ordinal): |
| 361 | func = self._FuncPtr((name_or_ordinal, self)) |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 362 | if not isinstance(name_or_ordinal, int): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 363 | func.__name__ = name_or_ordinal |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 364 | return func |
| 365 | |
| 366 | class PyDLL(CDLL): |
| 367 | """This class represents the Python library itself. It allows to |
| 368 | access Python API functions. The GIL is not released, and |
| 369 | Python exceptions are handled correctly. |
| 370 | """ |
| 371 | class _FuncPtr(_CFuncPtr): |
| 372 | _flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI |
| 373 | _restype_ = c_int # default, can be overridden in instances |
| 374 | |
| 375 | if _os.name in ("nt", "ce"): |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 376 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 377 | class WinDLL(CDLL): |
| 378 | """This class represents a dll exporting functions using the |
| 379 | Windows stdcall calling convention. |
| 380 | """ |
| 381 | class _FuncPtr(_CFuncPtr): |
| 382 | _flags_ = _FUNCFLAG_STDCALL |
| 383 | _restype_ = c_int # default, can be overridden in instances |
| 384 | |
| 385 | # XXX Hm, what about HRESULT as normal parameter? |
| 386 | # Mustn't it derive from c_long then? |
| 387 | from _ctypes import _check_HRESULT, _SimpleCData |
| 388 | class HRESULT(_SimpleCData): |
| 389 | _type_ = "l" |
| 390 | # _check_retval_ is called with the function's result when it |
| 391 | # is used as restype. It checks for the FAILED bit, and |
| 392 | # raises a WindowsError if it is set. |
| 393 | # |
| 394 | # The _check_retval_ method is implemented in C, so that the |
| 395 | # method definition itself is not included in the traceback |
| 396 | # when it raises an error - that is what we want (and Python |
| 397 | # doesn't have a way to raise an exception in the caller's |
| 398 | # frame). |
| 399 | _check_retval_ = _check_HRESULT |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 400 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 401 | class OleDLL(CDLL): |
| 402 | """This class represents a dll exporting functions using the |
| 403 | Windows stdcall calling convention, and returning HRESULT. |
| 404 | HRESULT error values are automatically raised as WindowsError |
| 405 | exceptions. |
| 406 | """ |
| 407 | class _FuncPtr(_CFuncPtr): |
| 408 | _flags_ = _FUNCFLAG_STDCALL |
| 409 | _restype_ = HRESULT |
| 410 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 411 | class LibraryLoader(object): |
| 412 | def __init__(self, dlltype): |
| 413 | self._dlltype = dlltype |
| 414 | |
| 415 | def __getattr__(self, name): |
| 416 | if name[0] == '_': |
| 417 | raise AttributeError(name) |
| 418 | dll = self._dlltype(name) |
| 419 | setattr(self, name, dll) |
| 420 | return dll |
| 421 | |
| 422 | def __getitem__(self, name): |
| 423 | return getattr(self, name) |
| 424 | |
| 425 | def LoadLibrary(self, name): |
| 426 | return self._dlltype(name) |
| 427 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 428 | cdll = LibraryLoader(CDLL) |
| 429 | pydll = LibraryLoader(PyDLL) |
| 430 | |
| 431 | if _os.name in ("nt", "ce"): |
| 432 | pythonapi = PyDLL("python dll", None, _sys.dllhandle) |
| 433 | elif _sys.platform == "cygwin": |
| 434 | pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2]) |
| 435 | else: |
| 436 | pythonapi = PyDLL(None) |
| 437 | |
| 438 | |
| 439 | if _os.name in ("nt", "ce"): |
| 440 | windll = LibraryLoader(WinDLL) |
| 441 | oledll = LibraryLoader(OleDLL) |
| 442 | |
| 443 | if _os.name == "nt": |
| 444 | GetLastError = windll.kernel32.GetLastError |
| 445 | else: |
| 446 | GetLastError = windll.coredll.GetLastError |
| 447 | |
| 448 | def WinError(code=None, descr=None): |
| 449 | if code is None: |
| 450 | code = GetLastError() |
| 451 | if descr is None: |
| 452 | descr = FormatError(code).strip() |
| 453 | return WindowsError(code, descr) |
| 454 | |
| 455 | _pointer_type_cache[None] = c_void_p |
| 456 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 457 | if sizeof(c_uint) == sizeof(c_void_p): |
| 458 | c_size_t = c_uint |
| 459 | elif sizeof(c_ulong) == sizeof(c_void_p): |
| 460 | c_size_t = c_ulong |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 461 | elif sizeof(c_ulonglong) == sizeof(c_void_p): |
| 462 | c_size_t = c_ulonglong |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 463 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 464 | # functions |
| 465 | |
| 466 | from _ctypes import _memmove_addr, _memset_addr, _string_at_addr, _cast_addr |
| 467 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 468 | ## void *memmove(void *, const void *, size_t); |
| 469 | memmove = CFUNCTYPE(c_void_p, c_void_p, c_void_p, c_size_t)(_memmove_addr) |
| 470 | |
| 471 | ## void *memset(void *, int, size_t) |
| 472 | memset = CFUNCTYPE(c_void_p, c_void_p, c_int, c_size_t)(_memset_addr) |
| 473 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 474 | def PYFUNCTYPE(restype, *argtypes): |
| 475 | class CFunctionType(_CFuncPtr): |
| 476 | _argtypes_ = argtypes |
| 477 | _restype_ = restype |
| 478 | _flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI |
| 479 | return CFunctionType |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 480 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 481 | _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] | 482 | def cast(obj, typ): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 483 | return _cast(obj, obj, typ) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 484 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 485 | _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] | 486 | def string_at(ptr, size=-1): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 487 | """string_at(addr[, size]) -> string |
| 488 | |
| 489 | Return the string at addr.""" |
| 490 | return _string_at(ptr, size) |
| 491 | |
| 492 | try: |
| 493 | from _ctypes import _wstring_at_addr |
| 494 | except ImportError: |
| 495 | pass |
| 496 | else: |
| 497 | _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] | 498 | def wstring_at(ptr, size=-1): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 499 | """wstring_at(addr[, size]) -> string |
| 500 | |
| 501 | Return the string at addr.""" |
| 502 | return _wstring_at(ptr, size) |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 503 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 504 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 505 | if _os.name in ("nt", "ce"): # COM stuff |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 506 | def DllGetClassObject(rclsid, riid, ppv): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 507 | try: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 508 | ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*']) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 509 | except ImportError: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 510 | return -2147221231 # CLASS_E_CLASSNOTAVAILABLE |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 511 | else: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 512 | return ccom.DllGetClassObject(rclsid, riid, ppv) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 513 | |
| 514 | def DllCanUnloadNow(): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 515 | try: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 516 | ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*']) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 517 | except ImportError: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 518 | return 0 # S_OK |
| 519 | return ccom.DllCanUnloadNow() |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 520 | |
| 521 | from ctypes._endian import BigEndianStructure, LittleEndianStructure |
| 522 | |
| 523 | # Fill in specifically-sized types |
| 524 | c_int8 = c_byte |
| 525 | c_uint8 = c_ubyte |
| 526 | for kind in [c_short, c_int, c_long, c_longlong]: |
| 527 | if sizeof(kind) == 2: c_int16 = kind |
| 528 | elif sizeof(kind) == 4: c_int32 = kind |
| 529 | elif sizeof(kind) == 8: c_int64 = kind |
| 530 | for kind in [c_ushort, c_uint, c_ulong, c_ulonglong]: |
| 531 | if sizeof(kind) == 2: c_uint16 = kind |
| 532 | elif sizeof(kind) == 4: c_uint32 = kind |
| 533 | elif sizeof(kind) == 8: c_uint64 = kind |
| 534 | del(kind) |