diff --git a/Lib/cmd.py b/Lib/cmd.py index 6f34e047284..10aa5acf4be 100644 --- a/Lib/cmd.py +++ b/Lib/cmd.py @@ -134,7 +134,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 e2b3022a2a5..7a13d27e151 100644 --- a/Lib/test/test_cmd.py +++ b/Lib/test/test_cmd.py @@ -9,7 +9,8 @@ import cmd import sys import trace import re -from io import StringIO +import unittest +import io class samplecmdclass(cmd.Cmd): """ @@ -166,9 +167,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 support, test_cmd support.run_doctest(test_cmd, verbose) + support.run_unittest(TestAlternateInput) def test_coverage(coverdir): tracer=trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], diff --git a/Misc/ACKS b/Misc/ACKS index a9f7885b299..519dda488c4 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -179,6 +179,7 @@ Vincent Delft Arnaud Delobelle Erik Demaine Roger Dev +Catherine Devlin Raghuram Devarakonda Toby Dickenson Mark Dickinson diff --git a/Misc/NEWS b/Misc/NEWS index 95051913b6e..8d665db90ea 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -78,6 +78,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 #3704: http.cookiejar was not properly handling URLs with a / in the parameters.