From: <fwi...@us...> - 2008-11-14 22:54:58
|
Revision: 5581 http://jython.svn.sourceforge.net/jython/?rev=5581&view=rev Author: fwierzbicki Date: 2008-11-14 22:54:53 +0000 (Fri, 14 Nov 2008) Log Message: ----------- Fix for http://bugs.jython.org/issue1141. __builtin__.__import__ had subtle differences with CPython. These differences would cause problems for those who wish to override __builtin__.__import__ statement: import hell.first_circle - CPython: ('hell.first_circle', None) - old Jython: ('hell.first_circle', ()) statement: import hell.first_circle as limbo - CPython: ('hell.first_circle', None) - old Jython: ('hell.first_circle', ('*',)) Also note that the second import statement with an "as" would return the head module instead of the tail module, the opposite of what CPython does. Modified Paths: -------------- trunk/jython/Lib/test/test_import_jy.py trunk/jython/src/org/python/core/__builtin__.java trunk/jython/src/org/python/core/imp.java Modified: trunk/jython/Lib/test/test_import_jy.py =================================================================== --- trunk/jython/Lib/test/test_import_jy.py 2008-11-14 17:53:21 UTC (rev 5580) +++ trunk/jython/Lib/test/test_import_jy.py 2008-11-14 22:54:53 UTC (rev 5581) @@ -96,9 +96,46 @@ self.assertEquals(bytecode, read(init_compiled), 'bytecode was recompiled') +class OverrideBuiltinsImportTestCase(unittest.TestCase): + def test_override(self): + tests = [ + ("import os.path" , "('os.path', None, -1, 'os')" ), + ("import os.path as path2", "('os.path', None, -1, 'os')" ), + ("from os.path import *" , "('os.path', ('*',), -1, 'posixpath')"), + ("from os.path import join", + "('os.path', ('join',), -1, 'posixpath')"), + ("from os.path import join as join2", + "('os.path', ('join',), -1, 'posixpath')"), + ("from os.path import join as join2, split as split2", + "('os.path', ('join', 'split'), -1, 'posixpath')"), + ] + import sys + # Replace __builtin__.__import__ to trace the calls + import __builtin__ + oldimp = __builtin__.__import__ + try: + def __import__(name, globs, locs, fromlist, level=-1): + mod = oldimp(name, globs, locs, fromlist, level) + globs["result"] = str((name, fromlist, level, mod.__name__)) + raise ImportError + + __builtin__.__import__ = __import__ + failed = 0 + for statement, expected in tests: + try: + c = compile(statement, "<unknown>", "exec") + exec c in locals(), globals() + raise Exception("ImportError expected.") + except ImportError: + pass + self.assertEquals(expected, result) + finally: + __builtin__.__import__ = oldimp + def test_main(): - test_support.run_unittest(MislabeledImportTestCase) + test_classes = [MislabeledImportTestCase, OverrideBuiltinsImportTestCase] + test_support.run_unittest(*test_classes) if __name__ == '__main__': test_main() Modified: trunk/jython/src/org/python/core/__builtin__.java =================================================================== --- trunk/jython/src/org/python/core/__builtin__.java 2008-11-14 17:53:21 UTC (rev 5580) +++ trunk/jython/src/org/python/core/__builtin__.java 2008-11-14 22:54:53 UTC (rev 5581) @@ -1266,7 +1266,8 @@ PyObject globals = ap.getPyObject(1, null); PyObject fromlist = ap.getPyObject(3, Py.EmptyTuple); int level = ap.getInt(4, imp.DEFAULT_LEVEL); - return imp.importName(module.intern(), fromlist.__len__() == 0, globals, fromlist, level); + return imp.importName(module.intern(), fromlist == Py.None || fromlist.__len__() == 0, + globals, fromlist, level); } } Modified: trunk/jython/src/org/python/core/imp.java =================================================================== --- trunk/jython/src/org/python/core/imp.java 2008-11-14 17:53:21 UTC (rev 5580) +++ trunk/jython/src/org/python/core/imp.java 2008-11-14 22:54:53 UTC (rev 5581) @@ -754,7 +754,7 @@ */ public static PyObject importOne(String mod, PyFrame frame) { PyObject module = __builtin__.__import__(mod, frame.f_globals, frame - .getLocals(), Py.EmptyTuple); + .getLocals(), Py.None); return module; } @@ -764,7 +764,19 @@ */ public static PyObject importOneAs(String mod, PyFrame frame) { PyObject module = __builtin__.__import__(mod, frame.f_globals, frame - .getLocals(), getStarArg()); + .getLocals(), Py.None); + int dot = mod.indexOf('.'); + while (dot != -1) { + int dot2 = mod.indexOf('.', dot + 1); + String name; + if (dot2 == -1) { + name = mod.substring(dot + 1); + } else { + name = mod.substring(dot + 1, dot2); + } + module = module.__getattr__(name); + dot = dot2; + } return module; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |