mirror of
https://github.com/python/cpython.git
synced 2025-10-02 05:12:23 +00:00
[3.7] bpo-32691: Use mod_spec.parent when running modules with pdb (GH-5510)
Previously the module name was used, which broke relative imports when pdb was run against a plain module or submodule.
(cherry picked from commit 38bfa8418f
)
Co-authored-by: Mario Corchero <mariocj89@gmail.com>
This commit is contained in:
parent
133514e9dc
commit
1a0239e12e
3 changed files with 34 additions and 2 deletions
|
@ -1532,7 +1532,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
|
||||||
__main__.__dict__.update({
|
__main__.__dict__.update({
|
||||||
"__name__": "__main__",
|
"__name__": "__main__",
|
||||||
"__file__": self.mainpyfile,
|
"__file__": self.mainpyfile,
|
||||||
"__package__": module_name,
|
"__package__": mod_spec.parent,
|
||||||
"__loader__": mod_spec.loader,
|
"__loader__": mod_spec.loader,
|
||||||
"__spec__": mod_spec,
|
"__spec__": mod_spec,
|
||||||
"__builtins__": __builtins__,
|
"__builtins__": __builtins__,
|
||||||
|
|
|
@ -1447,10 +1447,41 @@ class PdbTestCase(unittest.TestCase):
|
||||||
quit
|
quit
|
||||||
"""
|
"""
|
||||||
stdout, _ = self._run_pdb(['-m', self.module_name], commands)
|
stdout, _ = self._run_pdb(['-m', self.module_name], commands)
|
||||||
self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()))
|
self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout)
|
||||||
self.assertTrue(any("VAR from top" in l for l in stdout.splitlines()))
|
self.assertTrue(any("VAR from top" in l for l in stdout.splitlines()))
|
||||||
self.assertTrue(any("second var" in l for l in stdout.splitlines()))
|
self.assertTrue(any("second var" in l for l in stdout.splitlines()))
|
||||||
|
|
||||||
|
def test_relative_imports_on_plain_module(self):
|
||||||
|
# Validates running a plain module. See bpo32691
|
||||||
|
self.module_name = 't_main'
|
||||||
|
support.rmtree(self.module_name)
|
||||||
|
main_file = self.module_name + '/runme.py'
|
||||||
|
init_file = self.module_name + '/__init__.py'
|
||||||
|
module_file = self.module_name + '/module.py'
|
||||||
|
self.addCleanup(support.rmtree, self.module_name)
|
||||||
|
os.mkdir(self.module_name)
|
||||||
|
with open(init_file, 'w') as f:
|
||||||
|
f.write(textwrap.dedent("""
|
||||||
|
top_var = "VAR from top"
|
||||||
|
"""))
|
||||||
|
with open(main_file, 'w') as f:
|
||||||
|
f.write(textwrap.dedent("""
|
||||||
|
from . import module
|
||||||
|
pass # We'll stop here and print the vars
|
||||||
|
"""))
|
||||||
|
with open(module_file, 'w') as f:
|
||||||
|
f.write(textwrap.dedent("""
|
||||||
|
var = "VAR from module"
|
||||||
|
"""))
|
||||||
|
commands = """
|
||||||
|
b 3
|
||||||
|
c
|
||||||
|
p module.var
|
||||||
|
quit
|
||||||
|
"""
|
||||||
|
stdout, _ = self._run_pdb(['-m', self.module_name + '.runme'], commands)
|
||||||
|
self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout)
|
||||||
|
|
||||||
|
|
||||||
def load_tests(*args):
|
def load_tests(*args):
|
||||||
from test import test_pdb
|
from test import test_pdb
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Use mod_spec.parent when running modules with pdb
|
Loading…
Add table
Add a link
Reference in a new issue