PageRenderTime 74ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/module/cpyext/api.py

https://bitbucket.org/yrttyr/pypy
Python | 1176 lines | 1109 code | 47 blank | 20 comment | 67 complexity | e9baa323efba5ee60994a472ca3a4f8d MD5 | raw file
  1. import ctypes
  2. import sys, os
  3. import atexit
  4. import py
  5. from pypy.translator.goal import autopath
  6. from pypy.rpython.lltypesystem import rffi, lltype
  7. from pypy.rpython.tool import rffi_platform
  8. from pypy.rpython.lltypesystem import ll2ctypes
  9. from pypy.rpython.annlowlevel import llhelper
  10. from pypy.rlib.objectmodel import we_are_translated
  11. from pypy.translator.tool.cbuild import ExternalCompilationInfo
  12. from pypy.translator.gensupp import NameManager
  13. from pypy.tool.udir import udir
  14. from pypy.translator import platform
  15. from pypy.module.cpyext.state import State
  16. from pypy.interpreter.error import OperationError, operationerrfmt
  17. from pypy.interpreter.baseobjspace import W_Root
  18. from pypy.interpreter.gateway import unwrap_spec
  19. from pypy.interpreter.nestedscope import Cell
  20. from pypy.interpreter.module import Module
  21. from pypy.interpreter.function import StaticMethod
  22. from pypy.objspace.std.sliceobject import W_SliceObject
  23. from pypy.module.__builtin__.descriptor import W_Property
  24. from pypy.module.__builtin__.interp_classobj import W_ClassObject
  25. from pypy.module.__builtin__.interp_memoryview import W_MemoryView
  26. from pypy.rlib.entrypoint import entrypoint
  27. from pypy.rlib.rposix import is_valid_fd, validate_fd
  28. from pypy.rlib.unroll import unrolling_iterable
  29. from pypy.rlib.objectmodel import specialize
  30. from pypy.rlib.exports import export_struct
  31. from pypy.module import exceptions
  32. from pypy.module.exceptions import interp_exceptions
  33. # CPython 2.4 compatibility
  34. from py.builtin import BaseException
  35. from pypy.tool.sourcetools import func_with_new_name
  36. from pypy.rpython.lltypesystem.lloperation import llop
  37. DEBUG_WRAPPER = True
  38. # update these for other platforms
  39. Py_ssize_t = lltype.Typedef(rffi.SSIZE_T, 'Py_ssize_t')
  40. Py_ssize_tP = rffi.CArrayPtr(Py_ssize_t)
  41. size_t = rffi.ULONG
  42. ADDR = lltype.Signed
  43. pypydir = py.path.local(autopath.pypydir)
  44. include_dir = pypydir / 'module' / 'cpyext' / 'include'
  45. source_dir = pypydir / 'module' / 'cpyext' / 'src'
  46. translator_c_dir = pypydir / 'translator' / 'c'
  47. include_dirs = [
  48. include_dir,
  49. translator_c_dir,
  50. udir,
  51. ]
  52. class CConfig:
  53. _compilation_info_ = ExternalCompilationInfo(
  54. include_dirs=include_dirs,
  55. includes=['Python.h', 'stdarg.h'],
  56. compile_extra=['-DPy_BUILD_CORE'],
  57. )
  58. class CConfig2:
  59. _compilation_info_ = CConfig._compilation_info_
  60. class CConfig_constants:
  61. _compilation_info_ = CConfig._compilation_info_
  62. VA_LIST_P = rffi.VOIDP # rffi.COpaquePtr('va_list')
  63. CONST_STRING = lltype.Ptr(lltype.Array(lltype.Char,
  64. hints={'nolength': True}),
  65. use_cache=False)
  66. CONST_WSTRING = lltype.Ptr(lltype.Array(lltype.UniChar,
  67. hints={'nolength': True}),
  68. use_cache=False)
  69. assert CONST_STRING is not rffi.CCHARP
  70. assert CONST_STRING == rffi.CCHARP
  71. assert CONST_WSTRING is not rffi.CWCHARP
  72. assert CONST_WSTRING == rffi.CWCHARP
  73. # FILE* interface
  74. FILEP = rffi.COpaquePtr('FILE')
  75. if sys.platform == 'win32':
  76. fileno = rffi.llexternal('_fileno', [FILEP], rffi.INT)
  77. else:
  78. fileno = rffi.llexternal('fileno', [FILEP], rffi.INT)
  79. fopen = rffi.llexternal('fopen', [CONST_STRING, CONST_STRING], FILEP)
  80. _fclose = rffi.llexternal('fclose', [FILEP], rffi.INT)
  81. def fclose(fp):
  82. if not is_valid_fd(fileno(fp)):
  83. return -1
  84. return _fclose(fp)
  85. _fwrite = rffi.llexternal('fwrite',
  86. [rffi.VOIDP, rffi.SIZE_T, rffi.SIZE_T, FILEP],
  87. rffi.SIZE_T)
  88. def fwrite(buf, sz, n, fp):
  89. validate_fd(fileno(fp))
  90. return _fwrite(buf, sz, n, fp)
  91. _fread = rffi.llexternal('fread',
  92. [rffi.VOIDP, rffi.SIZE_T, rffi.SIZE_T, FILEP],
  93. rffi.SIZE_T)
  94. def fread(buf, sz, n, fp):
  95. validate_fd(fileno(fp))
  96. return _fread(buf, sz, n, fp)
  97. _feof = rffi.llexternal('feof', [FILEP], rffi.INT)
  98. def feof(fp):
  99. validate_fd(fileno(fp))
  100. return _feof(fp)
  101. constant_names = """
  102. Py_TPFLAGS_READY Py_TPFLAGS_READYING Py_TPFLAGS_HAVE_GETCHARBUFFER
  103. METH_COEXIST METH_STATIC METH_CLASS
  104. METH_NOARGS METH_VARARGS METH_KEYWORDS METH_O
  105. Py_TPFLAGS_HEAPTYPE Py_TPFLAGS_HAVE_CLASS
  106. Py_LT Py_LE Py_EQ Py_NE Py_GT Py_GE
  107. """.split()
  108. for name in constant_names:
  109. setattr(CConfig_constants, name, rffi_platform.ConstantInteger(name))
  110. udir.join('pypy_decl.h').write("/* Will be filled later */\n")
  111. udir.join('pypy_macros.h').write("/* Will be filled later */\n")
  112. globals().update(rffi_platform.configure(CConfig_constants))
  113. def copy_header_files(dstdir):
  114. assert dstdir.check(dir=True)
  115. headers = include_dir.listdir('*.h') + include_dir.listdir('*.inl')
  116. for name in ("pypy_decl.h", "pypy_macros.h"):
  117. headers.append(udir.join(name))
  118. for header in headers:
  119. target = dstdir.join(header.basename)
  120. try:
  121. header.copy(dstdir)
  122. except py.error.EACCES:
  123. target.remove() # maybe it was a read-only file
  124. header.copy(dstdir)
  125. target.chmod(0444) # make the file read-only, to make sure that nobody
  126. # edits it by mistake
  127. class NotSpecified(object):
  128. pass
  129. _NOT_SPECIFIED = NotSpecified()
  130. class CannotFail(object):
  131. pass
  132. CANNOT_FAIL = CannotFail()
  133. # The same function can be called in three different contexts:
  134. # (1) from C code
  135. # (2) in the test suite, though the "api" object
  136. # (3) from RPython code, for example in the implementation of another function.
  137. #
  138. # In contexts (2) and (3), a function declaring a PyObject argument type will
  139. # receive a wrapped pypy object if the parameter name starts with 'w_', a
  140. # reference (= rffi pointer) otherwise; conversion is automatic. Context (2)
  141. # only allows calls with a wrapped object.
  142. #
  143. # Functions with a PyObject return type should return a wrapped object.
  144. #
  145. # Functions may raise exceptions. In context (3), the exception flows normally
  146. # through the calling function. In context (1) and (2), the exception is
  147. # caught; if it is an OperationError, it is stored in the thread state; other
  148. # exceptions generate a OperationError(w_SystemError); and the funtion returns
  149. # the error value specifed in the API.
  150. #
  151. cpyext_namespace = NameManager('cpyext_')
  152. class ApiFunction:
  153. def __init__(self, argtypes, restype, callable, error=_NOT_SPECIFIED,
  154. c_name=None):
  155. self.argtypes = argtypes
  156. self.restype = restype
  157. self.functype = lltype.Ptr(lltype.FuncType(argtypes, restype))
  158. self.callable = callable
  159. if error is not _NOT_SPECIFIED:
  160. self.error_value = error
  161. self.c_name = c_name
  162. # extract the signature from the (CPython-level) code object
  163. from pypy.interpreter import pycode
  164. argnames, varargname, kwargname = pycode.cpython_code_signature(callable.func_code)
  165. assert argnames[0] == 'space'
  166. self.argnames = argnames[1:]
  167. assert len(self.argnames) == len(self.argtypes)
  168. def _freeze_(self):
  169. return True
  170. def get_llhelper(self, space):
  171. llh = getattr(self, '_llhelper', None)
  172. if llh is None:
  173. llh = llhelper(self.functype, self.get_wrapper(space))
  174. self._llhelper = llh
  175. return llh
  176. @specialize.memo()
  177. def get_wrapper(self, space):
  178. wrapper = getattr(self, '_wrapper', None)
  179. if wrapper is None:
  180. wrapper = make_wrapper(space, self.callable)
  181. self._wrapper = wrapper
  182. wrapper.relax_sig_check = True
  183. if self.c_name is not None:
  184. wrapper.c_name = cpyext_namespace.uniquename(self.c_name)
  185. return wrapper
  186. def cpython_api(argtypes, restype, error=_NOT_SPECIFIED, external=True):
  187. """
  188. Declares a function to be exported.
  189. - `argtypes`, `restype` are lltypes and describe the function signature.
  190. - `error` is the value returned when an applevel exception is raised. The
  191. special value 'CANNOT_FAIL' (also when restype is Void) turns an eventual
  192. exception into a wrapped SystemError. Unwrapped exceptions also cause a
  193. SytemError.
  194. - set `external` to False to get a C function pointer, but not exported by
  195. the API headers.
  196. """
  197. if isinstance(restype, lltype.Typedef):
  198. real_restype = restype.OF
  199. else:
  200. real_restype = restype
  201. if error is _NOT_SPECIFIED:
  202. if isinstance(real_restype, lltype.Ptr):
  203. error = lltype.nullptr(real_restype.TO)
  204. elif real_restype is lltype.Void:
  205. error = CANNOT_FAIL
  206. if type(error) is int:
  207. error = rffi.cast(real_restype, error)
  208. expect_integer = (isinstance(real_restype, lltype.Primitive) and
  209. rffi.cast(restype, 0) == 0)
  210. def decorate(func):
  211. func_name = func.func_name
  212. if external:
  213. c_name = None
  214. else:
  215. c_name = func_name
  216. api_function = ApiFunction(argtypes, restype, func, error, c_name=c_name)
  217. func.api_func = api_function
  218. if external:
  219. assert func_name not in FUNCTIONS, (
  220. "%s already registered" % func_name)
  221. if error is _NOT_SPECIFIED:
  222. raise ValueError("function %s has no return value for exceptions"
  223. % func)
  224. def make_unwrapper(catch_exception):
  225. names = api_function.argnames
  226. types_names_enum_ui = unrolling_iterable(enumerate(
  227. zip(api_function.argtypes,
  228. [tp_name.startswith("w_") for tp_name in names])))
  229. @specialize.ll()
  230. def unwrapper(space, *args):
  231. from pypy.module.cpyext.pyobject import Py_DecRef
  232. from pypy.module.cpyext.pyobject import make_ref, from_ref
  233. from pypy.module.cpyext.pyobject import Reference
  234. newargs = ()
  235. to_decref = []
  236. assert len(args) == len(api_function.argtypes)
  237. for i, (ARG, is_wrapped) in types_names_enum_ui:
  238. input_arg = args[i]
  239. if is_PyObject(ARG) and not is_wrapped:
  240. # build a reference
  241. if input_arg is None:
  242. arg = lltype.nullptr(PyObject.TO)
  243. elif isinstance(input_arg, W_Root):
  244. ref = make_ref(space, input_arg)
  245. to_decref.append(ref)
  246. arg = rffi.cast(ARG, ref)
  247. else:
  248. arg = input_arg
  249. elif is_PyObject(ARG) and is_wrapped:
  250. # convert to a wrapped object
  251. if input_arg is None:
  252. arg = input_arg
  253. elif isinstance(input_arg, W_Root):
  254. arg = input_arg
  255. else:
  256. arg = from_ref(space,
  257. rffi.cast(PyObject, input_arg))
  258. else:
  259. arg = input_arg
  260. newargs += (arg, )
  261. try:
  262. try:
  263. res = func(space, *newargs)
  264. except OperationError, e:
  265. if not catch_exception:
  266. raise
  267. if not hasattr(api_function, "error_value"):
  268. raise
  269. state = space.fromcache(State)
  270. state.set_exception(e)
  271. if is_PyObject(restype):
  272. return None
  273. else:
  274. return api_function.error_value
  275. if not we_are_translated():
  276. got_integer = isinstance(res, (int, long, float))
  277. assert got_integer == expect_integer
  278. if res is None:
  279. return None
  280. elif isinstance(res, Reference):
  281. return res.get_wrapped(space)
  282. else:
  283. return res
  284. finally:
  285. for arg in to_decref:
  286. Py_DecRef(space, arg)
  287. unwrapper.func = func
  288. unwrapper.api_func = api_function
  289. unwrapper._always_inline_ = 'try'
  290. return unwrapper
  291. unwrapper_catch = make_unwrapper(True)
  292. unwrapper_raise = make_unwrapper(False)
  293. if external:
  294. FUNCTIONS[func_name] = api_function
  295. INTERPLEVEL_API[func_name] = unwrapper_catch # used in tests
  296. return unwrapper_raise # used in 'normal' RPython code.
  297. return decorate
  298. def cpython_struct(name, fields, forward=None, level=1):
  299. configname = name.replace(' ', '__')
  300. if level == 1:
  301. config = CConfig
  302. else:
  303. config = CConfig2
  304. setattr(config, configname, rffi_platform.Struct(name, fields))
  305. if forward is None:
  306. forward = lltype.ForwardReference()
  307. TYPES[configname] = forward
  308. return forward
  309. INTERPLEVEL_API = {}
  310. FUNCTIONS = {}
  311. # These are C symbols which cpyext will export, but which are defined in .c
  312. # files somewhere in the implementation of cpyext (rather than being defined in
  313. # RPython).
  314. SYMBOLS_C = [
  315. 'Py_FatalError', 'PyOS_snprintf', 'PyOS_vsnprintf', 'PyArg_Parse',
  316. 'PyArg_ParseTuple', 'PyArg_UnpackTuple', 'PyArg_ParseTupleAndKeywords',
  317. 'PyArg_VaParse', 'PyArg_VaParseTupleAndKeywords', '_PyArg_NoKeywords',
  318. 'PyString_FromFormat', 'PyString_FromFormatV',
  319. 'PyModule_AddObject', 'PyModule_AddIntConstant', 'PyModule_AddStringConstant',
  320. 'Py_BuildValue', 'Py_VaBuildValue', 'PyTuple_Pack',
  321. 'PyErr_Format', 'PyErr_NewException', 'PyErr_NewExceptionWithDoc',
  322. 'PySys_WriteStdout', 'PySys_WriteStderr',
  323. 'PyEval_CallFunction', 'PyEval_CallMethod', 'PyObject_CallFunction',
  324. 'PyObject_CallMethod', 'PyObject_CallFunctionObjArgs', 'PyObject_CallMethodObjArgs',
  325. 'PyBuffer_FromMemory', 'PyBuffer_FromReadWriteMemory', 'PyBuffer_FromObject',
  326. 'PyBuffer_FromReadWriteObject', 'PyBuffer_New', 'PyBuffer_Type', 'init_bufferobject',
  327. 'PyCObject_FromVoidPtr', 'PyCObject_FromVoidPtrAndDesc', 'PyCObject_AsVoidPtr',
  328. 'PyCObject_GetDesc', 'PyCObject_Import', 'PyCObject_SetVoidPtr',
  329. 'PyCObject_Type', 'init_pycobject',
  330. 'PyCapsule_New', 'PyCapsule_IsValid', 'PyCapsule_GetPointer',
  331. 'PyCapsule_GetName', 'PyCapsule_GetDestructor', 'PyCapsule_GetContext',
  332. 'PyCapsule_SetPointer', 'PyCapsule_SetName', 'PyCapsule_SetDestructor',
  333. 'PyCapsule_SetContext', 'PyCapsule_Import', 'PyCapsule_Type', 'init_capsule',
  334. 'PyObject_AsReadBuffer', 'PyObject_AsWriteBuffer', 'PyObject_CheckReadBuffer',
  335. 'PyOS_getsig', 'PyOS_setsig',
  336. 'PyThread_get_thread_ident', 'PyThread_allocate_lock', 'PyThread_free_lock',
  337. 'PyThread_acquire_lock', 'PyThread_release_lock',
  338. 'PyThread_create_key', 'PyThread_delete_key', 'PyThread_set_key_value',
  339. 'PyThread_get_key_value', 'PyThread_delete_key_value',
  340. 'PyThread_ReInitTLS',
  341. 'PyStructSequence_InitType', 'PyStructSequence_New',
  342. ]
  343. TYPES = {}
  344. GLOBALS = { # this needs to include all prebuilt pto, otherwise segfaults occur
  345. '_Py_NoneStruct#': ('PyObject*', 'space.w_None'),
  346. '_Py_TrueStruct#': ('PyObject*', 'space.w_True'),
  347. '_Py_ZeroStruct#': ('PyObject*', 'space.w_False'),
  348. '_Py_NotImplementedStruct#': ('PyObject*', 'space.w_NotImplemented'),
  349. '_Py_EllipsisObject#': ('PyObject*', 'space.w_Ellipsis'),
  350. 'PyDateTimeAPI': ('PyDateTime_CAPI*', 'None'),
  351. }
  352. FORWARD_DECLS = []
  353. INIT_FUNCTIONS = []
  354. BOOTSTRAP_FUNCTIONS = []
  355. def build_exported_objects():
  356. # Standard exceptions
  357. for exc_name in exceptions.Module.interpleveldefs.keys():
  358. GLOBALS['PyExc_' + exc_name] = (
  359. 'PyTypeObject*',
  360. 'space.gettypeobject(interp_exceptions.W_%s.typedef)'% (exc_name, ))
  361. # Common types with their own struct
  362. for cpyname, pypyexpr in {
  363. "Type": "space.w_type",
  364. "String": "space.w_str",
  365. "Unicode": "space.w_unicode",
  366. "BaseString": "space.w_basestring",
  367. "Dict": "space.w_dict",
  368. "Tuple": "space.w_tuple",
  369. "List": "space.w_list",
  370. "Set": "space.w_set",
  371. "FrozenSet": "space.w_frozenset",
  372. "Int": "space.w_int",
  373. "Bool": "space.w_bool",
  374. "Float": "space.w_float",
  375. "Long": "space.w_long",
  376. "Complex": "space.w_complex",
  377. "ByteArray": "space.w_bytearray",
  378. "MemoryView": "space.gettypeobject(W_MemoryView.typedef)",
  379. "BaseObject": "space.w_object",
  380. 'None': 'space.type(space.w_None)',
  381. 'NotImplemented': 'space.type(space.w_NotImplemented)',
  382. 'Cell': 'space.gettypeobject(Cell.typedef)',
  383. 'Module': 'space.gettypeobject(Module.typedef)',
  384. 'Property': 'space.gettypeobject(W_Property.typedef)',
  385. 'Slice': 'space.gettypeobject(W_SliceObject.typedef)',
  386. 'Class': 'space.gettypeobject(W_ClassObject.typedef)',
  387. 'StaticMethod': 'space.gettypeobject(StaticMethod.typedef)',
  388. 'CFunction': 'space.gettypeobject(cpyext.methodobject.W_PyCFunctionObject.typedef)',
  389. 'WrapperDescr': 'space.gettypeobject(cpyext.methodobject.W_PyCMethodObject.typedef)'
  390. }.items():
  391. GLOBALS['Py%s_Type#' % (cpyname, )] = ('PyTypeObject*', pypyexpr)
  392. for cpyname in 'Method List Long Dict Tuple Class'.split():
  393. FORWARD_DECLS.append('typedef struct { PyObject_HEAD } '
  394. 'Py%sObject' % (cpyname, ))
  395. build_exported_objects()
  396. def get_structtype_for_ctype(ctype):
  397. from pypy.module.cpyext.typeobjectdefs import PyTypeObjectPtr
  398. from pypy.module.cpyext.cdatetime import PyDateTime_CAPI
  399. return {"PyObject*": PyObject, "PyTypeObject*": PyTypeObjectPtr,
  400. "PyDateTime_CAPI*": lltype.Ptr(PyDateTime_CAPI)}[ctype]
  401. PyTypeObject = lltype.ForwardReference()
  402. PyTypeObjectPtr = lltype.Ptr(PyTypeObject)
  403. # It is important that these PyObjects are allocated in a raw fashion
  404. # Thus we cannot save a forward pointer to the wrapped object
  405. # So we need a forward and backward mapping in our State instance
  406. PyObjectStruct = lltype.ForwardReference()
  407. PyObject = lltype.Ptr(PyObjectStruct)
  408. PyObjectFields = (("ob_refcnt", lltype.Signed), ("ob_type", PyTypeObjectPtr))
  409. PyVarObjectFields = PyObjectFields + (("ob_size", Py_ssize_t), )
  410. cpython_struct('PyObject', PyObjectFields, PyObjectStruct)
  411. PyVarObjectStruct = cpython_struct("PyVarObject", PyVarObjectFields)
  412. PyVarObject = lltype.Ptr(PyVarObjectStruct)
  413. Py_buffer = cpython_struct(
  414. "Py_buffer", (
  415. ('buf', rffi.VOIDP),
  416. ('obj', PyObject),
  417. ('len', Py_ssize_t),
  418. ('itemsize', Py_ssize_t),
  419. ('readonly', lltype.Signed),
  420. ('ndim', lltype.Signed),
  421. ('format', rffi.CCHARP),
  422. ('shape', Py_ssize_tP),
  423. ('strides', Py_ssize_tP),
  424. ('suboffsets', Py_ssize_tP),
  425. #('smalltable', rffi.CFixedArray(Py_ssize_t, 2)),
  426. ('internal', rffi.VOIDP)
  427. ))
  428. @specialize.memo()
  429. def is_PyObject(TYPE):
  430. if not isinstance(TYPE, lltype.Ptr):
  431. return False
  432. return hasattr(TYPE.TO, 'c_ob_refcnt') and hasattr(TYPE.TO, 'c_ob_type')
  433. # a pointer to PyObject
  434. PyObjectP = rffi.CArrayPtr(PyObject)
  435. VA_TP_LIST = {}
  436. #{'int': lltype.Signed,
  437. # 'PyObject*': PyObject,
  438. # 'PyObject**': PyObjectP,
  439. # 'int*': rffi.INTP}
  440. def configure_types():
  441. for config in (CConfig, CConfig2):
  442. for name, TYPE in rffi_platform.configure(config).iteritems():
  443. if name in TYPES:
  444. TYPES[name].become(TYPE)
  445. def build_type_checkers(type_name, cls=None):
  446. """
  447. Builds two api functions: Py_XxxCheck() and Py_XxxCheckExact().
  448. - if `cls` is None, the type is space.w_[type].
  449. - if `cls` is a string, it is the name of a space attribute, e.g. 'w_str'.
  450. - else `cls` must be a W_Class with a typedef.
  451. """
  452. if cls is None:
  453. attrname = "w_" + type_name.lower()
  454. def get_w_type(space):
  455. return getattr(space, attrname)
  456. elif isinstance(cls, str):
  457. def get_w_type(space):
  458. return getattr(space, cls)
  459. else:
  460. def get_w_type(space):
  461. return space.gettypeobject(cls.typedef)
  462. check_name = "Py" + type_name + "_Check"
  463. def check(space, w_obj):
  464. "Implements the Py_Xxx_Check function"
  465. w_obj_type = space.type(w_obj)
  466. w_type = get_w_type(space)
  467. return (space.is_w(w_obj_type, w_type) or
  468. space.is_true(space.issubtype(w_obj_type, w_type)))
  469. def check_exact(space, w_obj):
  470. "Implements the Py_Xxx_CheckExact function"
  471. w_obj_type = space.type(w_obj)
  472. w_type = get_w_type(space)
  473. return space.is_w(w_obj_type, w_type)
  474. check = cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)(
  475. func_with_new_name(check, check_name))
  476. check_exact = cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)(
  477. func_with_new_name(check_exact, check_name + "Exact"))
  478. return check, check_exact
  479. pypy_debug_catch_fatal_exception = rffi.llexternal('pypy_debug_catch_fatal_exception', [], lltype.Void)
  480. # Make the wrapper for the cases (1) and (2)
  481. def make_wrapper(space, callable):
  482. "NOT_RPYTHON"
  483. names = callable.api_func.argnames
  484. argtypes_enum_ui = unrolling_iterable(enumerate(zip(callable.api_func.argtypes,
  485. [name.startswith("w_") for name in names])))
  486. fatal_value = callable.api_func.restype._defl()
  487. @specialize.ll()
  488. def wrapper(*args):
  489. from pypy.module.cpyext.pyobject import make_ref, from_ref
  490. from pypy.module.cpyext.pyobject import Reference
  491. # we hope that malloc removal removes the newtuple() that is
  492. # inserted exactly here by the varargs specializer
  493. llop.gc_stack_bottom(lltype.Void) # marker for trackgcroot.py
  494. rffi.stackcounter.stacks_counter += 1
  495. retval = fatal_value
  496. boxed_args = ()
  497. try:
  498. if not we_are_translated() and DEBUG_WRAPPER:
  499. print >>sys.stderr, callable,
  500. assert len(args) == len(callable.api_func.argtypes)
  501. for i, (typ, is_wrapped) in argtypes_enum_ui:
  502. arg = args[i]
  503. if is_PyObject(typ) and is_wrapped:
  504. if arg:
  505. arg_conv = from_ref(space, rffi.cast(PyObject, arg))
  506. else:
  507. arg_conv = None
  508. else:
  509. arg_conv = arg
  510. boxed_args += (arg_conv, )
  511. state = space.fromcache(State)
  512. try:
  513. result = callable(space, *boxed_args)
  514. if not we_are_translated() and DEBUG_WRAPPER:
  515. print >>sys.stderr, " DONE"
  516. except OperationError, e:
  517. failed = True
  518. state.set_exception(e)
  519. except BaseException, e:
  520. failed = True
  521. if not we_are_translated():
  522. message = repr(e)
  523. import traceback
  524. traceback.print_exc()
  525. else:
  526. message = str(e)
  527. state.set_exception(OperationError(space.w_SystemError,
  528. space.wrap(message)))
  529. else:
  530. failed = False
  531. if failed:
  532. error_value = callable.api_func.error_value
  533. if error_value is CANNOT_FAIL:
  534. raise SystemError("The function '%s' was not supposed to fail"
  535. % (callable.__name__,))
  536. retval = error_value
  537. elif is_PyObject(callable.api_func.restype):
  538. if result is None:
  539. retval = rffi.cast(callable.api_func.restype,
  540. make_ref(space, None))
  541. elif isinstance(result, Reference):
  542. retval = result.get_ref(space)
  543. elif not rffi._isllptr(result):
  544. retval = rffi.cast(callable.api_func.restype,
  545. make_ref(space, result))
  546. else:
  547. retval = result
  548. elif callable.api_func.restype is not lltype.Void:
  549. retval = rffi.cast(callable.api_func.restype, result)
  550. except Exception, e:
  551. print 'Fatal error in cpyext, CPython compatibility layer, calling', callable.__name__
  552. print 'Either report a bug or consider not using this particular extension'
  553. if not we_are_translated():
  554. import traceback
  555. traceback.print_exc()
  556. print str(e)
  557. # we can't do much here, since we're in ctypes, swallow
  558. else:
  559. print str(e)
  560. pypy_debug_catch_fatal_exception()
  561. rffi.stackcounter.stacks_counter -= 1
  562. return retval
  563. callable._always_inline_ = 'try'
  564. wrapper.__name__ = "wrapper for %r" % (callable, )
  565. return wrapper
  566. def process_va_name(name):
  567. return name.replace('*', '_star')
  568. def setup_va_functions(eci):
  569. for name, TP in VA_TP_LIST.iteritems():
  570. name_no_star = process_va_name(name)
  571. func = rffi.llexternal('pypy_va_get_%s' % name_no_star, [VA_LIST_P],
  572. TP, compilation_info=eci)
  573. globals()['va_get_%s' % name_no_star] = func
  574. def setup_init_functions(eci, translating):
  575. init_buffer = rffi.llexternal('init_bufferobject', [], lltype.Void,
  576. compilation_info=eci, _nowrapper=True)
  577. init_pycobject = rffi.llexternal('init_pycobject', [], lltype.Void,
  578. compilation_info=eci, _nowrapper=True)
  579. init_capsule = rffi.llexternal('init_capsule', [], lltype.Void,
  580. compilation_info=eci, _nowrapper=True)
  581. INIT_FUNCTIONS.extend([
  582. lambda space: init_buffer(),
  583. lambda space: init_pycobject(),
  584. lambda space: init_capsule(),
  585. ])
  586. from pypy.module.posix.interp_posix import add_fork_hook
  587. if translating:
  588. reinit_tls = rffi.llexternal('PyThread_ReInitTLS', [], lltype.Void,
  589. compilation_info=eci)
  590. else:
  591. reinit_tls = rffi.llexternal('PyPyThread_ReInitTLS', [], lltype.Void,
  592. compilation_info=eci)
  593. add_fork_hook('child', reinit_tls)
  594. def init_function(func):
  595. INIT_FUNCTIONS.append(func)
  596. return func
  597. def bootstrap_function(func):
  598. BOOTSTRAP_FUNCTIONS.append(func)
  599. return func
  600. def run_bootstrap_functions(space):
  601. for func in BOOTSTRAP_FUNCTIONS:
  602. func(space)
  603. def c_function_signature(db, func):
  604. restype = db.gettype(func.restype).replace('@', '').strip()
  605. args = []
  606. for i, argtype in enumerate(func.argtypes):
  607. if argtype is CONST_STRING:
  608. arg = 'const char *@'
  609. elif argtype is CONST_WSTRING:
  610. arg = 'const wchar_t *@'
  611. else:
  612. arg = db.gettype(argtype)
  613. arg = arg.replace('@', 'arg%d' % (i,)).strip()
  614. args.append(arg)
  615. args = ', '.join(args) or "void"
  616. return restype, args
  617. #_____________________________________________________
  618. # Build the bridge DLL, Allow extension DLLs to call
  619. # back into Pypy space functions
  620. # Do not call this more than once per process
  621. def build_bridge(space):
  622. "NOT_RPYTHON"
  623. from pypy.module.cpyext.pyobject import make_ref
  624. export_symbols = list(FUNCTIONS) + SYMBOLS_C + list(GLOBALS)
  625. from pypy.translator.c.database import LowLevelDatabase
  626. db = LowLevelDatabase()
  627. generate_macros(export_symbols, rename=True, do_deref=True)
  628. # Structure declaration code
  629. members = []
  630. structindex = {}
  631. for name, func in sorted(FUNCTIONS.iteritems()):
  632. restype, args = c_function_signature(db, func)
  633. members.append('%s (*%s)(%s);' % (restype, name, args))
  634. structindex[name] = len(structindex)
  635. structmembers = '\n'.join(members)
  636. struct_declaration_code = """\
  637. struct PyPyAPI {
  638. %(members)s
  639. } _pypyAPI;
  640. struct PyPyAPI* pypyAPI = &_pypyAPI;
  641. """ % dict(members=structmembers)
  642. functions = generate_decls_and_callbacks(db, export_symbols)
  643. global_objects = []
  644. for name, (typ, expr) in GLOBALS.iteritems():
  645. if "#" in name:
  646. continue
  647. if typ == 'PyDateTime_CAPI*':
  648. continue
  649. elif name.startswith('PyExc_'):
  650. global_objects.append('%s _%s;' % (typ[:-1], name))
  651. else:
  652. global_objects.append('%s %s = NULL;' % (typ, name))
  653. global_code = '\n'.join(global_objects)
  654. prologue = ("#include <Python.h>\n"
  655. "#include <src/thread.c>\n")
  656. code = (prologue +
  657. struct_declaration_code +
  658. global_code +
  659. '\n' +
  660. '\n'.join(functions))
  661. eci = build_eci(True, export_symbols, code)
  662. eci = eci.compile_shared_lib(
  663. outputfilename=str(udir / "module_cache" / "pypyapi"))
  664. modulename = py.path.local(eci.libraries[-1])
  665. run_bootstrap_functions(space)
  666. # load the bridge, and init structure
  667. import ctypes
  668. bridge = ctypes.CDLL(str(modulename), mode=ctypes.RTLD_GLOBAL)
  669. space.fromcache(State).install_dll(eci)
  670. # populate static data
  671. for name, (typ, expr) in GLOBALS.iteritems():
  672. from pypy.module import cpyext
  673. w_obj = eval(expr)
  674. if name.endswith('#'):
  675. name = name[:-1]
  676. isptr = False
  677. else:
  678. isptr = True
  679. if name.startswith('PyExc_'):
  680. isptr = False
  681. INTERPLEVEL_API[name] = w_obj
  682. name = name.replace('Py', 'PyPy')
  683. if isptr:
  684. ptr = ctypes.c_void_p.in_dll(bridge, name)
  685. if typ == 'PyObject*':
  686. value = make_ref(space, w_obj)
  687. elif typ == 'PyDateTime_CAPI*':
  688. value = w_obj
  689. else:
  690. assert False, "Unknown static pointer: %s %s" % (typ, name)
  691. ptr.value = ctypes.cast(ll2ctypes.lltype2ctypes(value),
  692. ctypes.c_void_p).value
  693. elif typ in ('PyObject*', 'PyTypeObject*'):
  694. if name.startswith('PyPyExc_'):
  695. # we already have the pointer
  696. in_dll = ll2ctypes.get_ctypes_type(PyObject).in_dll(bridge, name)
  697. py_obj = ll2ctypes.ctypes2lltype(PyObject, in_dll)
  698. else:
  699. # we have a structure, get its address
  700. in_dll = ll2ctypes.get_ctypes_type(PyObject.TO).in_dll(bridge, name)
  701. py_obj = ll2ctypes.ctypes2lltype(PyObject, ctypes.pointer(in_dll))
  702. from pypy.module.cpyext.pyobject import (
  703. track_reference, get_typedescr)
  704. w_type = space.type(w_obj)
  705. typedescr = get_typedescr(w_type.instancetypedef)
  706. py_obj.c_ob_refcnt = 1
  707. py_obj.c_ob_type = rffi.cast(PyTypeObjectPtr,
  708. make_ref(space, w_type))
  709. typedescr.attach(space, py_obj, w_obj)
  710. track_reference(space, py_obj, w_obj)
  711. else:
  712. assert False, "Unknown static object: %s %s" % (typ, name)
  713. pypyAPI = ctypes.POINTER(ctypes.c_void_p).in_dll(bridge, 'pypyAPI')
  714. # implement structure initialization code
  715. for name, func in FUNCTIONS.iteritems():
  716. if name.startswith('cpyext_'): # XXX hack
  717. continue
  718. pypyAPI[structindex[name]] = ctypes.cast(
  719. ll2ctypes.lltype2ctypes(func.get_llhelper(space)),
  720. ctypes.c_void_p)
  721. setup_va_functions(eci)
  722. setup_init_functions(eci, translating=False)
  723. return modulename.new(ext='')
  724. def generate_macros(export_symbols, rename=True, do_deref=True):
  725. "NOT_RPYTHON"
  726. pypy_macros = []
  727. renamed_symbols = []
  728. for name in export_symbols:
  729. if name.startswith("PyPy"):
  730. renamed_symbols.append(name)
  731. continue
  732. if not rename:
  733. continue
  734. name = name.replace("#", "")
  735. newname = name.replace('Py', 'PyPy')
  736. if not rename:
  737. newname = name
  738. pypy_macros.append('#define %s %s' % (name, newname))
  739. if name.startswith("PyExc_"):
  740. pypy_macros.append('#define _%s _%s' % (name, newname))
  741. renamed_symbols.append(newname)
  742. if rename:
  743. export_symbols[:] = renamed_symbols
  744. else:
  745. export_symbols[:] = [sym.replace("#", "") for sym in export_symbols]
  746. # Generate defines
  747. for macro_name, size in [
  748. ("SIZEOF_LONG_LONG", rffi.LONGLONG),
  749. ("SIZEOF_VOID_P", rffi.VOIDP),
  750. ("SIZEOF_SIZE_T", rffi.SIZE_T),
  751. ("SIZEOF_TIME_T", rffi.TIME_T),
  752. ("SIZEOF_LONG", rffi.LONG),
  753. ("SIZEOF_SHORT", rffi.SHORT),
  754. ("SIZEOF_INT", rffi.INT)
  755. ]:
  756. pypy_macros.append("#define %s %s" % (macro_name, rffi.sizeof(size)))
  757. pypy_macros.append('')
  758. pypy_macros_h = udir.join('pypy_macros.h')
  759. pypy_macros_h.write('\n'.join(pypy_macros))
  760. def generate_decls_and_callbacks(db, export_symbols, api_struct=True):
  761. "NOT_RPYTHON"
  762. # implement function callbacks and generate function decls
  763. functions = []
  764. pypy_decls = []
  765. pypy_decls.append("#ifndef PYPY_STANDALONE\n")
  766. pypy_decls.append("#ifdef __cplusplus")
  767. pypy_decls.append("extern \"C\" {")
  768. pypy_decls.append("#endif\n")
  769. pypy_decls.append('#define Signed long /* xxx temporary fix */\n')
  770. pypy_decls.append('#define Unsigned unsigned long /* xxx temporary fix */\n')
  771. for decl in FORWARD_DECLS:
  772. pypy_decls.append("%s;" % (decl,))
  773. for name, func in sorted(FUNCTIONS.iteritems()):
  774. restype, args = c_function_signature(db, func)
  775. pypy_decls.append("PyAPI_FUNC(%s) %s(%s);" % (restype, name, args))
  776. if api_struct:
  777. callargs = ', '.join('arg%d' % (i,)
  778. for i in range(len(func.argtypes)))
  779. if func.restype is lltype.Void:
  780. body = "{ _pypyAPI.%s(%s); }" % (name, callargs)
  781. else:
  782. body = "{ return _pypyAPI.%s(%s); }" % (name, callargs)
  783. functions.append('%s %s(%s)\n%s' % (restype, name, args, body))
  784. for name in VA_TP_LIST:
  785. name_no_star = process_va_name(name)
  786. header = ('%s pypy_va_get_%s(va_list* vp)' %
  787. (name, name_no_star))
  788. pypy_decls.append(header + ';')
  789. functions.append(header + '\n{return va_arg(*vp, %s);}\n' % name)
  790. export_symbols.append('pypy_va_get_%s' % (name_no_star,))
  791. for name, (typ, expr) in GLOBALS.iteritems():
  792. if name.endswith('#'):
  793. name = name.replace("#", "")
  794. typ = typ.replace("*", "")
  795. elif name.startswith('PyExc_'):
  796. typ = 'PyObject*'
  797. pypy_decls.append('PyAPI_DATA(%s) %s;' % (typ, name))
  798. pypy_decls.append('#undef Signed /* xxx temporary fix */\n')
  799. pypy_decls.append('#undef Unsigned /* xxx temporary fix */\n')
  800. pypy_decls.append("#ifdef __cplusplus")
  801. pypy_decls.append("}")
  802. pypy_decls.append("#endif")
  803. pypy_decls.append("#endif /*PYPY_STANDALONE*/\n")
  804. pypy_decl_h = udir.join('pypy_decl.h')
  805. pypy_decl_h.write('\n'.join(pypy_decls))
  806. return functions
  807. def build_eci(building_bridge, export_symbols, code):
  808. "NOT_RPYTHON"
  809. # Build code and get pointer to the structure
  810. kwds = {}
  811. export_symbols_eci = export_symbols[:]
  812. compile_extra=['-DPy_BUILD_CORE']
  813. if building_bridge:
  814. if sys.platform == "win32":
  815. # '%s' undefined; assuming extern returning int
  816. compile_extra.append("/we4013")
  817. # Sometimes the library is wrapped into another DLL, ensure that
  818. # the correct bootstrap code is installed
  819. kwds["link_extra"] = ["msvcrt.lib"]
  820. elif sys.platform.startswith('linux'):
  821. compile_extra.append("-Werror=implicit-function-declaration")
  822. export_symbols_eci.append('pypyAPI')
  823. compile_extra.append('-g')
  824. else:
  825. kwds["includes"] = ['Python.h'] # this is our Python.h
  826. # Generate definitions for global structures
  827. structs = ["#include <Python.h>"]
  828. for name, (typ, expr) in GLOBALS.iteritems():
  829. if name.endswith('#'):
  830. structs.append('%s %s;' % (typ[:-1], name[:-1]))
  831. elif name.startswith('PyExc_'):
  832. structs.append('extern PyTypeObject _%s;' % (name,))
  833. structs.append('PyObject* %s = (PyObject*)&_%s;' % (name, name))
  834. elif typ == 'PyDateTime_CAPI*':
  835. structs.append('%s %s = NULL;' % (typ, name))
  836. struct_source = '\n'.join(structs)
  837. separate_module_sources = [code, struct_source]
  838. if sys.platform == 'win32':
  839. get_pythonapi_source = '''
  840. #include <windows.h>
  841. HANDLE pypy_get_pythonapi_handle() {
  842. MEMORY_BASIC_INFORMATION mi;
  843. memset(&mi, 0, sizeof(mi));
  844. if( !VirtualQueryEx(GetCurrentProcess(), &pypy_get_pythonapi_handle,
  845. &mi, sizeof(mi)) )
  846. return 0;
  847. return (HMODULE)mi.AllocationBase;
  848. }
  849. '''
  850. separate_module_sources.append(get_pythonapi_source)
  851. export_symbols_eci.append('pypy_get_pythonapi_handle')
  852. eci = ExternalCompilationInfo(
  853. include_dirs=include_dirs,
  854. separate_module_files=[source_dir / "varargwrapper.c",
  855. source_dir / "pyerrors.c",
  856. source_dir / "modsupport.c",
  857. source_dir / "getargs.c",
  858. source_dir / "abstract.c",
  859. source_dir / "stringobject.c",
  860. source_dir / "mysnprintf.c",
  861. source_dir / "pythonrun.c",
  862. source_dir / "sysmodule.c",
  863. source_dir / "bufferobject.c",
  864. source_dir / "cobject.c",
  865. source_dir / "structseq.c",
  866. source_dir / "capsule.c",
  867. source_dir / "pysignals.c",
  868. source_dir / "pythread.c",
  869. ],
  870. separate_module_sources=separate_module_sources,
  871. export_symbols=export_symbols_eci,
  872. compile_extra=compile_extra,
  873. **kwds
  874. )
  875. return eci
  876. def setup_library(space):
  877. "NOT_RPYTHON"
  878. from pypy.module.cpyext.pyobject import make_ref
  879. export_symbols = list(FUNCTIONS) + SYMBOLS_C + list(GLOBALS)
  880. from pypy.translator.c.database import LowLevelDatabase
  881. db = LowLevelDatabase()
  882. generate_macros(export_symbols, rename=False, do_deref=False)
  883. functions = generate_decls_and_callbacks(db, [], api_struct=False)
  884. code = "#include <Python.h>\n" + "\n".join(functions)
  885. eci = build_eci(False, export_symbols, code)
  886. space.fromcache(State).install_dll(eci)
  887. run_bootstrap_functions(space)
  888. setup_va_functions(eci)
  889. # populate static data
  890. for name, (typ, expr) in GLOBALS.iteritems():
  891. name = name.replace("#", "")
  892. if name.startswith('PyExc_'):
  893. name = '_' + name
  894. from pypy.module import cpyext
  895. w_obj = eval(expr)
  896. if typ in ('PyObject*', 'PyTypeObject*'):
  897. struct_ptr = make_ref(space, w_obj)
  898. elif typ == 'PyDateTime_CAPI*':
  899. continue
  900. else:
  901. assert False, "Unknown static data: %s %s" % (typ, name)
  902. struct = rffi.cast(get_structtype_for_ctype(typ), struct_ptr)._obj
  903. struct._compilation_info = eci
  904. export_struct(name, struct)
  905. for name, func in FUNCTIONS.iteritems():
  906. deco = entrypoint("cpyext", func.argtypes, name, relax=True)
  907. deco(func.get_wrapper(space))
  908. setup_init_functions(eci, translating=True)
  909. trunk_include = pypydir.dirpath() / 'include'
  910. copy_header_files(trunk_include)
  911. initfunctype = lltype.Ptr(lltype.FuncType([], lltype.Void))
  912. @unwrap_spec(path=str, name=str)
  913. def load_extension_module(space, path, name):
  914. if os.sep not in path:
  915. path = os.curdir + os.sep + path # force a '/' in the path
  916. state = space.fromcache(State)
  917. if state.find_extension(name, path) is not None:
  918. return
  919. old_context = state.package_context
  920. state.package_context = name, path
  921. try:
  922. from pypy.rlib import rdynload
  923. try:
  924. ll_libname = rffi.str2charp(path)
  925. try:
  926. dll = rdynload.dlopen(ll_libname)
  927. finally:
  928. lltype.free(ll_libname, flavor='raw')
  929. except rdynload.DLOpenError, e:
  930. raise operationerrfmt(
  931. space.w_ImportError,
  932. "unable to load extension module '%s': %s",
  933. path, e.msg)
  934. try:
  935. initptr = rdynload.dlsym(dll, 'init%s' % (name.split('.')[-1],))
  936. except KeyError:
  937. raise operationerrfmt(
  938. space.w_ImportError,
  939. "function init%s not found in library %s",
  940. name, path)
  941. initfunc = rffi.cast(initfunctype, initptr)
  942. generic_cpy_call(space, initfunc)
  943. state.check_and_raise_exception()
  944. finally:
  945. state.package_context = old_context
  946. state.fixup_extension(name, path)
  947. @specialize.ll()
  948. def generic_cpy_call(space, func, *args):
  949. FT = lltype.typeOf(func).TO
  950. return make_generic_cpy_call(FT, True, False)(space, func, *args)
  951. @specialize.ll()
  952. def generic_cpy_call_dont_decref(space, func, *args):
  953. FT = lltype.typeOf(func).TO
  954. return make_generic_cpy_call(FT, False, False)(space, func, *args)
  955. @specialize.ll()
  956. def generic_cpy_call_expect_null(space, func, *args):
  957. FT = lltype.typeOf(func).TO
  958. return make_generic_cpy_call(FT, True, True)(space, func, *args)
  959. @specialize.memo()
  960. def make_generic_cpy_call(FT, decref_args, expect_null):
  961. from pypy.module.cpyext.pyobject import make_ref, from_ref, Py_DecRef
  962. from pypy.module.cpyext.pyobject import RefcountState
  963. from pypy.module.cpyext.pyerrors import PyErr_Occurred
  964. unrolling_arg_types = unrolling_iterable(enumerate(FT.ARGS))
  965. RESULT_TYPE = FT.RESULT
  966. # copied and modified from rffi.py
  967. # We need tons of care to ensure that no GC operation and no
  968. # exception checking occurs in call_external_function.
  969. argnames = ', '.join(['a%d' % i for i in range(len(FT.ARGS))])
  970. source = py.code.Source("""
  971. def cpy_call_external(funcptr, %(argnames)s):
  972. # NB. it is essential that no exception checking occurs here!
  973. res = funcptr(%(argnames)s)
  974. return res
  975. """ % locals())
  976. miniglobals = {'__name__': __name__, # for module name propagation
  977. }
  978. exec source.compile() in miniglobals
  979. call_external_function = miniglobals['cpy_call_external']
  980. call_external_function._dont_inline_ = True
  981. call_external_function._annspecialcase_ = 'specialize:ll'
  982. call_external_function._gctransformer_hint_close_stack_ = True
  983. # don't inline, as a hack to guarantee that no GC pointer is alive
  984. # anywhere in call_external_function
  985. @specialize.ll()
  986. def generic_cpy_call(space, func, *args):
  987. boxed_args = ()
  988. to_decref = []
  989. assert len(args) == len(FT.ARGS)
  990. for i, ARG in unrolling_arg_types:
  991. arg = args[i]
  992. if is_PyObject(ARG):
  993. if arg is None:
  994. boxed_args += (lltype.nullptr(PyObject.TO),)
  995. elif isinstance(arg, W_Root):
  996. ref = make_ref(space, arg)
  997. boxed_args += (ref,)
  998. if decref_args:
  999. to_decref.append(ref)
  1000. else:
  1001. boxed_args += (arg,)
  1002. else:
  1003. boxed_args += (arg,)
  1004. try:
  1005. # create a new container for borrowed references
  1006. state = space.fromcache(RefcountState)
  1007. old_container = state.swap_borrow_container(None)
  1008. try:
  1009. # Call the function
  1010. result = call_external_function(func, *boxed_args)
  1011. finally:
  1012. state.swap_borrow_container(old_container)
  1013. if is_PyObject(RESULT_TYPE):
  1014. if result is None:
  1015. ret = result
  1016. elif isinstance(result, W_Root):
  1017. ret = result
  1018. else:
  1019. ret = from_ref(space, result)
  1020. # The object reference returned from a C function
  1021. # that is called from Python must be an owned reference
  1022. # - ownership is transferred from the function to its caller.
  1023. if result:
  1024. Py_DecRef(space, result)
  1025. # Check for exception consistency
  1026. has_error = PyErr_Occurred(space) is not None
  1027. has_result = ret is not None
  1028. if has_error and has_result:
  1029. raise OperationError(space.w_SystemError, space.wrap(
  1030. "An exception was set, but function returned a value"))
  1031. elif not expect_null and not has_error and not has_result:
  1032. raise OperationError(space.w_SystemError, space.wrap(
  1033. "Function returned a NULL result without setting an exception"))
  1034. if has_error:
  1035. state = space.fromcache(State)
  1036. state.check_and_raise_exception()
  1037. return ret
  1038. return result
  1039. finally:
  1040. if decref_args:
  1041. for ref in to_decref:
  1042. Py_DecRef(space, ref)
  1043. return generic_cpy_call