PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/module/cpyext/modsupport.py

https://github.com/ssadler/pypy
Python | 128 lines | 123 code | 2 blank | 3 comment | 8 complexity | 47cdaa76764b2723c961d95d4b996fd8 MD5 | raw file
  1. from pypy.rpython.lltypesystem import rffi, lltype
  2. from pypy.module.cpyext.api import cpython_api, cpython_struct, \
  3. METH_STATIC, METH_CLASS, METH_COEXIST, CANNOT_FAIL, CONST_STRING
  4. from pypy.module.cpyext.pyobject import PyObject, borrow_from
  5. from pypy.interpreter.module import Module
  6. from pypy.module.cpyext.methodobject import (
  7. W_PyCFunctionObject, PyCFunction_NewEx, PyDescr_NewMethod,
  8. PyMethodDef, PyStaticMethod_New)
  9. from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall
  10. from pypy.module.cpyext.state import State
  11. from pypy.interpreter.error import OperationError
  12. #@cpython_api([rffi.CCHARP], PyObject)
  13. def PyImport_AddModule(space, name):
  14. """Return the module object corresponding to a module name. The name argument
  15. may be of the form package.module. First check the modules dictionary if
  16. there's one there, and if not, create a new one and insert it in the modules
  17. dictionary.
  18. This function does not load or import the module; if the module wasn't already
  19. loaded, you will get an empty module object. Use PyImport_ImportModule()
  20. or one of its variants to import a module. Package structures implied by a
  21. dotted name for name are not created if not already present."""
  22. w_name = space.wrap(name)
  23. w_modules = space.sys.get('modules')
  24. w_mod = space.finditem_str(w_modules, name)
  25. if w_mod is None:
  26. w_mod = space.wrap(Module(space, w_name))
  27. space.setitem(w_modules, w_name, w_mod)
  28. return w_mod
  29. # This is actually the Py_InitModule4 function,
  30. # renamed to refuse modules built against CPython headers.
  31. @cpython_api([CONST_STRING, lltype.Ptr(PyMethodDef), CONST_STRING,
  32. PyObject, rffi.INT_real], PyObject)
  33. def _Py_InitPyPyModule(space, name, methods, doc, w_self, apiver):
  34. """
  35. Create a new module object based on a name and table of functions, returning
  36. the new module object. If doc is non-NULL, it will be used to define the
  37. docstring for the module. If self is non-NULL, it will passed to the
  38. functions of the module as their (otherwise NULL) first parameter. (This was
  39. added as an experimental feature, and there are no known uses in the current
  40. version of Python.) For apiver, the only value which should be passed is
  41. defined by the constant PYTHON_API_VERSION.
  42. Note that the name parameter is actually ignored, and the module name is
  43. taken from the package_context attribute of the cpyext.State in the space
  44. cache. CPython includes some extra checking here to make sure the module
  45. being initialized lines up with what's expected, but we don't.
  46. """
  47. from pypy.module.cpyext.typeobjectdefs import PyTypeObjectPtr
  48. modname = rffi.charp2str(name)
  49. state = space.fromcache(State)
  50. f_name, f_path = state.package_context
  51. w_mod = PyImport_AddModule(space, f_name)
  52. dict_w = {'__file__': space.wrap(f_path)}
  53. convert_method_defs(space, dict_w, methods, None, w_self, modname)
  54. for key, w_value in dict_w.items():
  55. space.setattr(w_mod, space.wrap(key), w_value)
  56. if doc:
  57. space.setattr(w_mod, space.wrap("__doc__"),
  58. space.wrap(rffi.charp2str(doc)))
  59. return borrow_from(None, w_mod)
  60. def convert_method_defs(space, dict_w, methods, w_type, w_self=None, name=None):
  61. w_name = space.wrap(name)
  62. methods = rffi.cast(rffi.CArrayPtr(PyMethodDef), methods)
  63. if methods:
  64. i = -1
  65. while True:
  66. i = i + 1
  67. method = methods[i]
  68. if not method.c_ml_name: break
  69. methodname = rffi.charp2str(method.c_ml_name)
  70. flags = rffi.cast(lltype.Signed, method.c_ml_flags)
  71. if w_type is None:
  72. if flags & METH_CLASS or flags & METH_STATIC:
  73. raise OperationError(space.w_ValueError,
  74. space.wrap("module functions cannot set METH_CLASS or METH_STATIC"))
  75. w_obj = space.wrap(W_PyCFunctionObject(space, method, w_self, w_name))
  76. else:
  77. if methodname in dict_w and not (flags & METH_COEXIST):
  78. continue
  79. if flags & METH_CLASS:
  80. if flags & METH_STATIC:
  81. raise OperationError(space.w_ValueError,
  82. space.wrap("method cannot be both class and static"))
  83. #w_obj = PyDescr_NewClassMethod(space, w_type, method)
  84. w_obj = space.w_Ellipsis # XXX
  85. elif flags & METH_STATIC:
  86. w_func = PyCFunction_NewEx(space, method, None, None)
  87. w_obj = PyStaticMethod_New(space, w_func)
  88. else:
  89. w_obj = PyDescr_NewMethod(space, w_type, method)
  90. dict_w[methodname] = w_obj
  91. @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
  92. def PyModule_Check(space, w_obj):
  93. w_type = space.gettypeobject(Module.typedef)
  94. w_obj_type = space.type(w_obj)
  95. return int(space.is_w(w_type, w_obj_type) or
  96. space.is_true(space.issubtype(w_obj_type, w_type)))
  97. @cpython_api([PyObject], PyObject)
  98. def PyModule_GetDict(space, w_mod):
  99. if PyModule_Check(space, w_mod):
  100. assert isinstance(w_mod, Module)
  101. w_dict = w_mod.getdict(space)
  102. return borrow_from(w_mod, w_dict)
  103. else:
  104. PyErr_BadInternalCall(space)
  105. @cpython_api([PyObject], rffi.CCHARP, error=0)
  106. def PyModule_GetName(space, module):
  107. """
  108. Return module's __name__ value. If the module does not provide one,
  109. or if it is not a string, SystemError is raised and NULL is returned."""
  110. raise NotImplementedError