Skip to content

Commit da7933e

Browse files
authored
bpo-40268: Add _PyInterpreterState_GetConfig() (pythonGH-19492)
Don't access PyInterpreterState.config member directly anymore, but use new functions: * _PyInterpreterState_GetConfig() * _PyInterpreterState_SetConfig() * _Py_GetConfig()
1 parent 14d5331 commit da7933e

File tree

19 files changed

+90
-64
lines changed

19 files changed

+90
-64
lines changed

Include/cpython/pystate.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ PyAPI_FUNC(void) _PyInterpreterState_SetEvalFrameFunc(
192192
PyInterpreterState *interp,
193193
_PyFrameEvalFunction eval_frame);
194194

195+
PyAPI_FUNC(const PyConfig*) _PyInterpreterState_GetConfig(PyInterpreterState *interp);
196+
197+
// Get the configuration of the currrent interpreter.
198+
// The caller must hold the GIL.
199+
PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);
200+
201+
195202
/* cross-interpreter data */
196203

197204
struct _xid;

Include/internal/pycore_pystate.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ PyAPI_FUNC(void) _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime);
380380
/* Used by _PyImport_Cleanup() */
381381
extern void _PyInterpreterState_ClearModules(PyInterpreterState *interp);
382382

383+
extern PyStatus _PyInterpreterState_SetConfig(
384+
PyInterpreterState *interp,
385+
const PyConfig *config);
386+
383387
PyAPI_FUNC(void) _PyGILState_Reinit(_PyRuntimeState *runtime);
384388

385389

Modules/_io/_iomodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#define PY_SSIZE_T_CLEAN
1111
#include "Python.h"
12-
#include "pycore_pystate.h" /* _PyInterpreterState_GET_UNSAFE() */
1312
#include "structmember.h"
1413
#include "_iomodule.h"
1514

