From: <zy...@us...> - 2008-12-19 06:00:50
|
Revision: 5779 http://jython.svn.sourceforge.net/jython/?rev=5779&view=rev Author: zyasoft Date: 2008-12-19 06:00:48 +0000 (Fri, 19 Dec 2008) Log Message: ----------- Enabled ClassDictInit modules to set their __name__ so that it matches the name part of name:class in org.python.modules.Setup (or equivalently registry setting python.modules.builtin). This fixes introspection issues where __import__(name).__name__ != name. Modified Paths: -------------- trunk/jython/Lib/test/test_builtin_jy.py trunk/jython/src/org/python/core/PyJavaType.java trunk/jython/src/org/python/modules/_newmodule.java trunk/jython/src/org/python/modules/_weakref/WeakrefModule.java trunk/jython/src/org/python/modules/random/RandomModule.java trunk/jython/src/org/python/modules/time/Time.java Modified: trunk/jython/Lib/test/test_builtin_jy.py =================================================================== --- trunk/jython/Lib/test/test_builtin_jy.py 2008-12-18 20:12:14 UTC (rev 5778) +++ trunk/jython/Lib/test/test_builtin_jy.py 2008-12-19 06:00:48 UTC (rev 5779) @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +1# -*- coding: utf-8 -*- import sys import unittest import test.test_support @@ -157,6 +157,18 @@ exec code in ns self.assertEqual(foo, ns['a']) +class ModuleNameTest(unittest.TestCase): + """Tests that the module when imported has the same __name__""" + + def test_names(self): + for name in sys.builtin_module_names: + if name != '_jython' and name not in ('time', '_random', 'array', '_collections', '_ast'): + module = __import__(name) + self.assertEqual(name, module.__name__) + + + + def test_main(): test.test_support.run_unittest(BuiltinTest, LoopTest, @@ -167,7 +179,9 @@ ReprTest, CallableTest, ConversionTest, - ExecEvalTest) + ExecEvalTest, + ModuleNameTest, + ) if __name__ == "__main__": test_main() Modified: trunk/jython/src/org/python/core/PyJavaType.java =================================================================== --- trunk/jython/src/org/python/core/PyJavaType.java 2008-12-18 20:12:14 UTC (rev 5778) +++ trunk/jython/src/org/python/core/PyJavaType.java 2008-12-19 06:00:48 UTC (rev 5779) @@ -281,6 +281,11 @@ try { Method m = underlying_class.getMethod("classDictInit", PyObject.class); m.invoke(null, dict); + // allow the class to override its name after it is loaded + PyObject nameSpecified = dict.__finditem__("__name__"); + if (nameSpecified != null) { + name = nameSpecified.toString(); + } } catch (Exception exc) { throw Py.JavaError(exc); } Modified: trunk/jython/src/org/python/modules/_newmodule.java =================================================================== --- trunk/jython/src/org/python/modules/_newmodule.java 2008-12-18 20:12:14 UTC (rev 5778) +++ trunk/jython/src/org/python/modules/_newmodule.java 2008-12-19 06:00:48 UTC (rev 5779) @@ -1,6 +1,7 @@ /* Copyright (c) 2001, 2003 Finn Bock, Samuele Pedroni */ package org.python.modules; +import org.python.core.ClassDictInit; import org.python.core.Py; import org.python.core.PyClass; import org.python.core.PyObject; @@ -11,8 +12,13 @@ * The internal new module; just provides a hack for new.classobj. * */ -public class _newmodule { +public class _newmodule implements ClassDictInit { + public static void classDictInit(PyObject dict) + { + dict.__setitem__("__name__", Py.newString("_new")); + } + public static PyObject classobj(String name, PyTuple bases, PyObject dict) { // XXX: Hack to return new style classes (originally from // r4225). types.ClassType (PyClass) should be doing this Modified: trunk/jython/src/org/python/modules/_weakref/WeakrefModule.java =================================================================== --- trunk/jython/src/org/python/modules/_weakref/WeakrefModule.java 2008-12-18 20:12:14 UTC (rev 5778) +++ trunk/jython/src/org/python/modules/_weakref/WeakrefModule.java 2008-12-19 06:00:48 UTC (rev 5779) @@ -20,6 +20,7 @@ public static void classDictInit(PyObject dict) { dict.__setitem__("__doc__", __doc__); + dict.__setitem__("__name__", Py.newString("_weakref")); dict.__setitem__("ref", ReferenceType.TYPE); dict.__setitem__("ReferenceType", ReferenceType.TYPE); dict.__setitem__("ProxyType", ProxyType.TYPE); Modified: trunk/jython/src/org/python/modules/random/RandomModule.java =================================================================== --- trunk/jython/src/org/python/modules/random/RandomModule.java 2008-12-18 20:12:14 UTC (rev 5778) +++ trunk/jython/src/org/python/modules/random/RandomModule.java 2008-12-19 06:00:48 UTC (rev 5779) @@ -9,6 +9,8 @@ private RandomModule() {} public static void classDictInit(PyObject dict) { + dict.invoke("clear"); dict.__setitem__("Random", PyRandom.TYPE); + dict.__setitem__("__name__", Py.newString("_random")); } } Modified: trunk/jython/src/org/python/modules/time/Time.java =================================================================== --- trunk/jython/src/org/python/modules/time/Time.java 2008-12-18 20:12:14 UTC (rev 5778) +++ trunk/jython/src/org/python/modules/time/Time.java 2008-12-19 06:00:48 UTC (rev 5779) @@ -112,6 +112,7 @@ dict.__setitem__("time", new TimeFunctions("time", 0, 0)); dict.__setitem__("clock", new TimeFunctions("clock", 1, 0)); dict.__setitem__("struct_time", PyTimeTuple.TYPE); + dict.__setitem__("__name__", Py.newString("time")); // calculate the static variables tzname, timezone, altzone, daylight TimeZone tz = TimeZone.getDefault(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |