[Python-checkins] r83380 - in python/branches/py3k: Lib/cmd.py Lib/test/test_cmd.py Misc/ACKS Misc/NEWS

r.david.murray python-checkins at python.org
Sun Aug 1 05:31:09 CEST 2010


Author: r.david.murray
Date: Sun Aug  1 05:31:09 2010
New Revision: 83380

Log:
#8620: Cmd no longer truncates last character if stdin ends without newline

Cmd used to blindly chop off the last character of every input line.  If
the input reached EOF and there was no final new line, it would truncate
the last character of the last command.  This fix instead strips trailing
\r\n from the input lines.  While this is a small behavior change, it
should not break any working code, since feeding a '\r\n' terminated
file to Cmd would previously leave the \r's on the lines, resulting
in failed command execution.

I wrote the unit test in preparation for a PyOhio TeachMe session
run by Catherine Devlin, and we can thank Catherine and the PyOhio
session attendees for the fix.  I've added Catherine to the Acks file
for organizing and leading the TeachMe session, out of which we will
hopefully get some new contributors.



Modified:
   python/branches/py3k/Lib/cmd.py
   python/branches/py3k/Lib/test/test_cmd.py
   python/branches/py3k/Misc/ACKS
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Lib/cmd.py
==============================================================================
--- python/branches/py3k/Lib/cmd.py	(original)
+++ python/branches/py3k/Lib/cmd.py	Sun Aug  1 05:31:09 2010
@@ -133,7 +133,7 @@
                         if not len(line):
                             line = 'EOF'
                         else:
-                            line = line[:-1] # chop \n
+                            line = line.rstrip('\r\n')
                 line = self.precmd(line)
                 stop = self.onecmd(line)
                 stop = self.postcmd(stop, line)

Modified: python/branches/py3k/Lib/test/test_cmd.py
==============================================================================
--- python/branches/py3k/Lib/test/test_cmd.py	(original)
+++ python/branches/py3k/Lib/test/test_cmd.py	Sun Aug  1 05:31:09 2010
@@ -8,7 +8,8 @@
 import cmd
 import sys
 import re
-from io import StringIO
+import unittest
+import io
 from test import support
 
 class samplecmdclass(cmd.Cmd):
@@ -168,9 +169,33 @@
     def do_exit(self, arg):
         return True
 
+
+class TestAlternateInput(unittest.TestCase):
+
+    class simplecmd(cmd.Cmd):
+
+        def do_print(self, args):
+            print(args, file=self.stdout)
+
+        def do_EOF(self, args):
+            return True
+
+    def test_file_with_missing_final_nl(self):
+        input = io.StringIO("print test\nprint test2")
+        output = io.StringIO()
+        cmd = self.simplecmd(stdin=input, stdout=output)
+        cmd.use_rawinput = False
+        cmd.cmdloop()
+        self.assertMultiLineEqual(output.getvalue(),
+            ("(Cmd) test\n"
+             "(Cmd) test2\n"
+             "(Cmd) "))
+
+
 def test_main(verbose=None):
     from test import test_cmd
     support.run_doctest(test_cmd, verbose)
+    support.run_unittest(TestAlternateInput)
 
 def test_coverage(coverdir):
     trace = support.import_module('trace')

Modified: python/branches/py3k/Misc/ACKS
==============================================================================
--- python/branches/py3k/Misc/ACKS	(original)
+++ python/branches/py3k/Misc/ACKS	Sun Aug  1 05:31:09 2010
@@ -195,6 +195,7 @@
 Arnaud Delobelle
 Erik Demaine
 Roger Dev
+Catherine Devlin
 Raghuram Devarakonda
 Caleb Deveraux
 Toby Dickenson

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Sun Aug  1 05:31:09 2010
@@ -15,6 +15,9 @@
 Library
 -------
 
+- Issue #8620: when a Cmd is fed input that reaches EOF without a final
+  newline, it no longer truncates the last character of the last command line.
+
 - Issue #5146: Handle UID THREAD command correctly in imaplib.
 
 - Issue #5147: Fix the header generated for cookie files written by


More information about the Python-checkins mailing list