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