PageRenderTime 54ms CodeModel.GetById 35ms app.highlight 17ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/cpyext/modsupport.py

https://github.com/alemacgo/pypy
Python | 128 lines | 123 code | 2 blank | 3 comment | 8 complexity | 47cdaa76764b2723c961d95d4b996fd8 MD5 | raw file
  1from pypy.rpython.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, borrow_from
  5from pypy.interpreter.module import Module
  6from pypy.module.cpyext.methodobject import (
  7    W_PyCFunctionObject, PyCFunction_NewEx, PyDescr_NewMethod,
  8    PyMethodDef, PyStaticMethod_New)
  9from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall
 10from pypy.module.cpyext.state import State
 11from pypy.interpreter.error import OperationError
 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)
 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    w_mod = PyImport_AddModule(space, f_name)
 58
 59    dict_w = {'__file__': space.wrap(f_path)}
 60    convert_method_defs(space, dict_w, methods, None, w_self, modname)
 61    for key, w_value in dict_w.items():
 62        space.setattr(w_mod, space.wrap(key), w_value)
 63    if doc:
 64        space.setattr(w_mod, space.wrap("__doc__"),
 65                      space.wrap(rffi.charp2str(doc)))
 66    return borrow_from(None, w_mod)
 67
 68
 69def convert_method_defs(space, dict_w, methods, w_type, w_self=None, name=None):
 70    w_name = space.wrap(name)
 71    methods = rffi.cast(rffi.CArrayPtr(PyMethodDef), methods)
 72    if methods:
 73        i = -1
 74        while True:
 75            i = i + 1
 76            method = methods[i]
 77            if not method.c_ml_name: break
 78
 79            methodname = rffi.charp2str(method.c_ml_name)
 80            flags = rffi.cast(lltype.Signed, method.c_ml_flags)
 81
 82            if w_type is None:
 83                if flags & METH_CLASS or flags & METH_STATIC:
 84                    raise OperationError(space.w_ValueError,
 85                            space.wrap("module functions cannot set METH_CLASS or METH_STATIC"))
 86                w_obj = space.wrap(W_PyCFunctionObject(space, method, w_self, w_name))
 87            else:
 88                if methodname in dict_w and not (flags & METH_COEXIST):
 89                    continue
 90                if flags & METH_CLASS:
 91                    if flags & METH_STATIC:
 92                        raise OperationError(space.w_ValueError,
 93                                space.wrap("method cannot be both class and static"))
 94                    #w_obj = PyDescr_NewClassMethod(space, w_type, method)
 95                    w_obj = space.w_Ellipsis # XXX
 96                elif flags & METH_STATIC:
 97                    w_func = PyCFunction_NewEx(space, method, None, None)
 98                    w_obj = PyStaticMethod_New(space, w_func)
 99                else:
100                    w_obj = PyDescr_NewMethod(space, w_type, method)
101
102            dict_w[methodname] = w_obj
103
104
105@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
106def PyModule_Check(space, w_obj):
107    w_type = space.gettypeobject(Module.typedef)
108    w_obj_type = space.type(w_obj)
109    return int(space.is_w(w_type, w_obj_type) or
110               space.is_true(space.issubtype(w_obj_type, w_type)))
111
112@cpython_api([PyObject], PyObject)
113def PyModule_GetDict(space, w_mod):
114    if PyModule_Check(space, w_mod):
115        assert isinstance(w_mod, Module)
116        w_dict = w_mod.getdict(space)
117        return borrow_from(w_mod, w_dict)
118    else:
119        PyErr_BadInternalCall(space)
120
121@cpython_api([PyObject], rffi.CCHARP, error=0)
122def PyModule_GetName(space, module):
123    """
124    Return module's __name__ value.  If the module does not provide one,
125    or if it is not a string, SystemError is raised and NULL is returned."""
126    raise NotImplementedError
127
128