]> granicus.if.org Git - python/commitdiff
Merged revisions 83381 via svnmerge from
authorR. David Murray <rdmurray@bitdance.com>
Sun, 1 Aug 2010 04:11:05 +0000 (04:11 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Sun, 1 Aug 2010 04:11:05 +0000 (04:11 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/release27-maint

................
  r83381 | r.david.murray | 2010-08-01 00:04:03 -0400 (Sun, 01 Aug 2010) | 23 lines

  Merged revisions 83380 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/branches/py3k

  ........
    r83380 | r.david.murray | 2010-07-31 23:31:09 -0400 (Sat, 31 Jul 2010) | 17 lines

    #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.
  ........
................

Lib/cmd.py
Lib/test/test_cmd.py
Misc/ACKS
Misc/NEWS

index 3f82b487103fffb3b8e67320dc78f0d0ac363bba..ec62c740eb52bf447781bfe568dc9a49b4a6ec91 100644 (file)
@@ -137,7 +137,7 @@ class Cmd:
                         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)
index 8898a32cb91e4ecd3f2a9b76d60b429cf52943c6..b48b0f11f3e10f5489f57848e922835802e79a4c 100644 (file)
@@ -7,6 +7,9 @@ Original by Michael Schneider
 
 import cmd
 import sys
+import re
+import unittest
+import StringIO
 
 class samplecmdclass(cmd.Cmd):
     """
@@ -165,9 +168,33 @@ class samplecmdclass(cmd.Cmd):
     def do_exit(self, arg):
         return True
 
+
+class TestAlternateInput(unittest.TestCase):
+
+    class simplecmd(cmd.Cmd):
+
+        def do_print(self, args):
+            print >>self.stdout, args
+
+        def do_EOF(self, args):
+            return True
+
+    def test_file_with_missing_final_nl(self):
+        input = StringIO.StringIO("print test\nprint test2")
+        output = StringIO.StringIO()
+        cmd = self.simplecmd(stdin=input, stdout=output)
+        cmd.use_rawinput = False
+        cmd.cmdloop()
+        self.assertEqual(output.getvalue(),
+            ("(Cmd) test\n"
+             "(Cmd) test2\n"
+             "(Cmd) "))
+
+
 def test_main(verbose=None):
     from test import test_support, test_cmd
     test_support.run_doctest(test_cmd, verbose)
+    test_support.run_unittest(TestAlternateInput)
 
 def test_coverage(coverdir):
     import trace
index 96523fea6c58f284b1a8c43d67e4e37f294b6107..2f6055e58361decc53b40bdc9c73d52825a4997a 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -170,6 +170,7 @@ Vincent Delft
 Arnaud Delobelle
 Erik Demaine
 Roger Dev
+Catherine Devlin
 Raghuram Devarakonda
 Scott Dial
 Toby Dickenson
index 307c3329790ffac3aec533db360774b3df5855f7..56b289e29bee10b8d901bdae87de6793eefbf77b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -84,6 +84,9 @@ C-API
 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 #7066: archive_util.make_archive now restores the cwd if an error is 
   raised. Initial patch by Ezio Melotti.