]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-87115: Set `__main__.__spec__` to `None` in pdb (GH-116141) (#116155)
authorTian Gao <gaogaotiantian@hotmail.com>
Thu, 29 Feb 2024 23:24:32 +0000 (15:24 -0800)
committerGitHub <noreply@github.com>
Thu, 29 Feb 2024 23:24:32 +0000 (23:24 +0000)
* gh-87115: Set `__main__.__spec__` to `None` in pdb (#116141)

(cherry picked from commit ccfc042bbf31e53c44b8aae444afd8365b798422)

* [3.11] gh-87115: Set `__main__.__spec__` to `None` in pdb (GH-116141)
(cherry picked from commit ccfc042bbf31e53c44b8aae444afd8365b798422)

Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
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 4a4a0b9d90f6d2090afdb692af97d08f79ac18df..fb7b0eccd16d1c45184de2eee876ac14ef82615d 100755 (executable)
@@ -153,6 +153,7 @@ class _ScriptTarget(str):
             __name__='__main__',
             __file__=self,
             __builtins__=__builtins__,
+            __spec__=None,
         )
 
     @property
index eed02737e8bd484577cfd52518f6fe4fc63cbbb5..15d9eaa1f93cb2561e0b628a7f9e099ecee0ee59 100644 (file)
@@ -1862,6 +1862,18 @@ def bœr():
             ('bœr', 1),
         )
 
+    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_issue7964(self):
         # open the file as binary so we can force \r\n newline
         with open(os_helper.TESTFN, 'wb') as f:
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`