Skip to content

Commit c96be81

Browse files
authored
bpo-36900: Replace global conf vars with config (GH-13299)
Replace global configuration variables with core_config read from the current interpreter. Cleanup dynload_hpux.c.
1 parent 3c93153 commit c96be81

File tree

9 files changed

+51
-37
lines changed

9 files changed

+51
-37
lines changed

Modules/_io/_iomodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#define PY_SSIZE_T_CLEAN
1111
#include "Python.h"
12+
#include "pycore_pystate.h" /* _PyInterpreterState_GET_UNSAFE() */
1213
#include "structmember.h"
1314
#include "_iomodule.h"
1415

@@ -376,7 +377,8 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
376377
{
377378
PyObject *RawIO_class = (PyObject *)&PyFileIO_Type;
378379
#ifdef MS_WINDOWS
379-
if (!Py_LegacyWindowsStdioFlag && _PyIO_get_console_type(path_or_fd) != '\0') {
380+
_PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config;
381+
if (!config->legacy_windows_stdio && _PyIO_get_console_type(path_or_fd) != '\0') {
380382
RawIO_class = (PyObject *)&PyWindowsConsoleIO_Type;
381383
encoding = "utf-8";
382384
}

Modules/main.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,8 @@ static int
403403
pymain_run_stdin(_PyCoreConfig *config, PyCompilerFlags *cf)
404404
{
405405
if (stdin_is_interactive(config)) {
406-
Py_InspectFlag = 0; /* do exit on SystemExit */
407406
config->inspect = 0;
407+
Py_InspectFlag = 0; /* do exit on SystemExit */
408408
pymain_run_startup(config, cf);
409409
pymain_run_interactive_hook();
410410
}
@@ -425,17 +425,17 @@ pymain_repl(_PyCoreConfig *config, PyCompilerFlags *cf, int *exitcode)
425425
{
426426
/* Check this environment variable at the end, to give programs the
427427
opportunity to set it from Python. */
428-
if (!Py_InspectFlag && _Py_GetEnv(config->use_environment, "PYTHONINSPECT")) {
429-
Py_InspectFlag = 1;
428+
if (!config->inspect && _Py_GetEnv(config->use_environment, "PYTHONINSPECT")) {
430429
config->inspect = 1;
430+
Py_InspectFlag = 1;
431431
}
432432

433-
if (!(Py_InspectFlag && stdin_is_interactive(config) && RUN_CODE(config))) {
433+
if (!(config->inspect && stdin_is_interactive(config) && RUN_CODE(config))) {
434434
return;
435435
}
436436

437-
Py_InspectFlag = 0;
438437
config->inspect = 0;
438+
Py_InspectFlag = 0;
439439
pymain_run_interactive_hook();
440440

441441
int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);

Objects/bytearrayobject.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,8 @@ bytearray_repr(PyByteArrayObject *self)
998998
static PyObject *
999999
bytearray_str(PyObject *op)
10001000
{
1001-
if (Py_BytesWarningFlag) {
1001+
_PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config;
1002+
if (config->bytes_warning) {
10021003
if (PyErr_WarnEx(PyExc_BytesWarning,
10031004
"str() on a bytearray instance", 1)) {
10041005
return NULL;
@@ -1023,7 +1024,8 @@ bytearray_richcompare(PyObject *self, PyObject *other, int op)
10231024
if (rc < 0)
10241025
return NULL;
10251026
if (rc) {
1026-
if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
1027+
_PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config;
1028+
if (config->bytes_warning && (op == Py_EQ || op == Py_NE)) {
10271029
if (PyErr_WarnEx(PyExc_BytesWarning,
10281030
"Comparison between bytearray and string", 1))
10291031
return NULL;

Objects/bytesobject.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,8 @@ bytes_repr(PyObject *op)
14211421
static PyObject *
14221422
bytes_str(PyObject *op)
14231423
{
1424-
if (Py_BytesWarningFlag) {
1424+
_PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config;
1425+
if (config->bytes_warning) {
14251426
if (PyErr_WarnEx(PyExc_BytesWarning,
14261427
"str() on a bytes instance", 1)) {
14271428
return NULL;
@@ -1578,7 +1579,8 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
15781579

15791580
/* Make sure both arguments are strings. */
15801581
if (!(PyBytes_Check(a) && PyBytes_Check(b))) {
1581-
if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
1582+
_PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config;
1583+
if (config->bytes_warning && (op == Py_EQ || op == Py_NE)) {
15821584
rc = PyObject_IsInstance((PyObject*)a,
15831585
(PyObject*)&PyUnicode_Type);
15841586
if (!rc)

Objects/moduleobject.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,13 +590,15 @@ _PyModule_ClearDict(PyObject *d)
590590
Py_ssize_t pos;
591591
PyObject *key, *value;
592592

593+
int verbose = _PyInterpreterState_GET_UNSAFE()->core_config.verbose;
594+
593595
/* First, clear only names starting with a single underscore */
594596
pos = 0;
595597
while (PyDict_Next(d, &pos, &key, &value)) {
596598
if (value != Py_None && PyUnicode_Check(key)) {
597599
if (PyUnicode_READ_CHAR(key, 0) == '_' &&
598600
PyUnicode_READ_CHAR(key, 1) != '_') {
599-
if (Py_VerboseFlag > 1) {
601+
if (verbose > 1) {
600602
const char *s = PyUnicode_AsUTF8(key);
601603
if (s != NULL)
602604
PySys_WriteStderr("# clear[1] %s\n", s);
@@ -617,7 +619,7 @@ _PyModule_ClearDict(PyObject *d)
617619
if (PyUnicode_READ_CHAR(key, 0) != '_' ||
618620
!_PyUnicode_EqualToASCIIString(key, "__builtins__"))
619621
{
620-
if (Py_VerboseFlag > 1) {
622+
if (verbose > 1) {
621623
const char *s = PyUnicode_AsUTF8(key);
622624
if (s != NULL)
623625
PySys_WriteStderr("# clear[2] %s\n", s);
@@ -675,8 +677,10 @@ module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
675677
static void
676678
module_dealloc(PyModuleObject *m)
677679
{
680+
int verbose = _PyInterpreterState_GET_UNSAFE()->core_config.verbose;
681+
678682
PyObject_GC_UnTrack(m);
679-
if (Py_VerboseFlag && m->md_name) {
683+
if (verbose && m->md_name) {
680684
PySys_FormatStderr("# destroy %S\n", m->md_name);
681685
}
682686
if (m->md_weaklist != NULL)

Python/compile.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "Python.h"
2525

2626
#include "Python-ast.h"
27+
#include "pycore_pystate.h" /* _PyInterpreterState_GET_UNSAFE() */
2728
#include "ast.h"
2829
#include "code.h"
2930
#include "symtable.h"
@@ -310,6 +311,7 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
310311
PyCodeObject *co = NULL;
311312
PyCompilerFlags local_flags;
312313
int merged;
314+
_PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config;
313315

314316
if (!__doc__) {
315317
__doc__ = PyUnicode_InternFromString("__doc__");
@@ -338,7 +340,7 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
338340
c.c_future->ff_features = merged;
339341
flags->cf_flags = merged;
340342
c.c_flags = flags;
341-
c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
343+
c.c_optimize = (optimize == -1) ? config->optimization_level : optimize;
342344
c.c_nestlevel = 0;
343345

344346
if (!_PyAST_Optimize(mod, arena, c.c_optimize)) {

Python/dynload_hpux.c

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,47 @@ dl_funcptr _PyImport_FindSharedFuncptr(const char *prefix,
1919
const char *shortname,
2020
const char *pathname, FILE *fp)
2121
{
22-
dl_funcptr p;
23-
shl_t lib;
24-
int flags;
25-
char funcname[258];
26-
27-
flags = BIND_FIRST | BIND_DEFERRED;
28-
if (Py_VerboseFlag) {
22+
int flags = BIND_FIRST | BIND_DEFERRED;
23+
int verbose = _PyInterpreterState_GET_UNSAFE()->core_config.verbose;
24+
if (verbose) {
2925
flags = BIND_FIRST | BIND_IMMEDIATE |
3026
BIND_NONFATAL | BIND_VERBOSE;
3127
printf("shl_load %s\n",pathname);
3228
}
33-
lib = shl_load(pathname, flags, 0);
29+
30+
shl_t lib = shl_load(pathname, flags, 0);
3431
/* XXX Chuck Blake once wrote that 0 should be BIND_NOSTART? */
3532
if (lib == NULL) {
36-
char buf[256];
37-
PyObject *pathname_ob = NULL;
38-
PyObject *buf_ob = NULL;
39-
PyObject *shortname_ob = NULL;
40-
41-
if (Py_VerboseFlag)
33+
if (verbose) {
4234
perror(pathname);
35+
}
36+
char buf[256];
4337
PyOS_snprintf(buf, sizeof(buf), "Failed to load %.200s",
4438
pathname);
45-
buf_ob = PyUnicode_FromString(buf);
46-
shortname_ob = PyUnicode_FromString(shortname);
47-
pathname_ob = PyUnicode_FromString(pathname);
39+
PyObject *buf_ob = PyUnicode_FromString(buf);
40+
PyObject *shortname_ob = PyUnicode_FromString(shortname);
41+
PyObject *pathname_ob = PyUnicode_FromString(pathname);
4842
PyErr_SetImportError(buf_ob, shortname_ob, pathname_ob);
4943
Py_DECREF(buf_ob);
5044
Py_DECREF(shortname_ob);
5145
Py_DECREF(pathname_ob);
5246
return NULL;
5347
}
48+
49+
char funcname[258];
5450
PyOS_snprintf(funcname, sizeof(funcname), FUNCNAME_PATTERN,
5551
prefix, shortname);
56-
if (Py_VerboseFlag)
52+
if (verbose) {
5753
printf("shl_findsym %s\n", funcname);
54+
}
55+
56+
dl_funcptr p;
5857
if (shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p) == -1) {
5958
shl_unload(lib);
6059
p = NULL;
6160
}
62-
if (p == NULL && Py_VerboseFlag)
61+
if (p == NULL && verbose) {
6362
perror(funcname);
64-
63+
}
6564
return p;
6665
}

Python/pylifecycle.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,13 @@ init_importlib(PyInterpreterState *interp, PyObject *sysmod)
150150
PyObject *importlib;
151151
PyObject *impmod;
152152
PyObject *value;
153+
int verbose = interp->core_config.verbose;
153154

154155
/* Import _importlib through its frozen version, _frozen_importlib. */
155156
if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
156157
return _Py_INIT_ERR("can't import _frozen_importlib");
157158
}
158-
else if (Py_VerboseFlag) {
159+
else if (verbose) {
159160
PySys_FormatStderr("import _frozen_importlib # frozen\n");
160161
}
161162
importlib = PyImport_AddModule("_frozen_importlib");
@@ -175,7 +176,7 @@ init_importlib(PyInterpreterState *interp, PyObject *sysmod)
175176
if (impmod == NULL) {
176177
return _Py_INIT_ERR("can't import _imp");
177178
}
178-
else if (Py_VerboseFlag) {
179+
else if (verbose) {
179180
PySys_FormatStderr("import _imp # builtin\n");
180181
}
181182
if (_PyImport_SetModuleString("_imp", impmod) < 0) {

Python/pythonrun.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,12 @@ handle_system_exit(void)
591591
PyObject *exception, *value, *tb;
592592
int exitcode = 0;
593593

594-
if (Py_InspectFlag)
594+
int inspect = _PyInterpreterState_GET_UNSAFE()->core_config.inspect;
595+
if (inspect) {
595596
/* Don't exit if -i flag was given. This flag is set to 0
596597
* when entering interactive mode for inspecting. */
597598
return;
599+
}
598600

599601
PyErr_Fetch(&exception, &value, &tb);
600602
fflush(stdout);

0 commit comments

Comments
 (0)