PageRenderTime 67ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/module/cpyext/api.py

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

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