Author: Armin Rigo <ar...@tunes.org> Branch: Changeset: r88603:bb6863a1903e Date: 2016-11-24 10:01 +0100 http://bitbucket.org/pypy/pypy/changeset/bb6863a1903e/
Log: Write PyModule_GetName() based on PyString_AsString() diff --git a/pypy/module/cpyext/modsupport.py b/pypy/module/cpyext/modsupport.py --- a/pypy/module/cpyext/modsupport.py +++ b/pypy/module/cpyext/modsupport.py @@ -1,7 +1,7 @@ from rpython.rtyper.lltypesystem import rffi, lltype from pypy.module.cpyext.api import cpython_api, cpython_struct, \ METH_STATIC, METH_CLASS, METH_COEXIST, CANNOT_FAIL, CONST_STRING -from pypy.module.cpyext.pyobject import PyObject +from pypy.module.cpyext.pyobject import PyObject, as_pyobj from pypy.interpreter.module import Module from pypy.module.cpyext.methodobject import ( W_PyCFunctionObject, PyCFunction_NewEx, PyDescr_NewMethod, @@ -124,11 +124,17 @@ else: PyErr_BadInternalCall(space) -@cpython_api([PyObject], rffi.CCHARP, error=0) -def PyModule_GetName(space, module): +@cpython_api([PyObject], rffi.CCHARP) +def PyModule_GetName(space, w_mod): """ Return module's __name__ value. If the module does not provide one, - or if it is not a string, SystemError is raised and NULL is returned.""" - raise NotImplementedError - - + or if it is not a string, SystemError is raised and NULL is returned. + """ + # NOTE: this version of the code works only because w_mod.w_name is + # a wrapped string object attached to w_mod; so it makes a + # PyStringObject that will live as long as the module itself, + # and returns a "char *" inside this PyStringObject. + if not isinstance(w_mod, Module): + raise oefmt(space.w_SystemError, "PyModule_GetName(): not a module") + from pypy.module.cpyext.bytesobject import PyString_AsString + return PyString_AsString(space, as_pyobj(space, w_mod.w_name)) diff --git a/pypy/module/cpyext/test/test_module.py b/pypy/module/cpyext/test/test_module.py new file mode 100644 --- /dev/null +++ b/pypy/module/cpyext/test/test_module.py @@ -0,0 +1,12 @@ +from pypy.module.cpyext.test.test_api import BaseApiTest +from rpython.rtyper.lltypesystem import rffi + + +class TestModuleObject(BaseApiTest): + def test_module_getname(self, space, api): + w_sys = space.wrap(space.sys) + p = api.PyModule_GetName(w_sys) + assert rffi.charp2str(p) == 'sys' + p2 = api.PyModule_GetName(w_sys) + assert p2 == p + self.raises(space, api, SystemError, api.PyModule_GetName, space.w_True) _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit