GH-102895 Add an option local_exit in code.interact to block exit() from terminating the whole process (GH-102896)

This commit is contained in:
Tian Gao 2023-10-18 11:36:43 -07:00 committed by GitHub
parent cb1bf89c40
commit e6eb8cafca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 114 additions and 33 deletions

View file

@ -10,11 +10,7 @@ from test.support import import_helper
code = import_helper.import_module('code')
class TestInteractiveConsole(unittest.TestCase):
def setUp(self):
self.console = code.InteractiveConsole()
self.mock_sys()
class MockSys:
def mock_sys(self):
"Mock system environment for InteractiveConsole"
@ -32,6 +28,13 @@ class TestInteractiveConsole(unittest.TestCase):
del self.sysmod.ps1
del self.sysmod.ps2
class TestInteractiveConsole(unittest.TestCase, MockSys):
def setUp(self):
self.console = code.InteractiveConsole()
self.mock_sys()
def test_ps1(self):
self.infunc.side_effect = EOFError('Finished')
self.console.interact()
@ -151,5 +154,21 @@ class TestInteractiveConsole(unittest.TestCase):
self.assertIn(expected, output)
class TestInteractiveConsoleLocalExit(unittest.TestCase, MockSys):
def setUp(self):
self.console = code.InteractiveConsole(local_exit=True)
self.mock_sys()
def test_exit(self):
# default exit message
self.infunc.side_effect = ["exit()"]
self.console.interact(banner='')
self.assertEqual(len(self.stderr.method_calls), 2)
err_msg = self.stderr.method_calls[1]
expected = 'now exiting InteractiveConsole...\n'
self.assertEqual(err_msg, ['write', (expected,), {}])
if __name__ == "__main__":
unittest.main()