From 565f15c97deca4b1a22f0cb34e6e188908e6d68b Mon Sep 17 00:00:00 2001 From: Yan Yanchii Date: Tue, 31 Dec 2024 18:08:35 +0100 Subject: [PATCH 1/8] Fix `test_cmd_line.test_non_interactive_output_buffering` fail with `PYTHONUNBUFFERED=1` --- Lib/test/test_cmd_line.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 634efda354407f..1366bf68f0cd95 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -337,6 +337,9 @@ def test_osx_android_utf8(self): self.assertEqual(p.returncode, 0) def test_non_interactive_output_buffering(self): + PYTHONUNBUFFERED_ENV_VAR = os.environ["PYTHONUNBUFFERED"] + # we expect buffered stdio + os.environ["PYTHONUNBUFFERED"] = "0" code = textwrap.dedent(""" import sys out = sys.stdout @@ -350,6 +353,8 @@ def test_non_interactive_output_buffering(self): self.assertEqual(proc.stdout, 'False False False\n' 'False False True\n') + # restore original value + os.environ["PYTHONUNBUFFERED"] = PYTHONUNBUFFERED_ENV_VAR def test_unbuffered_output(self): # Test expected operation of the '-u' switch From 7ff7de3fd15a2ba4697298380d66cb111fc43b2b Mon Sep 17 00:00:00 2001 From: Yan Yanchii Date: Tue, 31 Dec 2024 18:50:19 +0100 Subject: [PATCH 2/8] Use get if `PYTHONUNBUFFERED` is not defined` --- Lib/test/test_cmd_line.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 1366bf68f0cd95..d87bc5ffa31c2e 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -337,9 +337,13 @@ def test_osx_android_utf8(self): self.assertEqual(p.returncode, 0) def test_non_interactive_output_buffering(self): - PYTHONUNBUFFERED_ENV_VAR = os.environ["PYTHONUNBUFFERED"] - # we expect buffered stdio - os.environ["PYTHONUNBUFFERED"] = "0" + PYTHONUNBUFFERED = "PYTHONUNBUFFERED" + python_unbuffered_value = os.environ.get(PYTHONUNBUFFERED, "0") + python_unbuffered_on = python_unbuffered_value != "0" + if python_unbuffered_on: + # we expect buffered stdio + os.environ[PYTHONUNBUFFERED] = "0" + code = textwrap.dedent(""" import sys out = sys.stdout @@ -353,8 +357,10 @@ def test_non_interactive_output_buffering(self): self.assertEqual(proc.stdout, 'False False False\n' 'False False True\n') - # restore original value - os.environ["PYTHONUNBUFFERED"] = PYTHONUNBUFFERED_ENV_VAR + + if python_unbuffered_on: + # restore original value + os.environ[PYTHONUNBUFFERED] = python_unbuffered_value def test_unbuffered_output(self): # Test expected operation of the '-u' switch From a42798daebb752c9839fa7610304829a8091b0bb Mon Sep 17 00:00:00 2001 From: Yan Yanchii Date: Wed, 1 Jan 2025 13:00:04 +0100 Subject: [PATCH 3/8] Use `EnvironmentVarGuard` to set `PYTHONUNBUFFERED` --- Lib/test/test_cmd_line.py | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index d87bc5ffa31c2e..40e6474b515a75 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -337,30 +337,22 @@ def test_osx_android_utf8(self): self.assertEqual(p.returncode, 0) def test_non_interactive_output_buffering(self): - PYTHONUNBUFFERED = "PYTHONUNBUFFERED" - python_unbuffered_value = os.environ.get(PYTHONUNBUFFERED, "0") - python_unbuffered_on = python_unbuffered_value != "0" - if python_unbuffered_on: + with os_helper.EnvironmentVarGuard() as environ: # we expect buffered stdio - os.environ[PYTHONUNBUFFERED] = "0" - - code = textwrap.dedent(""" - import sys - out = sys.stdout - print(out.isatty(), out.write_through, out.line_buffering) - err = sys.stderr - print(err.isatty(), err.write_through, err.line_buffering) - """) - args = [sys.executable, '-c', code] - proc = subprocess.run(args, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, check=True) - self.assertEqual(proc.stdout, - 'False False False\n' - 'False False True\n') - - if python_unbuffered_on: - # restore original value - os.environ[PYTHONUNBUFFERED] = python_unbuffered_value + environ["PYTHONUNBUFFERED"] = "0" + code = textwrap.dedent(""" + import sys + out = sys.stdout + print(out.isatty(), out.write_through, out.line_buffering) + err = sys.stderr + print(err.isatty(), err.write_through, err.line_buffering) + """) + args = [sys.executable, '-c', code] + proc = subprocess.run(args, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, check=True) + self.assertEqual(proc.stdout, + 'False False False\n' + 'False False True\n') def test_unbuffered_output(self): # Test expected operation of the '-u' switch From 6bc56b8210184f8e00494c9eb4839dfb7f66bb82 Mon Sep 17 00:00:00 2001 From: Yan Yanchii Date: Thu, 2 Jan 2025 20:12:35 +0100 Subject: [PATCH 4/8] use unittest.skipIf --- Lib/test/test_cmd_line.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 40e6474b515a75..2724360d37363f 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -336,23 +336,21 @@ def test_osx_android_utf8(self): self.assertEqual(stdout, expected) self.assertEqual(p.returncode, 0) + @unittest.skipIf(os.environ.get("PYTHONUNBUFFERED", "0") != "0", "Python stdio buffering is disabled.") def test_non_interactive_output_buffering(self): - with os_helper.EnvironmentVarGuard() as environ: - # we expect buffered stdio - environ["PYTHONUNBUFFERED"] = "0" - code = textwrap.dedent(""" - import sys - out = sys.stdout - print(out.isatty(), out.write_through, out.line_buffering) - err = sys.stderr - print(err.isatty(), err.write_through, err.line_buffering) - """) - args = [sys.executable, '-c', code] - proc = subprocess.run(args, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, check=True) - self.assertEqual(proc.stdout, - 'False False False\n' - 'False False True\n') + code = textwrap.dedent(""" + import sys + out = sys.stdout + print(out.isatty(), out.write_through, out.line_buffering) + err = sys.stderr + print(err.isatty(), err.write_through, err.line_buffering) + """) + args = [sys.executable, '-c', code] + proc = subprocess.run(args, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, check=True) + self.assertEqual(proc.stdout, + 'False False False\n' + 'False False True\n') def test_unbuffered_output(self): # Test expected operation of the '-u' switch From c13851c6f8289e196baf18d44d569d5ee7c0ba22 Mon Sep 17 00:00:00 2001 From: Yan Yanchii Date: Thu, 2 Jan 2025 20:13:59 +0100 Subject: [PATCH 5/8] fix spaces --- Lib/test/test_cmd_line.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 2724360d37363f..5988e4d055e290 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -347,10 +347,10 @@ def test_non_interactive_output_buffering(self): """) args = [sys.executable, '-c', code] proc = subprocess.run(args, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, check=True) + stderr=subprocess.PIPE, text=True, check=True) self.assertEqual(proc.stdout, - 'False False False\n' - 'False False True\n') + 'False False False\n' + 'False False True\n') def test_unbuffered_output(self): # Test expected operation of the '-u' switch From db3031ad3d7691e17be174039e6db1d1f39f8439 Mon Sep 17 00:00:00 2001 From: Yan Yanchii Date: Thu, 2 Jan 2025 21:00:58 +0100 Subject: [PATCH 6/8] add line break --- Lib/test/test_cmd_line.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 5988e4d055e290..2ea1ba9f49695c 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -336,7 +336,8 @@ def test_osx_android_utf8(self): self.assertEqual(stdout, expected) self.assertEqual(p.returncode, 0) - @unittest.skipIf(os.environ.get("PYTHONUNBUFFERED", "0") != "0", "Python stdio buffering is disabled.") + @unittest.skipIf(os.environ.get("PYTHONUNBUFFERED", "0") != "0", + "Python stdio buffering is disabled.") def test_non_interactive_output_buffering(self): code = textwrap.dedent(""" import sys From 9aa58665b414d554dd347f75b4f9c8241a7cd119 Mon Sep 17 00:00:00 2001 From: Yan Yanchii Date: Thu, 2 Jan 2025 21:05:32 +0100 Subject: [PATCH 7/8] fix lint --- Lib/test/test_cmd_line.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 2ea1ba9f49695c..ecc79c433ecc50 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -336,8 +336,8 @@ def test_osx_android_utf8(self): self.assertEqual(stdout, expected) self.assertEqual(p.returncode, 0) - @unittest.skipIf(os.environ.get("PYTHONUNBUFFERED", "0") != "0", - "Python stdio buffering is disabled.") + @unittest.skipIf(os.environ.get("PYTHONUNBUFFERED", "0") != "0", + "Python stdio buffering is disabled.") def test_non_interactive_output_buffering(self): code = textwrap.dedent(""" import sys From cf6f0fc041cc102d4735e57a2a2cc3df33ddae98 Mon Sep 17 00:00:00 2001 From: Yan Yanchii Date: Sat, 11 Jan 2025 19:53:41 +0100 Subject: [PATCH 8/8] fix alignment --- Lib/test/test_cmd_line.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index ecc79c433ecc50..04f7543da4c411 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -337,7 +337,7 @@ def test_osx_android_utf8(self): self.assertEqual(p.returncode, 0) @unittest.skipIf(os.environ.get("PYTHONUNBUFFERED", "0") != "0", - "Python stdio buffering is disabled.") + "Python stdio buffering is disabled.") def test_non_interactive_output_buffering(self): code = textwrap.dedent(""" import sys