https://github.com/python/cpython/commit/fdcbc29f26448f47201ec40fcf3b6405631c85d3
commit: fdcbc29f26448f47201ec40fcf3b6405631c85d3
branch: main
author: Tian Gao <gaogaotiant...@hotmail.com>
committer: gaogaotiantian <gaogaotiant...@hotmail.com>
date: 2025-02-28T13:15:55-05:00
summary:

gh-130660: Restore sys.ps1 and sys.ps2 after code.interact (#130661)

files:
A Misc/NEWS.d/next/Library/2025-02-28-01-10-14.gh-issue-130660.VIThEz.rst
M Lib/code.py
M Lib/test/test_code_module.py

diff --git a/Lib/code.py b/Lib/code.py
index 1cc2ed8b1dbf28..41331dfd071f11 100644
--- a/Lib/code.py
+++ b/Lib/code.py
@@ -219,12 +219,17 @@ def interact(self, banner=None, exitmsg=None):
         """
         try:
             sys.ps1
+            delete_ps1_after = False
         except AttributeError:
             sys.ps1 = ">>> "
+            delete_ps1_after = True
         try:
-            sys.ps2
+            _ps2 = sys.ps2
+            delete_ps2_after = False
         except AttributeError:
             sys.ps2 = "... "
+            delete_ps2_after = True
+
         cprt = 'Type "help", "copyright", "credits" or "license" for more 
information.'
         if banner is None:
             self.write("Python %s on %s\n%s\n(%s)\n" %
@@ -287,6 +292,12 @@ def interact(self, banner=None, exitmsg=None):
             if _quit is not None:
                 builtins.quit = _quit
 
+            if delete_ps1_after:
+                del sys.ps1
+
+            if delete_ps2_after:
+                del sys.ps2
+
             if exitmsg is None:
                 self.write('now exiting %s...\n' % self.__class__.__name__)
             elif exitmsg != '':
diff --git a/Lib/test/test_code_module.py b/Lib/test/test_code_module.py
index faa0b38f8373e3..57fb130070b34e 100644
--- a/Lib/test/test_code_module.py
+++ b/Lib/test/test_code_module.py
@@ -39,19 +39,47 @@ def setUp(self):
         self.mock_sys()
 
     def test_ps1(self):
-        self.infunc.side_effect = EOFError('Finished')
+        self.infunc.side_effect = [
+            "import code",
+            "code.sys.ps1",
+            EOFError('Finished')
+        ]
         self.console.interact()
-        self.assertEqual(self.sysmod.ps1, '>>> ')
+        output = ''.join(''.join(call[1]) for call in self.stdout.method_calls)
+        self.assertIn('>>> ', output)
+        self.assertNotHasAttr(self.sysmod, 'ps1')
+
+        self.infunc.side_effect = [
+            "import code",
+            "code.sys.ps1",
+            EOFError('Finished')
+        ]
         self.sysmod.ps1 = 'custom1> '
         self.console.interact()
+        output = ''.join(''.join(call[1]) for call in self.stdout.method_calls)
+        self.assertIn('custom1> ', output)
         self.assertEqual(self.sysmod.ps1, 'custom1> ')
 
     def test_ps2(self):
-        self.infunc.side_effect = EOFError('Finished')
+        self.infunc.side_effect = [
+            "import code",
+            "code.sys.ps2",
+            EOFError('Finished')
+        ]
         self.console.interact()
-        self.assertEqual(self.sysmod.ps2, '... ')
+        output = ''.join(''.join(call[1]) for call in self.stdout.method_calls)
+        self.assertIn('... ', output)
+        self.assertNotHasAttr(self.sysmod, 'ps2')
+
+        self.infunc.side_effect = [
+            "import code",
+            "code.sys.ps2",
+            EOFError('Finished')
+        ]
         self.sysmod.ps2 = 'custom2> '
         self.console.interact()
+        output = ''.join(''.join(call[1]) for call in self.stdout.method_calls)
+        self.assertIn('custom2> ', output)
         self.assertEqual(self.sysmod.ps2, 'custom2> ')
 
     def test_console_stderr(self):
diff --git 
a/Misc/NEWS.d/next/Library/2025-02-28-01-10-14.gh-issue-130660.VIThEz.rst 
b/Misc/NEWS.d/next/Library/2025-02-28-01-10-14.gh-issue-130660.VIThEz.rst
new file mode 100644
index 00000000000000..92984e7e2d53c8
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-02-28-01-10-14.gh-issue-130660.VIThEz.rst
@@ -0,0 +1 @@
+``sys.ps1`` and ``sys.ps2`` are now restored after :func:`code.interact` call.

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to