@@ -377,7 +376,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
377376
{
378377
PyObject *RawIO_class = (PyObject *)&PyFileIO_Type;
379378
#ifdef MS_WINDOWS
380-
PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
379+
const PyConfig *config = _Py_GetConfig();
381380
if (!config->legacy_windows_stdio && _PyIO_get_console_type(path_or_fd) != '\0') {
382381
RawIO_class = (PyObject *)&PyWindowsConsoleIO_Type;
383382
encoding = "utf-8";

Modules/_io/iobase.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,7 @@ iobase_finalize(PyObject *self)
287287
shutdown issues). */
288288
if (res == NULL) {
289289
#ifndef Py_DEBUG
290-
const PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
291-
if (config->dev_mode) {
290+
if (_Py_GetConfig()->dev_mode) {
292291
PyErr_WriteUnraisable(self);
293292
}
294293
else {

Modules/_io/textio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define PY_SSIZE_T_CLEAN
1010
#include "Python.h"
1111
#include "pycore_object.h"
12+
#include "pycore_pystate.h"
1213
#include "structmember.h"
1314
#include "_iomodule.h"
1415

@@ -996,7 +997,7 @@ io_check_errors(PyObject *errors)
996997
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
997998
#ifndef Py_DEBUG
998999
/* In release mode, only check in development mode (-X dev) */
999-
if (!interp->config.dev_mode) {
1000+
if (!_PyInterpreterState_GetConfig(interp)->dev_mode) {
10001001
return 0;
10011002
}
10021003
#else

Modules/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ pymain_run_module(const wchar_t *modname, int set_argv0)
301301

302302

303303
static int
304-
pymain_run_file(PyConfig *config, PyCompilerFlags *cf)
304+
pymain_run_file(const PyConfig *config, PyCompilerFlags *cf)
305305
{
306306
const wchar_t *filename = config->run_filename;
307307
if (PySys_Audit("cpython.run_file", "u", filename) < 0) {
@@ -499,7 +499,7 @@ pymain_run_python(int *exitcode)
499499
{
500500
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
501501
/* pymain_run_stdin() modify the config */
502-
PyConfig *config = &interp->config;
502+
PyConfig *config = (PyConfig*)_PyInterpreterState_GetConfig(interp);
503503

504504
PyObject *main_importer_path = NULL;
505505
if (config->run_filename != NULL) {

Objects/bytearrayobject.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "pycore_bytes_methods.h"
77
#include "pycore_object.h"
88
#include "pycore_pymem.h"
9-
#include "pycore_pystate.h"
109
#include "structmember.h"
1110
#include "bytesobject.h"
1211
#include "pystrhex.h"
@@ -997,8 +996,7 @@ bytearray_repr(PyByteArrayObject *self)
997996
static PyObject *
998997
bytearray_str(PyObject *op)
999998
{
1000-
PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
1001-
if (config->bytes_warning) {
999+
if (_Py_GetConfig()->bytes_warning) {
10021000
if (PyErr_WarnEx(PyExc_BytesWarning,
10031001
"str() on a bytearray instance", 1)) {
10041002
return NULL;
@@ -1023,8 +1021,7 @@ bytearray_richcompare(PyObject *self, PyObject *other, int op)
10231021
if (rc < 0)
10241022
return NULL;
10251023
if (rc) {
1026-
PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
1027-
if (config->bytes_warning && (op == Py_EQ || op == Py_NE)) {
1024+
if (_Py_GetConfig()->bytes_warning && (op == Py_EQ || op == Py_NE)) {
10281025
if (PyErr_WarnEx(PyExc_BytesWarning,
10291026
"Comparison between bytearray and string", 1))
10301027
return NULL;

Objects/bytesobject.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "pycore_bytes_methods.h"
88
#include "pycore_object.h"
99
#include "pycore_pymem.h"
10-
#include "pycore_pystate.h"
1110

1211
#include "pystrhex.h"
1312
#include <stddef.h>
@@ -1342,8 +1341,7 @@ bytes_repr(PyObject *op)
13421341
static PyObject *
13431342
bytes_str(PyObject *op)
13441343
{
1345-
PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
1346-
if (config->bytes_warning) {
1344+
if (_Py_GetConfig()->bytes_warning) {
13471345
if (PyErr_WarnEx(PyExc_BytesWarning,
13481346
"str() on a bytes instance", 1)) {
13491347
return NULL;
@@ -1500,8 +1498,7 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
15001498

15011499
/* Make sure both arguments are strings. */
15021500
if (!(PyBytes_Check(a) && PyBytes_Check(b))) {
1503-
PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
1504-
if (config->bytes_warning && (op == Py_EQ || op == Py_NE)) {
1501+
if (_Py_GetConfig()->bytes_warning && (op == Py_EQ || op == Py_NE)) {
15051502
rc = PyObject_IsInstance((PyObject*)a,
15061503
(PyObject*)&PyUnicode_Type);
15071504
if (!rc)

Objects/moduleobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ _PyModule_ClearDict(PyObject *d)
572572
Py_ssize_t pos;
573573
PyObject *key, *value;
574574

575-
int verbose = _PyInterpreterState_GET_UNSAFE()->config.verbose;
575+
int verbose = _Py_GetConfig()->verbose;
576576

577577
/* First, clear only names starting with a single underscore */
578578
pos = 0;
@@ -659,7 +659,7 @@ module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
659659
static void
660660
module_dealloc(PyModuleObject *m)
661661
{
662-
int verbose = _PyInterpreterState_GET_UNSAFE()->config.verbose;
662+
int verbose = _Py_GetConfig()->verbose;
663663

664664
PyObject_GC_UnTrack(m);
665665
if (verbose && m->md_name) {

Objects/unicodeobject.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ unicode_check_encoding_errors(const char *encoding, const char *errors)
439439
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
440440
#ifndef Py_DEBUG
441441
/* In release mode, only check in development mode (-X dev) */
442-
if (!interp->config.dev_mode) {
442+
if (!_PyInterpreterState_GetConfig(interp)->dev_mode) {
443443
return 0;
444444
}
445445
#else
@@ -3632,7 +3632,8 @@ PyUnicode_EncodeFSDefault(PyObject *unicode)
36323632
/* Before _PyUnicode_InitEncodings() is called, the Python codec
36333633
machinery is not ready and so cannot be used:
36343634
use wcstombs() in this case. */
3635-
const wchar_t *filesystem_errors = interp->config.filesystem_errors;
3635+
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
3636+
const wchar_t *filesystem_errors = config->filesystem_errors;
36363637
assert(filesystem_errors != NULL);
36373638
_Py_error_handler errors = get_error_handler_wide(filesystem_errors);
36383639
assert(errors != _Py_ERROR_UNKNOWN);
@@ -3868,7 +3869,8 @@ PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
38683869
/* Before _PyUnicode_InitEncodings() is called, the Python codec
38693870
machinery is not ready and so cannot be used:
38703871
use mbstowcs() in this case. */
3871-
const wchar_t *filesystem_errors = interp->config.filesystem_errors;
3872+
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
3873+
const wchar_t *filesystem_errors = config->filesystem_errors;
38723874
assert(filesystem_errors != NULL);
38733875
_Py_error_handler errors = get_error_handler_wide(filesystem_errors);
38743876
assert(errors != _Py_ERROR_UNKNOWN);
@@ -15894,7 +15896,7 @@ static PyStatus
1589415896
init_stdio_encoding(PyThreadState *tstate)
1589515897
{
1589615898
/* Update the stdio encoding to the normalized Python codec name. */
15897-
PyConfig *config = &tstate->interp->config;
15899+
PyConfig *config = (PyConfig*)_PyInterpreterState_GetConfig(tstate->interp);
1589815900
if (config_get_codec_name(&config->stdio_encoding) < 0) {
1589915901
return _PyStatus_ERR("failed to get the Python codec name "
1590015902
"of the stdio encoding");
@@ -15906,7 +15908,7 @@ init_stdio_encoding(PyThreadState *tstate)
1590615908
static int
1590715909
init_fs_codec(PyInterpreterState *interp)
1590815910
{
15909-
PyConfig *config = &interp->config;
15911+
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
1591015912

1591115913
_Py_error_handler error_handler;
1591215914
error_handler = get_error_handler_wide(config->filesystem_errors);
@@ -15964,7 +15966,7 @@ init_fs_encoding(PyThreadState *tstate)
1596415966
/* Update the filesystem encoding to the normalized Python codec name.
1596515967
For example, replace "ANSI_X3.4-1968" (locale encoding) with "ascii"
1596615968
(Python codec name). */
15967-
PyConfig *config = &interp->config;
15969+
PyConfig *config = (PyConfig*)_PyInterpreterState_GetConfig(interp);
1596815970
if (config_get_codec_name(&config->filesystem_encoding) < 0) {
1596915971
_Py_DumpPathConfig(tstate);
1597015972
return _PyStatus_ERR("failed to get the Python codec "
@@ -16008,7 +16010,7 @@ int
1600816010
_PyUnicode_EnableLegacyWindowsFSEncoding(void)
1600916011
{
1601016012
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
16011-
PyConfig *config = &interp->config;
16013+
PyConfig *config = (PyConfig *)_PyInterpreterState_GetConfig(interp);
1601216014

1601316015
/* Set the filesystem encoding to mbcs/replace (PEP 529) */
1601416016
wchar_t *encoding = _PyMem_RawWcsdup(L"mbcs");

0 commit comments

Comments
 (0)