Skip to content

Commit 80297ba

Browse files
committed
gh-145990: sort --help-env sections by environment name
manual backport of GH-145997
1 parent 6434bdb commit 80297ba

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

Lib/test/test_cmd_line.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# See test_cmd_line_script.py for testing of script execution
44

55
import os
6+
import re
67
import subprocess
78
import sys
89
import sysconfig
@@ -59,6 +60,14 @@ def test_help(self):
5960
def test_help_env(self):
6061
out = self.verify_valid_flag('--help-env')
6162
self.assertIn(b'PYTHONHOME', out)
63+
# Env vars in each section should be sorted alphabetically
64+
# (ignoring underscores so PYTHON_FOO and PYTHONFOO intermix naturally)
65+
sort_key = lambda name: name.replace(b'_', b'').lower()
66+
sections = out.split(b'These variables have equivalent')
67+
for section in sections:
68+
envvars = re.findall(rb'^(PYTHON\w+)', section, re.MULTILINE)
69+
self.assertEqual(envvars, sorted(envvars, key=sort_key),
70+
"env vars should be sorted alphabetically")
6271

6372
@support.cpython_only
6473
def test_help_xoptions(self):

Python/initconfig.c

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -362,49 +362,51 @@ The following implementation-specific options are available:\n\
362362
/* Envvars that don't have equivalent command-line options are listed first */
363363
static const char usage_envvars[] =
364364
"Environment variables that change behavior:\n"
365-
"PYTHONSTARTUP : file executed on interactive startup (no default)\n"
366-
"PYTHONPATH : '%lc'-separated list of directories prefixed to the\n"
367-
" default module search path. The result is sys.path.\n"
368-
"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
369-
" The default module search path uses %s.\n"
370-
"PYTHONPLATLIBDIR: override sys.platlibdir\n"
365+
"PYTHONASYNCIODEBUG: enable asyncio debug mode\n"
366+
"PYTHON_BASIC_REPL: use the traditional parser-based REPL\n"
367+
"PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n"
368+
" debugger. It can be set to the callable of your debugger of\n"
369+
" choice.\n"
371370
"PYTHONCASEOK : ignore case in 'import' statements (Windows)\n"
372-
"PYTHONIOENCODING: encoding[:errors] used for stdin/stdout/stderr\n"
373-
"PYTHONHASHSEED : if this variable is set to 'random', a random value is used\n"
374-
" to seed the hashes of str and bytes objects. It can also be\n"
375-
" set to an integer in the range [0,4294967295] to get hash\n"
376-
" values with a predictable seed.\n"
377-
"PYTHONMALLOC : set the Python memory allocators and/or install debug hooks\n"
378-
" on Python memory allocators. Use PYTHONMALLOC=debug to\n"
379-
" install debug hooks.\n"
380-
"PYTHONMALLOCSTATS: print memory allocator statistics\n"
381371
"PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n"
382372
" coercion behavior. Use PYTHONCOERCECLOCALE=warn to request\n"
383373
" display of locale coercion and locale compatibility warnings\n"
384374
" on stderr.\n"
385-
"PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n"
386-
" debugger. It can be set to the callable of your debugger of\n"
387-
" choice.\n"
388375
"PYTHON_COLORS : if this variable is set to 1, the interpreter will colorize\n"
389376
" various kinds of output. Setting it to 0 deactivates\n"
390377
" this behavior.\n"
391-
"PYTHON_HISTORY : the location of a .python_history file.\n"
392-
"PYTHONASYNCIODEBUG: enable asyncio debug mode\n"
393378
#ifdef Py_TRACE_REFS
394379
"PYTHONDUMPREFS : dump objects and reference counts still alive after shutdown\n"
395380
"PYTHONDUMPREFSFILE: dump objects and reference counts to the specified file\n"
396381
#endif
397382
#ifdef __APPLE__
398383
"PYTHONEXECUTABLE: set sys.argv[0] to this value (macOS only)\n"
399384
#endif
385+
"PYTHONHASHSEED : if this variable is set to 'random', a random value is used\n"
386+
" to seed the hashes of str and bytes objects. It can also be\n"
387+
" set to an integer in the range [0,4294967295] to get hash\n"
388+
" values with a predictable seed.\n"
389+
"PYTHON_HISTORY : the location of a .python_history file.\n"
390+
"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
391+
" The default module search path uses %s.\n"
392+
"PYTHONIOENCODING: encoding[:errors] used for stdin/stdout/stderr\n"
400393
#ifdef MS_WINDOWS
401394
"PYTHONLEGACYWINDOWSFSENCODING: use legacy \"mbcs\" encoding for file system\n"
402395
"PYTHONLEGACYWINDOWSSTDIO: use legacy Windows stdio\n"
403396
#endif
397+
"PYTHONMALLOC : set the Python memory allocators and/or install debug hooks\n"
398+
" on Python memory allocators. Use PYTHONMALLOC=debug to\n"
399+
" install debug hooks.\n"
400+
"PYTHONMALLOCSTATS: print memory allocator statistics\n"
401+
"PYTHONPATH : '%lc'-separated list of directories prefixed to the\n"
402+
" default module search path. The result is sys.path.\n"
403+
"PYTHONPLATLIBDIR: override sys.platlibdir\n"
404+
"PYTHONSTARTUP : file executed on interactive startup (no default)\n"
404405
"PYTHONUSERBASE : defines the user base directory (site.USER_BASE)\n"
405-
"PYTHON_BASIC_REPL: use the traditional parser-based REPL\n"
406406
"\n"
407407
"These variables have equivalent command-line options (see --help for details):\n"
408+
"PYTHON_CONTEXT_AWARE_WARNINGS: if true (1), enable thread-safe warnings\n"
409+
" module behaviour (-X context_aware_warnings)\n"
408410
"PYTHON_CPU_COUNT: override the return value of os.cpu_count() (-X cpu_count)\n"
409411
"PYTHONDEBUG : enable parser debug mode (-d)\n"
410412
"PYTHONDEVMODE : enable Python Development Mode (-X dev)\n"
@@ -423,9 +425,9 @@ static const char usage_envvars[] =
423425
" (-X no_debug_ranges)\n"
424426
"PYTHONNOUSERSITE: disable user site directory (-s)\n"
425427
"PYTHONOPTIMIZE : enable level 1 optimizations (-O)\n"
426-
"PYTHONPERFSUPPORT: support the Linux \"perf\" profiler (-X perf)\n"
427428
"PYTHON_PERF_JIT_SUPPORT: enable Linux \"perf\" profiler support with JIT\n"
428429
" (-X perf_jit)\n"
430+
"PYTHONPERFSUPPORT: support the Linux \"perf\" profiler (-X perf)\n"
429431
#ifdef Py_DEBUG
430432
"PYTHON_PRESITE: import this module before site (-X presite)\n"
431433
#endif
@@ -436,13 +438,11 @@ static const char usage_envvars[] =
436438
#ifdef Py_STATS
437439
"PYTHONSTATS : turns on statistics gathering (-X pystats)\n"
438440
#endif
441+
"PYTHON_THREAD_INHERIT_CONTEXT: if true (1), threads inherit context vars\n"
442+
" (-X thread_inherit_context)\n"
439443
#ifdef Py_GIL_DISABLED
440444
"PYTHON_TLBC : when set to 0, disables thread-local bytecode (-X tlbc)\n"
441445
#endif
442-
"PYTHON_THREAD_INHERIT_CONTEXT: if true (1), threads inherit context vars\n"
443-
" (-X thread_inherit_context)\n"
444-
"PYTHON_CONTEXT_AWARE_WARNINGS: if true (1), enable thread-safe warnings module\n"
445-
" behaviour (-X context_aware_warnings)\n"
446446
"PYTHONTRACEMALLOC: trace Python memory allocations (-X tracemalloc)\n"
447447
"PYTHONUNBUFFERED: disable stdout/stderr buffering (-u)\n"
448448
"PYTHONUTF8 : control the UTF-8 mode (-X utf8)\n"
@@ -2839,7 +2839,7 @@ config_usage(int error, const wchar_t* program)
28392839
static void
28402840
config_envvars_usage(void)
28412841
{
2842-
printf(usage_envvars, (wint_t)DELIM, (wint_t)DELIM, PYTHONHOMEHELP);
2842+
printf(usage_envvars, (wint_t)DELIM, PYTHONHOMEHELP, (wint_t)DELIM);
28432843
}
28442844

28452845
static void

0 commit comments

Comments
 (0)