| 1 |
from _ctypes.basics import ( |
| 2 |
_CData, _CDataMeta, cdata_from_address, ArgumentError, keepalive_key, |
| 3 |
is_struct_shape, sizeof) |
| 4 |
from _ctypes.primitive import SimpleType, _SimpleCData |
| 5 |
from _ctypes.builtin import get_errno, set_errno, get_last_error, set_last_error |
| 6 |
import _rawffi |
| 7 |
from _rawffi import alt as _ffi |
| 8 |
from __pypy__ import newmemoryview |
| 9 |
import sys |
| 10 |
import traceback |
| 11 |
|
| 12 |
|
| 13 |
try: |
| 14 |
from __pypy__ import builtinify |
| 15 |
except ImportError: |
| 16 |
builtinify = lambda f: f |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
PARAMFLAG_FIN = 0x1 |
| 21 |
PARAMFLAG_FOUT = 0x2 |
| 22 |
PARAMFLAG_FLCID = 0x4 |
| 23 |
PARAMFLAG_COMBINED = PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID |
| 24 |
|
| 25 |
VALID_PARAMFLAGS = ( |
| 26 |
0, |
| 27 |
PARAMFLAG_FIN, |
| 28 |
PARAMFLAG_FIN | PARAMFLAG_FOUT, |
| 29 |
PARAMFLAG_FIN | PARAMFLAG_FLCID |
| 30 |
) |
| 31 |
|
| 32 |
WIN64 = sys.platform == 'win32' and sys.maxsize == 2**63 - 1 |
| 33 |
|
| 34 |
CTYPES_MAX_ARGCOUNT = 1024 |
| 35 |
|
| 36 |
def get_com_error(errcode, riid, pIunk): |
| 37 |
"Win32 specific: build a COM Error exception" |
| 38 |
|
| 39 |
from _ctypes import COMError |
| 40 |
return COMError(errcode, None, None) |
| 41 |
|
| 42 |
|
| 43 |
@builtinify |
| 44 |
def call_function(func, args): |
| 45 |
"Only for debugging so far: So that we can call CFunction instances" |
| 46 |
funcptr = CFuncPtr(func) |
| 47 |
funcptr.restype = int |
| 48 |
return funcptr(*args) |
| 49 |
|
| 50 |
|
| 51 |
class CFuncPtrType(_CDataMeta): |
| 52 |
|
| 53 |
|
| 54 |
def __init__(cls, name, bases, clsdict): |
| 55 |
super().__init__(name, bases, clsdict) |
| 56 |
if '_argtypes_' in clsdict: |
| 57 |
argtypes = clsdict['_argtypes_'] |
| 58 |
if argtypes is not None and len(argtypes) > CTYPES_MAX_ARGCOUNT: |
| 59 |
raise ArgumentError( |
| 60 |
"_argtypes_ has too many arguments (%d), maximum is %d" |
| 61 |
% (len(argtypes), CTYPES_MAX_ARGCOUNT)) |
| 62 |
|
| 63 |
def _sizeofinstances(self): |
| 64 |
return _rawffi.sizeof('P') |
| 65 |
|
| 66 |
def _alignmentofinstances(self): |
| 67 |
return _rawffi.alignment('P') |
| 68 |
|
| 69 |
def _is_pointer_like(self): |
| 70 |
return True |
| 71 |
|
| 72 |
from_address = cdata_from_address |
| 73 |
|
| 74 |
def _getformat(self): |
| 75 |
return 'X{}' |
| 76 |
|
| 77 |
|
| 78 |
class CFuncPtr(_CData, metaclass=CFuncPtrType): |
| 79 |
|
| 80 |
_argtypes_ = None |
| 81 |
_restype_ = None |
| 82 |
_errcheck_ = None |
| 83 |
_flags_ = 0 |
| 84 |
_ffiargshape_ = 'P' |
| 85 |
_ffishape_ = 'P' |
| 86 |
_fficompositesize_ = None |
| 87 |
_ffiarray = _rawffi.Array('P') |
| 88 |
_needs_free = False |
| 89 |
callable = None |
| 90 |
_ptr = None |
| 91 |
_buffer = None |
| 92 |
_address = None |
| 93 |
|
| 94 |
_paramflags = None |
| 95 |
_com_index = None |
| 96 |
_com_iid = None |
| 97 |
_is_fastpath = False |
| 98 |
|
| 99 |
def _getargtypes(self): |
| 100 |
return self._argtypes_ |
| 101 |
|
| 102 |
def _setargtypes(self, argtypes): |
| 103 |
self._ptr = None |
| 104 |
if argtypes is None: |
| 105 |
self._argtypes_ = () |
| 106 |
else: |
| 107 |
for i, argtype in enumerate(argtypes): |
| 108 |
if not hasattr(argtype, 'from_param'): |
| 109 |
raise TypeError( |
| 110 |
"item %d in _argtypes_ has no from_param method" % ( |
| 111 |
i + 1,)) |
| 112 |
self._argtypes_ = list(argtypes) |
| 113 |
|
| 114 |
argtypes = property(_getargtypes, _setargtypes) |
| 115 |
|
| 116 |
def _getparamflags(self): |
| 117 |
return self._paramflags |
| 118 |
|
| 119 |
def _setparamflags(self, paramflags): |
| 120 |
if paramflags is None or not self._argtypes_: |
| 121 |
self._paramflags = None |
| 122 |
return |
| 123 |
if not isinstance(paramflags, tuple): |
| 124 |
raise TypeError("paramflags must be a tuple or None") |
| 125 |
if len(paramflags) != len(self._argtypes_): |
| 126 |
raise ValueError("paramflags must have the same length as argtypes") |
| 127 |
for idx, paramflag in enumerate(paramflags): |
| 128 |
paramlen = len(paramflag) |
| 129 |
name = default = None |
| 130 |
if paramlen == 1: |
| 131 |
flag = paramflag[0] |
| 132 |
elif paramlen == 2: |
| 133 |
flag, name = paramflag |
| 134 |
elif paramlen == 3: |
| 135 |
flag, name, default = paramflag |
| 136 |
else: |
| 137 |
raise TypeError( |
| 138 |
"paramflags must be a sequence of (int [,string [,value]]) " |
| 139 |
"tuples" |
| 140 |
) |
| 141 |
if not isinstance(flag, int): |
| 142 |
raise TypeError( |
| 143 |
"paramflags must be a sequence of (int [,string [,value]]) " |
| 144 |
"tuples" |
| 145 |
) |
| 146 |
_flag = flag & PARAMFLAG_COMBINED |
| 147 |
if _flag == PARAMFLAG_FOUT: |
| 148 |
typ = self._argtypes_[idx] |
| 149 |
if getattr(typ, '_ffiargshape_', None) not in ('P', 'z', 'Z'): |
| 150 |
raise TypeError( |
| 151 |
"'out' parameter %d must be a pointer type, not %s" |
| 152 |
% (idx + 1, type(typ).__name__) |
| 153 |
) |
| 154 |
elif _flag not in VALID_PARAMFLAGS: |
| 155 |
raise TypeError("paramflag value %d not supported" % flag) |
| 156 |
self._paramflags = paramflags |
| 157 |
|
| 158 |
paramflags = property(_getparamflags, _setparamflags) |
| 159 |
|
| 160 |
def _getrestype(self): |
| 161 |
return self._restype_ |
| 162 |
|
| 163 |
def _setrestype(self, restype): |
| 164 |
self._ptr = None |
| 165 |
if restype is int: |
| 166 |
from ctypes import c_int |
| 167 |
restype = c_int |
| 168 |
if not (isinstance(restype, _CDataMeta) or restype is None or |
| 169 |
callable(restype)): |
| 170 |
raise TypeError("restype must be a type, a callable, or None") |
| 171 |
self._restype_ = restype |
| 172 |
|
| 173 |
def _delrestype(self): |
| 174 |
self._ptr = None |
| 175 |
del self._restype_ |
| 176 |
|
| 177 |
restype = property(_getrestype, _setrestype, _delrestype) |
| 178 |
|
| 179 |
def _geterrcheck(self): |
| 180 |
return getattr(self, '_errcheck_', None) |
| 181 |
|
| 182 |
def _seterrcheck(self, errcheck): |
| 183 |
if not callable(errcheck): |
| 184 |
raise TypeError("The errcheck attribute must be callable") |
| 185 |
self._errcheck_ = errcheck |
| 186 |
|
| 187 |
def _delerrcheck(self): |
| 188 |
try: |
| 189 |
del self._errcheck_ |
| 190 |
except AttributeError: |
| 191 |
pass |
| 192 |
|
| 193 |
errcheck = property(_geterrcheck, _seterrcheck, _delerrcheck) |
| 194 |
|
| 195 |
def _ffishapes(self, args, restype): |
| 196 |
if args is None: |
| 197 |
args = [] |
| 198 |
argtypes = [arg._ffiargshape_ for arg in args] |
| 199 |
if restype is not None: |
| 200 |
if not isinstance(restype, SimpleType): |
| 201 |
raise TypeError("invalid result type for callback function") |
| 202 |
restype = restype._ffiargshape_ |
| 203 |
else: |
| 204 |
restype = 'O' |
| 205 |
return argtypes, restype |
| 206 |
|
| 207 |
def _set_address(self, address): |
| 208 |
if not self._buffer: |
| 209 |
self._buffer = _rawffi.Array('P')(1) |
| 210 |
self._buffer[0] = address |
| 211 |
|
| 212 |
def _get_address(self): |
| 213 |
return self._buffer[0] |
| 214 |
|
| 215 |
def __init__(self, *args): |
| 216 |
self.name = None |
| 217 |
self._objects = {keepalive_key(0): self} |
| 218 |
self._needs_free = True |
| 219 |
|
| 220 |
|
| 221 |
if not args: |
| 222 |
self._set_address(0) |
| 223 |
return |
| 224 |
|
| 225 |
argsl = list(args) |
| 226 |
argument = argsl.pop(0) |
| 227 |
|
| 228 |
|
| 229 |
if isinstance(argument, int) and not argsl: |
| 230 |
self._set_address(argument) |
| 231 |
restype = self._restype_ |
| 232 |
if restype is None: |
| 233 |
import ctypes |
| 234 |
restype = ctypes.c_int |
| 235 |
if self._argtypes_ is None: |
| 236 |
self._argtypes_ = [] |
| 237 |
self._ptr = self._getfuncptr_fromaddress(self._argtypes_, restype) |
| 238 |
return |
| 239 |
|
| 240 |
|
| 241 |
if callable(argument) and not argsl: |
| 242 |
self.callable = argument |
| 243 |
ffiargs, ffires = self._ffishapes(self._argtypes_, self._restype_) |
| 244 |
if self._restype_ is None: |
| 245 |
ffires = None |
| 246 |
self._ptr = _rawffi.CallbackPtr(self._wrap_callable(argument, |
| 247 |
self.argtypes), |
| 248 |
ffiargs, ffires, self._flags_) |
| 249 |
self._buffer = self._ptr.byptr() |
| 250 |
return |
| 251 |
|
| 252 |
|
| 253 |
if isinstance(argument, tuple) and len(argument) == 2: |
| 254 |
import ctypes |
| 255 |
self.name, dll = argument |
| 256 |
if isinstance(dll, str): |
| 257 |
self.dll = ctypes.CDLL(self.dll) |
| 258 |
else: |
| 259 |
self.dll = dll |
| 260 |
if argsl: |
| 261 |
self.paramflags = argsl.pop(0) |
| 262 |
if argsl: |
| 263 |
raise TypeError("Unknown constructor %s" % (args,)) |
| 264 |
|
| 265 |
ptr = self._getfuncptr([], ctypes.c_int) |
| 266 |
self._set_address(ptr.getaddr()) |
| 267 |
return |
| 268 |
|
| 269 |
|
| 270 |
if (sys.platform == 'win32' and isinstance(argument, int) |
| 271 |
and argsl): |
| 272 |
ffiargs, ffires = self._ffishapes(self._argtypes_, self._restype_) |
| 273 |
self._com_index = argument + 0x1000 |
| 274 |
self.name = argsl.pop(0) |
| 275 |
if argsl: |
| 276 |
self.paramflags = argsl.pop(0) |
| 277 |
if argsl: |
| 278 |
self._com_iid = argsl.pop(0) |
| 279 |
if argsl: |
| 280 |
raise TypeError("Unknown constructor %s" % (args,)) |
| 281 |
return |
| 282 |
|
| 283 |
raise TypeError("Unknown constructor %s" % (args,)) |
| 284 |
_init_no_arg_ = __init__ |
| 285 |
|
| 286 |
def _wrap_callable(self, to_call, argtypes): |
| 287 |
def f(*args): |
| 288 |
if argtypes: |
| 289 |
args = [argtype._CData_retval(argtype.from_address(arg)._buffer) |
| 290 |
for argtype, arg in zip(argtypes, args)] |
| 291 |
try: |
| 292 |
return to_call(*args) |
| 293 |
except SystemExit as e: |
| 294 |
handle_system_exit(e) |
| 295 |
raise |
| 296 |
|
| 297 |
return f |
| 298 |
|
| 299 |
def __call__(self, *args, **kwargs): |
| 300 |
argtypes = self._argtypes_ |
| 301 |
if self.callable is not None: |
| 302 |
if len(args) > CTYPES_MAX_ARGCOUNT: |
| 303 |
raise ArgumentError("too many arguments (%s), maximum is %s" % (len(args), CTYPES_MAX_ARGCOUNT)) |
| 304 |
if len(args) == len(argtypes): |
| 305 |
pass |
| 306 |
elif self._flags_ & _rawffi.FUNCFLAG_CDECL: |
| 307 |
if len(args) < len(argtypes): |
| 308 |
plural = len(argtypes) > 1 and "s" or "" |
| 309 |
raise TypeError( |
| 310 |
"This function takes at least %d argument%s (%s given)" |
| 311 |
% (len(argtypes), plural, len(args))) |
| 312 |
else: |
| 313 |
|
| 314 |
|
| 315 |
args = args[:len(argtypes)] |
| 316 |
else: |
| 317 |
plural = len(self._argtypes_) > 1 and "s" or "" |
| 318 |
raise TypeError( |
| 319 |
"This function takes %d argument%s (%s given)" |
| 320 |
% (len(self._argtypes_), plural, len(args))) |
| 321 |
|
| 322 |
try: |
| 323 |
newargs = self._convert_args_for_callback(argtypes, args) |
| 324 |
except (UnicodeError, TypeError, ValueError) as e: |
| 325 |
raise ArgumentError(str(e)) |
| 326 |
try: |
| 327 |
try: |
| 328 |
res = self.callable(*newargs) |
| 329 |
except SystemExit as e: |
| 330 |
try: |
| 331 |
handle_system_exit(e) |
| 332 |
except Exception: |
| 333 |
traceback.print_exc(file=sys.stderr) |
| 334 |
return 0 |
| 335 |
raise |
| 336 |
except Exception as e: |
| 337 |
from __pypy__ import write_unraisable |
| 338 |
write_unraisable('in calling ctypes callback function', e, self.callable) |
| 339 |
return 0 |
| 340 |
if self._restype_ is not None: |
| 341 |
if self._restype_._ffishape_ == 'O': |
| 342 |
return res |
| 343 |
try: |
| 344 |
return self._restype_(res).value |
| 345 |
except Exception as e: |
| 346 |
from __pypy__ import write_unraisable |
| 347 |
write_unraisable( |
| 348 |
"on converting result of ctypes callback function", |
| 349 |
e, self.callable) |
| 350 |
return |
| 351 |
|
| 352 |
if argtypes is None: |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
argtypes = [] |
| 360 |
|
| 361 |
if self._com_index: |
| 362 |
from ctypes import cast, c_void_p, POINTER |
| 363 |
if not args: |
| 364 |
raise ValueError( |
| 365 |
"native COM method call without 'this' parameter" |
| 366 |
) |
| 367 |
thisvalue = args[0] |
| 368 |
thisarg = cast(thisvalue, POINTER(POINTER(c_void_p))) |
| 369 |
keepalives, newargs, argtypes, outargs, errcheckargs, variadic_args = ( |
| 370 |
self._convert_args(argtypes, args[1:], kwargs)) |
| 371 |
newargs.insert(0, thisarg) |
| 372 |
argtypes.insert(0, c_void_p) |
| 373 |
else: |
| 374 |
thisarg = None |
| 375 |
keepalives, newargs, argtypes, outargs, errcheckargs, variadic_args = ( |
| 376 |
self._convert_args(argtypes, args, kwargs)) |
| 377 |
|
| 378 |
funcptr = self._getfuncptr(argtypes, self._restype_, thisarg, variadic_args=variadic_args) |
| 379 |
result = self._call_funcptr(funcptr, *newargs) |
| 380 |
result, forced = self._do_errcheck(result, errcheckargs) |
| 381 |
|
| 382 |
if not outargs or forced: |
| 383 |
return result |
| 384 |
|
| 385 |
from ctypes import c_void_p |
| 386 |
simple_cdata = type(c_void_p()).__bases__[0] |
| 387 |
outargs = [x.value if type(x).__bases__[0] is simple_cdata else x |
| 388 |
for x in outargs] |
| 389 |
|
| 390 |
if len(outargs) == 1: |
| 391 |
return outargs[0] |
| 392 |
return tuple(outargs) |
| 393 |
|
| 394 |
def _call_funcptr(self, funcptr, *newargs): |
| 395 |
if self._flags_ & _rawffi.FUNCFLAG_USE_ERRNO: |
| 396 |
tmp = _rawffi.get_errno() |
| 397 |
_rawffi.set_errno(get_errno()) |
| 398 |
set_errno(tmp) |
| 399 |
if self._flags_ & _rawffi.FUNCFLAG_USE_LASTERROR: |
| 400 |
tmp = _rawffi.get_last_error() |
| 401 |
_rawffi.set_last_error(get_last_error()) |
| 402 |
set_last_error(tmp) |
| 403 |
try: |
| 404 |
result = funcptr(*newargs) |
| 405 |
finally: |
| 406 |
if self._flags_ & _rawffi.FUNCFLAG_USE_ERRNO: |
| 407 |
tmp = _rawffi.get_errno() |
| 408 |
_rawffi.set_errno(get_errno()) |
| 409 |
set_errno(tmp) |
| 410 |
if self._flags_ & _rawffi.FUNCFLAG_USE_LASTERROR: |
| 411 |
tmp = _rawffi.get_last_error() |
| 412 |
_rawffi.set_last_error(get_last_error()) |
| 413 |
set_last_error(tmp) |
| 414 |
|
| 415 |
try: |
| 416 |
return self._build_result(self._restype_, result) |
| 417 |
finally: |
| 418 |
funcptr.free_temp_buffers() |
| 419 |
|
| 420 |
def _do_errcheck(self, result, args): |
| 421 |
|
| 422 |
if self._errcheck_: |
| 423 |
args = tuple(args) |
| 424 |
v = self._errcheck_(result, self, args) |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
if v is not args: |
| 431 |
return v, True |
| 432 |
return result, False |
| 433 |
|
| 434 |
def _getfuncptr_fromaddress(self, argtypes, restype, variadic_args=0): |
| 435 |
address = self._get_address() |
| 436 |
ffiargs = [argtype.get_ffi_argtype() for argtype in argtypes] |
| 437 |
ffires = restype.get_ffi_argtype() |
| 438 |
return _ffi.FuncPtr.fromaddr(address, '', ffiargs, ffires, self._flags_, |
| 439 |
variadic_args=variadic_args) |
| 440 |
|
| 441 |
def _getfuncptr(self, argtypes, restype, thisarg=None, variadic_args=0): |
| 442 |
if self._ptr is not None and (argtypes is self._argtypes_ or argtypes == self._argtypes_): |
| 443 |
return self._ptr |
| 444 |
if restype is None or not isinstance(restype, _CDataMeta): |
| 445 |
import ctypes |
| 446 |
restype = ctypes.c_int |
| 447 |
if self._buffer is not None: |
| 448 |
ptr = self._getfuncptr_fromaddress(argtypes, restype, variadic_args=variadic_args) |
| 449 |
if argtypes == self._argtypes_: |
| 450 |
self._ptr = ptr |
| 451 |
return ptr |
| 452 |
|
| 453 |
if self._com_index: |
| 454 |
|
| 455 |
if not thisarg: |
| 456 |
raise ValueError("COM method call without VTable") |
| 457 |
ptr = thisarg[0][self._com_index - 0x1000] |
| 458 |
ffiargs = [argtype.get_ffi_argtype() for argtype in argtypes] |
| 459 |
ffires = restype.get_ffi_argtype() |
| 460 |
return _ffi.FuncPtr.fromaddr(ptr, '', ffiargs, ffires, self._flags_) |
| 461 |
|
| 462 |
cdll = self.dll._handle |
| 463 |
try: |
| 464 |
ffi_argtypes = [argtype.get_ffi_argtype() for argtype in argtypes] |
| 465 |
ffi_restype = restype.get_ffi_argtype() |
| 466 |
self._ptr = cdll.getfunc(self.name, ffi_argtypes, ffi_restype, variadic_args=variadic_args) |
| 467 |
return self._ptr |
| 468 |
except AttributeError: |
| 469 |
if self._flags_ & _rawffi.FUNCFLAG_CDECL: |
| 470 |
raise |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
if WIN64: |
| 475 |
raise |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
for i in range(33): |
| 480 |
mangled_name = "_%s@%d" % (self.name, i * 4) |
| 481 |
try: |
| 482 |
return cdll.getfunc(mangled_name, |
| 483 |
ffi_argtypes, ffi_restype, |
| 484 |
|
| 485 |
) |
| 486 |
except AttributeError: |
| 487 |
pass |
| 488 |
raise |
| 489 |
|
| 490 |
@classmethod |
| 491 |
def _conv_param(cls, argtype, arg): |
| 492 |
if argtype is not None: |
| 493 |
arg = argtype.from_param(arg) |
| 494 |
while hasattr(arg, '_as_parameter_'): |
| 495 |
arg = arg._as_parameter_ |
| 496 |
if isinstance(arg, _CData): |
| 497 |
return arg, arg._to_ffi_param(), type(arg) |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
from ctypes import c_char_p, c_wchar_p, c_void_p, c_int |
| 502 |
|
| 503 |
if isinstance(arg, bytes): |
| 504 |
cobj = c_char_p(arg) |
| 505 |
elif isinstance(arg, str): |
| 506 |
cobj = c_wchar_p(arg) |
| 507 |
elif arg is None: |
| 508 |
cobj = c_void_p() |
| 509 |
elif isinstance(arg, int): |
| 510 |
cobj = c_int(arg) |
| 511 |
else: |
| 512 |
raise TypeError("Don't know how to handle %s" % (arg,)) |
| 513 |
|
| 514 |
return cobj, cobj._to_ffi_param(), type(cobj) |
| 515 |
|
| 516 |
def _convert_args_for_callback(self, argtypes, args): |
| 517 |
from _ctypes.structure import StructOrUnion |
| 518 |
|
| 519 |
assert len(argtypes) == len(args) |
| 520 |
newargs = [] |
| 521 |
for argtype, arg in zip(argtypes, args): |
| 522 |
param = argtype.from_param(arg) |
| 523 |
_type_ = getattr(argtype, '_type_', None) |
| 524 |
if _type_ == 'P': |
| 525 |
param = param._get_buffer_value() |
| 526 |
elif self._is_primitive(argtype): |
| 527 |
param = param.value |
| 528 |
elif isinstance(param, StructOrUnion): |
| 529 |
newparam = StructOrUnion.__new__(type(param)) |
| 530 |
param._copy_to(newparam._buffer.buffer) |
| 531 |
param = newparam |
| 532 |
newargs.append(param) |
| 533 |
return newargs |
| 534 |
|
| 535 |
def _convert_args(self, argtypes, args, kwargs, marker=object()): |
| 536 |
newargs = [] |
| 537 |
outargs = [] |
| 538 |
keepalives = [] |
| 539 |
newargtypes = [] |
| 540 |
total = len(args) |
| 541 |
paramflags = self._paramflags |
| 542 |
|
| 543 |
if not paramflags and total < len(argtypes): |
| 544 |
raise TypeError("not enough arguments") |
| 545 |
|
| 546 |
if paramflags: |
| 547 |
errcheckargs = [] |
| 548 |
inargs_idx = 0 |
| 549 |
for i, argtype in enumerate(argtypes): |
| 550 |
flag = 0 |
| 551 |
defval = marker |
| 552 |
paramflag = paramflags[i] |
| 553 |
paramlen = len(paramflag) |
| 554 |
name = None |
| 555 |
if paramlen == 1: |
| 556 |
flag = paramflag[0] |
| 557 |
elif paramlen == 2: |
| 558 |
flag, name = paramflag |
| 559 |
elif paramlen == 3: |
| 560 |
flag, name, defval = paramflag |
| 561 |
flag = flag & PARAMFLAG_COMBINED |
| 562 |
if flag == PARAMFLAG_FIN | PARAMFLAG_FLCID: |
| 563 |
val = defval |
| 564 |
if val is marker: |
| 565 |
val = 0 |
| 566 |
errcheckargs.append(val) |
| 567 |
keepalive, newarg, newargtype = self._conv_param(argtype, val) |
| 568 |
keepalives.append(keepalive) |
| 569 |
newargs.append(newarg) |
| 570 |
newargtypes.append(newargtype) |
| 571 |
elif flag in (0, PARAMFLAG_FIN): |
| 572 |
if inargs_idx < total: |
| 573 |
val = args[inargs_idx] |
| 574 |
inargs_idx += 1 |
| 575 |
elif kwargs and name in kwargs: |
| 576 |
val = kwargs[name] |
| 577 |
inargs_idx += 1 |
| 578 |
elif defval is not marker: |
| 579 |
val = defval |
| 580 |
elif name: |
| 581 |
raise TypeError("required argument '%s' missing" % name) |
| 582 |
else: |
| 583 |
raise TypeError("not enough arguments") |
| 584 |
errcheckargs.append(val) |
| 585 |
keepalive, newarg, newargtype = self._conv_param(argtype, val) |
| 586 |
keepalives.append(keepalive) |
| 587 |
newargs.append(newarg) |
| 588 |
newargtypes.append(newargtype) |
| 589 |
elif flag == PARAMFLAG_FOUT: |
| 590 |
if defval is not marker: |
| 591 |
val = defval |
| 592 |
keepalive, newarg, newargtype = self._conv_param(argtype, defval) |
| 593 |
else: |
| 594 |
import ctypes |
| 595 |
val = argtype._type_._newowninstance_() |
| 596 |
keepalive = None |
| 597 |
newarg = ctypes.byref(val) |
| 598 |
newargtype = type(newarg) |
| 599 |
errcheckargs.append(val) |
| 600 |
outargs.append(val) |
| 601 |
keepalives.append(keepalive) |
| 602 |
newargs.append(newarg) |
| 603 |
newargtypes.append(newargtype) |
| 604 |
else: |
| 605 |
raise ValueError("paramflag %d not yet implemented" % flag) |
| 606 |
else: |
| 607 |
errcheckargs = args |
| 608 |
for i, argtype in enumerate(argtypes): |
| 609 |
try: |
| 610 |
keepalive, newarg, newargtype = self._conv_param(argtype, args[i]) |
| 611 |
except (UnicodeError, TypeError, ValueError) as e: |
| 612 |
raise ArgumentError(str(e)) |
| 613 |
keepalives.append(keepalive) |
| 614 |
newargs.append(newarg) |
| 615 |
newargtypes.append(newargtype) |
| 616 |
|
| 617 |
variadic_args = 0 |
| 618 |
if len(newargs) < len(args): |
| 619 |
variadic_args = len(args) - len(newargs) |
| 620 |
extra = args[len(newargs):] |
| 621 |
for i, arg in enumerate(extra): |
| 622 |
try: |
| 623 |
keepalive, newarg, newargtype = self._conv_param(None, arg) |
| 624 |
except (UnicodeError, TypeError, ValueError) as e: |
| 625 |
raise ArgumentError(str(e)) |
| 626 |
keepalives.append(keepalive) |
| 627 |
newargs.append(newarg) |
| 628 |
newargtypes.append(newargtype) |
| 629 |
return keepalives, newargs, newargtypes, outargs, errcheckargs, variadic_args |
| 630 |
|
| 631 |
@staticmethod |
| 632 |
def _is_primitive(argtype): |
| 633 |
return argtype.__bases__[0] is _SimpleCData |
| 634 |
|
| 635 |
def _wrap_result(self, restype, result): |
| 636 |
""" |
| 637 |
Convert from low-level repr of the result to the high-level python |
| 638 |
one. |
| 639 |
""" |
| 640 |
|
| 641 |
|
| 642 |
if (self._is_primitive(restype) and restype._type_ != '?' |
| 643 |
and not restype._is_pointer_like()): |
| 644 |
return result |
| 645 |
|
| 646 |
shape = restype._ffishape_ |
| 647 |
if is_struct_shape(shape): |
| 648 |
buf = result |
| 649 |
else: |
| 650 |
buf = _rawffi.Array(shape)(1, autofree=True) |
| 651 |
buf[0] = result |
| 652 |
retval = restype._CData_retval(buf) |
| 653 |
return retval |
| 654 |
|
| 655 |
def _build_result(self, restype, result): |
| 656 |
"""Build the function result: |
| 657 |
If there is no OUT parameter, return the actual function result |
| 658 |
If there is one OUT parameter, return it |
| 659 |
If there are many OUT parameters, return a tuple""" |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
retval = None |
| 667 |
|
| 668 |
if restype is not None: |
| 669 |
checker = getattr(self.restype, '_check_retval_', None) |
| 670 |
if checker: |
| 671 |
val = restype(result) |
| 672 |
|
| 673 |
|
| 674 |
if '_type_' in restype.__dict__: |
| 675 |
val = val.value |
| 676 |
|
| 677 |
|
| 678 |
if self._com_index: |
| 679 |
if result & 0x80000000: |
| 680 |
raise get_com_error(result, None, None) |
| 681 |
else: |
| 682 |
retval = checker(val) |
| 683 |
elif not isinstance(restype, _CDataMeta): |
| 684 |
retval = restype(result) |
| 685 |
else: |
| 686 |
retval = self._wrap_result(restype, result) |
| 687 |
|
| 688 |
return retval |
| 689 |
|
| 690 |
def __bool__(self): |
| 691 |
return self._com_index is not None or bool(self._buffer[0]) |
| 692 |
|
| 693 |
def __del__(self): |
| 694 |
if self._needs_free: |
| 695 |
|
| 696 |
if self._buffer is None: |
| 697 |
return |
| 698 |
self._buffer.free() |
| 699 |
self._buffer = None |
| 700 |
if isinstance(self._ptr, _rawffi.CallbackPtr): |
| 701 |
self._ptr.free() |
| 702 |
self._ptr = None |
| 703 |
self._needs_free = False |
| 704 |
|
| 705 |
|
| 706 |
def handle_system_exit(e): |
| 707 |
|
| 708 |
|
| 709 |
if sys.flags.inspect: |
| 710 |
return |
| 711 |
else: |
| 712 |
code = e.code |
| 713 |
if isinstance(code, int): |
| 714 |
exitcode = code |
| 715 |
else: |
| 716 |
f = getattr(sys, 'stderr', None) |
| 717 |
if f is None: |
| 718 |
f = sys.__stderr__ |
| 719 |
print(code, file=f) |
| 720 |
exitcode = 1 |
| 721 |
|
| 722 |
_rawffi.exit(exitcode) |