diff --git a/Lib/pdb.py b/Lib/pdb.py index 4a4a0b9d90f6d2..fb7b0eccd16d1c 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -153,6 +153,7 @@ def namespace(self): __name__='__main__', __file__=self, __builtins__=__builtins__, + __spec__=None, ) @property diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index eed02737e8bd48..15d9eaa1f93cb2 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -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 index 00000000000000..844340583cd456 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-02-29-20-06-06.gh-issue-87115.FVMiOR.rst @@ -0,0 +1 @@ +Set ``__main__.__spec__`` to ``None`` when running a script with :mod:`pdb`