From: <pj...@us...> - 2009-03-30 19:15:52
|
Revision: 6125 http://jython.svn.sourceforge.net/jython/?rev=6125&view=rev Author: pjenvey Date: 2009-03-30 19:15:47 +0000 (Mon, 30 Mar 2009) Log Message: ----------- handle the case where a module that failed to import was already deleted from sys.modules thanks Thijs Triemstra fixes #1246 Modified Paths: -------------- trunk/jython/Lib/test/test_import_jy.py trunk/jython/src/org/python/core/imp.java Added Paths: ----------- trunk/jython/Lib/test/module_deleter.py Added: trunk/jython/Lib/test/module_deleter.py =================================================================== --- trunk/jython/Lib/test/module_deleter.py (rev 0) +++ trunk/jython/Lib/test/module_deleter.py 2009-03-30 19:15:47 UTC (rev 6125) @@ -0,0 +1,3 @@ +import sys +del sys.modules[__name__] +1 / 0 Modified: trunk/jython/Lib/test/test_import_jy.py =================================================================== --- trunk/jython/Lib/test/test_import_jy.py 2009-03-30 17:12:32 UTC (rev 6124) +++ trunk/jython/Lib/test/test_import_jy.py 2009-03-30 19:15:47 UTC (rev 6125) @@ -159,6 +159,9 @@ self.assertEquals(Metis, Zeus.Athena.__bases__[0]) self.assertEquals(Zeus, Metis.__bases__[0]) + def test_sys_modules_deletion(self): + self.assertRaises(ZeroDivisionError, __import__, 'test.module_deleter') + def test_main(): test_support.run_unittest(MislabeledImportTestCase, OverrideBuiltinsImportTestCase, Modified: trunk/jython/src/org/python/core/imp.java =================================================================== --- trunk/jython/src/org/python/core/imp.java 2009-03-30 17:12:32 UTC (rev 6124) +++ trunk/jython/src/org/python/core/imp.java 2009-03-30 19:15:47 UTC (rev 6125) @@ -66,6 +66,26 @@ return module; } + /** + * Remove name form sys.modules if it's there. + * + * @param name the module name + */ + private static void removeModule(String name) { + name = name.intern(); + PyObject modules = Py.getSystemState().modules; + if (modules.__finditem__(name) != null) { + try { + modules.__delitem__(name); + } catch (PyException pye) { + // another thread may have deleted it + if (!Py.matchException(pye, Py.KeyError)) { + throw pye; + } + } + } + } + private static byte[] readBytes(InputStream fp) { try { return FileUtil.readBytes(fp); @@ -295,7 +315,7 @@ PyFrame f = new PyFrame(code, module.__dict__, module.__dict__, null); code.call(f); } catch (RuntimeException t) { - Py.getSystemState().modules.__delitem__(name.intern()); + removeModule(name); throw t; } return module; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |