summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorR. David Murray <rdmurray@bitdance.com>2010-08-01 03:31:09 (GMT)
committerR. David Murray <rdmurray@bitdance.com>2010-08-01 03:31:09 (GMT)
commit7905d61b2cff841dfdc79bfca92ce23c717ab384 (patch)
treefe25e85b5b72595a14145fc4c6007a8f61131529
parentf0f2e65a54e66a6137dc8cd7b580f3775985c556 (diff)
downloadcpython-7905d61b2cff841dfdc79bfca92ce23c717ab384.zip
cpython-7905d61b2cff841dfdc79bfca92ce23c717ab384.tar.gz
cpython-7905d61b2cff841dfdc79bfca92ce23c717ab384.tar.bz2
#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.
-rw-r--r--Lib/cmd.py2
-rw-r--r--Lib/test/test_cmd.py27
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS3
4 files changed, 31 insertions, 2 deletions
diff --git a/Lib/cmd.py b/Lib/cmd.py
index c791d6b..85282f8 100644
--- a/Lib/cmd.py
+++ b/Lib/cmd.py
@@ -133,7 +133,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)
diff --git a/Lib/test/test_cmd.py b/Lib/test/test_cmd.py
index b3ad5d0..75aa6bb 100644
--- a/Lib/test/test_cmd.py
+++ b/Lib/test/test_cmd.py
@@ -8,7 +8,8 @@ Original by Michael Schneider
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 @@ 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(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')
diff --git a/Misc/ACKS b/Misc/ACKS
index 3c186b4..3d02af7 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -195,6 +195,7 @@ Vincent Delft
Arnaud Delobelle
Erik Demaine
Roger Dev
+Catherine Devlin
Raghuram Devarakonda
Caleb Deveraux
Toby Dickenson
diff --git a/Misc/NEWS b/Misc/NEWS
index fffed33..fd47e96 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,9 @@ Core and Builtins
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