Commit fbca9085 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

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.
parent de427556
...@@ -123,7 +123,7 @@ PyAPI_FUNC(const char *) _Py_gitversion(void); ...@@ -123,7 +123,7 @@ PyAPI_FUNC(const char *) _Py_gitversion(void);
#ifndef Py_LIMITED_API #ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _PyBuiltin_Init(void); PyAPI_FUNC(PyObject *) _PyBuiltin_Init(void);
PyAPI_FUNC(_PyInitError) _PySys_BeginInit(PyObject **sysmod); PyAPI_FUNC(_PyInitError) _PySys_BeginInit(PyObject **sysmod);
PyAPI_FUNC(int) _PySys_EndInit(PyObject *sysdict, _PyMainInterpreterConfig *config); PyAPI_FUNC(int) _PySys_EndInit(PyObject *sysdict, PyInterpreterState *interp);
PyAPI_FUNC(_PyInitError) _PyImport_Init(PyInterpreterState *interp); PyAPI_FUNC(_PyInitError) _PyImport_Init(PyInterpreterState *interp);
PyAPI_FUNC(void) _PyExc_Init(PyObject * bltinmod); PyAPI_FUNC(void) _PyExc_Init(PyObject * bltinmod);
PyAPI_FUNC(_PyInitError) _PyImportHooks_Init(void); PyAPI_FUNC(_PyInitError) _PyImportHooks_Init(void);
......
...@@ -249,7 +249,9 @@ PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *); ...@@ -249,7 +249,9 @@ PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_Get(void); PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_Get(void);
#endif #endif
#ifdef Py_BUILD_CORE #ifdef Py_BUILD_CORE
/* Macro which should only be used for performance critical code */ /* Macro which should only be used for performance critical code.
Need "#include "internal/pystate.h". See also _PyInterpreterState_Get()
and _PyGILState_GetInterpreterStateUnsafe(). */
# define _PyInterpreterState_GET_UNSAFE() (PyThreadState_GET()->interp) # define _PyInterpreterState_GET_UNSAFE() (PyThreadState_GET()->interp)
#endif #endif
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000
...@@ -369,7 +371,9 @@ PyAPI_FUNC(int) PyGILState_Check(void); ...@@ -369,7 +371,9 @@ PyAPI_FUNC(int) PyGILState_Check(void);
GILState implementation. GILState implementation.
Return NULL before _PyGILState_Init() is called and after _PyGILState_Fini() Return NULL before _PyGILState_Init() is called and after _PyGILState_Fini()
is called. */ is called.
See also _PyInterpreterState_Get() and _PyInterpreterState_GET_UNSAFE(). */
PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void); PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
#endif /* !Py_LIMITED_API */ #endif /* !Py_LIMITED_API */
......
...@@ -951,18 +951,18 @@ pymain_init_stdio(_PyMain *pymain, _PyCoreConfig *config) ...@@ -951,18 +951,18 @@ pymain_init_stdio(_PyMain *pymain, _PyCoreConfig *config)
static void static void
pymain_header(_PyMain *pymain) pymain_header(_PyMain *pymain, const _PyCoreConfig *config)
{ {
if (Py_QuietFlag) { if (config->quiet) {
return; return;
} }
if (!Py_VerboseFlag && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) { if (!config->verbose && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) {
return; return;
} }
fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform()); fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
if (!Py_NoSiteFlag) { if (config->site_import) {
fprintf(stderr, "%s\n", COPYRIGHT); fprintf(stderr, "%s\n", COPYRIGHT);
} }
} }
...@@ -1041,12 +1041,12 @@ wstrlist_as_pylist(int len, wchar_t **list) ...@@ -1041,12 +1041,12 @@ wstrlist_as_pylist(int len, wchar_t **list)
static void static void
pymain_import_readline(_PyMain *pymain) pymain_import_readline(_PyMain *pymain, const _PyCoreConfig *config)
{ {
if (Py_IsolatedFlag) { if (config->isolated) {
return; return;
} }
if (!Py_InspectFlag && RUN_CODE(pymain)) { if (!config->inspect && RUN_CODE(pymain)) {
return; return;
} }
if (!isatty(fileno(stdin))) { if (!isatty(fileno(stdin))) {
...@@ -1591,8 +1591,8 @@ pymain_run_python(_PyMain *pymain, PyInterpreterState *interp) ...@@ -1591,8 +1591,8 @@ pymain_run_python(_PyMain *pymain, PyInterpreterState *interp)
PyCompilerFlags cf = {.cf_flags = 0}; PyCompilerFlags cf = {.cf_flags = 0};
pymain_header(pymain); pymain_header(pymain, config);
pymain_import_readline(pymain); pymain_import_readline(pymain, config);
if (pymain->command) { if (pymain->command) {
pymain->status = pymain_run_command(pymain->command, &cf); pymain->status = pymain_run_command(pymain->command, &cf);
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "Python.h" #include "Python.h"
#include "Python-ast.h" #include "Python-ast.h"
#include "internal/pystate.h"
#include "node.h" #include "node.h"
#include "code.h" #include "code.h"
...@@ -2765,6 +2766,8 @@ _PyBuiltin_Init(void) ...@@ -2765,6 +2766,8 @@ _PyBuiltin_Init(void)
{ {
PyObject *mod, *dict, *debug; PyObject *mod, *dict, *debug;
const _PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config;
if (PyType_Ready(&PyFilter_Type) < 0 || if (PyType_Ready(&PyFilter_Type) < 0 ||
PyType_Ready(&PyMap_Type) < 0 || PyType_Ready(&PyMap_Type) < 0 ||
PyType_Ready(&PyZip_Type) < 0) PyType_Ready(&PyZip_Type) < 0)
...@@ -2823,7 +2826,7 @@ _PyBuiltin_Init(void) ...@@ -2823,7 +2826,7 @@ _PyBuiltin_Init(void)
SETBUILTIN("tuple", &PyTuple_Type); SETBUILTIN("tuple", &PyTuple_Type);
SETBUILTIN("type", &PyType_Type); SETBUILTIN("type", &PyType_Type);
SETBUILTIN("zip", &PyZip_Type); SETBUILTIN("zip", &PyZip_Type);
debug = PyBool_FromLong(Py_OptimizeFlag == 0); debug = PyBool_FromLong(config->optimization_level == 0);
if (PyDict_SetItemString(dict, "__debug__", debug) < 0) { if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
Py_DECREF(debug); Py_DECREF(debug);
return NULL; return NULL;
......
...@@ -800,7 +800,7 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp, ...@@ -800,7 +800,7 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp,
return _Py_INIT_ERR("can't initialize time"); return _Py_INIT_ERR("can't initialize time");
} }
if (_PySys_EndInit(interp->sysdict, &interp->config) < 0) { if (_PySys_EndInit(interp->sysdict, interp) < 0) {
return _Py_INIT_ERR("can't finish initializing sys"); return _Py_INIT_ERR("can't finish initializing sys");
} }
...@@ -1285,7 +1285,7 @@ new_interpreter(PyThreadState **tstate_p) ...@@ -1285,7 +1285,7 @@ new_interpreter(PyThreadState **tstate_p)
goto handle_error; goto handle_error;
Py_INCREF(interp->sysdict); Py_INCREF(interp->sysdict);
PyDict_SetItemString(interp->sysdict, "modules", modules); PyDict_SetItemString(interp->sysdict, "modules", modules);
_PySys_EndInit(interp->sysdict, &interp->config); _PySys_EndInit(interp->sysdict, interp);
} }
bimod = _PyImport_FindBuiltin("builtins", modules); bimod = _PyImport_FindBuiltin("builtins", modules);
...@@ -1543,7 +1543,7 @@ is_valid_fd(int fd) ...@@ -1543,7 +1543,7 @@ is_valid_fd(int fd)
/* returns Py_None if the fd is not valid */ /* returns Py_None if the fd is not valid */
static PyObject* static PyObject*
create_stdio(PyObject* io, create_stdio(const _PyCoreConfig *config, PyObject* io,
int fd, int write_mode, const char* name, int fd, int write_mode, const char* name,
const char* encoding, const char* errors) const char* encoding, const char* errors)
{ {
...@@ -1556,6 +1556,7 @@ create_stdio(PyObject* io, ...@@ -1556,6 +1556,7 @@ create_stdio(PyObject* io,
_Py_IDENTIFIER(isatty); _Py_IDENTIFIER(isatty);
_Py_IDENTIFIER(TextIOWrapper); _Py_IDENTIFIER(TextIOWrapper);
_Py_IDENTIFIER(mode); _Py_IDENTIFIER(mode);
const int buffered_stdio = config->buffered_stdio;
if (!is_valid_fd(fd)) if (!is_valid_fd(fd))
Py_RETURN_NONE; Py_RETURN_NONE;
...@@ -1565,7 +1566,7 @@ create_stdio(PyObject* io, ...@@ -1565,7 +1566,7 @@ create_stdio(PyObject* io,
depends on the presence of a read1() method which only exists on depends on the presence of a read1() method which only exists on
buffered streams. buffered streams.
*/ */
if (Py_UnbufferedStdioFlag && write_mode) if (!buffered_stdio && write_mode)
buffering = 0; buffering = 0;
else else
buffering = -1; buffering = -1;
...@@ -1607,11 +1608,11 @@ create_stdio(PyObject* io, ...@@ -1607,11 +1608,11 @@ create_stdio(PyObject* io,
Py_DECREF(res); Py_DECREF(res);
if (isatty == -1) if (isatty == -1)
goto error; goto error;
if (Py_UnbufferedStdioFlag) if (!buffered_stdio)
write_through = Py_True; write_through = Py_True;
else else
write_through = Py_False; write_through = Py_False;
if (isatty && !Py_UnbufferedStdioFlag) if (isatty && buffered_stdio)
line_buffering = Py_True; line_buffering = Py_True;
else else
line_buffering = Py_False; line_buffering = Py_False;
...@@ -1720,7 +1721,7 @@ init_sys_streams(PyInterpreterState *interp) ...@@ -1720,7 +1721,7 @@ init_sys_streams(PyInterpreterState *interp)
* and fileno() may point to an invalid file descriptor. For example * and fileno() may point to an invalid file descriptor. For example
* GUI apps don't have valid standard streams by default. * GUI apps don't have valid standard streams by default.
*/ */
std = create_stdio(iomod, fd, 0, "<stdin>", std = create_stdio(config, iomod, fd, 0, "<stdin>",
config->stdio_encoding, config->stdio_encoding,
config->stdio_errors); config->stdio_errors);
if (std == NULL) if (std == NULL)
...@@ -1731,7 +1732,7 @@ init_sys_streams(PyInterpreterState *interp) ...@@ -1731,7 +1732,7 @@ init_sys_streams(PyInterpreterState *interp)
/* Set sys.stdout */ /* Set sys.stdout */
fd = fileno(stdout); fd = fileno(stdout);
std = create_stdio(iomod, fd, 1, "<stdout>", std = create_stdio(config, iomod, fd, 1, "<stdout>",
config->stdio_encoding, config->stdio_encoding,
config->stdio_errors); config->stdio_errors);
if (std == NULL) if (std == NULL)
...@@ -1743,7 +1744,7 @@ init_sys_streams(PyInterpreterState *interp) ...@@ -1743,7 +1744,7 @@ init_sys_streams(PyInterpreterState *interp)
#if 1 /* Disable this if you have trouble debugging bootstrap stuff */ #if 1 /* Disable this if you have trouble debugging bootstrap stuff */
/* Set sys.stderr, replaces the preliminary stderr */ /* Set sys.stderr, replaces the preliminary stderr */
fd = fileno(stderr); fd = fileno(stderr);
std = create_stdio(iomod, fd, 1, "<stderr>", std = create_stdio(config, iomod, fd, 1, "<stderr>",
config->stdio_encoding, config->stdio_encoding,
"backslashreplace"); "backslashreplace");
if (std == NULL) if (std == NULL)
......
...@@ -2076,7 +2076,7 @@ make_flags(void) ...@@ -2076,7 +2076,7 @@ make_flags(void)
{ {
int pos = 0; int pos = 0;
PyObject *seq; PyObject *seq;
_PyCoreConfig *core_config = &_PyGILState_GetInterpreterStateUnsafe()->core_config; const _PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config;
seq = PyStructSequence_New(&FlagsType); seq = PyStructSequence_New(&FlagsType);
if (seq == NULL) if (seq == NULL)
...@@ -2085,23 +2085,23 @@ make_flags(void) ...@@ -2085,23 +2085,23 @@ make_flags(void)
#define SetFlag(flag) \ #define SetFlag(flag) \
PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag)) PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag))
SetFlag(Py_DebugFlag); SetFlag(config->parser_debug);
SetFlag(Py_InspectFlag); SetFlag(config->inspect);
SetFlag(Py_InteractiveFlag); SetFlag(config->interactive);
SetFlag(Py_OptimizeFlag); SetFlag(config->optimization_level);
SetFlag(Py_DontWriteBytecodeFlag); SetFlag(!config->write_bytecode);
SetFlag(Py_NoUserSiteDirectory); SetFlag(!config->user_site_directory);
SetFlag(Py_NoSiteFlag); SetFlag(!config->site_import);
SetFlag(Py_IgnoreEnvironmentFlag); SetFlag(!config->use_environment);
SetFlag(Py_VerboseFlag); SetFlag(config->verbose);
/* SetFlag(saw_unbuffered_flag); */ /* SetFlag(saw_unbuffered_flag); */
/* SetFlag(skipfirstline); */ /* SetFlag(skipfirstline); */
SetFlag(Py_BytesWarningFlag); SetFlag(config->bytes_warning);
SetFlag(Py_QuietFlag); SetFlag(config->quiet);
SetFlag(Py_HashRandomizationFlag); SetFlag(config->use_hash_seed == 0 || config->hash_seed != 0);
SetFlag(Py_IsolatedFlag); SetFlag(config->isolated);
PyStructSequence_SET_ITEM(seq, pos++, PyBool_FromLong(core_config->dev_mode)); PyStructSequence_SET_ITEM(seq, pos++, PyBool_FromLong(config->dev_mode));
SetFlag(Py_UTF8Mode); SetFlag(config->utf8_mode);
#undef SetFlag #undef SetFlag
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
...@@ -2474,8 +2474,10 @@ err_occurred: ...@@ -2474,8 +2474,10 @@ err_occurred:
} while (0) } while (0)
int int
_PySys_EndInit(PyObject *sysdict, _PyMainInterpreterConfig *config) _PySys_EndInit(PyObject *sysdict, PyInterpreterState *interp)
{ {
const _PyCoreConfig *core_config = &interp->core_config;
const _PyMainInterpreterConfig *config = &interp->config;
int res; int res;
/* _PyMainInterpreterConfig_Read() must set all these variables */ /* _PyMainInterpreterConfig_Read() must set all these variables */
...@@ -2523,7 +2525,7 @@ _PySys_EndInit(PyObject *sysdict, _PyMainInterpreterConfig *config) ...@@ -2523,7 +2525,7 @@ _PySys_EndInit(PyObject *sysdict, _PyMainInterpreterConfig *config)
} }
SET_SYS_FROM_STRING_INT_RESULT("dont_write_bytecode", SET_SYS_FROM_STRING_INT_RESULT("dont_write_bytecode",
PyBool_FromLong(Py_DontWriteBytecodeFlag)); PyBool_FromLong(!core_config->write_bytecode));
if (get_warnoptions() == NULL) if (get_warnoptions() == NULL)
return -1; return -1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment