Skip to content

Commit 102cd61

Browse files
authored
[3.13] gh-145990: sort --help-env first section by name (others and xoptions already sorted) (GH-146003)
* sort `--help-env` sections by environment variable name Only needed in the first section, in 3.13 the second section was already sorted. * regression test for --help-env & --help-xoptions sort order Manual backport of GH-145997
1 parent 8469402 commit 102cd61

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

Lib/test/test_cmd_line.py

Lines changed: 12 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 tempfile
@@ -54,11 +55,22 @@ def test_help(self):
5455
def test_help_env(self):
5556
out = self.verify_valid_flag('--help-env')
5657
self.assertIn(b'PYTHONHOME', out)
58+
# Env vars in each section should be sorted alphabetically
59+
# (ignoring underscores so PYTHON_FOO and PYTHONFOO intermix naturally)
60+
sort_key = lambda name: name.replace(b'_', b'').lower()
61+
sections = out.split(b'These variables have equivalent')
62+
for section in sections:
63+
envvars = re.findall(rb'^(PYTHON\w+)', section, re.MULTILINE)
64+
self.assertEqual(envvars, sorted(envvars, key=sort_key),
65+
"env vars should be sorted alphabetically")
5766

5867
@support.cpython_only
5968
def test_help_xoptions(self):
6069
out = self.verify_valid_flag('--help-xoptions')
6170
self.assertIn(b'-X dev', out)
71+
options = re.findall(rb'^-X (\w+)', out, re.MULTILINE)
72+
self.assertEqual(options, sorted(options),
73+
"options should be sorted alphabetically")
6274

6375
@support.cpython_only
6476
def test_help_all(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: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -238,32 +238,32 @@ The following implementation-specific options are available:\n\
238238
/* Envvars that don't have equivalent command-line options are listed first */
239239
static const char usage_envvars[] =
240240
"Environment variables that change behavior:\n"
241-
"PYTHONSTARTUP : file executed on interactive startup (no default)\n"
242-
"PYTHONPATH : '%lc'-separated list of directories prefixed to the\n"
243-
" default module search path. The result is sys.path.\n"
244-
"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
245-
" The default module search path uses %s.\n"
246-
"PYTHONPLATLIBDIR: override sys.platlibdir\n"
241+
"PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n"
242+
" debugger. It can be set to the callable of your debugger of\n"
243+
" choice.\n"
247244
"PYTHONCASEOK : ignore case in 'import' statements (Windows)\n"
248-
"PYTHONIOENCODING: encoding[:errors] used for stdin/stdout/stderr\n"
249-
"PYTHONHASHSEED : if this variable is set to 'random', a random value is used\n"
250-
" to seed the hashes of str and bytes objects. It can also be\n"
251-
" set to an integer in the range [0,4294967295] to get hash\n"
252-
" values with a predictable seed.\n"
253-
"PYTHONMALLOC : set the Python memory allocators and/or install debug hooks\n"
254-
" on Python memory allocators. Use PYTHONMALLOC=debug to\n"
255-
" install debug hooks.\n"
256245
"PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n"
257246
" coercion behavior. Use PYTHONCOERCECLOCALE=warn to request\n"
258247
" display of locale coercion and locale compatibility warnings\n"
259248
" on stderr.\n"
260-
"PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n"
261-
" debugger. It can be set to the callable of your debugger of\n"
262-
" choice.\n"
263249
"PYTHON_COLORS : if this variable is set to 1, the interpreter will colorize\n"
264250
" various kinds of output. Setting it to 0 deactivates\n"
265251
" this behavior.\n"
252+
"PYTHONHASHSEED : if this variable is set to 'random', a random value is used\n"
253+
" to seed the hashes of str and bytes objects. It can also be\n"
254+
" set to an integer in the range [0,4294967295] to get hash\n"
255+
" values with a predictable seed.\n"
266256
"PYTHON_HISTORY : the location of a .python_history file.\n"
257+
"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
258+
" The default module search path uses %s.\n"
259+
"PYTHONIOENCODING: encoding[:errors] used for stdin/stdout/stderr\n"
260+
"PYTHONMALLOC : set the Python memory allocators and/or install debug hooks\n"
261+
" on Python memory allocators. Use PYTHONMALLOC=debug to\n"
262+
" install debug hooks.\n"
263+
"PYTHONPATH : '%lc'-separated list of directories prefixed to the\n"
264+
" default module search path. The result is sys.path.\n"
265+
"PYTHONPLATLIBDIR: override sys.platlibdir\n"
266+
"PYTHONSTARTUP : file executed on interactive startup (no default)\n"
267267
"\n"
268268
"These variables have equivalent command-line options (see --help for details):\n"
269269
"PYTHON_CPU_COUNT: override the return value of os.cpu_count() (-X cpu_count)\n"
@@ -2407,7 +2407,7 @@ config_usage(int error, const wchar_t* program)
24072407
static void
24082408
config_envvars_usage(void)
24092409
{
2410-
printf(usage_envvars, (wint_t)DELIM, (wint_t)DELIM, PYTHONHOMEHELP);
2410+
printf(usage_envvars, (wint_t)DELIM, PYTHONHOMEHELP, (wint_t)DELIM);
24112411
}
24122412

24132413
static void

0 commit comments

Comments
 (0)