[Python-checkins] r56509 - in python/trunk/Lib: runpy.py test/test_runpy.py

nick.coghlan python-checkins at python.org
Mon Jul 23 15:41:45 CEST 2007


Author: nick.coghlan
Date: Mon Jul 23 15:41:45 2007
New Revision: 56509
Modified:
 python/trunk/Lib/runpy.py
 python/trunk/Lib/test/test_runpy.py
Log:
Correctly cleanup sys.modules after executing runpy relative import 
tests
Restore Python 2.4 ImportError when attempting to execute a package 
(as imports cannot be guaranteed to work properly if you try it)
Modified: python/trunk/Lib/runpy.py
==============================================================================
--- python/trunk/Lib/runpy.py	(original)
+++ python/trunk/Lib/runpy.py	Mon Jul 23 15:41:45 2007
@@ -84,10 +84,13 @@
 """
 loader = get_loader(mod_name)
 if loader is None:
- raise ImportError("No module named " + mod_name)
+ raise ImportError("No module named %s" % mod_name)
+ if loader.is_package(mod_name):
+ raise ImportError(("%s is a package and cannot " +
+ "be directly executed") % mod_name)
 code = loader.get_code(mod_name)
 if code is None:
- raise ImportError("No code object available for " + mod_name)
+ raise ImportError("No code object available for %s" % mod_name)
 filename = _get_filename(loader, mod_name)
 if run_name is None:
 run_name = mod_name
Modified: python/trunk/Lib/test/test_runpy.py
==============================================================================
--- python/trunk/Lib/test/test_runpy.py	(original)
+++ python/trunk/Lib/test/test_runpy.py	Mon Jul 23 15:41:45 2007
@@ -77,12 +77,16 @@
 self.fail("Expected import error for " + mod_name)
 
 def test_invalid_names(self):
+ # Builtin module
 self.expect_import_error("sys")
+ # Non-existent modules
 self.expect_import_error("sys.imp.eric")
 self.expect_import_error("os.path.half")
 self.expect_import_error("a.bee")
 self.expect_import_error(".howard")
 self.expect_import_error("..eaten")
+ # Package
+ self.expect_import_error("logging")
 
 def test_library_module(self):
 run_module("runpy")
@@ -115,13 +119,9 @@
 return pkg_dir, mod_fname, mod_name
 
 def _del_pkg(self, top, depth, mod_name):
- for i in range(depth+1): # Don't forget the module itself
- parts = mod_name.rsplit(".", i)
- entry = parts[0]
- try:
+ for entry in list(sys.modules):
+ if entry.startswith("__runpy_pkg__"):
 del sys.modules[entry]
- except KeyError, ex:
- if verbose: print ex # Persist with cleaning up
 if verbose: print " Removed sys.modules entries"
 del sys.path[0]
 if verbose: print " Removed sys.path entry"


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /