From 2269f76836a039970073808677804f4abf4d9c32 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Mon, 3 Jun 2024 18:58:24 -0300 Subject: [PATCH 01/10] Add some missing environment variables to '--help-env'. --- Python/initconfig.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Python/initconfig.c b/Python/initconfig.c index a28c08c5318ddc..9b8433839b57d8 100644 --- a/Python/initconfig.c +++ b/Python/initconfig.c @@ -249,6 +249,7 @@ static const char usage_envvars[] = "PYTHONMALLOC : set the Python memory allocators and/or install debug hooks\n" " on Python memory allocators. Use PYTHONMALLOC=debug to\n" " install debug hooks.\n" +"PYTHONMALLOCSTATS: print memory allocator statistics\n" "PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n" " coercion behavior. Use PYTHONCOERCECLOCALE=warn to request\n" " display of locale coercion and locale compatibility warnings\n" @@ -260,6 +261,20 @@ static const char usage_envvars[] = " various kinds of output. Setting it to 0 deactivates\n" " this behavior.\n" "PYTHON_HISTORY : the location of a .python_history file.\n" +"PYTHONASYNCIODEBUG: enable asyncio debug mode\n" +#ifdef Py_DEBUG +"PYTHONDUMPREFS : dump objects and reference counts still alive after shutdown\n" +"PYTHONDUMPREFSFILE: dump objects and reference counts to the specified file\n" +#endif +#ifdef __APPLE__ +"PYTHONEXECUTABLE: set sys.argv[0] to this value (macOS only)\n" +#endif +#ifdef MS_WINDOWS +"PYTHONLEGACYWINDOWSFSENCODING: use legacy \"mbcs\" encoding for file system\n" +"PYTHONLEGACYWINDOWSSTDIO: use legacy Windows stdio\n" +#endif +"PYTHONUSERBASE : defines the user base directory (site.USER_BASE)\n" +"PYTHON_BASIC_REPL: use the traditional parser-based REPL\n" "\n" "These variables have equivalent command-line options (see --help for details):\n" "PYTHON_CPU_COUNT: override the return value of os.cpu_count() (-X cpu_count)\n" @@ -281,6 +296,8 @@ static const char usage_envvars[] = "PYTHONNOUSERSITE: disable user site directory (-s)\n" "PYTHONOPTIMIZE : enable level 1 optimizations (-O)\n" "PYTHONPERFSUPPORT: support the Linux \"perf\" profiler (-X perf)\n" +"PYTHON_PERF_JIT_SUPPORT: enable Linux \"perf\" profiler support with JIT\n" +" (-X perf_jit)\n" #ifdef Py_DEBUG "PYTHON_PRESITE: import this module before site (-X presite)\n" #endif From b0fc110959053467c20a5a081f68dc7d8e882605 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 3 Jun 2024 22:06:29 +0000 Subject: [PATCH 02/10] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Documentation/2024-06-03-22-06-26.gh-issue-119574.Ik9kOO.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Documentation/2024-06-03-22-06-26.gh-issue-119574.Ik9kOO.rst diff --git a/Misc/NEWS.d/next/Documentation/2024-06-03-22-06-26.gh-issue-119574.Ik9kOO.rst b/Misc/NEWS.d/next/Documentation/2024-06-03-22-06-26.gh-issue-119574.Ik9kOO.rst new file mode 100644 index 00000000000000..902e7c17fc2e9d --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2024-06-03-22-06-26.gh-issue-119574.Ik9kOO.rst @@ -0,0 +1 @@ +Added some missing environment variables to the output of :option:`--help-env`. From e51a266febacf56c3741de538973f09445ff2ca4 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Sat, 8 Jun 2024 09:25:44 -0300 Subject: [PATCH 03/10] Add some tests for the env vars newly added to '--help-env'. --- Lib/test/test_cmd_line.py | 68 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 058470082fbbf0..aafec907843300 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -5,6 +5,7 @@ import os import subprocess import sys +import sysconfig import tempfile import textwrap import unittest @@ -912,6 +913,73 @@ def test_python_gil(self): self.assertEqual(proc.stdout.rstrip(), expected) self.assertEqual(proc.stderr, '') + def test_python_asyncio_debug(self): + code = "import asyncio; print(asyncio.get_event_loop().get_debug())" + rc, out, err = assert_python_ok('-c', code, PYTHONASYNCIODEBUG='1') + self.assertIn(b'True', out) + + @unittest.skipUnless(sysconfig.get_config_var('Py_TRACE_REFS'), "Requires --with-trace-refs build option") + def test_python_dump_refs(self): + code = 'import sys; sys._clear_type_cache()' + rc, out, err = assert_python_ok('-c', code, PYTHONDUMPREFS='1') + self.assertEqual(rc, 0) + + @unittest.skipUnless(sysconfig.get_config_var('Py_TRACE_REFS'), "Requires --with-trace-refs build option") + def test_python_dump_refs_file(self): + with tempfile.NamedTemporaryFile() as dump_file: + code = 'import sys; sys._clear_type_cache()' + rc, out, err = assert_python_ok('-c', code, PYTHONDUMPREFSFILE=dump_file.name) + self.assertEqual(rc, 0) + with open(dump_file.name, 'r') as file: + contents = file.read() + self.assertIn('Dumping', contents) + + @unittest.skipUnless(sys.platform == 'darwin', 'PYTHONEXECUTABLE only works on macOS') + def test_python_executable(self): + code = 'import sys; print(sys.executable)' + expected = "/busr/bbin/bpython" + rc, out, err = assert_python_ok('-c', code, PYTHONEXECUTABLE=expected) + self.assertIn(expected.encode(), out) + + @unittest.skipUnless(sys.platform == 'win32', 'Test only applicable on Windows') + def test_python_legacy_windows_fs_encoding(self): + code = "import sys; print(sys.getfilesystemencoding())" + expected = 'mbcs' + rc, out, err = assert_python_ok('-c', code, PYTHONLEGACYWINDOWSFSENCODING='1') + self.assertIn(expected.encode(), out) + + @unittest.skipUnless(sys.platform == 'win32', 'Test only applicable on Windows') + def test_python_legacy_windows_stdio(self): + code = "import sys; print(sys.stdin.encoding, sys.stdout.encoding)" + expected = 'cp' + rc, out, err = assert_python_ok('-c', code, PYTHONLEGACYWINDOWSSTDIO='1') + self.assertIn(expected.encode(), out) + + def test_python_malloc_stats(self): + code = "pass" + rc, out, err = assert_python_ok('-c', code, PYTHONMALLOCSTATS='1') + self.assertIn(b'Small block threshold', err) + + def test_python_user_base(self): + code = "import site; print(site.USER_BASE)" + expected = "/custom/userbase" + rc, out, err = assert_python_ok('-c', code, PYTHONUSERBASE=expected) + self.assertIn(expected.encode(), out) + + def test_python_basic_repl(self): + # Currently this only tests that the env var is set + code = "import os; print('PYTHON_BASIC_REPL' in os.environ)" + expected = "True" + rc, out, err = assert_python_ok('-c', code, PYTHON_BASIC_REPL='1') + self.assertIn(expected.encode(), out) + + @unittest.skipUnless(sysconfig.get_config_var('HAVE_PERF_TRAMPOLINE'), "Requires HAVE_PERF_TRAMPOLINE support") + def test_python_perf_jit_support(self): + code = "import sys; print(sys.is_stack_trampoline_active())" + expected = "True" + rc, out, err = assert_python_ok('-c', code, PYTHON_PERF_JIT_SUPPORT='1') + self.assertIn(expected.encode(), out) + @unittest.skipUnless(sys.platform == 'win32', 'bpo-32457 only applies on Windows') def test_argv0_normalization(self): From e111a94f29c69658a89d785640ee0197cd79b6de Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Sat, 8 Jun 2024 10:05:03 -0300 Subject: [PATCH 04/10] Fix test_python_dump_refs_file. --- 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 aafec907843300..72ef89d7d7cc32 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -932,7 +932,7 @@ def test_python_dump_refs_file(self): self.assertEqual(rc, 0) with open(dump_file.name, 'r') as file: contents = file.read() - self.assertIn('Dumping', contents) + self.assertIn('Remaining objects', contents) @unittest.skipUnless(sys.platform == 'darwin', 'PYTHONEXECUTABLE only works on macOS') def test_python_executable(self): From d3b8ac966c7103ff04357f5bc9a3e1d99a3295ad Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Sat, 8 Jun 2024 14:00:15 -0300 Subject: [PATCH 05/10] Skip test_python_malloc_stats in ASAN builds. --- Lib/test/test_cmd_line.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 72ef89d7d7cc32..d99a37187a53dc 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -955,6 +955,8 @@ def test_python_legacy_windows_stdio(self): rc, out, err = assert_python_ok('-c', code, PYTHONLEGACYWINDOWSSTDIO='1') self.assertIn(expected.encode(), out) + @unittest.skipIf("-fsanitize" in sysconfig.get_config_var('PY_CFLAGS'), + "PYTHONMALLOCSTATS doesn't work with ASAN") def test_python_malloc_stats(self): code = "pass" rc, out, err = assert_python_ok('-c', code, PYTHONMALLOCSTATS='1') From 4e87ecc30edd888d43c31960262d4868432b790b Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Sat, 8 Jun 2024 14:49:15 -0300 Subject: [PATCH 06/10] Fix test_python_malloc_stats on Windows. --- 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 d99a37187a53dc..722b192e5d2196 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -955,7 +955,7 @@ def test_python_legacy_windows_stdio(self): rc, out, err = assert_python_ok('-c', code, PYTHONLEGACYWINDOWSSTDIO='1') self.assertIn(expected.encode(), out) - @unittest.skipIf("-fsanitize" in sysconfig.get_config_var('PY_CFLAGS'), + @unittest.skipIf("-fsanitize" in sysconfig.get_config_vars().get('PY_CFLAGS', ()), "PYTHONMALLOCSTATS doesn't work with ASAN") def test_python_malloc_stats(self): code = "pass" From d4f0bf1fdcb6a29a249cd29a2ce9e7f2e01a7e62 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Tue, 18 Jun 2024 06:54:49 -0300 Subject: [PATCH 07/10] Update Python/initconfig.c Co-authored-by: Petr Viktorin --- Python/initconfig.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/initconfig.c b/Python/initconfig.c index 9b8433839b57d8..51897a2d0aef66 100644 --- a/Python/initconfig.c +++ b/Python/initconfig.c @@ -262,7 +262,7 @@ static const char usage_envvars[] = " this behavior.\n" "PYTHON_HISTORY : the location of a .python_history file.\n" "PYTHONASYNCIODEBUG: enable asyncio debug mode\n" -#ifdef Py_DEBUG +#ifdef Py_TRACE_REFS "PYTHONDUMPREFS : dump objects and reference counts still alive after shutdown\n" "PYTHONDUMPREFSFILE: dump objects and reference counts to the specified file\n" #endif From 82aecfb3dd9f68176b8d8b129ad2d489c68e6863 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Tue, 18 Jun 2024 07:02:22 -0300 Subject: [PATCH 08/10] Remove PYTHONEXECUTABLE from --help-env. --- Lib/test/test_cmd_line.py | 11 ++--------- Python/initconfig.c | 3 --- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 722b192e5d2196..a80b131ad1e813 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -934,21 +934,14 @@ def test_python_dump_refs_file(self): contents = file.read() self.assertIn('Remaining objects', contents) - @unittest.skipUnless(sys.platform == 'darwin', 'PYTHONEXECUTABLE only works on macOS') - def test_python_executable(self): - code = 'import sys; print(sys.executable)' - expected = "/busr/bbin/bpython" - rc, out, err = assert_python_ok('-c', code, PYTHONEXECUTABLE=expected) - self.assertIn(expected.encode(), out) - - @unittest.skipUnless(sys.platform == 'win32', 'Test only applicable on Windows') + @unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows') def test_python_legacy_windows_fs_encoding(self): code = "import sys; print(sys.getfilesystemencoding())" expected = 'mbcs' rc, out, err = assert_python_ok('-c', code, PYTHONLEGACYWINDOWSFSENCODING='1') self.assertIn(expected.encode(), out) - @unittest.skipUnless(sys.platform == 'win32', 'Test only applicable on Windows') + @unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows') def test_python_legacy_windows_stdio(self): code = "import sys; print(sys.stdin.encoding, sys.stdout.encoding)" expected = 'cp' diff --git a/Python/initconfig.c b/Python/initconfig.c index 51897a2d0aef66..677c3f444e56d1 100644 --- a/Python/initconfig.c +++ b/Python/initconfig.c @@ -266,9 +266,6 @@ static const char usage_envvars[] = "PYTHONDUMPREFS : dump objects and reference counts still alive after shutdown\n" "PYTHONDUMPREFSFILE: dump objects and reference counts to the specified file\n" #endif -#ifdef __APPLE__ -"PYTHONEXECUTABLE: set sys.argv[0] to this value (macOS only)\n" -#endif #ifdef MS_WINDOWS "PYTHONLEGACYWINDOWSFSENCODING: use legacy \"mbcs\" encoding for file system\n" "PYTHONLEGACYWINDOWSSTDIO: use legacy Windows stdio\n" From 10191c990f9ac5307f679fa38d4c73ea173881a0 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 18 Jun 2024 15:02:16 +0200 Subject: [PATCH 09/10] Revert "Remove PYTHONEXECUTABLE from --help-env." Apologies for the bad review! This reverts commit 82aecfb3dd9f68176b8d8b129ad2d489c68e6863. --- Lib/test/test_cmd_line.py | 11 +++++++++-- Python/initconfig.c | 3 +++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index a80b131ad1e813..722b192e5d2196 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -934,14 +934,21 @@ def test_python_dump_refs_file(self): contents = file.read() self.assertIn('Remaining objects', contents) - @unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows') + @unittest.skipUnless(sys.platform == 'darwin', 'PYTHONEXECUTABLE only works on macOS') + def test_python_executable(self): + code = 'import sys; print(sys.executable)' + expected = "/busr/bbin/bpython" + rc, out, err = assert_python_ok('-c', code, PYTHONEXECUTABLE=expected) + self.assertIn(expected.encode(), out) + + @unittest.skipUnless(sys.platform == 'win32', 'Test only applicable on Windows') def test_python_legacy_windows_fs_encoding(self): code = "import sys; print(sys.getfilesystemencoding())" expected = 'mbcs' rc, out, err = assert_python_ok('-c', code, PYTHONLEGACYWINDOWSFSENCODING='1') self.assertIn(expected.encode(), out) - @unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows') + @unittest.skipUnless(sys.platform == 'win32', 'Test only applicable on Windows') def test_python_legacy_windows_stdio(self): code = "import sys; print(sys.stdin.encoding, sys.stdout.encoding)" expected = 'cp' diff --git a/Python/initconfig.c b/Python/initconfig.c index 677c3f444e56d1..51897a2d0aef66 100644 --- a/Python/initconfig.c +++ b/Python/initconfig.c @@ -266,6 +266,9 @@ static const char usage_envvars[] = "PYTHONDUMPREFS : dump objects and reference counts still alive after shutdown\n" "PYTHONDUMPREFSFILE: dump objects and reference counts to the specified file\n" #endif +#ifdef __APPLE__ +"PYTHONEXECUTABLE: set sys.argv[0] to this value (macOS only)\n" +#endif #ifdef MS_WINDOWS "PYTHONLEGACYWINDOWSFSENCODING: use legacy \"mbcs\" encoding for file system\n" "PYTHONLEGACYWINDOWSSTDIO: use legacy Windows stdio\n" From 15b55050783e511523e37339bf0e7865ba358d8f Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 18 Jun 2024 15:09:07 +0200 Subject: [PATCH 10/10] Leave the support.MS_WINDOWS in --- 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 722b192e5d2196..c5010eaf0556fb 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -941,14 +941,14 @@ def test_python_executable(self): rc, out, err = assert_python_ok('-c', code, PYTHONEXECUTABLE=expected) self.assertIn(expected.encode(), out) - @unittest.skipUnless(sys.platform == 'win32', 'Test only applicable on Windows') + @unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows') def test_python_legacy_windows_fs_encoding(self): code = "import sys; print(sys.getfilesystemencoding())" expected = 'mbcs' rc, out, err = assert_python_ok('-c', code, PYTHONLEGACYWINDOWSFSENCODING='1') self.assertIn(expected.encode(), out) - @unittest.skipUnless(sys.platform == 'win32', 'Test only applicable on Windows') + @unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows') def test_python_legacy_windows_stdio(self): code = "import sys; print(sys.stdin.encoding, sys.stdout.encoding)" expected = 'cp'