PageRenderTime 35ms CodeModel.GetById 21ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/module/cpyext/modsupport.py

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