0
0
mirror of https://github.com/python/cpython.git synced 2024-11-24 17:47:13 +01:00

bpo-40268: Add _PyInterpreterState_GetConfig() (GH-19492)

Don't access PyInterpreterState.config member directly anymore, but
use new functions:

* _PyInterpreterState_GetConfig()
* _PyInterpreterState_SetConfig()
* _Py_GetConfig()
This commit is contained in:
Victor Stinner 2020-04-13 03:04:28 +02:00 committed by GitHub
parent 14d5331eb5
commit da7933ecc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 90 additions and 64 deletions

View File

@ -192,6 +192,13 @@ PyAPI_FUNC(void) _PyInterpreterState_SetEvalFrameFunc(
PyInterpreterState *interp,
_PyFrameEvalFunction eval_frame);
PyAPI_FUNC(const PyConfig*) _PyInterpreterState_GetConfig(PyInterpreterState *interp);
// Get the configuration of the currrent interpreter.
// The caller must hold the GIL.
PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);
/* cross-interpreter data */
struct _xid;

View File

@ -380,6 +380,10 @@ PyAPI_FUNC(void) _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime);
/* Used by _PyImport_Cleanup() */
extern void _PyInterpreterState_ClearModules(PyInterpreterState *interp);
extern PyStatus _PyInterpreterState_SetConfig(
PyInterpreterState *interp,
const PyConfig *config);
PyAPI_FUNC(void) _PyGILState_Reinit(_PyRuntimeState *runtime);

View File

