mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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:
parent
cb1bf89c40
commit
e6eb8cafca
5 changed files with 114 additions and 33 deletions
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue