Skip to content

Commit 83edae3

Browse files
authored
gh-145990: sort --help-env sections by environment variable name (GH-145997)
* sort --help-env alphabetically by name. * add a sorting regression test in test_help_env.
1 parent b062f39 commit 83edae3

File tree

3 files changed

+37
-28
lines changed

3 files changed

+37
-28
lines changed

Lib/test/test_cmd_line.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ def test_help(self):
6060
def test_help_env(self):
6161
out = self.verify_valid_flag('--help-env')
6262
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")
6371

6472
@support.cpython_only
6573
def test_help_xoptions(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``python --help-env`` sections are now sorted by environment variable name.

Python/initconfig.c

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -371,49 +371,51 @@ The following implementation-specific options are available:\n\
371371
/* Envvars that don't have equivalent command-line options are listed first */
372372
static const char usage_envvars[] =
373373
"Environment variables that change behavior:\n"
374-
"PYTHONSTARTUP : file executed on interactive startup (no default)\n"
375-
"PYTHONPATH : '%lc'-separated list of directories prefixed to the\n"
376-
" default module search path. The result is sys.path.\n"
377-
"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
378-
" The default module search path uses %s.\n"
379-
"PYTHONPLATLIBDIR: override sys.platlibdir\n"
374+
"PYTHONASYNCIODEBUG: enable asyncio debug mode\n"
375+
"PYTHON_BASIC_REPL: use the traditional parser-based REPL\n"
376+
"PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n"
377+
" debugger. It can be set to the callable of your debugger of\n"
378+
" choice.\n"
380379
"PYTHONCASEOK : ignore case in 'import' statements (Windows)\n"
381-
"PYTHONIOENCODING: encoding[:errors] used for stdin/stdout/stderr\n"
382-
"PYTHONHASHSEED : if this variable is set to 'random', a random value is used\n"
383-
" to seed the hashes of str and bytes objects. It can also be\n"
384-
" set to an integer in the range [0,4294967295] to get hash\n"
385-
" values with a predictable seed.\n"
386-
"PYTHONMALLOC : set the Python memory allocators and/or install debug hooks\n"
387-
" on Python memory allocators. Use PYTHONMALLOC=debug to\n"
388-
" install debug hooks.\n"
389-
"PYTHONMALLOCSTATS: print memory allocator statistics\n"
390380
"PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n"
391381
" coercion behavior. Use PYTHONCOERCECLOCALE=warn to request\n"
392382
" display of locale coercion and locale compatibility warnings\n"
393383
" on stderr.\n"
394-
"PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n"
395-
" debugger. It can be set to the callable of your debugger of\n"
396-
" choice.\n"
397384
"PYTHON_COLORS : if this variable is set to 1, the interpreter will colorize\n"
398385
" various kinds of output. Setting it to 0 deactivates\n"
399386
" this behavior.\n"
400-
"PYTHON_HISTORY : the location of a .python_history file.\n"
401-
"PYTHONASYNCIODEBUG: enable asyncio debug mode\n"
402387
#ifdef Py_TRACE_REFS
403388
"PYTHONDUMPREFS : dump objects and reference counts still alive after shutdown\n"
404389
"PYTHONDUMPREFSFILE: dump objects and reference counts to the specified file\n"
405390
#endif
406391
#ifdef __APPLE__
407392
"PYTHONEXECUTABLE: set sys.argv[0] to this value (macOS only)\n"
408393
#endif
394+
"PYTHONHASHSEED : if this variable is set to 'random', a random value is used\n"
395+
" to seed the hashes of str and bytes objects. It can also be\n"
396+
" set to an integer in the range [0,4294967295] to get hash\n"
397+
" values with a predictable seed.\n"
398+
"PYTHON_HISTORY : the location of a .python_history file.\n"
399+
"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
400+
" The default module search path uses %s.\n"
401+
"PYTHONIOENCODING: encoding[:errors] used for stdin/stdout/stderr\n"
409402
#ifdef MS_WINDOWS
410403
"PYTHONLEGACYWINDOWSFSENCODING: use legacy \"mbcs\" encoding for file system\n"
411404
"PYTHONLEGACYWINDOWSSTDIO: use legacy Windows stdio\n"
412405
#endif
406+
"PYTHONMALLOC : set the Python memory allocators and/or install debug hooks\n"
407+
" on Python memory allocators. Use PYTHONMALLOC=debug to\n"
408+
" install debug hooks.\n"
409+
"PYTHONMALLOCSTATS: print memory allocator statistics\n"
410+
"PYTHONPATH : '%lc'-separated list of directories prefixed to the\n"
411+
" default module search path. The result is sys.path.\n"
412+
"PYTHONPLATLIBDIR: override sys.platlibdir\n"
413+
"PYTHONSTARTUP : file executed on interactive startup (no default)\n"
413414
"PYTHONUSERBASE : defines the user base directory (site.USER_BASE)\n"
414-
"PYTHON_BASIC_REPL: use the traditional parser-based REPL\n"
415415
"\n"
416416
"These variables have equivalent command-line options (see --help for details):\n"
417+
"PYTHON_CONTEXT_AWARE_WARNINGS: if true (1), enable thread-safe warnings\n"
418+
" module behaviour (-X context_aware_warnings)\n"
417419
"PYTHON_CPU_COUNT: override the return value of os.cpu_count() (-X cpu_count)\n"
418420
"PYTHONDEBUG : enable parser debug mode (-d)\n"
419421
"PYTHONDEVMODE : enable Python Development Mode (-X dev)\n"
@@ -428,31 +430,29 @@ static const char usage_envvars[] =
428430
"PYTHONINSPECT : inspect interactively after running script (-i)\n"
429431
"PYTHONINTMAXSTRDIGITS: limit the size of int<->str conversions;\n"
430432
" 0 disables the limit (-X int_max_str_digits=N)\n"
433+
"PYTHON_LAZY_IMPORTS: control global lazy imports (-X lazy_imports)\n"
431434
"PYTHONNODEBUGRANGES: don't include extra location information in code objects\n"
432435
" (-X no_debug_ranges)\n"
433436
"PYTHONNOUSERSITE: disable user site directory (-s)\n"
434437
"PYTHONOPTIMIZE : enable level 1 optimizations (-O)\n"
435-
"PYTHONPERFSUPPORT: support the Linux \"perf\" profiler (-X perf)\n"
436438
"PYTHON_PERF_JIT_SUPPORT: enable Linux \"perf\" profiler support with JIT\n"
437439
" (-X perf_jit)\n"
440+
"PYTHONPERFSUPPORT: support the Linux \"perf\" profiler (-X perf)\n"
438441
#ifdef Py_DEBUG
439442
"PYTHON_PRESITE: import this module before site (-X presite)\n"
440443
#endif
441444
"PYTHONPROFILEIMPORTTIME: show how long each import takes (-X importtime)\n"
442-
"PYTHON_LAZY_IMPORTS: control global lazy imports (-X lazy_imports)\n"
443445
"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files\n"
444446
" (-X pycache_prefix)\n"
445447
"PYTHONSAFEPATH : don't prepend a potentially unsafe path to sys.path.\n"
446448
#ifdef Py_STATS
447449
"PYTHONSTATS : turns on statistics gathering (-X pystats)\n"
448450
#endif
451+
"PYTHON_THREAD_INHERIT_CONTEXT: if true (1), threads inherit context vars\n"
452+
" (-X thread_inherit_context)\n"
449453
#ifdef Py_GIL_DISABLED
450454
"PYTHON_TLBC : when set to 0, disables thread-local bytecode (-X tlbc)\n"
451455
#endif
452-
"PYTHON_THREAD_INHERIT_CONTEXT: if true (1), threads inherit context vars\n"
453-
" (-X thread_inherit_context)\n"
454-
"PYTHON_CONTEXT_AWARE_WARNINGS: if true (1), enable thread-safe warnings module\n"
455-
" behaviour (-X context_aware_warnings)\n"
456456
"PYTHONTRACEMALLOC: trace Python memory allocations (-X tracemalloc)\n"
457457
"PYTHONUNBUFFERED: disable stdout/stderr buffering (-u)\n"
458458
"PYTHONUTF8 : control the UTF-8 mode (-X utf8)\n"
@@ -2947,7 +2947,7 @@ config_usage(int error, const wchar_t* program)
29472947
static void
29482948
config_envvars_usage(void)
29492949
{
2950-
printf(usage_envvars, (wint_t)DELIM, (wint_t)DELIM, PYTHONHOMEHELP);
2950+
printf(usage_envvars, (wint_t)DELIM, PYTHONHOMEHELP, (wint_t)DELIM);
29512951
}
29522952

29532953
static void

0 commit comments

Comments
 (0)