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: |
Collin Winter | a73bfee | 2007-08-30 03:47:13 +0000 | [diff] [blame] | 17 | raise Exception("Version number mismatch", __version__, _ctypes_version) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 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 |
Collin Winter | a73bfee | 2007-08-30 03:47:13 +0000 | [diff] [blame] | 70 | raise TypeError(init) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 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 |
Collin Winter | a73bfee | 2007-08-30 03:47:13 +0000 | [diff] [blame] | 300 | raise TypeError(init) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 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: |
Collin Winter | a73bfee | 2007-08-30 03:47:13 +0000 | [diff] [blame] | 307 | raise RuntimeError("This type already exists in the cache") |
Guido van Rossum | 1b01e5c | 2006-08-19 02:45:06 +0000 | [diff] [blame] | 308 | if id(pointer) not in _pointer_type_cache: |
Collin Winter | a73bfee | 2007-08-30 03:47:13 +0000 | [diff] [blame] | 309 | raise RuntimeError("What's this???") |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 310 | pointer.set_type(cls) |
| 311 | _pointer_type_cache[cls] = pointer |
| 312 | del _pointer_type_cache[id(pointer)] |
| 313 | |
| 314 | |
| 315 | def pointer(inst): |
| 316 | return POINTER(type(inst))(inst) |
| 317 | |
| 318 | # XXX Deprecated |
| 319 | def ARRAY(typ, len): |
| 320 | return typ * len |
| 321 | |
| 322 | ################################################################ |
| 323 | |
| 324 | |
| 325 | class 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 Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 337 | reacquires it afterwards. |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 338 | """ |
| 339 | class _FuncPtr(_CFuncPtr): |
| 340 | _flags_ = _FUNCFLAG_CDECL |
| 341 | _restype_ = c_int # default, can be overridden in instances |
| 342 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 343 | def __init__(self, name, mode=DEFAULT_MODE, handle=None): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 344 | 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 354 | id(self) & (_sys.maxint*2 + 1)) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 355 | |
| 356 | def __getattr__(self, name): |
| 357 | if name.startswith('__') and name.endswith('__'): |
Collin Winter | a73bfee | 2007-08-30 03:47:13 +0000 | [diff] [blame] | 358 | raise AttributeError(name) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 359 | func = self.__getitem__(name) |
| 360 | setattr(self, name, func) |
| 361 | return func |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 362 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 363 | def __getitem__(self, name_or_ordinal): |
| 364 | func = self._FuncPtr((name_or_ordinal, self)) |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 365 | if not isinstance(name_or_ordinal, int): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 366 | func.__name__ = name_or_ordinal |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 367 | return func |
| 368 | |
| 369 | class 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 | |
| 378 | if _os.name in ("nt", "ce"): |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 379 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 380 | 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 Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 403 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 404 | 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 Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 414 | class 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 Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 431 | cdll = LibraryLoader(CDLL) |
| 432 | pydll = LibraryLoader(PyDLL) |
| 433 | |
| 434 | if _os.name in ("nt", "ce"): |
| 435 | pythonapi = PyDLL("python dll", None, _sys.dllhandle) |
| 436 | elif _sys.platform == "cygwin": |
| 437 | pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2]) |
| 438 | else: |
| 439 | pythonapi = PyDLL(None) |
| 440 | |
| 441 | |
| 442 | if _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 Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 460 | if sizeof(c_uint) == sizeof(c_void_p): |
| 461 | c_size_t = c_uint |
| 462 | elif sizeof(c_ulong) == sizeof(c_void_p): |
| 463 | c_size_t = c_ulong |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 464 | elif sizeof(c_ulonglong) == sizeof(c_void_p): |
| 465 | c_size_t = c_ulonglong |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 466 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 467 | # functions |
| 468 | |
| 469 | from _ctypes import _memmove_addr, _memset_addr, _string_at_addr, _cast_addr |
| 470 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 471 | ## void *memmove(void *, const void *, size_t); |
| 472 | memmove = CFUNCTYPE(c_void_p, c_void_p, c_void_p, c_size_t)(_memmove_addr) |
| 473 | |
| 474 | ## void *memset(void *, int, size_t) |
| 475 | memset = CFUNCTYPE(c_void_p, c_void_p, c_int, c_size_t)(_memset_addr) |
| 476 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 477 | def PYFUNCTYPE(restype, *argtypes): |
| 478 | class CFunctionType(_CFuncPtr): |
| 479 | _argtypes_ = argtypes |
| 480 | _restype_ = restype |
| 481 | _flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI |
| 482 | return CFunctionType |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 483 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 484 | _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] | 485 | def cast(obj, typ): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 486 | return _cast(obj, obj, typ) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 487 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 488 | _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] | 489 | def string_at(ptr, size=-1): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 490 | """string_at(addr[, size]) -> string |
| 491 | |
| 492 | Return the string at addr.""" |
| 493 | return _string_at(ptr, size) |
| 494 | |
| 495 | try: |
| 496 | from _ctypes import _wstring_at_addr |
| 497 | except ImportError: |
| 498 | pass |
| 499 | else: |
| 500 | _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] | 501 | def wstring_at(ptr, size=-1): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 502 | """wstring_at(addr[, size]) -> string |
| 503 | |
| 504 | Return the string at addr.""" |
| 505 | return _wstring_at(ptr, size) |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 506 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 507 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 508 | if _os.name in ("nt", "ce"): # COM stuff |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 509 | def DllGetClassObject(rclsid, riid, ppv): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 510 | try: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 511 | ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*']) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 512 | except ImportError: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 513 | return -2147221231 # CLASS_E_CLASSNOTAVAILABLE |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 514 | else: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 515 | return ccom.DllGetClassObject(rclsid, riid, ppv) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 516 | |
| 517 | def DllCanUnloadNow(): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 518 | try: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 519 | ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*']) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 520 | except ImportError: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 521 | return 0 # S_OK |
| 522 | return ccom.DllCanUnloadNow() |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 523 | |
| 524 | from ctypes._endian import BigEndianStructure, LittleEndianStructure |
| 525 | |
| 526 | # Fill in specifically-sized types |
| 527 | c_int8 = c_byte |
| 528 | c_uint8 = c_ubyte |
| 529 | for 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 |
| 533 | for 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 |
| 537 | del(kind) |
Thomas Heller | 674e938 | 2007-08-31 13:06:44 +0000 | [diff] [blame^] | 538 | |
| 539 | # XXX for whatever reasons, creating the first instance of a callback |
| 540 | # function is needed for the unittests on Win64 to succeed. This MAY |
| 541 | # be a compiler bug, since the problem occurs only when _ctypes is |
| 542 | # compiled with the MS SDK compiler. Or an uninitialized variable? |
| 543 | CFUNCTYPE(c_int)(lambda: None) |