Skip to content

Commit 49b5e20

Browse files
authored
[3.9] bpo-44461: Check early that a pdb target is valid for execution. (GH-27227) (GH-27400)
* [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 <[email protected]> * 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 <[email protected]>. (cherry picked from commit ee03bad) Co-authored-by: Jason R. Coombs <[email protected]> * Ensure os_helper is imported. * Actually, os_helper doesn't exist yet. Just reference rmtree from support.
1 parent 899e37b commit 49b5e20

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Lib/pdb.py

+8
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

+14
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,20 @@ def test_module_without_a_main(self):
16321632
self.assertIn("ImportError: No module named t_main.__main__",
16331633
stdout.splitlines())
16341634

1635+
def test_package_without_a_main(self):
1636+
pkg_name = 't_pkg'
1637+
module_name = 't_main'
1638+
support.rmtree(pkg_name)
1639+
modpath = pkg_name + '/' + module_name
1640+
os.makedirs(modpath)
1641+
with open(modpath + '/__init__.py', 'w') as f:
1642+
pass
1643+
self.addCleanup(support.rmtree, pkg_name)
1644+
stdout, stderr = self._run_pdb(['-m', modpath.replace('/', '.')], "")
1645+
self.assertIn(
1646+
"'t_pkg.t_main' is a package and cannot be directly executed",
1647+
stdout)
1648+
16351649
def test_blocks_at_first_code_line(self):
16361650
script = """
16371651
#This is a comment, on line 2
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)