| 1 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 2 |
from rpython.rlib.rarithmetic import widen |
| 3 |
from pypy.module.cpyext.api import ( |
| 4 |
METH_STATIC, METH_CLASS, METH_COEXIST, CANNOT_FAIL, CONST_STRING, |
| 5 |
METH_NOARGS, METH_O, METH_VARARGS, build_type_checkers, |
| 6 |
parse_dir, bootstrap_function, generic_cpy_call, cts, cpython_api, |
| 7 |
generic_cpy_call_dont_convert_result, slot_function, |
| 8 |
PYTHON_ABI_VERSION, PYTHON_API_VERSION) |
| 9 |
from pypy.module.cpyext.pyobject import (PyObject, as_pyobj, make_typedescr, |
| 10 |
keepalive_until_here, from_ref) |
| 11 |
from pypy.interpreter.module import Module |
| 12 |
from pypy.module.cpyext.methodobject import ( |
| 13 |
W_PyCFunctionObject, W_PyCMethodObject, |
| 14 |
PyMethodDef, W_PyCClassMethodObject, StaticMethod) |
| 15 |
from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall |
| 16 |
from pypy.module.cpyext.state import State |
| 17 |
from pypy.interpreter.error import oefmt |
| 18 |
|
| 19 |
cts.parse_header(parse_dir / 'cpyext_moduleobject.h') |
| 20 |
PyModuleDef = cts.gettype('PyModuleDef *') |
| 21 |
PyModuleObject = cts.gettype('PyModuleObject *') |
| 22 |
PyModuleDef_Slot = cts.gettype('PyModuleDef_Slot') |
| 23 |
|
| 24 |
@bootstrap_function |
| 25 |
def init_moduleobject(space): |
| 26 |
make_typedescr(Module.typedef, basestruct=PyModuleObject.TO, |
| 27 |
dealloc=module_dealloc) |
| 28 |
|
| 29 |
@slot_function([PyObject], lltype.Void) |
| 30 |
def module_dealloc(space, py_obj): |
| 31 |
py_module = rffi.cast(PyModuleObject, py_obj) |
| 32 |
if py_module.c_md_state: |
| 33 |
lltype.free(py_module.c_md_state, flavor='raw') |
| 34 |
from pypy.module.cpyext.object import _dealloc |
| 35 |
_dealloc(space, py_obj) |
| 36 |
|
| 37 |
PyModule_Check, PyModule_CheckExact = build_type_checkers("Module", Module) |
| 38 |
|
| 39 |
@cpython_api([CONST_STRING], PyObject) |
| 40 |
def PyModule_New(space, name): |
| 41 |
""" |
| 42 |
Return a new module object with the __name__ attribute set to name. |
| 43 |
Only the module's __doc__ and __name__ attributes are filled in; |
| 44 |
the caller is responsible for providing a __file__ attribute.""" |
| 45 |
return Module(space, space.newtext(rffi.charp2str(name))) |
| 46 |
|
| 47 |
@cpython_api([PyObject], PyObject) |
| 48 |
def PyModule_NewObject(space, w_name): |
| 49 |
""" |
| 50 |
Return a new module object with the __name__ attribute set to name. |
| 51 |
Only the module's __doc__ and __name__ attributes are filled in; |
| 52 |
the caller is responsible for providing a __file__ attribute.""" |
| 53 |
return Module(space, w_name) |
| 54 |
|
| 55 |
@cpython_api([PyModuleDef, rffi.INT_real], PyObject) |
| 56 |
def PyModule_Create2(space, module, api_version): |
| 57 |
"""Create a new module object, given the definition in module, assuming the |
| 58 |
API version module_api_version. If that version does not match the version |
| 59 |
of the running interpreter, a RuntimeWarning is emitted. |
| 60 |
|
| 61 |
Most uses of this function should be using PyModule_Create() |
| 62 |
instead; only use this if you are sure you need it.""" |
| 63 |
|
| 64 |
modname = rffi.charp2str(rffi.cast(rffi.CCHARP, module.c_m_name)) |
| 65 |
if module.c_m_doc: |
| 66 |
doc = rffi.charp2str(rffi.cast(rffi.CCHARP, module.c_m_doc)) |
| 67 |
else: |
| 68 |
doc = None |
| 69 |
methods = module.c_m_methods |
| 70 |
|
| 71 |
state = space.fromcache(State) |
| 72 |
f_name, f_path = state.package_context |
| 73 |
if f_name is not None: |
| 74 |
modname = f_name |
| 75 |
w_mod = Module(space, space.newtext(modname)) |
| 76 |
py_mod = rffi.cast(PyModuleObject, as_pyobj(space, w_mod)) |
| 77 |
py_mod.c_md_def = module |
| 78 |
state.package_context = None, None |
| 79 |
|
| 80 |
if f_path is not None: |
| 81 |
dict_w = {'__file__': space.newfilename(f_path)} |
| 82 |
else: |
| 83 |
dict_w = {} |
| 84 |
convert_method_defs(space, dict_w, methods, None, w_mod, modname) |
| 85 |
for key, w_value in dict_w.items(): |
| 86 |
space.setattr(w_mod, space.newtext(key), w_value) |
| 87 |
if doc: |
| 88 |
space.setattr(w_mod, space.newtext("__doc__"), |
| 89 |
space.newtext(doc)) |
| 90 |
|
| 91 |
if module.c_m_size > 0: |
| 92 |
py_mod.c_md_state = lltype.malloc(rffi.VOIDP.TO, module.c_m_size, |
| 93 |
flavor='raw', zero=True) |
| 94 |
return w_mod |
| 95 |
|
| 96 |
|
| 97 |
createfunctype = lltype.Ptr(lltype.FuncType([PyObject, PyModuleDef], PyObject)) |
| 98 |
execfunctype = lltype.Ptr(lltype.FuncType([PyObject], rffi.INT_real)) |
| 99 |
|
| 100 |
Py_mod_exec = 2 |
| 101 |
|
| 102 |
def create_module_from_def_and_spec(space, moddef, w_spec, name): |
| 103 |
moddef = rffi.cast(PyModuleDef, moddef) |
| 104 |
if moddef.c_m_size < 0: |
| 105 |
raise oefmt(space.w_SystemError, |
| 106 |
"module %s: m_size may not be negative for multi-phase " |
| 107 |
"initialization", name) |
| 108 |
createf = lltype.nullptr(rffi.VOIDP.TO) |
| 109 |
has_execution_slots = False |
| 110 |
cur_slot = rffi.cast(rffi.CArrayPtr(PyModuleDef_Slot), moddef.c_m_slots) |
| 111 |
if cur_slot: |
| 112 |
while True: |
| 113 |
slot = rffi.cast(lltype.Signed, cur_slot[0].c_slot) |
| 114 |
if slot == 0: |
| 115 |
break |
| 116 |
elif slot == 1: |
| 117 |
if createf: |
| 118 |
raise oefmt(space.w_SystemError, |
| 119 |
"module %s has multiple create slots", name) |
| 120 |
createf = cur_slot[0].c_value |
| 121 |
elif slot == Py_mod_exec: |
| 122 |
has_execution_slots = True |
| 123 |
else: |
| 124 |
raise oefmt(space.w_SystemError, |
| 125 |
"module %s uses unknown slot ID %d", name, slot) |
| 126 |
cur_slot = rffi.ptradd(cur_slot, 1) |
| 127 |
if createf: |
| 128 |
createf = rffi.cast(createfunctype, createf) |
| 129 |
w_mod = generic_cpy_call(space, createf, w_spec, moddef) |
| 130 |
else: |
| 131 |
w_mod = Module(space, space.newtext(name)) |
| 132 |
if isinstance(w_mod, Module): |
| 133 |
mod = rffi.cast(PyModuleObject, as_pyobj(space, w_mod)) |
| 134 |
|
| 135 |
mod.c_md_def = moddef |
| 136 |
else: |
| 137 |
if moddef.c_m_size > 0 or moddef.c_m_traverse or moddef.c_m_clear or \ |
| 138 |
moddef.c_m_free: |
| 139 |
raise oefmt(space.w_SystemError, |
| 140 |
"module %s is not a module object, but requests " |
| 141 |
"module state", name) |
| 142 |
if has_execution_slots: |
| 143 |
raise oefmt(space.w_SystemError, |
| 144 |
"module %s specifies execution slots, but did not " |
| 145 |
"create a ModuleType instance", name) |
| 146 |
dict_w = {} |
| 147 |
convert_method_defs(space, dict_w, moddef.c_m_methods, None, w_mod, name) |
| 148 |
for key, w_value in dict_w.items(): |
| 149 |
space.setattr(w_mod, space.newtext(key), w_value) |
| 150 |
if moddef.c_m_doc: |
| 151 |
doc = rffi.charp2str(rffi.cast(rffi.CCHARP, moddef.c_m_doc)) |
| 152 |
space.setattr(w_mod, space.newtext('__doc__'), space.newtext(doc)) |
| 153 |
return w_mod |
| 154 |
|
| 155 |
|
| 156 |
def exec_def(space, mod, moddef): |
| 157 |
from pypy.module.cpyext.pyerrors import PyErr_Occurred |
| 158 |
cur_slot = rffi.cast(rffi.CArrayPtr(PyModuleDef_Slot), moddef.c_m_slots) |
| 159 |
if moddef.c_m_size >= 0 and not mod.c_md_state: |
| 160 |
|
| 161 |
|
| 162 |
mod.c_md_state = lltype.malloc( |
| 163 |
rffi.VOIDP.TO, moddef.c_m_size, flavor='raw', zero=True) |
| 164 |
pyobj = rffi.cast(PyObject, mod) |
| 165 |
while cur_slot and rffi.cast(lltype.Signed, cur_slot[0].c_slot): |
| 166 |
if rffi.cast(lltype.Signed, cur_slot[0].c_slot) == Py_mod_exec: |
| 167 |
execf = rffi.cast(execfunctype, cur_slot[0].c_value) |
| 168 |
res = generic_cpy_call_dont_convert_result(space, execf, pyobj) |
| 169 |
state = space.fromcache(State) |
| 170 |
if rffi.cast(lltype.Signed, res): |
| 171 |
state.check_and_raise_exception() |
| 172 |
raise oefmt(space.w_SystemError, |
| 173 |
"execution of module %s failed without setting an " |
| 174 |
"exception", rffi.constcharp2str(moddef.c_m_name)) |
| 175 |
else: |
| 176 |
if state.clear_exception(): |
| 177 |
raise oefmt(space.w_SystemError, |
| 178 |
"execution of module %s raised unreported " |
| 179 |
"exception", rffi.constcharp2str(moddef.c_m_name)) |
| 180 |
cur_slot = rffi.ptradd(cur_slot, 1) |
| 181 |
|
| 182 |
def convert_method_defs(space, dict_w, methods, w_type, w_self=None, name=None, type_name=None): |
| 183 |
w_name = space.newtext_or_none(name) |
| 184 |
methods = rffi.cast(rffi.CArrayPtr(PyMethodDef), methods) |
| 185 |
if methods: |
| 186 |
i = -1 |
| 187 |
while True: |
| 188 |
i = i + 1 |
| 189 |
method = methods[i] |
| 190 |
if not method.c_ml_name: break |
| 191 |
|
| 192 |
methodname = rffi.charp2str(rffi.cast(rffi.CCHARP, method.c_ml_name)) |
| 193 |
flags = rffi.cast(lltype.Signed, method.c_ml_flags) |
| 194 |
|
| 195 |
if w_type is None: |
| 196 |
if flags & METH_CLASS or flags & METH_STATIC: |
| 197 |
raise oefmt(space.w_ValueError, |
| 198 |
"module functions cannot set METH_CLASS or " |
| 199 |
"METH_STATIC") |
| 200 |
w_obj = W_PyCFunctionObject(space, method, w_self, w_name) |
| 201 |
else: |
| 202 |
if methodname in dict_w and not (flags & METH_COEXIST): |
| 203 |
continue |
| 204 |
if flags & METH_CLASS: |
| 205 |
if flags & METH_STATIC: |
| 206 |
raise oefmt(space.w_ValueError, |
| 207 |
"method cannot be both class and static") |
| 208 |
w_obj = W_PyCClassMethodObject(space, method, w_type, type_name) |
| 209 |
elif flags & METH_STATIC: |
| 210 |
w_func = W_PyCFunctionObject(space, method, None, None, type_name) |
| 211 |
w_obj = StaticMethod(w_func) |
| 212 |
else: |
| 213 |
w_obj = W_PyCMethodObject(space, method, None, None, w_type, type_name) |
| 214 |
|
| 215 |
dict_w[methodname] = w_obj |
| 216 |
|
| 217 |
|
| 218 |
@cpython_api([PyObject], PyObject, result_borrowed=True) |
| 219 |
def PyModule_GetDict(space, w_mod): |
| 220 |
if PyModule_Check(space, w_mod): |
| 221 |
assert isinstance(w_mod, Module) |
| 222 |
w_dict = w_mod.getdict(space) |
| 223 |
return w_dict |
| 224 |
else: |
| 225 |
PyErr_BadInternalCall(space) |
| 226 |
|
| 227 |
@cpython_api([PyObject], rffi.CCHARP) |
| 228 |
def PyModule_GetName(space, w_mod): |
| 229 |
""" |
| 230 |
Return module's __name__ value. If the module does not provide one, |
| 231 |
or if it is not a string, SystemError is raised and NULL is returned. |
| 232 |
""" |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
if not isinstance(w_mod, Module): |
| 238 |
raise oefmt(space.w_SystemError, "PyModule_GetName(): not a module") |
| 239 |
from pypy.module.cpyext.unicodeobject import PyUnicode_AsUTF8 |
| 240 |
return PyUnicode_AsUTF8(space, as_pyobj(space, w_mod.w_name)) |
| 241 |
|
| 242 |
@cpython_api([PyObject], PyObject) |
| 243 |
def PyModule_GetFilenameObject(space, w_mod): |
| 244 |
""" |
| 245 |
Return the name of the file from which module was loaded using module's |
| 246 |
__file__ attribute. If this is not defined, or if it is not a |
| 247 |
unicode string, raise SystemError and return NULL; otherwise return |
| 248 |
a reference to a PyUnicodeObject. |
| 249 |
""" |
| 250 |
if not isinstance(w_mod, Module): |
| 251 |
raise oefmt(space.w_SystemError, "PyModule_GetFilenameObject(): not a module") |
| 252 |
|
| 253 |
w_dict = w_mod.getdict(space) |
| 254 |
return space.getitem(w_dict, space.newtext("__file__")) |
| 255 |
|
| 256 |
@cpython_api([PyObject], PyObject) |
| 257 |
def PyModule_GetNameObject(space, w_mod): |
| 258 |
""" |
| 259 |
Return the name of module |
| 260 |
""" |
| 261 |
if not isinstance(w_mod, Module): |
| 262 |
raise oefmt(space.w_SystemError, "PyModule_GetNameObject(): not a module") |
| 263 |
return w_mod.w_name |
| 264 |
|
| 265 |
@cpython_api([PyObject, lltype.Ptr(PyMethodDef)], rffi.INT_real, error=-1) |
| 266 |
def PyModule_AddFunctions(space, w_mod, methods): |
| 267 |
if not isinstance(w_mod, Module): |
| 268 |
raise oefmt(space.w_SystemError, "PyModule_AddFuntions(): not a module") |
| 269 |
name = space.utf8_w(w_mod.w_name) |
| 270 |
dict_w = {} |
| 271 |
convert_method_defs(space, dict_w, methods, None, w_mod, name=name) |
| 272 |
for key, w_value in dict_w.items(): |
| 273 |
space.setattr(w_mod, space.newtext(key), w_value) |
| 274 |
return 0 |
| 275 |
|
| 276 |
@cpython_api([PyObject, PyModuleDef], rffi.INT_real, error=-1) |
| 277 |
def PyModule_ExecDef(space, w_mod, c_def): |
| 278 |
if not isinstance(w_mod, Module): |
| 279 |
raise oefmt(space.w_SystemError, "PyModule_ExecDef(): not a module") |
| 280 |
py_mod = rffi.cast(PyModuleObject, as_pyobj(space, w_mod)) |
| 281 |
exec_def(space, py_mod, c_def) |
| 282 |
keepalive_until_here(w_mod) |
| 283 |
return 0 |
| 284 |
|
| 285 |
@cts.decl("PyObject *PyModule_FromDefAndSpec2(PyModuleDef*, PyObject*, int)") |
| 286 |
def PyModule_FromDefAndSpec2(space, moddef, spec, module_api_version): |
| 287 |
w_spec = space.w_None |
| 288 |
if spec: |
| 289 |
w_spec = from_ref(space, spec) |
| 290 |
name = space.text_w(space.getattr(w_spec, space.newtext("name"))) |
| 291 |
module_api_version = widen(module_api_version) |
| 292 |
if module_api_version != PYTHON_API_VERSION and module_api_version != PYTHON_ABI_VERSION: |
| 293 |
msg = ("Python C API version mismatch for module %s: " |
| 294 |
"This Python has API version %d, module %s has version %d." % |
| 295 |
(name[:100], |
| 296 |
PYTHON_API_VERSION, name[:100], module_api_version)) |
| 297 |
space.warn(space.newtext(msg), space.w_RuntimeWarning) |
| 298 |
return create_module_from_def_and_spec(space, moddef, w_spec, name) |
| 299 |
|
| 300 |
@cts.decl("PyObject *PyModule_FromDefAndSpec(PyModuleDef*, PyObject*)") |
| 301 |
def PyModule_FromDefAndSpec(space, moddef, spec): |
| 302 |
w_spec = space.w_None |
| 303 |
if spec: |
| 304 |
w_spec = from_ref(space, spec) |
| 305 |
name = space.text_w(space.getattr(w_spec, space.newtext("name"))) |
| 306 |
return create_module_from_def_and_spec(space, moddef, w_spec, name) |