]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-118158: Fix missing newline in py_compile CLI error output (#149008)
authorXiao Yuan <yuanx749@gmail.com>
Tue, 16 Jun 2026 16:41:52 +0000 (19:41 +0300)
committerGitHub <noreply@github.com>
Tue, 16 Jun 2026 16:41:52 +0000 (09:41 -0700)
Restore trailing newline in error messages from `python -m py_compile`,
lost when `main()` was refactored to use argparse.

Lib/py_compile.py
Lib/test/test_py_compile.py
Misc/NEWS.d/next/Library/2026-04-26-11-25-38.gh-issue-118158.xIy05H.rst [new file with mode: 0644]

index 7ca479141e01e4ef93f25a41a99b871cd9036b07..b22446e2f098dee18615231d6e36f7d19b5f098d 100644 (file)
@@ -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__":
index da2d630d7ace7bb84f4dd0bb5c2809266be8a81d..09113e983fcd94d8f586e0a22a72629c8dd2e411 100644 (file)
@@ -327,6 +327,12 @@ class PyCompileCLITestCase(unittest.TestCase):
         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)
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 (file)
index 0000000..bd22871
--- /dev/null
@@ -0,0 +1 @@
+Ensure py_compile CLI error messages end with a newline. Contributed by Xiao Yuan.