PageRenderTime 69ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/cpyext/api.py

https://bitbucket.org/vanl/pypy
Python | 1300 lines | 1231 code | 48 blank | 21 comment | 68 complexity | 5072dc8a9109e1e755ba7e584dac53eb MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, AGPL-3.0

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file