From: <fwi...@us...> - 2008-10-11 21:29:38
|
Revision: 5374 http://jython.svn.sourceforge.net/jython/?rev=5374&view=rev Author: fwierzbicki Date: 2008-10-11 21:29:29 +0000 (Sat, 11 Oct 2008) Log Message: ----------- A very basic start to supporting pep 328 style relative imports like from . import foo This patch just passes the "level" (that is number of dots) information from the compile to the import machinary without actually using it, and forbids from . import * As Python 2.5 does. Modified Paths: -------------- trunk/jython/src/org/python/compiler/CodeCompiler.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-11 20:54:47 UTC (rev 5373) +++ trunk/jython/src/org/python/compiler/CodeCompiler.java 2008-10-11 21:29:29 UTC (rev 5374) @@ -768,6 +768,9 @@ code.ldc(node.module); //Note: parser does not allow node.names.length == 0 if (node.names.length == 1 && node.names[0].name.equals("*")) { + if (node.level > 0) { + throw new ParseException("'import *' not allowed with 'from .'", node); + } if (my_scope.func_level > 0) { module.error("import * only allowed at module level", false, node); @@ -799,7 +802,8 @@ makeStrings(code, fromNames, fromNames.length); loadFrame(); - code.invokestatic("org/python/core/imp", "importFrom", "(" + $str + $strArr + $pyFrame + ")" + $pyObjArr); + 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++) { code.aload(tmp); Modified: trunk/jython/src/org/python/core/imp.java =================================================================== --- trunk/jython/src/org/python/core/imp.java 2008-10-11 20:54:47 UTC (rev 5373) +++ trunk/jython/src/org/python/core/imp.java 2008-10-11 21:29:29 UTC (rev 5374) @@ -753,12 +753,30 @@ } /** + * replaced by importFrom with level param. Kept for backwards compatibility. + * @deprecated use importFrom with level param. + */ + public static PyObject[] importFrom(String mod, String[] names, + PyFrame frame) { + return importFromAs(mod, names, null, frame, 0); + } + + /** * Called from jython generated code when a statement like "from spam.eggs * import foo, bar" is executed. */ public static PyObject[] importFrom(String mod, String[] names, + PyFrame frame, int level) { + return importFromAs(mod, names, null, frame, level); + } + + /** + * replaced by importFromAs with level param. Kept for backwards compatibility. + * @deprecated use importFromAs with level param. + */ + public static PyObject[] importFromAs(String mod, String[] names, PyFrame frame) { - return importFromAs(mod, names, null, frame); + return importFromAs(mod, names, null, frame, 0); } /** @@ -766,7 +784,7 @@ * import foo as spam" is executed. */ public static PyObject[] importFromAs(String mod, String[] names, - String[] asnames, PyFrame frame) { + String[] asnames, PyFrame frame, int level) { PyObject[] pyNames = new PyObject[names.length]; for (int i = 0; i < names.length; i++) { pyNames[i] = Py.newString(names[i]); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |