From: <fwi...@us...> - 2008-10-15 17:07:58
|
Revision: 5399 http://jython.svn.sourceforge.net/jython/?rev=5399&view=rev Author: fwierzbicki Date: 2008-10-15 17:06:58 +0000 (Wed, 15 Oct 2008) Log Message: ----------- New dotted and absolute import support. I have local tests, but have not figured out how to integrate them into the regression tests. Examining the tests in CPython does not appear to give much guidance here, the testing of these features looks pretty thin (mainly they test features around __package__ which is new in 2.6). Maybe I just haven't found the relevant tests? Modified Paths: -------------- trunk/jython/src/org/python/core/imp.java Modified: trunk/jython/src/org/python/core/imp.java =================================================================== --- trunk/jython/src/org/python/core/imp.java 2008-10-15 15:56:58 UTC (rev 5398) +++ trunk/jython/src/org/python/core/imp.java 2008-10-15 17:06:58 UTC (rev 5399) @@ -529,6 +529,9 @@ * @return the parent name for a module */ private static String getParent(PyObject dict, int level) { + if (dict == null || level == 0) { + return null; + } PyObject tmp = dict.__finditem__("__name__"); if (tmp == null) { return null; @@ -538,13 +541,23 @@ tmp = dict.__finditem__("__path__"); if (tmp != null && tmp instanceof PyList) { return name.intern(); - } else { - int dot = name.lastIndexOf('.'); + } + int dot = name.lastIndexOf('.'); + if (dot == -1) { + if (level > 0) { + throw Py.ValueError("Attempted relative import in non-package"); + } + return null; + } + name = name.substring(0, dot); + while (--level > 0) { + dot = name.lastIndexOf('.'); if (dot == -1) { - return null; + throw Py.ValueError("Attempted relative import beyond toplevel package"); } - return name.substring(0, dot).intern(); + name = name.substring(0, dot); } + return name.intern(); } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |