From: <pj...@us...> - 2008-12-11 05:20:59
|
Revision: 5735 http://jython.svn.sourceforge.net/jython/?rev=5735&view=rev Author: pjenvey Date: 2008-12-11 05:20:55 +0000 (Thu, 11 Dec 2008) Log Message: ----------- o fix imp.find_module not finding builtin modules o add __builtin__ and sys to sys.builtin_module_names and another hack for re-importing __builtin__ like we have for sys fixes #1161 thanks Sven Reimers Modified Paths: -------------- trunk/jython/Lib/test/test_import_jy.py trunk/jython/src/org/python/core/PySystemState.java trunk/jython/src/org/python/core/imp.java trunk/jython/src/org/python/modules/imp.java Modified: trunk/jython/Lib/test/test_import_jy.py =================================================================== --- trunk/jython/Lib/test/test_import_jy.py 2008-12-11 00:39:49 UTC (rev 5734) +++ trunk/jython/Lib/test/test_import_jy.py 2008-12-11 05:20:55 UTC (rev 5735) @@ -90,6 +90,7 @@ self.assertEquals(bytecode, read(init_compiled), 'bytecode was recompiled') + class OverrideBuiltinsImportTestCase(unittest.TestCase): def test_override(self): tests = [ @@ -127,9 +128,19 @@ finally: __builtin__.__import__ = oldimp +class ImpTestCase(unittest.TestCase): + + def test_imp_find_module_builtins(self): + self.assertEqual(imp.find_module('sys'), (None, 'sys', ('', '', 6))) + self.assertEqual(imp.find_module('__builtin__'), + (None, '__builtin__', ('', '', 6))) + self.assertEqual(imp.find_module('imp'), (None, 'imp', ('', '', 6))) + + def test_main(): - test_classes = [MislabeledImportTestCase, OverrideBuiltinsImportTestCase] - test_support.run_unittest(*test_classes) + test_support.run_unittest(MislabeledImportTestCase, + OverrideBuiltinsImportTestCase, + ImpTestCase) if __name__ == '__main__': test_main() Modified: trunk/jython/src/org/python/core/PySystemState.java =================================================================== --- trunk/jython/src/org/python/core/PySystemState.java 2008-12-11 00:39:49 UTC (rev 5734) +++ trunk/jython/src/org/python/core/PySystemState.java 2008-12-11 05:20:55 UTC (rev 5735) @@ -709,6 +709,10 @@ private static void initBuiltins(Properties props) { builtinNames = new Hashtable(); + // add the oddball builtins that are specially handled + builtinNames.put("__builtin__", ""); + builtinNames.put("sys", ""); + // add builtins specified in the Setup.java file for (int i=0; i < Setup.builtinModules.length; i++) addBuiltin(Setup.builtinModules[i]); @@ -727,7 +731,7 @@ builtin_module_names = new PyTuple(built_mod); } - static String getBuiltin(String name) { + public static String getBuiltin(String name) { return (String)builtinNames.get(name); } Modified: trunk/jython/src/org/python/core/imp.java =================================================================== --- trunk/jython/src/org/python/core/imp.java 2008-12-11 00:39:49 UTC (rev 5734) +++ trunk/jython/src/org/python/core/imp.java 2008-12-11 05:20:55 UTC (rev 5735) @@ -389,10 +389,13 @@ private static PyObject loadBuiltin(String name) { if (name == "sys") { - Py.writeComment(IMPORT_LOG, "'" + name + "' as sys in " - + "builtin modules"); + Py.writeComment(IMPORT_LOG, "'" + name + "' as sys in builtin modules"); return Py.java2py(Py.getSystemState()); } + if (name == "__builtin__") { + Py.writeComment(IMPORT_LOG, "'" + name + "' as __builtin__ in builtin modules"); + return new PyModule("__builtin__", PySystemState.builtins); + } String mod = PySystemState.getBuiltin(name); if (mod != null) { Class c = Py.findClassEx(mod, "builtin modules"); Modified: trunk/jython/src/org/python/modules/imp.java =================================================================== --- trunk/jython/src/org/python/modules/imp.java 2008-12-11 00:39:49 UTC (rev 5734) +++ trunk/jython/src/org/python/modules/imp.java 2008-12-11 05:20:55 UTC (rev 5735) @@ -129,10 +129,6 @@ return null; } - public static PyObject find_module(String name) { - return find_module(name, null); - } - public static PyObject load_source(String modname, String filename) { return load_source(modname, filename, null); } @@ -158,11 +154,20 @@ return mod; } + public static PyObject find_module(String name) { + return find_module(name, Py.None); + } + public static PyObject find_module(String name, PyObject path) { - if (path == null || path == Py.None) { + if (path == Py.None && PySystemState.getBuiltin(name) != null) { + return new PyTuple(Py.None, Py.newString(name), + new PyTuple(Py.EmptyString, Py.EmptyString, + Py.newInteger(C_BUILTIN))); + } + + if (path == Py.None) { path = Py.getSystemState().path; } - for (PyObject p : path.asIterable()) { ModuleInfo mi = findFromSource(name, p.toString(), false, true); if(mi == null) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |