Skip to content

Commit 0226aa1

Browse files
committed
Simplify config->import_time initialisation
1 parent ee7a4a0 commit 0226aa1

File tree

1 file changed

+29
-52
lines changed

1 file changed

+29
-52
lines changed

Python/initconfig.c

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,7 @@ _PyConfig_InitCompatConfig(PyConfig *config)
10051005
memset(config, 0, sizeof(*config));
10061006

10071007
config->_config_init = (int)_PyConfig_INIT_COMPAT;
1008+
config->import_time = -1;
10081009
config->isolated = -1;
10091010
config->use_environment = -1;
10101011
config->dev_mode = -1;
@@ -2247,61 +2248,36 @@ config_init_run_presite(PyConfig *config)
22472248
}
22482249
#endif
22492250

2250-
/* Set `config->import_time` based on `value` from `-Ximporttime(=.*)?`. */
22512251
static PyStatus
2252-
config_set_import_time(PyConfig *config, const wchar_t *value)
2252+
config_init_import_time(PyConfig *config)
22532253
{
2254-
int numeric_value;
2254+
int importtime = 0;
22552255

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) {
2258-
config->import_time = 1;
2259-
}
2260-
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-
2274-
return _PyStatus_OK();
2275-
}
2276-
2277-
/* Configure `config->import_time` by checking -Ximporttime then the
2278-
* PYTHONPROFILEIMPORTTIME environment variable. Defaults to 0.
2279-
*/
2280-
static PyStatus
2281-
config_read_import_time(PyConfig *config)
2282-
{
2283-
/* Check the -X option first. */
2284-
const wchar_t *xoption_value = NULL;
2285-
xoption_value = config_get_xoption_value(config, L"importtime");
2286-
if (xoption_value != NULL) {
2287-
return config_set_import_time(config, xoption_value);
2256+
const char *env = config_get_env(config, "PYTHONPROFILEIMPORTTIME");
2257+
if (env) {
2258+
if (_Py_str_to_int(env, &importtime) != 0) {
2259+
importtime = 1;
2260+
}
2261+
if (importtime < 0 || importtime > 2) {
2262+
return _PyStatus_ERR(
2263+
"PYTHONPROFILEIMPORTTIME: numeric values other than 1 and 2 "
2264+
"are reserved for future use.");
2265+
}
22882266
}
22892267

2290-
/* If there's no -Ximporttime, look for ENV flag */
2291-
wchar_t *env_value = NULL;
2292-
/* `CONFIG_GET_ENV_DUP` requires dest to be initialized to `NULL`. */
2293-
PyStatus status = CONFIG_GET_ENV_DUP(config, &env_value,
2294-
L"PYTHONPROFILEIMPORTTIME",
2295-
"PYTHONPROFILEIMPORTTIME");
2296-
if (_PyStatus_EXCEPTION(status)) {
2297-
return status;
2298-
}
2299-
if (env_value != NULL) {
2300-
status = config_set_import_time(config, env_value);
2301-
PyMem_RawFree(env_value);
2302-
return status;
2268+
const wchar_t *x_value = config_get_xoption_value(config, L"importtime");
2269+
if (x_value) {
2270+
if (*x_value == 0 || config_wstr_to_int(x_value, &importtime) != 0) {
2271+
importtime = 1;
2272+
}
2273+
if (importtime < 0 || importtime > 2) {
2274+
return _PyStatus_ERR(
2275+
"-X importtime: values other than 1 and 2 "
2276+
"are reserved for future use.");
2277+
}
23032278
}
23042279

2280+
config->import_time = importtime;
23052281
return _PyStatus_OK();
23062282
}
23072283

@@ -2321,10 +2297,11 @@ config_read_complex_options(PyConfig *config)
23212297
}
23222298

23232299
PyStatus status;
2324-
2325-
status = config_read_import_time(config);
2326-
if (_PyStatus_EXCEPTION(status)) {
2327-
return status;
2300+
if (config->import_time < 0) {
2301+
status = config_init_import_time(config);
2302+
if (_PyStatus_EXCEPTION(status)) {
2303+
return status;
2304+
}
23282305
}
23292306

23302307
if (config->tracemalloc < 0) {

0 commit comments

Comments
 (0)