@ -9,7 +9,6 @@
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "pycore_pystate.h" /* _PyInterpreterState_GET_UNSAFE() */
#include "structmember.h"
#include "_iomodule.h"
@ -377,7 +376,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
{
PyObject *RawIO_class = (PyObject *)&PyFileIO_Type;
#ifdef MS_WINDOWS
PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
const PyConfig *config = _Py_GetConfig();
if (!config->legacy_windows_stdio && _PyIO_get_console_type(path_or_fd) != '\0') {
RawIO_class = (PyObject *)&PyWindowsConsoleIO_Type;
encoding = "utf-8";

View File

@ -287,8 +287,7 @@ iobase_finalize(PyObject *self)
shutdown issues). */
if (res == NULL) {
#ifndef Py_DEBUG
const PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
if (config->dev_mode) {
if (_Py_GetConfig()->dev_mode) {
PyErr_WriteUnraisable(self);
}
else {

View File

@ -9,6 +9,7 @@
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "pycore_object.h"
#include "pycore_pystate.h"
#include "structmember.h"
#include "_iomodule.h"
@ -996,7 +997,7 @@ io_check_errors(PyObject *errors)
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
#ifndef Py_DEBUG
/* In release mode, only check in development mode (-X dev) */
if (!interp->config.dev_mode) {
if (!_PyInterpreterState_GetConfig(interp)->dev_mode) {
return 0;
}
#else

View File

@ -301,7 +301,7 @@ pymain_run_module(const wchar_t *modname, int set_argv0)
static int
pymain_run_file(PyConfig *config, PyCompilerFlags *cf)
pymain_run_file(const PyConfig *config, PyCompilerFlags *cf)
{
const wchar_t *filename = config->run_filename;
if (PySys_Audit("cpython.run_file", "u", filename) < 0) {
@ -499,7 +499,7 @@ pymain_run_python(int *exitcode)
{
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
/* pymain_run_stdin() modify the config */
PyConfig *config = &interp->config;
PyConfig *config = (PyConfig*)_PyInterpreterState_GetConfig(interp);
PyObject *main_importer_path = NULL;
if (config->run_filename != NULL) {

View File

@ -6,7 +6,6 @@
#include "pycore_bytes_methods.h"
#include "pycore_object.h"
#include "pycore_pymem.h"
#include "pycore_pystate.h"
#include "structmember.h"
#include "bytesobject.h"
#include "pystrhex.h"
@ -997,8 +996,7 @@ bytearray_repr(PyByteArrayObject *self)
static PyObject *
bytearray_str(PyObject *op)
{
PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
if (config->bytes_warning) {
if (_Py_GetConfig()->bytes_warning) {
if (PyErr_WarnEx(PyExc_BytesWarning,
"str() on a bytearray instance", 1)) {
return NULL;
@ -1023,8 +1021,7 @@ bytearray_richcompare(PyObject *self, PyObject *other, int op)
if (rc < 0)
return NULL;
if (rc) {
PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
if (config->bytes_warning && (op == Py_EQ || op == Py_NE)) {
if (_Py_GetConfig()->bytes_warning && (op == Py_EQ || op == Py_NE)) {
if (PyErr_WarnEx(PyExc_BytesWarning,
"Comparison between bytearray and string", 1))
return NULL;

View File

@ -7,7 +7,6 @@
#include "pycore_bytes_methods.h"
#include "pycore_object.h"
#include "pycore_pymem.h"
#include "pycore_pystate.h"
#include "pystrhex.h"
#include <stddef.h>
@ -1342,8 +1341,7 @@ bytes_repr(PyObject *op)
static PyObject *
bytes_str(PyObject *op)
{
PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
if (config->bytes_warning) {
if (_Py_GetConfig()->bytes_warning) {
if (PyErr_WarnEx(PyExc_BytesWarning,
"str() on a bytes instance", 1)) {
return NULL;
@ -1500,8 +1498,7 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
/* Make sure both arguments are strings. */
if (!(PyBytes_Check(a) && PyBytes_Check(b))) {
PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
if (config->bytes_warning && (op == Py_EQ || op == Py_NE)) {
if (_Py_GetConfig()->bytes_warning && (op == Py_EQ || op == Py_NE)) {
rc = PyObject_IsInstance((PyObject*)a,
(PyObject*)&PyUnicode_Type);
if (!rc)

View File

@ -572,7 +572,7 @@ _PyModule_ClearDict(PyObject *d)
Py_ssize_t pos;
PyObject *key, *value;
int verbose = _PyInterpreterState_GET_UNSAFE()->config.verbose;
int verbose = _Py_GetConfig()->verbose;
/* First, clear only names starting with a single underscore */
pos = 0;
@ -659,7 +659,7 @@ module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
static void
module_dealloc(PyModuleObject *m)
{
int verbose = _PyInterpreterState_GET_UNSAFE()->config.verbose;
int verbose = _Py_GetConfig()->verbose;
PyObject_GC_UnTrack(m);
if (verbose && m->md_name) {

View File

@ -439,7 +439,7 @@ unicode_check_encoding_errors(const char *encoding, const char *errors)
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
#ifndef Py_DEBUG
/* In release mode, only check in development mode (-X dev) */
if (!interp->config.dev_mode) {
if (!_PyInterpreterState_GetConfig(interp)->dev_mode) {
return 0;
}
#else
@ -3632,7 +3632,8 @@ PyUnicode_EncodeFSDefault(PyObject *unicode)
/* Before _PyUnicode_InitEncodings() is called, the Python codec
machinery is not ready and so cannot be used:
use wcstombs() in this case. */
const wchar_t *filesystem_errors = interp->config.filesystem_errors;
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
const wchar_t *filesystem_errors = config->filesystem_errors;
assert(filesystem_errors != NULL);
_Py_error_handler errors = get_error_handler_wide(filesystem_errors);
assert(errors != _Py_ERROR_UNKNOWN);
@ -3868,7 +3869,8 @@ PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
/* Before _PyUnicode_InitEncodings() is called, the Python codec
machinery is not ready and so cannot be used:
use mbstowcs() in this case. */
const wchar_t *filesystem_errors = interp->config.filesystem_errors;
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
const wchar_t *filesystem_errors = config->filesystem_errors;
assert(filesystem_errors != NULL);
_Py_error_handler errors = get_error_handler_wide(filesystem_errors);
assert(errors != _Py_ERROR_UNKNOWN);
@ -15894,7 +15896,7 @@ static PyStatus
init_stdio_encoding(PyThreadState *tstate)
{
/* Update the stdio encoding to the normalized Python codec name. */
PyConfig *config = &tstate->interp->config;
PyConfig *config = (PyConfig*)_PyInterpreterState_GetConfig(tstate->interp);
if (config_get_codec_name(&config->stdio_encoding) < 0) {
return _PyStatus_ERR("failed to get the Python codec name "
"of the stdio encoding");
@ -15906,7 +15908,7 @@ init_stdio_encoding(PyThreadState *tstate)
static int
init_fs_codec(PyInterpreterState *interp)
{
PyConfig *config = &interp->config;
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
_Py_error_handler error_handler;
error_handler = get_error_handler_wide(config->filesystem_errors);
@ -15964,7 +15966,7 @@ init_fs_encoding(PyThreadState *tstate)
/* Update the filesystem encoding to the normalized Python codec name.
For example, replace "ANSI_X3.4-1968" (locale encoding) with "ascii"
(Python codec name). */
PyConfig *config = &interp->config;
PyConfig *config = (PyConfig*)_PyInterpreterState_GetConfig(interp);
if (config_get_codec_name(&config->filesystem_encoding) < 0) {
_Py_DumpPathConfig(tstate);
return _PyStatus_ERR("failed to get the Python codec "
@ -16008,7 +16010,7 @@ int
_PyUnicode_EnableLegacyWindowsFSEncoding(void)
{
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
PyConfig *config = &interp->config;
PyConfig *config = (PyConfig *)_PyInterpreterState_GetConfig(interp);
/* Set the filesystem encoding to mbcs/replace (PEP 529) */
wchar_t *encoding = _PyMem_RawWcsdup(L"mbcs");

View File

@ -2770,7 +2770,7 @@ _PyBuiltin_Init(PyThreadState *tstate)
{
PyObject *mod, *dict, *debug;
const PyConfig *config = &tstate->interp->config;
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
if (PyType_Ready(&PyFilter_Type) < 0 ||
PyType_Ready(&PyMap_Type) < 0 ||

View File

@ -23,7 +23,6 @@
#include "Python.h"
#include "pycore_pystate.h" /* _PyInterpreterState_GET_UNSAFE() */
#include "Python-ast.h"
#include "ast.h"
#include "code.h"
@ -323,7 +322,6 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
PyCodeObject *co = NULL;
PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
int merged;
PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
if (!__doc__) {
__doc__ = PyUnicode_InternFromString("__doc__");
@ -350,7 +348,7 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
c.c_future->ff_features = merged;
flags->cf_flags = merged;
c.c_flags = flags;
c.c_optimize = (optimize == -1) ? config->optimization_level : optimize;
c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
c.c_nestlevel = 0;
c.c_do_not_emit_bytecode = 0;

View File

@ -6,7 +6,6 @@
#include "Python.h"
#include "importdl.h"
#include "pycore_pystate.h"
#if defined(__hp9000s300)
#define FUNCNAME_PATTERN "_%.20s_%.200s"
@ -21,7 +20,7 @@ dl_funcptr _PyImport_FindSharedFuncptr(const char *prefix,
const char *pathname, FILE *fp)
{
int flags = BIND_FIRST | BIND_DEFERRED;
int verbose = _PyInterpreterState_GET_UNSAFE()->config.verbose;
int verbose = _Py_GetConfig()->verbose;
if (verbose) {
flags = BIND_FIRST | BIND_IMMEDIATE |
BIND_NONFATAL | BIND_VERBOSE;

View File

@ -102,7 +102,7 @@ _PyImportZip_Init(PyThreadState *tstate)
goto error;
}
int verbose = tstate->interp->config.verbose;
int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;
if (verbose) {
PySys_WriteStderr("# installing zipimport hook\n");
}
@ -446,7 +446,7 @@ _PyImport_Cleanup(PyThreadState *tstate)
/* XXX Perhaps these precautions are obsolete. Who knows? */
int verbose = interp->config.verbose;
int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
if (verbose) {
PySys_WriteStderr("# clear builtins._\n");
}
@ -811,7 +811,7 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
return NULL;
}
int verbose = tstate->interp->config.verbose;
int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;
if (verbose) {
PySys_FormatStderr("import %U # previously loaded (%R)\n",
name, filename);
@ -1523,7 +1523,7 @@ remove_importlib_frames(PyThreadState *tstate)
which end with a call to "_call_with_frames_removed". */
_PyErr_Fetch(tstate, &exception, &value, &base_tb);
if (!exception || tstate->interp->config.verbose) {
if (!exception || _PyInterpreterState_GetConfig(tstate->interp)->verbose) {
goto done;
}
@ -1727,7 +1727,7 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
_Py_IDENTIFIER(_find_and_load);
PyObject *mod = NULL;
PyInterpreterState *interp = tstate->interp;
int import_time = interp->config.import_time;
int import_time = _PyInterpreterState_GetConfig(interp)->import_time;
static int import_level;
static _PyTime_t accumulated;
@ -2413,7 +2413,7 @@ PyInit__imp(void)
goto failure;
}
const wchar_t *mode = _PyInterpreterState_GET_UNSAFE()->config.check_hash_pycs_mode;
const wchar_t *mode = _Py_GetConfig()->check_hash_pycs_mode;
PyObject *pyc_mode = PyUnicode_FromWideChar(mode, -1);
if (pyc_mode == NULL) {
goto failure;

View File

@ -2595,7 +2595,7 @@ _Py_GetConfigsAsDict(void)
Py_CLEAR(dict);
/* core config */
const PyConfig *config = &tstate->interp->config;
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
dict = config_as_dict(config);
if (dict == NULL) {
goto error;
@ -2662,7 +2662,7 @@ _Py_DumpPathConfig(PyThreadState *tstate)
PySys_WriteStderr("\n"); \
} while (0)
PyConfig *config = &tstate->interp->config;
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
DUMP_CONFIG("PYTHONHOME", home);
DUMP_CONFIG("PYTHONPATH", pythonpath_env);
DUMP_CONFIG("program name", program_name);

View File

@ -157,7 +157,7 @@ init_importlib(PyThreadState *tstate, PyObject *sysmod)
PyObject *impmod;
PyObject *value;
PyInterpreterState *interp = tstate->interp;
int verbose = interp->config.verbose;
int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
/* Import _importlib through its frozen version, _frozen_importlib. */
if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
@ -473,11 +473,11 @@ pyinit_core_reconfigure(_PyRuntimeState *runtime,
_PyConfig_Write(config, runtime);
status = _PyConfig_Copy(&interp->config, config);
status = _PyInterpreterState_SetConfig(interp, config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
config = &interp->config;
config = _PyInterpreterState_GetConfig(interp);
if (config->_install_importlib) {
status = _PyConfig_WritePathConfig(config);
@ -558,7 +558,7 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
return _PyStatus_ERR("can't make main interpreter");
}
PyStatus status = _PyConfig_Copy(&interp->config, config);
PyStatus status = _PyInterpreterState_SetConfig(interp, config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
@ -692,7 +692,7 @@ pycore_init_import_warnings(PyThreadState *tstate, PyObject *sysmod)
return status;
}
const PyConfig *config = &tstate->interp->config;
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
if (_Py_IsMainInterpreter(tstate)) {
/* Initialize _warnings. */
status = _PyWarnings_InitState(tstate);
@ -953,7 +953,7 @@ done:
static PyStatus
_Py_ReconfigureMainInterpreter(PyThreadState *tstate)
{
PyConfig *config = &tstate->interp->config;
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
PyObject *argv = _PyWideStringList_AsList(&config->argv);
if (argv == NULL) {
@ -977,7 +977,7 @@ init_interp_main(PyThreadState *tstate)
PyStatus status;
int is_main_interp = _Py_IsMainInterpreter(tstate);
PyInterpreterState *interp = tstate->interp;
PyConfig *config = &interp->config;
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
if (!config->_install_importlib) {
/* Special mode for freeze_importlib: run with no import system
@ -1146,7 +1146,7 @@ Py_InitializeFromConfig(const PyConfig *config)
if (_PyStatus_EXCEPTION(status)) {
return status;
}
config = &tstate->interp->config;
config = _PyInterpreterState_GetConfig(tstate->interp);
if (config->_init_main) {
status = pyinit_main(tstate);
@ -1571,16 +1571,16 @@ new_interpreter(PyThreadState **tstate_p)
PyThreadState *save_tstate = PyThreadState_Swap(tstate);
/* Copy the current interpreter config into the new interpreter */
PyConfig *config;
const PyConfig *config;
if (save_tstate != NULL) {
config = &save_tstate->interp->config;
config = _PyInterpreterState_GetConfig(save_tstate->interp);
} else {
/* No current thread state, copy from the main interpreter */
PyInterpreterState *main_interp = PyInterpreterState_Main();
config = &main_interp->config;
config = _PyInterpreterState_GetConfig(main_interp);
}
status = _PyConfig_Copy(&interp->config, config);
status = _PyInterpreterState_SetConfig(interp, config);
if (_PyStatus_EXCEPTION(status)) {
goto error;
}
@ -1953,7 +1953,7 @@ init_sys_streams(PyThreadState *tstate)
int fd;
PyObject * encoding_attr;
PyStatus res = _PyStatus_OK();
const PyConfig *config = &tstate->interp->config;
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
/* Check that stdin is not a directory
Using shell redirection, you can redirect stdin to a directory,

View File

@ -790,7 +790,7 @@ _PyInterpreterState_ClearModules(PyInterpreterState *interp)
void
PyThreadState_Clear(PyThreadState *tstate)
{
int verbose = tstate->interp->config.verbose;
int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;
if (verbose && tstate->frame != NULL) {
/* bpo-20526: After the main thread calls
@ -1808,6 +1808,30 @@ _PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp,
interp->eval_frame = eval_frame;
}
const PyConfig*
_PyInterpreterState_GetConfig(PyInterpreterState *interp)
{
return &interp->config;
}
PyStatus
_PyInterpreterState_SetConfig(PyInterpreterState *interp,
const PyConfig *config)
{
return _PyConfig_Copy(&interp->config, config);
}
const PyConfig*
_Py_GetConfig(void)
{
assert(PyGILState_Check());
PyThreadState *tstate = _PyThreadState_GET();
return _PyInterpreterState_GetConfig(tstate->interp);
}
#ifdef __cplusplus
}
#endif

View File

@ -96,7 +96,7 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *
PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
int nomem_count = 0;
#ifdef Py_REF_DEBUG
int show_ref_count = _PyInterpreterState_GET_UNSAFE()->config.show_ref_count;
int show_ref_count = _Py_GetConfig()->show_ref_count;
#endif
filename = PyUnicode_DecodeFSDefault(filename_str);
@ -584,7 +584,7 @@ print_error_text(PyObject *f, int offset, PyObject *text_obj)
int
_Py_HandleSystemExit(int *exitcode_p)
{
int inspect = _PyInterpreterState_GET_UNSAFE()->config.inspect;
int inspect = _Py_GetConfig()->inspect;
if (inspect) {
/* Don't exit if -i flag was given. This flag is set to 0
* when entering interactive mode for inspecting. */

View File

@ -23,7 +23,6 @@ Data members:
#include "pycore_pyerrors.h"
#include "pycore_pylifecycle.h"
#include "pycore_pymem.h"
#include "pycore_pystate.h"
#include "pycore_tupleobject.h"
#include "pythread.h"
#include "pydtrace.h"
@ -337,7 +336,7 @@ _PySys_ClearAuditHooks(PyThreadState *ts)
return;
}
const PyConfig *config = &ts->interp->config;
const PyConfig *config = _PyInterpreterState_GetConfig(ts->interp);
if (config->verbose) {
PySys_WriteStderr("# clear sys.audit hooks\n");
}
@ -846,8 +845,8 @@ static PyObject *
sys_getfilesystemencoding_impl(PyObject *module)
/*[clinic end generated code: output=1dc4bdbe9be44aa7 input=8475f8649b8c7d8c]*/
{
PyThreadState *tstate = _PyThreadState_GET();
const PyConfig *config = &tstate->interp->config;
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
return PyUnicode_FromWideChar(config->filesystem_encoding, -1);
}
@ -861,8 +860,8 @@ static PyObject *
sys_getfilesystemencodeerrors_impl(PyObject *module)
/*[clinic end generated code: output=ba77b36bbf7c96f5 input=22a1e8365566f1e5]*/
{
PyThreadState *tstate = _PyThreadState_GET();
const PyConfig *config = &tstate->interp->config;
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
return PyUnicode_FromWideChar(config->filesystem_errors, -1);
}
@ -2455,7 +2454,7 @@ make_flags(PyThreadState *tstate)
{
PyInterpreterState *interp = tstate->interp;
const PyPreConfig *preconfig = &interp->runtime->preconfig;
const PyConfig *config = &interp->config;
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
PyObject *seq = PyStructSequence_New(&FlagsType);
if (seq == NULL) {
@ -2889,7 +2888,7 @@ int
_PySys_InitMain(PyThreadState *tstate)
{
PyObject *sysdict = tstate->interp->sysdict;
const PyConfig *config = &tstate->interp->config;
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
int res;
#define COPY_LIST(KEY, VALUE) \