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 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 29 | if int(_os.uname().release.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 | |
Victor Stinner | 765531d | 2013-03-26 01:11:54 +0100 | [diff] [blame] | 37 | # WINOLEAPI -> HRESULT |
| 38 | # WINOLEAPI_(type) |
| 39 | # |
| 40 | # STDMETHODCALLTYPE |
| 41 | # |
| 42 | # STDMETHOD(name) |
| 43 | # STDMETHOD_(type, name) |
| 44 | # |
| 45 | # STDAPICALLTYPE |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 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 |
Ezio Melotti | ee2a392 | 2016-01-09 16:08:24 +0200 | [diff] [blame^] | 50 | create_string_buffer(aBytes, anInteger) -> character array |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 51 | """ |
Serhiy Storchaka | 4f06d60 | 2014-08-09 09:33:05 +0300 | [diff] [blame] | 52 | if isinstance(init, 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 = {} |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 73 | def CFUNCTYPE(restype, *argtypes, **kw): |
| 74 | """CFUNCTYPE(restype, *argtypes, |
| 75 | use_errno=False, use_last_error=False) -> function prototype. |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 76 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 77 | restype: the result type |
| 78 | argtypes: a sequence specifying the argument types |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 79 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 80 | The function prototype can be called in different ways to create a |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 81 | callable object: |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 82 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 83 | prototype(integer address) -> foreign function |
| 84 | prototype(callable) -> create and return a C callable function from callable |
| 85 | prototype(integer index, method name[, paramflags]) -> foreign function calling a COM method |
| 86 | prototype((ordinal number, dll object)[, paramflags]) -> foreign function exported by ordinal |
| 87 | prototype((function name, dll object)[, paramflags]) -> foreign function exported by name |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 88 | """ |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 89 | flags = _FUNCFLAG_CDECL |
| 90 | if kw.pop("use_errno", False): |
| 91 | flags |= _FUNCFLAG_USE_ERRNO |
| 92 | if kw.pop("use_last_error", False): |
| 93 | flags |= _FUNCFLAG_USE_LASTERROR |
| 94 | if kw: |
| 95 | raise ValueError("unexpected keyword argument(s) %s" % kw.keys()) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 96 | try: |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 97 | return _c_functype_cache[(restype, argtypes, flags)] |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 98 | except KeyError: |
| 99 | class CFunctionType(_CFuncPtr): |
| 100 | _argtypes_ = argtypes |
| 101 | _restype_ = restype |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 102 | _flags_ = flags |
| 103 | _c_functype_cache[(restype, argtypes, flags)] = CFunctionType |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 104 | return CFunctionType |
| 105 | |
| 106 | if _os.name in ("nt", "ce"): |
| 107 | from _ctypes import LoadLibrary as _dlopen |
| 108 | from _ctypes import FUNCFLAG_STDCALL as _FUNCFLAG_STDCALL |
| 109 | if _os.name == "ce": |
| 110 | # 'ce' doesn't have the stdcall calling convention |
| 111 | _FUNCFLAG_STDCALL = _FUNCFLAG_CDECL |
| 112 | |
| 113 | _win_functype_cache = {} |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 114 | def WINFUNCTYPE(restype, *argtypes, **kw): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 115 | # docstring set later (very similar to CFUNCTYPE.__doc__) |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 116 | flags = _FUNCFLAG_STDCALL |
| 117 | if kw.pop("use_errno", False): |
| 118 | flags |= _FUNCFLAG_USE_ERRNO |
| 119 | if kw.pop("use_last_error", False): |
| 120 | flags |= _FUNCFLAG_USE_LASTERROR |
| 121 | if kw: |
| 122 | raise ValueError("unexpected keyword argument(s) %s" % kw.keys()) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 123 | try: |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 124 | return _win_functype_cache[(restype, argtypes, flags)] |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 125 | except KeyError: |
| 126 | class WinFunctionType(_CFuncPtr): |
| 127 | _argtypes_ = argtypes |
| 128 | _restype_ = restype |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 129 | _flags_ = flags |
| 130 | _win_functype_cache[(restype, argtypes, flags)] = WinFunctionType |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 131 | return WinFunctionType |
| 132 | if WINFUNCTYPE.__doc__: |
| 133 | WINFUNCTYPE.__doc__ = CFUNCTYPE.__doc__.replace("CFUNCTYPE", "WINFUNCTYPE") |
| 134 | |
| 135 | elif _os.name == "posix": |
| 136 | from _ctypes import dlopen as _dlopen |
| 137 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 138 | from _ctypes import sizeof, byref, addressof, alignment, resize |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 139 | from _ctypes import get_errno, set_errno |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 140 | from _ctypes import _SimpleCData |
| 141 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 142 | def _check_size(typ, typecode=None): |
| 143 | # Check if sizeof(ctypes_type) against struct.calcsize. This |
| 144 | # should protect somewhat against a misconfigured libffi. |
| 145 | from struct import calcsize |
| 146 | if typecode is None: |
| 147 | # Most _type_ codes are the same as used in struct |
| 148 | typecode = typ._type_ |
| 149 | actual, required = sizeof(typ), calcsize(typecode) |
| 150 | if actual != required: |
| 151 | raise SystemError("sizeof(%s) wrong: %d instead of %d" % \ |
| 152 | (typ, actual, required)) |
| 153 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 154 | class py_object(_SimpleCData): |
| 155 | _type_ = "O" |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 156 | def __repr__(self): |
| 157 | try: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 158 | return super().__repr__() |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 159 | except ValueError: |
| 160 | return "%s(<NULL>)" % type(self).__name__ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 161 | _check_size(py_object, "P") |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 162 | |
| 163 | class c_short(_SimpleCData): |
| 164 | _type_ = "h" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 165 | _check_size(c_short) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 166 | |
| 167 | class c_ushort(_SimpleCData): |
| 168 | _type_ = "H" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 169 | _check_size(c_ushort) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 170 | |
| 171 | class c_long(_SimpleCData): |
| 172 | _type_ = "l" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 173 | _check_size(c_long) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 174 | |
| 175 | class c_ulong(_SimpleCData): |
| 176 | _type_ = "L" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 177 | _check_size(c_ulong) |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 178 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 179 | if _calcsize("i") == _calcsize("l"): |
| 180 | # if int and long have the same size, make c_int an alias for c_long |
| 181 | c_int = c_long |
| 182 | c_uint = c_ulong |
| 183 | else: |
| 184 | class c_int(_SimpleCData): |
| 185 | _type_ = "i" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 186 | _check_size(c_int) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 187 | |
| 188 | class c_uint(_SimpleCData): |
| 189 | _type_ = "I" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 190 | _check_size(c_uint) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 191 | |
| 192 | class c_float(_SimpleCData): |
| 193 | _type_ = "f" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 194 | _check_size(c_float) |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 195 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 196 | class c_double(_SimpleCData): |
| 197 | _type_ = "d" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 198 | _check_size(c_double) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 199 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 200 | class c_longdouble(_SimpleCData): |
Thomas Heller | ff72122 | 2008-01-17 18:46:55 +0000 | [diff] [blame] | 201 | _type_ = "g" |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 202 | if sizeof(c_longdouble) == sizeof(c_double): |
| 203 | c_longdouble = c_double |
| 204 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 205 | if _calcsize("l") == _calcsize("q"): |
| 206 | # if long and long long have the same size, make c_longlong an alias for c_long |
| 207 | c_longlong = c_long |
| 208 | c_ulonglong = c_ulong |
| 209 | else: |
| 210 | class c_longlong(_SimpleCData): |
| 211 | _type_ = "q" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 212 | _check_size(c_longlong) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 213 | |
| 214 | class c_ulonglong(_SimpleCData): |
| 215 | _type_ = "Q" |
| 216 | ## def from_param(cls, val): |
| 217 | ## return ('d', float(val), val) |
| 218 | ## from_param = classmethod(from_param) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 219 | _check_size(c_ulonglong) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 220 | |
| 221 | class c_ubyte(_SimpleCData): |
| 222 | _type_ = "B" |
| 223 | c_ubyte.__ctype_le__ = c_ubyte.__ctype_be__ = c_ubyte |
| 224 | # backward compatibility: |
| 225 | ##c_uchar = c_ubyte |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 226 | _check_size(c_ubyte) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 227 | |
| 228 | class c_byte(_SimpleCData): |
| 229 | _type_ = "b" |
| 230 | c_byte.__ctype_le__ = c_byte.__ctype_be__ = c_byte |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 231 | _check_size(c_byte) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 232 | |
| 233 | class c_char(_SimpleCData): |
| 234 | _type_ = "c" |
| 235 | c_char.__ctype_le__ = c_char.__ctype_be__ = c_char |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 236 | _check_size(c_char) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 237 | |
| 238 | class c_char_p(_SimpleCData): |
| 239 | _type_ = "z" |
Steve Dower | e6bb7eb | 2015-03-25 21:58:36 -0700 | [diff] [blame] | 240 | def __repr__(self): |
| 241 | return "%s(%s)" % (self.__class__.__name__, c_void_p.from_buffer(self).value) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 242 | _check_size(c_char_p, "P") |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 243 | |
| 244 | class c_void_p(_SimpleCData): |
| 245 | _type_ = "P" |
| 246 | c_voidp = c_void_p # backwards compatibility (to a bug) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 247 | _check_size(c_void_p) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 248 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 249 | class c_bool(_SimpleCData): |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 250 | _type_ = "?" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 251 | |
Thomas Heller | 3071f81 | 2008-04-14 16:17:33 +0000 | [diff] [blame] | 252 | from _ctypes import POINTER, pointer, _pointer_type_cache |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 253 | |
Victor Stinner | cf44883 | 2010-07-28 00:15:03 +0000 | [diff] [blame] | 254 | class c_wchar_p(_SimpleCData): |
| 255 | _type_ = "Z" |
Steve Dower | e6bb7eb | 2015-03-25 21:58:36 -0700 | [diff] [blame] | 256 | def __repr__(self): |
| 257 | return "%s(%s)" % (self.__class__.__name__, c_void_p.from_buffer(self).value) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 258 | |
Victor Stinner | cf44883 | 2010-07-28 00:15:03 +0000 | [diff] [blame] | 259 | class c_wchar(_SimpleCData): |
| 260 | _type_ = "u" |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 261 | |
Meador Inge | 11e3813 | 2011-11-25 22:33:32 -0600 | [diff] [blame] | 262 | def _reset_cache(): |
| 263 | _pointer_type_cache.clear() |
| 264 | _c_functype_cache.clear() |
| 265 | if _os.name in ("nt", "ce"): |
| 266 | _win_functype_cache.clear() |
| 267 | # _SimpleCData.c_wchar_p_from_param |
| 268 | POINTER(c_wchar).from_param = c_wchar_p.from_param |
| 269 | # _SimpleCData.c_char_p_from_param |
| 270 | POINTER(c_char).from_param = c_char_p.from_param |
| 271 | _pointer_type_cache[None] = c_void_p |
| 272 | # XXX for whatever reasons, creating the first instance of a callback |
| 273 | # function is needed for the unittests on Win64 to succeed. This MAY |
| 274 | # be a compiler bug, since the problem occurs only when _ctypes is |
| 275 | # compiled with the MS SDK compiler. Or an uninitialized variable? |
| 276 | CFUNCTYPE(c_int)(lambda: None) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 277 | |
Victor Stinner | cf44883 | 2010-07-28 00:15:03 +0000 | [diff] [blame] | 278 | def create_unicode_buffer(init, size=None): |
| 279 | """create_unicode_buffer(aString) -> character array |
| 280 | create_unicode_buffer(anInteger) -> character array |
| 281 | create_unicode_buffer(aString, anInteger) -> character array |
| 282 | """ |
Serhiy Storchaka | 4f06d60 | 2014-08-09 09:33:05 +0300 | [diff] [blame] | 283 | if isinstance(init, str): |
Victor Stinner | cf44883 | 2010-07-28 00:15:03 +0000 | [diff] [blame] | 284 | if size is None: |
| 285 | size = len(init)+1 |
| 286 | buftype = c_wchar * size |
| 287 | buf = buftype() |
| 288 | buf.value = init |
| 289 | return buf |
| 290 | elif isinstance(init, int): |
| 291 | buftype = c_wchar * init |
| 292 | buf = buftype() |
| 293 | return buf |
| 294 | raise TypeError(init) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 295 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 296 | |
| 297 | # XXX Deprecated |
| 298 | def SetPointerType(pointer, cls): |
| 299 | if _pointer_type_cache.get(cls, None) is not None: |
Collin Winter | a73bfee | 2007-08-30 03:47:13 +0000 | [diff] [blame] | 300 | raise RuntimeError("This type already exists in the cache") |
Guido van Rossum | 1b01e5c | 2006-08-19 02:45:06 +0000 | [diff] [blame] | 301 | if id(pointer) not in _pointer_type_cache: |
Collin Winter | a73bfee | 2007-08-30 03:47:13 +0000 | [diff] [blame] | 302 | raise RuntimeError("What's this???") |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 303 | pointer.set_type(cls) |
| 304 | _pointer_type_cache[cls] = pointer |
| 305 | del _pointer_type_cache[id(pointer)] |
| 306 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 307 | # XXX Deprecated |
| 308 | def ARRAY(typ, len): |
| 309 | return typ * len |
| 310 | |
| 311 | ################################################################ |
| 312 | |
| 313 | |
| 314 | class CDLL(object): |
| 315 | """An instance of this class represents a loaded dll/shared |
| 316 | library, exporting functions using the standard C calling |
| 317 | convention (named 'cdecl' on Windows). |
| 318 | |
| 319 | The exported functions can be accessed as attributes, or by |
| 320 | indexing with the function name. Examples: |
| 321 | |
| 322 | <obj>.qsort -> callable object |
| 323 | <obj>['qsort'] -> callable object |
| 324 | |
| 325 | Calling the functions releases the Python GIL during the call and |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 326 | reacquires it afterwards. |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 327 | """ |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 328 | _func_flags_ = _FUNCFLAG_CDECL |
| 329 | _func_restype_ = c_int |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 330 | |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 331 | def __init__(self, name, mode=DEFAULT_MODE, handle=None, |
| 332 | use_errno=False, |
| 333 | use_last_error=False): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 334 | self._name = name |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 335 | flags = self._func_flags_ |
| 336 | if use_errno: |
| 337 | flags |= _FUNCFLAG_USE_ERRNO |
| 338 | if use_last_error: |
| 339 | flags |= _FUNCFLAG_USE_LASTERROR |
| 340 | |
| 341 | class _FuncPtr(_CFuncPtr): |
| 342 | _flags_ = flags |
| 343 | _restype_ = self._func_restype_ |
| 344 | self._FuncPtr = _FuncPtr |
| 345 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 346 | if handle is None: |
| 347 | self._handle = _dlopen(self._name, mode) |
| 348 | else: |
| 349 | self._handle = handle |
| 350 | |
| 351 | def __repr__(self): |
Serhiy Storchaka | 0c937b3 | 2014-07-22 12:14:52 +0300 | [diff] [blame] | 352 | return "<%s '%s', handle %x at %#x>" % \ |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 353 | (self.__class__.__name__, self._name, |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 354 | (self._handle & (_sys.maxsize*2 + 1)), |
| 355 | id(self) & (_sys.maxsize*2 + 1)) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 356 | |
| 357 | def __getattr__(self, name): |
| 358 | if name.startswith('__') and name.endswith('__'): |
Collin Winter | a73bfee | 2007-08-30 03:47:13 +0000 | [diff] [blame] | 359 | raise AttributeError(name) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 360 | func = self.__getitem__(name) |
| 361 | setattr(self, name, func) |
| 362 | return func |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 363 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 364 | def __getitem__(self, name_or_ordinal): |
| 365 | func = self._FuncPtr((name_or_ordinal, self)) |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 366 | if not isinstance(name_or_ordinal, int): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 367 | func.__name__ = name_or_ordinal |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 368 | return func |
| 369 | |
| 370 | class PyDLL(CDLL): |
| 371 | """This class represents the Python library itself. It allows to |
| 372 | access Python API functions. The GIL is not released, and |
| 373 | Python exceptions are handled correctly. |
| 374 | """ |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 375 | _func_flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 376 | |
| 377 | if _os.name in ("nt", "ce"): |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 378 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 379 | class WinDLL(CDLL): |
| 380 | """This class represents a dll exporting functions using the |
| 381 | Windows stdcall calling convention. |
| 382 | """ |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 383 | _func_flags_ = _FUNCFLAG_STDCALL |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 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 |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 392 | # raises an OSError if it is set. |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 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. |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 404 | HRESULT error values are automatically raised as OSError |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 405 | exceptions. |
| 406 | """ |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 407 | _func_flags_ = _FUNCFLAG_STDCALL |
| 408 | _func_restype_ = HRESULT |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 409 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 410 | class LibraryLoader(object): |
| 411 | def __init__(self, dlltype): |
| 412 | self._dlltype = dlltype |
| 413 | |
| 414 | def __getattr__(self, name): |
| 415 | if name[0] == '_': |
| 416 | raise AttributeError(name) |
| 417 | dll = self._dlltype(name) |
| 418 | setattr(self, name, dll) |
| 419 | return dll |
| 420 | |
| 421 | def __getitem__(self, name): |
| 422 | return getattr(self, name) |
| 423 | |
| 424 | def LoadLibrary(self, name): |
| 425 | return self._dlltype(name) |
| 426 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 427 | cdll = LibraryLoader(CDLL) |
| 428 | pydll = LibraryLoader(PyDLL) |
| 429 | |
| 430 | if _os.name in ("nt", "ce"): |
| 431 | pythonapi = PyDLL("python dll", None, _sys.dllhandle) |
| 432 | elif _sys.platform == "cygwin": |
| 433 | pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2]) |
| 434 | else: |
| 435 | pythonapi = PyDLL(None) |
| 436 | |
| 437 | |
| 438 | if _os.name in ("nt", "ce"): |
| 439 | windll = LibraryLoader(WinDLL) |
| 440 | oledll = LibraryLoader(OleDLL) |
| 441 | |
| 442 | if _os.name == "nt": |
| 443 | GetLastError = windll.kernel32.GetLastError |
| 444 | else: |
| 445 | GetLastError = windll.coredll.GetLastError |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 446 | from _ctypes import get_last_error, set_last_error |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 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() |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 453 | return OSError(None, descr, None, code) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 454 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 455 | if sizeof(c_uint) == sizeof(c_void_p): |
| 456 | c_size_t = c_uint |
Gregory P. Smith | 1a53091 | 2010-03-01 04:59:27 +0000 | [diff] [blame] | 457 | c_ssize_t = c_int |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 458 | elif sizeof(c_ulong) == sizeof(c_void_p): |
| 459 | c_size_t = c_ulong |
Gregory P. Smith | 1a53091 | 2010-03-01 04:59:27 +0000 | [diff] [blame] | 460 | c_ssize_t = c_long |
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 |
Gregory P. Smith | 1a53091 | 2010-03-01 04:59:27 +0000 | [diff] [blame] | 463 | c_ssize_t = c_longlong |
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 | 47bc809 | 2008-08-19 06:38:12 +0000 | [diff] [blame] | 486 | _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] | 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: |
Thomas Heller | 47bc809 | 2008-08-19 06:38:12 +0000 | [diff] [blame] | 498 | _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] | 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 | |
Meador Inge | 11e3813 | 2011-11-25 22:33:32 -0600 | [diff] [blame] | 537 | _reset_cache() |