From b53ad2192157ae4478867234c6b8cf0ede059fb5 Mon Sep 17 00:00:00 2001 From: yuanx749 Date: Sun, 26 Apr 2026 11:13:24 +0300 Subject: [PATCH 1/3] Add test --- Lib/test/test_py_compile.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/test/test_py_compile.py b/Lib/test/test_py_compile.py index da2d630d7ace7b..09113e983fcd94 100644 --- a/Lib/test/test_py_compile.py +++ b/Lib/test/test_py_compile.py @@ -327,6 +327,12 @@ def test_bad_syntax_with_quiet(self): self.assertEqual(stdout, b'') self.assertEqual(stderr, b'') + def test_bad_syntax_stderr_ends_with_newline(self): + with open(self.source_path, 'w') as file: + file.write(' print("hello world")\n') + rc, stdout, stderr = self.pycompilecmd_failure(self.source_path) + self.assertTrue(stderr.endswith(b'\n')) + def test_file_not_exists(self): should_not_exists = os.path.join(os.path.dirname(__file__), 'should_not_exists.py') rc, stdout, stderr = self.pycompilecmd_failure(self.source_path, should_not_exists) From 885680a82e7eacf0415665a9529698de0c535527 Mon Sep 17 00:00:00 2001 From: yuanx749 Date: Sun, 26 Apr 2026 11:16:17 +0300 Subject: [PATCH 2/3] Fix --- Lib/py_compile.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/py_compile.py b/Lib/py_compile.py index 694ea9304da9f9..a12217fc6cc53e 100644 --- a/Lib/py_compile.py +++ b/Lib/py_compile.py @@ -202,12 +202,12 @@ def main(): if args.quiet: parser.exit(1) else: - parser.exit(1, error.msg) + parser.exit(1, error.msg + '\n') except OSError as error: if args.quiet: parser.exit(1) else: - parser.exit(1, str(error)) + parser.exit(1, str(error) + '\n') if __name__ == "__main__": From af26ddf916848b066c45dc4ff826d97c5948b01e Mon Sep 17 00:00:00 2001 From: yuanx749 Date: Sun, 26 Apr 2026 11:29:34 +0300 Subject: [PATCH 3/3] Add news --- .../next/Library/2026-04-26-11-25-38.gh-issue-118158.xIy05H.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-04-26-11-25-38.gh-issue-118158.xIy05H.rst diff --git a/Misc/NEWS.d/next/Library/2026-04-26-11-25-38.gh-issue-118158.xIy05H.rst b/Misc/NEWS.d/next/Library/2026-04-26-11-25-38.gh-issue-118158.xIy05H.rst new file mode 100644 index 00000000000000..bd22871c1c0c17 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-04-26-11-25-38.gh-issue-118158.xIy05H.rst @@ -0,0 +1 @@ +Ensure py_compile CLI error messages end with a newline. Contributed by Xiao Yuan.