]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.9] bpo-44461: Check early that a pdb target is valid for execution. (GH-27227...
authorJason R. Coombs <jaraco@jaraco.com>
Wed, 28 Jul 2021 22:48:52 +0000 (18:48 -0400)
committerGitHub <noreply@github.com>
Wed, 28 Jul 2021 22:48:52 +0000 (18:48 -0400)
* [3.9] bpo-44461: Check early that a pdb target is valid for execution. (GH-27227)

* bpo-44461: Fix bug with pdb's handling of import error due to a package which does not have a __main__ module

* ðŸ“œðŸ€– Added by blurb_it.

* remove "else"

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
* If running as a module, first check that it can run as a module. Alternate fix for bpo-44461.

Co-authored-by: Irit Katriel
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>.
(cherry picked from commit ee03bad25e83b00ba5fc2a0265b48c6286e6b3f7)

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
* Ensure os_helper is imported.

* Actually, os_helper doesn't exist yet. Just reference rmtree from support.

Lib/pdb.py
Lib/test/test_pdb.py
Misc/NEWS.d/next/Library/2021-06-29-21-17-17.bpo-44461.acqRnV.rst [new file with mode: 0644]

index 1b4ff54833fcb424ff730828dbf01573ab99cbee..943211158ac41e417496bdccbbcfb8a113504ae0 100755 (executable)
@@ -1694,6 +1694,14 @@ def main():
         print('Error:', mainpyfile, 'does not exist')
         sys.exit(1)
 
+    if run_as_module:
+        import runpy
+        try:
+            runpy._get_module_details(mainpyfile)
+        except Exception:
+            traceback.print_exc()
+            sys.exit(1)
+
     sys.argv[:] = args      # Hide "pdb.py" and pdb options from argument list
 
     if not run_as_module:
index 193207a9aba50b54f19ac0e152709c8486aafa7a..0653a64b0a697a2e6f12154ffad354fabe485aec 100644 (file)
@@ -1632,6 +1632,20 @@ def bœr():
         self.assertIn("ImportError: No module named t_main.__main__",
                       stdout.splitlines())
 
+    def test_package_without_a_main(self):
+        pkg_name = 't_pkg'
+        module_name = 't_main'
+        support.rmtree(pkg_name)
+        modpath = pkg_name + '/' + module_name
+        os.makedirs(modpath)
+        with open(modpath + '/__init__.py', 'w') as f:
+            pass
+        self.addCleanup(support.rmtree, pkg_name)
+        stdout, stderr = self._run_pdb(['-m', modpath.replace('/', '.')], "")
+        self.assertIn(
+            "'t_pkg.t_main' is a package and cannot be directly executed",
+            stdout)
+
     def test_blocks_at_first_code_line(self):
         script = """
                 #This is a comment, on line 2
diff --git a/Misc/NEWS.d/next/Library/2021-06-29-21-17-17.bpo-44461.acqRnV.rst b/Misc/NEWS.d/next/Library/2021-06-29-21-17-17.bpo-44461.acqRnV.rst
new file mode 100644 (file)
index 0000000..02e25e9
--- /dev/null
@@ -0,0 +1 @@
+Fix bug with :mod:`pdb`'s handling of import error due to a package which does not have a ``__main__`` module
\ No newline at end of file