Skip to content

Commit 306cc26

Browse files
committed
[3.10] 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 <iritkatriel@yahoo.com> 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 ee03bad) Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
1 parent 68e3dca commit 306cc26

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Lib/pdb.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,14 @@ def main():
16941694
print('Error:', mainpyfile, 'does not exist')
16951695
sys.exit(1)
16961696

1697+
if run_as_module:
1698+
import runpy
1699+
try:
1700+
runpy._get_module_details(mainpyfile)
1701+
except Exception:
1702+
traceback.print_exc()
1703+
sys.exit(1)
1704+
16971705
sys.argv[:] = args # Hide "pdb.py" and pdb options from argument list
16981706

16991707
if not run_as_module:

Lib/test/test_pdb.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,20 @@ def test_module_without_a_main(self):
16951695
self.assertIn("ImportError: No module named t_main.__main__",
16961696
stdout.splitlines())
16971697

1698+
def test_package_without_a_main(self):
1699+
pkg_name = 't_pkg'
1700+
module_name = 't_main'
1701+
os_helper.rmtree(pkg_name)
1702+
modpath = pkg_name + '/' + module_name
1703+
os.makedirs(modpath)
1704+
with open(modpath + '/__init__.py', 'w') as f:
1705+
pass
1706+
self.addCleanup(os_helper.rmtree, pkg_name)
1707+
stdout, stderr = self._run_pdb(['-m', modpath.replace('/', '.')], "")
1708+
self.assertIn(
1709+
"'t_pkg.t_main' is a package and cannot be directly executed",
1710+
stdout)
1711+
16981712
def test_blocks_at_first_code_line(self):
16991713
script = """
17001714
#This is a comment, on line 2
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug with :mod:`pdb`'s handling of import error due to a package which does not have a ``__main__`` module

0 commit comments

Comments
 (0)