[Python-checkins] cpython: Closes #17730: in code.interact(), when banner="", do not print anything.

georg.brandl python-checkins at python.org
Sun Oct 13 21:48:40 CEST 2013


http://hg.python.org/cpython/rev/2933598a7802
changeset:   86331:2933598a7802
user:        Georg Brandl <georg at python.org>
date:        Sun Oct 13 21:49:06 2013 +0200
summary:
  Closes #17730: in code.interact(), when banner="", do not print anything.

Also adds tests for banner printing.

files:
  Doc/library/code.rst         |   5 ++++-
  Lib/code.py                  |   2 +-
  Lib/test/test_code_module.py |  14 ++++++++++++++
  3 files changed, 19 insertions(+), 2 deletions(-)


diff --git a/Doc/library/code.rst b/Doc/library/code.rst
--- a/Doc/library/code.rst
+++ b/Doc/library/code.rst
@@ -132,12 +132,15 @@
 
 .. method:: InteractiveConsole.interact(banner=None)
 
-   Closely emulate the interactive Python console. The optional banner argument
+   Closely emulate the interactive Python console. The optional *banner* argument
    specify the banner to print before the first interaction; by default it prints a
    banner similar to the one printed by the standard Python interpreter, followed
    by the class name of the console object in parentheses (so as not to confuse
    this with the real interpreter -- since it's so close!).
 
+   .. versionchanged:: 3.4
+      To suppress printing any banner, pass an empty string.
+
 
 .. method:: InteractiveConsole.push(line)
 
diff --git a/Lib/code.py b/Lib/code.py
--- a/Lib/code.py
+++ b/Lib/code.py
@@ -216,7 +216,7 @@
             self.write("Python %s on %s\n%s\n(%s)\n" %
                        (sys.version, sys.platform, cprt,
                         self.__class__.__name__))
-        else:
+        elif banner:
             self.write("%s\n" % str(banner))
         more = 0
         while 1:
diff --git a/Lib/test/test_code_module.py b/Lib/test/test_code_module.py
--- a/Lib/test/test_code_module.py
+++ b/Lib/test/test_code_module.py
@@ -64,6 +64,20 @@
         self.console.interact()
         self.assertTrue(hook.called)
 
+    def test_banner(self):
+        # with banner
+        self.infunc.side_effect = EOFError('Finished')
+        self.console.interact(banner='Foo')
+        self.assertEqual(len(self.stderr.method_calls), 2)
+        banner_call = self.stderr.method_calls[0]
+        self.assertEqual(banner_call, ['write', ('Foo\n',), {}])
+
+        # no banner
+        self.stderr.reset_mock()
+        self.infunc.side_effect = EOFError('Finished')
+        self.console.interact(banner='')
+        self.assertEqual(len(self.stderr.method_calls), 1)
+
 
 def test_main():
     support.run_unittest(TestInteractiveConsole)

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list