Skip to content

Commit c81a5e6

Browse files
authored
gh-119574: Add some missing environment variables to '--help-env'. (GH-120006)
1 parent e9f4d80 commit c81a5e6

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

Lib/test/test_cmd_line.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import subprocess
77
import sys
8+
import sysconfig
89
import tempfile
910
import textwrap
1011
import unittest
@@ -912,6 +913,75 @@ def test_python_gil(self):
912913
self.assertEqual(proc.stdout.rstrip(), expected)
913914
self.assertEqual(proc.stderr, '')
914915

916+
def test_python_asyncio_debug(self):
917+
code = "import asyncio; print(asyncio.get_event_loop().get_debug())"
918+
rc, out, err = assert_python_ok('-c', code, PYTHONASYNCIODEBUG='1')
919+
self.assertIn(b'True', out)
920+
921+
@unittest.skipUnless(sysconfig.get_config_var('Py_TRACE_REFS'), "Requires --with-trace-refs build option")
922+
def test_python_dump_refs(self):
923+
code = 'import sys; sys._clear_type_cache()'
924+
rc, out, err = assert_python_ok('-c', code, PYTHONDUMPREFS='1')
925+
self.assertEqual(rc, 0)
926+
927+
@unittest.skipUnless(sysconfig.get_config_var('Py_TRACE_REFS'), "Requires --with-trace-refs build option")
928+
def test_python_dump_refs_file(self):
929+
with tempfile.NamedTemporaryFile() as dump_file:
930+
code = 'import sys; sys._clear_type_cache()'
931+
rc, out, err = assert_python_ok('-c', code, PYTHONDUMPREFSFILE=dump_file.name)
932+
self.assertEqual(rc, 0)
933+
with open(dump_file.name, 'r') as file:
934+
contents = file.read()
935+
self.assertIn('Remaining objects', contents)
936+
937+
@unittest.skipUnless(sys.platform == 'darwin', 'PYTHONEXECUTABLE only works on macOS')
938+
def test_python_executable(self):
939+
code = 'import sys; print(sys.executable)'
940+
expected = "/busr/bbin/bpython"
941+
rc, out, err = assert_python_ok('-c', code, PYTHONEXECUTABLE=expected)
942+
self.assertIn(expected.encode(), out)
943+
944+
@unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows')
945+
def test_python_legacy_windows_fs_encoding(self):
946+
code = "import sys; print(sys.getfilesystemencoding())"
947+
expected = 'mbcs'
948+
rc, out, err = assert_python_ok('-c', code, PYTHONLEGACYWINDOWSFSENCODING='1')
949+
self.assertIn(expected.encode(), out)
950+
951+
@unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows')
952+
def test_python_legacy_windows_stdio(self):
953+
code = "import sys; print(sys.stdin.encoding, sys.stdout.encoding)"
954+
expected = 'cp'
955+
rc, out, err = assert_python_ok('-c', code, PYTHONLEGACYWINDOWSSTDIO='1')
956+
self.assertIn(expected.encode(), out)
957+
958+
@unittest.skipIf("-fsanitize" in sysconfig.get_config_vars().get('PY_CFLAGS', ()),
959+
"PYTHONMALLOCSTATS doesn't work with ASAN")
960+
def test_python_malloc_stats(self):
961+
code = "pass"
962+
rc, out, err = assert_python_ok('-c', code, PYTHONMALLOCSTATS='1')
963+
self.assertIn(b'Small block threshold', err)
964+
965+
def test_python_user_base(self):
966+
code = "import site; print(site.USER_BASE)"
967+
expected = "/custom/userbase"
968+
rc, out, err = assert_python_ok('-c', code, PYTHONUSERBASE=expected)
969+
self.assertIn(expected.encode(), out)
970+
971+
def test_python_basic_repl(self):
972+
# Currently this only tests that the env var is set
973+
code = "import os; print('PYTHON_BASIC_REPL' in os.environ)"
974+
expected = "True"
975+
rc, out, err = assert_python_ok('-c', code, PYTHON_BASIC_REPL='1')
976+
self.assertIn(expected.encode(), out)
977+
978+
@unittest.skipUnless(sysconfig.get_config_var('HAVE_PERF_TRAMPOLINE'), "Requires HAVE_PERF_TRAMPOLINE support")
979+
def test_python_perf_jit_support(self):
980+
code = "import sys; print(sys.is_stack_trampoline_active())"
981+
expected = "True"
982+
rc, out, err = assert_python_ok('-c', code, PYTHON_PERF_JIT_SUPPORT='1')
983+
self.assertIn(expected.encode(), out)
984+
915985
@unittest.skipUnless(sys.platform == 'win32',
916986
'bpo-32457 only applies on Windows')
917987
def test_argv0_normalization(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added some missing environment variables to the output of :option:`--help-env`.

Python/initconfig.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ static const char usage_envvars[] =
249249
"PYTHONMALLOC : set the Python memory allocators and/or install debug hooks\n"
250250
" on Python memory allocators. Use PYTHONMALLOC=debug to\n"
251251
" install debug hooks.\n"
252+
"PYTHONMALLOCSTATS: print memory allocator statistics\n"
252253
"PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n"
253254
" coercion behavior. Use PYTHONCOERCECLOCALE=warn to request\n"
254255
" display of locale coercion and locale compatibility warnings\n"
@@ -260,6 +261,20 @@ static const char usage_envvars[] =
260261
" various kinds of output. Setting it to 0 deactivates\n"
261262
" this behavior.\n"
262263
"PYTHON_HISTORY : the location of a .python_history file.\n"
264+
"PYTHONASYNCIODEBUG: enable asyncio debug mode\n"
265+
#ifdef Py_TRACE_REFS
266+
"PYTHONDUMPREFS : dump objects and reference counts still alive after shutdown\n"
267+
"PYTHONDUMPREFSFILE: dump objects and reference counts to the specified file\n"
268+
#endif
269+
#ifdef __APPLE__
270+
"PYTHONEXECUTABLE: set sys.argv[0] to this value (macOS only)\n"
271+
#endif
272+
#ifdef MS_WINDOWS
273+
"PYTHONLEGACYWINDOWSFSENCODING: use legacy \"mbcs\" encoding for file system\n"
274+
"PYTHONLEGACYWINDOWSSTDIO: use legacy Windows stdio\n"
275+
#endif
276+
"PYTHONUSERBASE : defines the user base directory (site.USER_BASE)\n"
277+
"PYTHON_BASIC_REPL: use the traditional parser-based REPL\n"
263278
"\n"
264279
"These variables have equivalent command-line options (see --help for details):\n"
265280
"PYTHON_CPU_COUNT: override the return value of os.cpu_count() (-X cpu_count)\n"
@@ -281,6 +296,8 @@ static const char usage_envvars[] =
281296
"PYTHONNOUSERSITE: disable user site directory (-s)\n"
282297
"PYTHONOPTIMIZE : enable level 1 optimizations (-O)\n"
283298
"PYTHONPERFSUPPORT: support the Linux \"perf\" profiler (-X perf)\n"
299+
"PYTHON_PERF_JIT_SUPPORT: enable Linux \"perf\" profiler support with JIT\n"
300+
" (-X perf_jit)\n"
284301
#ifdef Py_DEBUG
285302
"PYTHON_PRESITE: import this module before site (-X presite)\n"
286303
#endif

0 commit comments

Comments
 (0)