]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-87115: Set `__main__.__spec__` to `None` in pdb (#116141)
authorTian Gao <gaogaotiantian@hotmail.com>
Thu, 29 Feb 2024 21:39:50 +0000 (13:39 -0800)
committerGitHub <noreply@github.com>
Thu, 29 Feb 2024 21:39:50 +0000 (21:39 +0000)
Lib/pdb.py
Lib/test/test_pdb.py
Misc/NEWS.d/next/Library/2024-02-29-20-06-06.gh-issue-87115.FVMiOR.rst [new file with mode: 0644]

index 0754e8b628cf570ffb12134c49f9f6a936edb9f0..519c1ccd5640a130ee6c26d32cfe84d29ddbb391 100755 (executable)
@@ -188,6 +188,7 @@ class _ScriptTarget(str):
             __name__='__main__',
             __file__=self,
             __builtins__=__builtins__,
+            __spec__=None,
         )
 
     @property
index 2b0795cdad707e190639a035e70011681e314873..3dd275caf43200ee0f4742f7b9392823087a5140 100644 (file)
@@ -2695,6 +2695,18 @@ def bœr():
             ('bœr', 2),
         )
 
+    def test_spec(self):
+        # Test that __main__.__spec__ is set to None when running a script
+        script = """
+            import __main__
+            print(__main__.__spec__)
+        """
+
+        commands = "continue"
+
+        stdout, _ = self.run_pdb_script(script, commands)
+        self.assertIn('None', stdout)
+
     def test_find_function_first_executable_line(self):
         code = textwrap.dedent("""\
             def foo(): pass
diff --git a/Misc/NEWS.d/next/Library/2024-02-29-20-06-06.gh-issue-87115.FVMiOR.rst b/Misc/NEWS.d/next/Library/2024-02-29-20-06-06.gh-issue-87115.FVMiOR.rst
new file mode 100644 (file)
index 0000000..8443405
--- /dev/null
@@ -0,0 +1 @@
+Set ``__main__.__spec__`` to ``None`` when running a script with :mod:`pdb`