Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down
3 changes: 1 addition & 2 deletions Modules/_io/_iomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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";
Expand Down
3 changes: 1 addition & 2 deletions Modules/_io/iobase.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 2 additions & 5 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 9 additions & 7 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand All @@ -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);
Expand Down Expand Up @@ -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 "
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
4 changes: 1 addition & 3 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#include "Python.h"

#include "pycore_pystate.h" /* _PyInterpreterState_GET_UNSAFE() */
#include "Python-ast.h"
#include "ast.h"
#include "code.h"
Expand Down Expand Up @@ -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__");
Expand All @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions Python/dynload_hpux.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include "Python.h"
#include "importdl.h"
#include "pycore_pystate.h"

#if defined(__hp9000s300)
#define FUNCNAME_PATTERN "_%.20s_%.200s"
Expand All @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading