diff --git a/Lib/cmd.py b/Lib/cmd.py index 3f82b487103..ec62c740eb5 100644 --- a/Lib/cmd.py +++ b/Lib/cmd.py @@ -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) diff --git a/Lib/test/test_cmd.py b/Lib/test/test_cmd.py index 8898a32cb91..b48b0f11f3e 100644 --- a/Lib/test/test_cmd.py +++ b/Lib/test/test_cmd.py @@ -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 diff --git a/Misc/ACKS b/Misc/ACKS index 96523fea6c5..2f6055e5836 100644 --- 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 diff --git a/Misc/NEWS b/Misc/NEWS index 307c3329790..56b289e29be 100644 --- 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.