From e1b1784b96e40d03583f7cb16310ca7e738a1eb0 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 27 Jul 2021 21:51:42 -0400 Subject: [PATCH 1/3] [3.9] bpo-44461: Check early that a pdb target is valid for execution. (GH-27227) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 * 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 --- Lib/pdb.py | 8 ++++++++ Lib/test/test_pdb.py | 14 ++++++++++++++ .../2021-06-29-21-17-17.bpo-44461.acqRnV.rst | 1 + 3 files changed, 23 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-06-29-21-17-17.bpo-44461.acqRnV.rst diff --git a/Lib/pdb.py b/Lib/pdb.py index 1b4ff54833fcb4..943211158ac41e 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -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: diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 193207a9aba50b..24d3bcc772cd7b 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -1632,6 +1632,20 @@ def test_module_without_a_main(self): 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' + os_helper.rmtree(pkg_name) + modpath = pkg_name + '/' + module_name + os.makedirs(modpath) + with open(modpath + '/__init__.py', 'w') as f: + pass + self.addCleanup(os_helper.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 index 00000000000000..02e25e928b9cff --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-06-29-21-17-17.bpo-44461.acqRnV.rst @@ -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 From 8ae7ef2ed2e3cec8946c72f7d9c664c2c85dfa2b Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 28 Jul 2021 17:43:29 -0400 Subject: [PATCH 2/3] Ensure os_helper is imported. --- Lib/test/test_pdb.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 24d3bcc772cd7b..d61bd887387576 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -14,6 +14,7 @@ from contextlib import ExitStack from io import StringIO from test import support +from test.support import os_helper # This little helper class is essential for testing pdb under doctest. from test.test_doctest import _FakeInput from unittest.mock import patch From b3d230592222014c252716b1b49e833b7ad8ddf7 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 28 Jul 2021 18:00:31 -0400 Subject: [PATCH 3/3] Actually, os_helper doesn't exist yet. Just reference rmtree from support. --- Lib/test/test_pdb.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index d61bd887387576..0653a64b0a697a 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -14,7 +14,6 @@ from contextlib import ExitStack from io import StringIO from test import support -from test.support import os_helper # This little helper class is essential for testing pdb under doctest. from test.test_doctest import _FakeInput from unittest.mock import patch @@ -1636,12 +1635,12 @@ def test_module_without_a_main(self): def test_package_without_a_main(self): pkg_name = 't_pkg' module_name = 't_main' - os_helper.rmtree(pkg_name) + support.rmtree(pkg_name) modpath = pkg_name + '/' + module_name os.makedirs(modpath) with open(modpath + '/__init__.py', 'w') as f: pass - self.addCleanup(os_helper.rmtree, pkg_name) + 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",