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