Skip to content

Commit fbca908

Browse files
authored
bpo-34523: Use _PyCoreConfig instead of globals (GH-9005)
Use the core configuration of the interpreter, rather than using global configuration variables. For example, replace Py_QuietFlag with core_config->quiet.
1 parent de42755 commit fbca908

File tree

6 files changed

+50
-40
lines changed

6 files changed

+50
-40
lines changed

Include/pylifecycle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ PyAPI_FUNC(const char *) _Py_gitversion(void);
123123
#ifndef Py_LIMITED_API
124124
PyAPI_FUNC(PyObject *) _PyBuiltin_Init(void);
125125
PyAPI_FUNC(_PyInitError) _PySys_BeginInit(PyObject **sysmod);
126-
PyAPI_FUNC(int) _PySys_EndInit(PyObject *sysdict, _PyMainInterpreterConfig *config);
126+
PyAPI_FUNC(int) _PySys_EndInit(PyObject *sysdict, PyInterpreterState *interp);
127127
PyAPI_FUNC(_PyInitError) _PyImport_Init(PyInterpreterState *interp);
128128
PyAPI_FUNC(void) _PyExc_Init(PyObject * bltinmod);
129129
PyAPI_FUNC(_PyInitError) _PyImportHooks_Init(void);

Include/pystate.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
249249
PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_Get(void);
250250
#endif
251251
#ifdef Py_BUILD_CORE
252-
/* Macro which should only be used for performance critical code */
252+
/* Macro which should only be used for performance critical code.
253+
Need "#include "internal/pystate.h". See also _PyInterpreterState_Get()
254+
and _PyGILState_GetInterpreterStateUnsafe(). */
253255
# define _PyInterpreterState_GET_UNSAFE() (PyThreadState_GET()->interp)
254256
#endif
255257
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000
@@ -369,7 +371,9 @@ PyAPI_FUNC(int) PyGILState_Check(void);
369371
GILState implementation.
370372
371373
Return NULL before _PyGILState_Init() is called and after _PyGILState_Fini()
372-
is called. */
374+
is called.
375+
376+
See also _PyInterpreterState_Get() and _PyInterpreterState_GET_UNSAFE(). */
373377
PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
374378
#endif /* !Py_LIMITED_API */
375379

Modules/main.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -951,18 +951,18 @@ pymain_init_stdio(_PyMain *pymain, _PyCoreConfig *config)
951951

952952

