Skip to content

Commit 85ddc46

Browse files
committed
gh-145990: sort --help-env sections by name`
1 parent 4a71946 commit 85ddc46

File tree

2 files changed

+37
-28
lines changed

2 files changed

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

29522952
static void

0 commit comments

Comments
 (0)