[Python-checkins] cpython (merge 3.5 -> default): Issue #19771: Merge runpy error adjustment from 3.5

martin.panter python-checkins at python.org
Sat Dec 12 02:31:24 EST 2015


https://hg.python.org/cpython/rev/323c10701e5d
changeset:   99539:323c10701e5d
parent:      99538:9be59ad8af80
parent:      99536:850cc65ceda4
user:        Martin Panter <vadmium+py at gmail.com>
date:        Sat Dec 12 07:16:33 2015 +0000
summary:
  Issue #19771: Merge runpy error adjustment from 3.5

files:
  Lib/runpy.py                     |   2 ++
  Lib/test/test_cmd_line_script.py |  13 +++++++++++++
  Misc/NEWS                        |   4 ++++
  3 files changed, 19 insertions(+), 0 deletions(-)


diff --git a/Lib/runpy.py b/Lib/runpy.py
--- a/Lib/runpy.py
+++ b/Lib/runpy.py
@@ -132,6 +132,8 @@
             pkg_main_name = mod_name + ".__main__"
             return _get_module_details(pkg_main_name, error)
         except error as e:
+            if mod_name not in sys.modules:
+                raise  # No module loaded; being a package is irrelevant
             raise error(("%s; %r is a package and cannot " +
                                "be directly executed") %(e, mod_name))
     loader = spec.loader
diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py
--- a/Lib/test/test_cmd_line_script.py
+++ b/Lib/test/test_cmd_line_script.py
@@ -442,6 +442,19 @@
                 self.assertRegex(err, regex)
                 self.assertNotIn(b'Traceback', err)
 
+    def test_dash_m_bad_pyc(self):
+        with support.temp_dir() as script_dir, \
+                support.change_cwd(path=script_dir):
+            os.mkdir('test_pkg')
+            # Create invalid *.pyc as empty file
+            with open('test_pkg/__init__.pyc', 'wb'):
+                pass
+            err = self.check_dash_m_failure('test_pkg')
+            self.assertRegex(err, br'Error while finding spec.*'
+                br'ImportError.*bad magic number')
+            self.assertNotIn(b'is a package', err)
+            self.assertNotIn(b'Traceback', err)
+
     def test_dash_m_init_traceback(self):
         # These were wrapped in an ImportError and tracebacks were
         # suppressed; see Issue 14285
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -136,6 +136,10 @@
   "runpy" module now lets exceptions from package initialization pass back to
   the caller, rather than raising ImportError.
 
+- Issue #19771: Also in runpy and the "-m" option, omit the irrelevant
+  message ". . . is a package and cannot be directly executed" if the package
+  could not even be initialized (e.g. due to a bad *.pyc file).
+
 - Issue #25177: Fixed problem with the mean of very small and very large
   numbers. As a side effect, statistics.mean and statistics.variance should
   be significantly faster.

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list