]> granicus.if.org Git - python/commitdiff
bpo-32691: Use mod_spec.parent when running modules with pdb (GH-5474)
authorMario Corchero <mariocj89@gmail.com>
Sat, 3 Feb 2018 06:40:11 +0000 (06:40 +0000)
committerNick Coghlan <ncoghlan@gmail.com>
Sat, 3 Feb 2018 06:40:11 +0000 (16:40 +1000)
Previously the module name was used, which broke relative imports when pdb was run against a plain module or submodule.

Lib/pdb.py
Lib/test/test_pdb.py
Misc/NEWS.d/next/Library/2018-02-01-15-53-35.bpo-32691.VLWVTq.rst [new file with mode: 0644]

index 366a85b31960a612068aba6148b4d66ed3a5c887..60bdb7675c8131660b2064150fa739911382c1d2 100755 (executable)
@@ -1532,7 +1532,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
         __main__.__dict__.update({
             "__name__": "__main__",
             "__file__": self.mainpyfile,
-            "__package__": module_name,
+            "__package__": mod_spec.parent,
             "__loader__": mod_spec.loader,
             "__spec__": mod_spec,
             "__builtins__": __builtins__,
index 70d8d1d32613fbf6774f28254084531982425b47..9aa38e08dd6e6c204a9585d25adf0ed6d3a15823 100644 (file)
@@ -1447,10 +1447,41 @@ class PdbTestCase(unittest.TestCase):
             quit
         """
         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("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):
     from test import test_pdb
diff --git a/Misc/NEWS.d/next/Library/2018-02-01-15-53-35.bpo-32691.VLWVTq.rst b/Misc/NEWS.d/next/Library/2018-02-01-15-53-35.bpo-32691.VLWVTq.rst
new file mode 100644 (file)
index 0000000..93f898e
--- /dev/null
@@ -0,0 +1 @@
+Use mod_spec.parent when running modules with pdb