953953
static void
954-
pymain_header(_PyMain *pymain)
954+
pymain_header(_PyMain *pymain, const _PyCoreConfig *config)
955955
{
956-
if (Py_QuietFlag) {
956+
if (config->quiet) {
957957
return;
958958
}
959959

960-
if (!Py_VerboseFlag && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) {
960+
if (!config->verbose && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) {
961961
return;
962962
}
963963

964964
fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
965-
if (!Py_NoSiteFlag) {
965+
if (config->site_import) {
966966
fprintf(stderr, "%s\n", COPYRIGHT);
967967
}
968968
}
@@ -1041,12 +1041,12 @@ wstrlist_as_pylist(int len, wchar_t **list)
10411041

10421042

10431043
static void
1044-
pymain_import_readline(_PyMain *pymain)
1044+
pymain_import_readline(_PyMain *pymain, const _PyCoreConfig *config)
10451045
{
1046-
if (Py_IsolatedFlag) {
1046+
if (config->isolated) {
10471047
return;
10481048
}
1049-
if (!Py_InspectFlag && RUN_CODE(pymain)) {
1049+
if (!config->inspect && RUN_CODE(pymain)) {
10501050
return;
10511051
}
10521052
if (!isatty(fileno(stdin))) {
@@ -1591,8 +1591,8 @@ pymain_run_python(_PyMain *pymain, PyInterpreterState *interp)
15911591

15921592
PyCompilerFlags cf = {.cf_flags = 0};
15931593

1594-
pymain_header(pymain);
1595-
pymain_import_readline(pymain);
1594+
pymain_header(pymain, config);
1595+
pymain_import_readline(pymain, config);
15961596

15971597
if (pymain->command) {
15981598
pymain->status = pymain_run_command(pymain->command, &cf);

Python/bltinmodule.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "Python.h"
44
#include "Python-ast.h"
5+
#include "internal/pystate.h"
56

67
#include "node.h"
78
#include "code.h"
@@ -2765,6 +2766,8 @@ _PyBuiltin_Init(void)
27652766
{
27662767
PyObject *mod, *dict, *debug;
27672768

2769+
const _PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config;
2770+
27682771
if (PyType_Ready(&PyFilter_Type) < 0 ||
27692772
PyType_Ready(&PyMap_Type) < 0 ||
27702773
PyType_Ready(&PyZip_Type) < 0)
@@ -2823,7 +2826,7 @@ _PyBuiltin_Init(void)
28232826
SETBUILTIN("tuple", &PyTuple_Type);
28242827
SETBUILTIN("type", &PyType_Type);
28252828
SETBUILTIN("zip", &PyZip_Type);
2826-
debug = PyBool_FromLong(Py_OptimizeFlag == 0);
2829+
debug = PyBool_FromLong(config->optimization_level == 0);
28272830
if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
28282831
Py_DECREF(debug);
28292832
return NULL;

Python/pylifecycle.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp,
800800
return _Py_INIT_ERR("can't initialize time");
801801
}
802802

803-
if (_PySys_EndInit(interp->sysdict, &interp->config) < 0) {
803+
if (_PySys_EndInit(interp->sysdict, interp) < 0) {
804804
return _Py_INIT_ERR("can't finish initializing sys");
805805
}
806806

@@ -1285,7 +1285,7 @@ new_interpreter(PyThreadState **tstate_p)
12851285
goto handle_error;
12861286
Py_INCREF(interp->sysdict);
12871287
PyDict_SetItemString(interp->sysdict, "modules", modules);
1288-
_PySys_EndInit(interp->sysdict, &interp->config);
1288+
_PySys_EndInit(interp->sysdict, interp);
12891289
}
12901290

12911291
bimod = _PyImport_FindBuiltin("builtins", modules);
@@ -1543,7 +1543,7 @@ is_valid_fd(int fd)
15431543

15441544
/* returns Py_None if the fd is not valid */
15451545
static PyObject*
1546-
create_stdio(PyObject* io,
1546+
create_stdio(const _PyCoreConfig *config, PyObject* io,
15471547
int fd, int write_mode, const char* name,
15481548
const char* encoding, const char* errors)
15491549
{
@@ -1556,6 +1556,7 @@ create_stdio(PyObject* io,
15561556
_Py_IDENTIFIER(isatty);
15571557
_Py_IDENTIFIER(TextIOWrapper);
15581558
_Py_IDENTIFIER(mode);
1559+
const int buffered_stdio = config->buffered_stdio;
15591560

15601561
if (!is_valid_fd(fd))
15611562
Py_RETURN_NONE;
@@ -1565,7 +1566,7 @@ create_stdio(PyObject* io,
15651566
depends on the presence of a read1() method which only exists on
15661567
buffered streams.
15671568
*/
1568-
if (Py_UnbufferedStdioFlag && write_mode)
1569+
if (!buffered_stdio && write_mode)
15691570
buffering = 0;
15701571
else
15711572
buffering = -1;
@@ -1607,11 +1608,11 @@ create_stdio(PyObject* io,
16071608
Py_DECREF(res);
16081609
if (isatty == -1)
16091610
goto error;
1610-
if (Py_UnbufferedStdioFlag)
1611+
if (!buffered_stdio)
16111612
write_through = Py_True;
16121613
else
16131614
write_through = Py_False;
1614-
if (isatty && !Py_UnbufferedStdioFlag)
1615+
if (isatty && buffered_stdio)
16151616
line_buffering = Py_True;
16161617
else
16171618
line_buffering = Py_False;
@@ -1720,7 +1721,7 @@ init_sys_streams(PyInterpreterState *interp)
17201721
* and fileno() may point to an invalid file descriptor. For example
17211722
* GUI apps don't have valid standard streams by default.
17221723
*/
1723-
std = create_stdio(iomod, fd, 0, "<stdin>",
1724+
std = create_stdio(config, iomod, fd, 0, "<stdin>",
17241725
config->stdio_encoding,
17251726
config->stdio_errors);
17261727
if (std == NULL)
@@ -1731,7 +1732,7 @@ init_sys_streams(PyInterpreterState *interp)
17311732

17321733
/* Set sys.stdout */
17331734
fd = fileno(stdout);
1734-
std = create_stdio(iomod, fd, 1, "<stdout>",
1735+
std = create_stdio(config, iomod, fd, 1, "<stdout>",
17351736
config->stdio_encoding,
17361737
config->stdio_errors);
17371738
if (std == NULL)
@@ -1743,7 +1744,7 @@ init_sys_streams(PyInterpreterState *interp)
17431744
#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
17441745
/* Set sys.stderr, replaces the preliminary stderr */
17451746
fd = fileno(stderr);
1746-
std = create_stdio(iomod, fd, 1, "<stderr>",
1747+
std = create_stdio(config, iomod, fd, 1, "<stderr>",
17471748
config->stdio_encoding,
17481749
"backslashreplace");
17491750
if (std == NULL)

Python/sysmodule.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,7 +2076,7 @@ make_flags(void)
20762076
{
20772077
int pos = 0;
20782078
PyObject *seq;
2079-
_PyCoreConfig *core_config = &_PyGILState_GetInterpreterStateUnsafe()->core_config;
2079+
const _PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config;
20802080

20812081
seq = PyStructSequence_New(&FlagsType);
20822082
if (seq == NULL)
@@ -2085,23 +2085,23 @@ make_flags(void)
20852085
#define SetFlag(flag) \
20862086
PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag))
20872087

2088-
SetFlag(Py_DebugFlag);
2089-
SetFlag(Py_InspectFlag);
2090-
SetFlag(Py_InteractiveFlag);
2091-
SetFlag(Py_OptimizeFlag);
2092-
SetFlag(Py_DontWriteBytecodeFlag);
2093-
SetFlag(Py_NoUserSiteDirectory);
2094-
SetFlag(Py_NoSiteFlag);
2095-
SetFlag(Py_IgnoreEnvironmentFlag);
2096-
SetFlag(Py_VerboseFlag);
2088+
SetFlag(config->parser_debug);
2089+
SetFlag(config->inspect);
2090+
SetFlag(config->interactive);
2091+
SetFlag(config->optimization_level);
2092+
SetFlag(!config->write_bytecode);
2093+
SetFlag(!config->user_site_directory);
2094+
SetFlag(!config->site_import);
2095+
SetFlag(!config->use_environment);
2096+
SetFlag(config->verbose);
20972097
/* SetFlag(saw_unbuffered_flag); */
20982098
/* SetFlag(skipfirstline); */
2099-
SetFlag(Py_BytesWarningFlag);
2100-
SetFlag(Py_QuietFlag);
2101-
SetFlag(Py_HashRandomizationFlag);
2102-
SetFlag(Py_IsolatedFlag);
2103-
PyStructSequence_SET_ITEM(seq, pos++, PyBool_FromLong(core_config->dev_mode));
2104-
SetFlag(Py_UTF8Mode);
2099+
SetFlag(config->bytes_warning);
2100+
SetFlag(config->quiet);
2101+
SetFlag(config->use_hash_seed == 0 || config->hash_seed != 0);
2102+
SetFlag(config->isolated);
2103+
PyStructSequence_SET_ITEM(seq, pos++, PyBool_FromLong(config->dev_mode));
2104+
SetFlag(config->utf8_mode);
21052105
#undef SetFlag
21062106

21072107
if (PyErr_Occurred()) {
@@ -2474,8 +2474,10 @@ _PySys_BeginInit(PyObject **sysmod)
24742474
} while (0)
24752475

24762476
int
2477-
_PySys_EndInit(PyObject *sysdict, _PyMainInterpreterConfig *config)
2477+
_PySys_EndInit(PyObject *sysdict, PyInterpreterState *interp)
24782478
{
2479+
const _PyCoreConfig *core_config = &interp->core_config;
2480+
const _PyMainInterpreterConfig *config = &interp->config;
24792481
int res;
24802482

24812483
/* _PyMainInterpreterConfig_Read() must set all these variables */
@@ -2523,7 +2525,7 @@ _PySys_EndInit(PyObject *sysdict, _PyMainInterpreterConfig *config)
25232525
}
25242526

25252527
SET_SYS_FROM_STRING_INT_RESULT("dont_write_bytecode",
2526-
PyBool_FromLong(Py_DontWriteBytecodeFlag));
2528+
PyBool_FromLong(!core_config->write_bytecode));
25272529

25282530
if (get_warnoptions() == NULL)
25292531
return -1;

0 commit comments

Comments
 (0)