Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Lib/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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" %
Expand Down Expand Up @@ -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 != '':
Expand Down
36 changes: 32 additions & 4 deletions Lib/test/test_code_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.assertFalse(hasattr(self.sysmod, 'ps1'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertNotHasAttr

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it depends on whether we consider this a bug fix and backport it. assertNotHasAttr is new in 3.14 which would cause issues when we backport the test. If we only want to fix this in 3.14 (which is fine by me because I only need this in 3.14), then sure we can use the latest method.

@gvanrossum do you think we should backport this? Or just leave it be for the previous versions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods were backported as private helpers in test.support. You will need to add an import and a mix-in, but you will get clearer testing code with better error reporting.


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.assertFalse(hasattr(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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``sys.ps1`` and ``sys.ps2`` are now restored after :func:`code.interact` call.
Loading