# HG changeset patch
# User Haoyu Bai <baihaoyu@gmail.com>
# Date 1273834582 -28800
# Branch T422
# Node ID f9703a7c50c3a2857038cefb4939f2405fccefd9
# Parent 92d1eb8aa41e99950ce8bbb070af69e18a85df2f
Fix T422 by making module name as a StringConst
diff -r 92d1eb8aa41e -r f9703a7c50c3 Cython/Compiler/ExprNodes.py
|
a
|
b
|
|
| 4207 | 4207 | if self.doc: |
| 4208 | 4208 | self.doc.analyse_types(env) |
| 4209 | 4209 | self.doc = self.doc.coerce_to_pyobject(env) |
| 4210 | | self.module_name = env.global_scope().qualified_name |
| | 4210 | self.module_name = env.global_scope().module_name |
| 4211 | 4211 | self.type = py_object_type |
| 4212 | 4212 | self.is_temp = 1 |
| 4213 | 4213 | env.use_utility_code(create_class_utility_code); |
| … |
… |
|
| 4224 | 4224 | 'PyDict_SetItemString(%s, "__doc__", %s)' % ( |
| 4225 | 4225 | self.dict.py_result(), |
| 4226 | 4226 | self.doc.py_result())) |
| | 4227 | py_mod_name = code.globalstate.get_py_string_const( |
| | 4228 | self.module_name, identifier=True) |
| 4227 | 4229 | code.putln( |
| 4228 | | '%s = __Pyx_CreateClass(%s, %s, %s, "%s"); %s' % ( |
| | 4230 | '%s = __Pyx_CreateClass(%s, %s, %s, %s); %s' % ( |
| 4229 | 4231 | self.result(), |
| 4230 | 4232 | self.bases.py_result(), |
| 4231 | 4233 | self.dict.py_result(), |
| 4232 | 4234 | cname, |
| 4233 | | self.module_name, |
| | 4235 | py_mod_name.cname, |
| 4234 | 4236 | code.error_goto_if_null(self.result(), self.pos))) |
| 4235 | 4237 | code.put_gotref(self.py_result()) |
| 4236 | 4238 | |
| … |
… |
|
| 4300 | 4302 | # pymethdef_cname string PyMethodDef structure |
| 4301 | 4303 | # self_object ExprNode or None |
| 4302 | 4304 | # binding bool |
| | 4305 | # module_name str |
| 4303 | 4306 | |
| 4304 | 4307 | subexprs = [] |
| 4305 | 4308 | self_object = None |
| … |
… |
|
| 4311 | 4314 | def analyse_types(self, env): |
| 4312 | 4315 | if self.binding: |
| 4313 | 4316 | env.use_utility_code(binding_cfunc_utility_code) |
| | 4317 | self.module_name = env.global_scope().module_name |
| 4314 | 4318 | |
| 4315 | 4319 | def may_be_none(self): |
| 4316 | 4320 | return False |
| … |
… |
|
| 4326 | 4330 | |
| 4327 | 4331 | def generate_result_code(self, code): |
| 4328 | 4332 | if self.binding: |
| 4329 | | constructor = "%s_New" % Naming.binding_cfunc |
| | 4333 | constructor = "%s_NewEx" % Naming.binding_cfunc |
| 4330 | 4334 | else: |
| 4331 | | constructor = "PyCFunction_New" |
| | 4335 | constructor = "PyCFunction_NewEx" |
| | 4336 | py_mod_name = code.globalstate.get_py_string_const( |
| | 4337 | self.module_name, identifier=True) |
| 4332 | 4338 | code.putln( |
| 4333 | | "%s = %s(&%s, %s); %s" % ( |
| | 4339 | '%s = %s(&%s, %s, %s); %s' % ( |
| 4334 | 4340 | self.result(), |
| 4335 | 4341 | constructor, |
| 4336 | 4342 | self.pymethdef_cname, |
| 4337 | 4343 | self.self_result_code(), |
| | 4344 | py_mod_name.cname, |
| 4338 | 4345 | code.error_goto_if_null(self.result(), self.pos))) |
| 4339 | 4346 | code.put_gotref(self.py_result()) |
| 4340 | 4347 | |
| … |
… |
|
| 6790 | 6797 | |
| 6791 | 6798 | create_class_utility_code = UtilityCode( |
| 6792 | 6799 | proto = """ |
| 6793 | | static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name, const char *modname); /*proto*/ |
| | 6800 | static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name, PyObject *modname); /*proto*/ |
| 6794 | 6801 | """, |
| 6795 | 6802 | impl = """ |
| 6796 | 6803 | static PyObject *__Pyx_CreateClass( |
| 6797 | | PyObject *bases, PyObject *dict, PyObject *name, const char *modname) |
| | 6804 | PyObject *bases, PyObject *dict, PyObject *name, PyObject *modname) |
| 6798 | 6805 | { |
| 6799 | | PyObject *py_modname; |
| 6800 | 6806 | PyObject *result = 0; |
| 6801 | 6807 | |
| 6802 | | #if PY_MAJOR_VERSION < 3 |
| 6803 | | py_modname = PyString_FromString(modname); |
| 6804 | | #else |
| 6805 | | py_modname = PyUnicode_FromString(modname); |
| 6806 | | #endif |
| 6807 | | if (!py_modname) |
| 6808 | | goto bad; |
| 6809 | | if (PyDict_SetItemString(dict, "__module__", py_modname) < 0) |
| | 6808 | if (PyDict_SetItemString(dict, "__module__", modname) < 0) |
| 6810 | 6809 | goto bad; |
| 6811 | 6810 | #if PY_MAJOR_VERSION < 3 |
| 6812 | 6811 | result = PyClass_New(bases, dict, name); |
| … |
… |
|
| 6814 | 6813 | result = PyObject_CallFunctionObjArgs((PyObject *)&PyType_Type, name, bases, dict, NULL); |
| 6815 | 6814 | #endif |
| 6816 | 6815 | bad: |
| 6817 | | Py_XDECREF(py_modname); |
| 6818 | 6816 | return result; |
| 6819 | 6817 | } |
| 6820 | 6818 | """) |
diff -r 92d1eb8aa41e -r f9703a7c50c3 Cython/Compiler/Symtab.py
|
a
|
b
|
|
| 800 | 800 | # Treat Spam/__init__.pyx specially, so that when Python loads |
| 801 | 801 | # Spam/__init__.so, initSpam() is defined. |
| 802 | 802 | self.module_name = parent_module.module_name |
| | 803 | self.module_name = EncodedString(self.module_name) |
| 803 | 804 | self.context = context |
| 804 | 805 | self.module_cname = Naming.module_cname |
| 805 | 806 | self.module_dict_cname = Naming.moddict_cname |
diff -r 92d1eb8aa41e -r f9703a7c50c3 tests/run/method_module_name_T422.pyx
|
a
|
b
|
|
| | 1 | """ |
| | 2 | >>> Foo.incr.__module__ is not None |
| | 3 | True |
| | 4 | >>> Foo.incr.__module__ == Foo.__module__ == bar.__module__ |
| | 5 | True |
| | 6 | |
| | 7 | """ |
| | 8 | class Foo(object): |
| | 9 | def incr(self,x): |
| | 10 | return x+1 |
| | 11 | |
| | 12 | def bar(): |
| | 13 | pass |