Skip to content

Commit fbc3c3c

Browse files
committed
Closes python#17730: in code.interact(), when banner="", do not print anything.
Also adds tests for banner printing.
1 parent a8fc7f6 commit fbc3c3c

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

Doc/library/code.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,15 @@ interpreter objects as well as the following additions.
132132

133133
.. method:: InteractiveConsole.interact(banner=None)
134134

135-
Closely emulate the interactive Python console. The optional banner argument
135+
Closely emulate the interactive Python console. The optional *banner* argument
136136
specify the banner to print before the first interaction; by default it prints a
137137
banner similar to the one printed by the standard Python interpreter, followed
138138
by the class name of the console object in parentheses (so as not to confuse
139139
this with the real interpreter -- since it's so close!).
140140

141+
.. versionchanged:: 3.4
142+
To suppress printing any banner, pass an empty string.
143+
141144

142145
.. method:: InteractiveConsole.push(line)
143146

Lib/code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def interact(self, banner=None):
216216
self.write("Python %s on %s\n%s\n(%s)\n" %
217217
(sys.version, sys.platform, cprt,
218218
self.__class__.__name__))
219-
else:
219+
elif banner:
220220
self.write("%s\n" % str(banner))
221221
more = 0
222222
while 1:

Lib/test/test_code_module.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ def test_sysexcepthook(self):
6464
self.console.interact()
6565
self.assertTrue(hook.called)
6666

67+
def test_banner(self):
68+
# with banner
69+
self.infunc.side_effect = EOFError('Finished')
70+
self.console.interact(banner='Foo')
71+
self.assertEqual(len(self.stderr.method_calls), 2)
72+
banner_call = self.stderr.method_calls[0]
73+
self.assertEqual(banner_call, ['write', ('Foo\n',), {}])
74+
75+
# no banner
76+
self.stderr.reset_mock()
77+
self.infunc.side_effect = EOFError('Finished')
78+
self.console.interact(banner='')
79+
self.assertEqual(len(self.stderr.method_calls), 1)
80+
6781

6882
def test_main():
6983
support.run_unittest(TestInteractiveConsole)

0 commit comments

Comments
 (0)