Skip to content

Commit f01c750

Browse files
committed
Fix python -VV
* Make test_cmd_line.test_version() stricter * Adjust documentation.
1 parent 3a5ae3e commit f01c750

5 files changed

Lines changed: 27 additions & 21 deletions

File tree

‎Doc/c-api/init_config.rst‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ Error Handling
116116
117117
Get the *config* exit code.
118118
119-
Return ``0``.
119+
Return ``0`` and leave *\*exitcode* unchanged.
120120
121121
In Python 3.15, :c:func:`Py_InitializeFromInitConfig` sets an exit code if a
122122
command line option wants to exit Python. This is no longer the case in
123-
Python 3.16. Instead, the option is processed in :c:func:`Py_RunMain`. This
124-
function became useless.
123+
Python 3.16. Instead, the option is now processed in :c:func:`Py_RunMain`.
124+
This function became useless.
125125
126126
.. deprecated:: next
127127
@@ -787,9 +787,9 @@ PyStatus
787787
Exit Python with the specified exit code.
788788
789789
.. deprecated:: next
790-
:c:func:`Py_InitializeFromInitConfig` no longer sets an exit code if a
790+
:c:func:`Py_InitializeFromConfig` no longer sets an exit code if a
791791
command line option wants to exit Python. Instead, the option is
792-
processed in :c:func:`Py_RunMain`.
792+
now processed in :c:func:`Py_RunMain`.
793793
794794
Functions to handle a status:
795795
@@ -807,9 +807,9 @@ PyStatus
807807
Is the result an exit?
808808
809809
.. deprecated:: next
810-
:c:func:`Py_InitializeFromInitConfig` no longer sets an exit code if a
810+
:c:func:`Py_InitializeFromConfig` no longer sets an exit code if a
811811
command line option wants to exit Python. Instead, the option is
812-
processed in :c:func:`Py_RunMain`.
812+
now processed in :c:func:`Py_RunMain`.
813813
814814
.. c:function:: void Py_ExitStatusException(PyStatus status)
815815

‎Doc/whatsnew/3.16.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ Deprecated C APIs
11331133
member. :c:func:`Py_InitializeFromInitConfig` and
11341134
:c:func:`Py_InitializeFromConfig` can no longer return an exit code.
11351135
Instead, if a command line option wants to exit Python, the option is
1136-
processed in :c:func:`Py_RunMain`.
1136+
now processed in :c:func:`Py_RunMain`.
11371137
(Contributed by Victor Stinner in :gh:`158080`.)
11381138

11391139
* :c:func:`PyModule_GetFilename` is no longer deprecated, but using

‎Lib/test/test_cmd_line.py‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,16 @@ def test_site_flag(self):
133133

134134
@support.cpython_only
135135
def test_version(self):
136-
version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii")
137-
for switch in '-V', '--version', '-VV':
138-
rc, out, err = assert_python_ok(switch)
139-
self.assertNotStartsWith(err, version)
140-
self.assertStartsWith(out, version)
136+
short_version = ('Python %d.%d' % sys.version_info[:2])
137+
for switch in ('-V', '--version'):
138+
with self.subTest(switch=switch):
139+
rc, out, err = assert_python_ok(switch)
140+
self.assertStartsWith(out, short_version.encode())
141+
self.assertEqual(err, b'')
142+
143+
rc, out, err = assert_python_ok('-VV')
144+
self.assertEqual(out.rstrip(), f"Python {sys.version}".encode())
145+
self.assertEqual(err, b'')
141146

142147
def test_verbose(self):
143148
# -v causes imports to write to stderr. If the write to

‎Misc/NEWS.d/next/C_API/2026-09-24-15-27-35.gh-issue-158080.YIqypf.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ Deprecate :c:func:`PyInitConfig_GetExitCode`, :c:func:`PyStatus_Exit`, and
33
member. :c:func:`Py_InitializeFromInitConfig` and
44
:c:func:`Py_InitializeFromConfig` can no longer return an exit code.
55
Instead, if a command line option wants to exit Python, the option is
6-
processed in :c:func:`Py_RunMain`. Patch by Victor Stinner.
6+
now processed in :c:func:`Py_RunMain`. Patch by Victor Stinner.

‎Python/initconfig.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2997,7 +2997,7 @@ static PyStatus
29972997
config_parse_cmdline(PyConfig *config, PyWideStringList *warnoptions,
29982998
Py_ssize_t *opt_index)
29992999
{
3000-
// DEFER_OPTION() only stores the first parsed deferred option
3000+
// Only store the first option
30013001
#define DEFER_OPTION(OPTION) \
30023002
do { \
30033003
if (config->_deferred_cmdline_option == 0) { \
@@ -3143,12 +3143,6 @@ config_parse_cmdline(PyConfig *config, PyWideStringList *warnoptions,
31433143

31443144
case 'V':
31453145
print_version++;
3146-
if (print_version >= 2) {
3147-
DEFER_OPTION('W');
3148-
}
3149-
else {
3150-
DEFER_OPTION(c);
3151-
}
31523146
break;
31533147

31543148
case 'W':
@@ -3175,6 +3169,13 @@ config_parse_cmdline(PyConfig *config, PyWideStringList *warnoptions,
31753169
}
31763170
} while (config->_deferred_cmdline_option == 0);
31773171

3172+
if (print_version >= 2) {
3173+
DEFER_OPTION('W');
3174+
}
3175+
else if (print_version >= 1) {
3176+
DEFER_OPTION('V');
3177+
}
3178+
31783179
if (config->run_command == NULL && config->run_module == NULL
31793180
&& _PyOS_optind < argv->length
31803181
&& wcscmp(argv->items[_PyOS_optind], L"-") != 0

0 commit comments

Comments
 (0)