Skip to content

Commit e53dea1

Browse files
committed
Reserve other numeric values for future use
1 parent b11b373 commit e53dea1

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

Lib/test/test_cmd_line.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,12 +1074,21 @@ def test_import_time(self):
10741074
self.assertRegex(res_err, r"import time: \s*\d+ \| \s*\d+ \| \s*os")
10751075
self.assertNotRegex(res_err, r"import time: cached\s* \| cached\s* \| os")
10761076

1077+
code = "import os"
1078+
res = assert_python_ok('-X', 'importtime=true', '-c', code)
1079+
res_err = res.err.decode("utf-8")
1080+
self.assertRegex(res_err, r"import time: \s*\d+ \| \s*\d+ \| \s*os")
1081+
self.assertNotRegex(res_err, r"import time: cached\s* \| cached\s* \| os")
1082+
10771083
code = "import os; import os"
10781084
res = assert_python_ok('-X', 'importtime=2', '-c', code)
10791085
res_err = res.err.decode("utf-8")
10801086
self.assertRegex(res_err, r"import time: \s*\d+ \| \s*\d+ \| \s*os")
10811087
self.assertRegex(res_err, r"import time: cached\s* \| cached\s* \| os")
10821088

1089+
assert_python_failure('-X', 'importtime=-1', '-c', code)
1090+
assert_python_failure('-X', 'importtime=3', '-c', code)
1091+
10831092
def res2int(self, res):
10841093
out = res.out.strip().decode("utf-8")
10851094
return tuple(int(i) for i in out.split())

Python/initconfig.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,16 +2251,26 @@ config_init_run_presite(PyConfig *config)
22512251
static PyStatus
22522252
config_set_import_time(PyConfig *config, const wchar_t *value)
22532253
{
2254-
/* We recognize 2 as a special value: if the importtime flag is
2255-
* set to 2, we also print import cache hits. Otherwise, we fall
2256-
* back to standard -X importtime behavior. */
2257-
if (wcscmp(value, L"2") == 0) {
2258-
config->import_time = 2;
2259-
}
2260-
else if (value != NULL) {
2254+
int numeric_value;
2255+
2256+
// If no value is specified or the value is not an integer, use 1.
2257+
if (*value == 0 || config_wstr_to_int(value, &numeric_value) != 0) {
22612258
config->import_time = 1;
22622259
}
22632260

2261+
/* -Ximporttime=1 incurs the default behavior. -Ximporttime=2 also
2262+
* prints import cache hits. All other numeric values are reserved.
2263+
*/
2264+
else if (0 <= numeric_value && numeric_value <= 2) {
2265+
config->import_time = numeric_value;
2266+
}
2267+
2268+
else {
2269+
return _PyStatus_ERR(
2270+
"-X importtime: numeric values other than 1 or 2 are "
2271+
"reserved for future use");
2272+
}
2273+
22642274
return _PyStatus_OK();
22652275
}
22662276

0 commit comments

Comments
 (0)