From: <fwi...@us...> - 2008-10-12 16:28:25
|
Revision: 5379 http://jython.svn.sourceforge.net/jython/?rev=5379&view=rev Author: fwierzbicki Date: 2008-10-12 16:28:15 +0000 (Sun, 12 Oct 2008) Log Message: ----------- More groundwork for absolute_import. Passing level info into most of the places it needs to go, also now sending the correct level information when "from __future__ import absolute_import" is issued. Added a constant in imp.java DEFAULT_LEVEL, so that it can be changed in one place when absolute import becomes the default behavior. Modified Paths: -------------- trunk/jython/src/org/python/compiler/CodeCompiler.java trunk/jython/src/org/python/compiler/Future.java trunk/jython/src/org/python/core/__builtin__.java trunk/jython/src/org/python/core/imp.java Modified: trunk/jython/src/org/python/compiler/CodeCompiler.java =================================================================== --- trunk/jython/src/org/python/compiler/CodeCompiler.java 2008-10-12 00:25:07 UTC (rev 5378) +++ trunk/jython/src/org/python/compiler/CodeCompiler.java 2008-10-12 16:28:15 UTC (rev 5379) @@ -802,7 +802,16 @@ makeStrings(code, fromNames, fromNames.length); loadFrame(); - code.iconst(node.level); + + if (node.level == 0) { + if (module.getFutures().isAbsoluteImportOn()) { + code.iconst_0(); + } else { + code.iconst_m1(); + } + } else { + code.iconst(node.level); + } code.invokestatic("org/python/core/imp", "importFrom", "(" + $str + $strArr + $pyFrame + "I" + ")" + $pyObjArr); int tmp = storeTop(); for (int i = 0; i < node.names.length; i++) { Modified: trunk/jython/src/org/python/compiler/Future.java =================================================================== --- trunk/jython/src/org/python/compiler/Future.java 2008-10-12 00:25:07 UTC (rev 5378) +++ trunk/jython/src/org/python/compiler/Future.java 2008-10-12 16:28:15 UTC (rev 5379) @@ -115,4 +115,8 @@ return with_statement; } + public boolean isAbsoluteImportOn() { + return absolute_import; + } + } Modified: trunk/jython/src/org/python/core/__builtin__.java =================================================================== --- trunk/jython/src/org/python/core/__builtin__.java 2008-10-12 00:25:07 UTC (rev 5378) +++ trunk/jython/src/org/python/core/__builtin__.java 2008-10-12 16:28:15 UTC (rev 5379) @@ -1198,18 +1198,22 @@ } public static PyObject __import__(String name) { - return __import__(name, null, null, null); + return __import__(name, null, null, null, imp.DEFAULT_LEVEL); } public static PyObject __import__(String name, PyObject globals) { - return __import__(name, globals, null, null); + return __import__(name, globals, null, null, imp.DEFAULT_LEVEL); } public static PyObject __import__(String name, PyObject globals, PyObject locals) { - return __import__(name, globals, locals, null); + return __import__(name, globals, locals, null, imp.DEFAULT_LEVEL); } public static PyObject __import__(String name, PyObject globals, PyObject locals, PyObject fromlist) { + return __import__(name, globals, locals, fromlist, imp.DEFAULT_LEVEL); + } + + public static PyObject __import__(String name, PyObject globals, PyObject locals, PyObject fromlist, int level) { PyFrame frame = Py.getFrame(); if (frame == null) { return null; @@ -1224,7 +1228,7 @@ return null; } - PyObject module = __import__.__call__(new PyObject[]{Py.newString(name), globals, locals, fromlist}); + PyObject module = __import__.__call__(new PyObject[]{Py.newString(name), globals, locals, fromlist, Py.newInteger(level)}); return module; } } @@ -1280,12 +1284,13 @@ PyObject globals = (argc > 1 && args[1] != null) ? args[1] : null; PyObject fromlist = (argc > 3 && args[3] != null) ? args[3] : Py.EmptyTuple; + int level = (argc > 4 && args[4] != null) ? Py.py2int(args[4]) : imp.DEFAULT_LEVEL; - return load(module, globals, fromlist); + return load(module, globals, fromlist, level); } - private PyObject load(String module, PyObject globals, PyObject fromlist) { - PyObject mod = imp.importName(module.intern(), fromlist.__len__() == 0, globals, fromlist); + private PyObject load(String module, PyObject globals, PyObject fromlist, int level) { + PyObject mod = imp.importName(module.intern(), fromlist.__len__() == 0, globals, fromlist, level); return mod; } Modified: trunk/jython/src/org/python/core/imp.java =================================================================== --- trunk/jython/src/org/python/core/imp.java 2008-10-12 00:25:07 UTC (rev 5378) +++ trunk/jython/src/org/python/core/imp.java 2008-10-12 16:28:15 UTC (rev 5379) @@ -24,6 +24,9 @@ public static final int APIVersion = 15; + //This should change to 0 for Python 2.7 and 3.0 see PEP 328 + public static final int DEFAULT_LEVEL = -1; + /** A non-empty fromlist for __import__'ing sub-modules. */ private static final PyObject nonEmptyFromlist = new PyTuple(Py.newString("__doc__")); @@ -518,9 +521,14 @@ * 'a.b.c' would return 'a.b'. * * @param dict the __dict__ of a loaded module + * @param level used for relative and absolute imports. -1 means try both, + * 0 means absolute only, positive ints represent the level to + * look upward for a relative path. See PEP 328 at + * http://www.python.org/dev/peps/pep-0328/ + * * @return the parent name for a module */ - private static String getParent(PyObject dict) { + private static String getParent(PyObject dict, int level) { PyObject tmp = dict.__finditem__("__name__"); if (tmp == null) { return null; @@ -639,7 +647,7 @@ * @return a module */ private static PyObject import_name(String name, boolean top, - PyObject modDict, PyObject fromlist) { + PyObject modDict, PyObject fromlist, int level) { if (name.length() == 0) { throw Py.ValueError("Empty module name"); } @@ -647,7 +655,7 @@ PyObject pkgMod = null; String pkgName = null; if (modDict != null && !(modDict instanceof PyNone)) { - pkgName = getParent(modDict); + pkgName = getParent(modDict, level); pkgMod = modules.__finditem__(pkgName); if (pkgMod != null && !(pkgMod instanceof PyModule)) { pkgMod = null; @@ -705,7 +713,7 @@ * @return an imported module (Java or Python) */ public static PyObject importName(String name, boolean top) { - return import_name(name, top, null, null); + return import_name(name, top, null, null, DEFAULT_LEVEL); } /** @@ -718,10 +726,10 @@ * @return an imported module (Java or Python) */ public static PyObject importName(String name, boolean top, - PyObject modDict, PyObject fromlist) { + PyObject modDict, PyObject fromlist, int level) { importLock.lock(); try { - return import_name(name, top, modDict, fromlist); + return import_name(name, top, modDict, fromlist, level); } finally { importLock.unlock(); } @@ -758,7 +766,7 @@ */ public static PyObject[] importFrom(String mod, String[] names, PyFrame frame) { - return importFromAs(mod, names, null, frame, 0); + return importFromAs(mod, names, null, frame, DEFAULT_LEVEL); } /** @@ -776,7 +784,7 @@ */ public static PyObject[] importFromAs(String mod, String[] names, PyFrame frame) { - return importFromAs(mod, names, null, frame, 0); + return importFromAs(mod, names, null, frame, DEFAULT_LEVEL); } /** @@ -791,7 +799,7 @@ } PyObject module = __builtin__.__import__(mod, frame.f_globals, frame.getLocals(), - new PyTuple(pyNames)); + new PyTuple(pyNames), level); PyObject[] submods = new PyObject[names.length]; for (int i = 0; i < names.length; i++) { PyObject submod = module.__findattr__(names[i]); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |