Skip to content

Commit 5d86246

Browse files
authored
bpo-32030: Fix compiler warnings (#4921)
Fix compiler warnings in Py_FinalizeEx(): only define variables if they are needed, add #ifdef. Other cleanup changes: * _PyWarnings_InitWithConfig() is no more needed: call _PyWarnings_Init() instead. * Inline pymain_init_main_interpreter() in its caller. This subfunction is no more justifed.
1 parent 21be85f commit 5d86246

File tree

4 files changed

+17
-40
lines changed

4 files changed

+17
-40
lines changed

Include/warnings.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ extern "C" {
77
#ifndef Py_LIMITED_API
88
PyAPI_FUNC(PyObject*) _PyWarnings_Init(void);
99
#endif
10-
#ifdef Py_BUILD_CORE
11-
PyAPI_FUNC(PyObject*) _PyWarnings_InitWithConfig(const _PyCoreConfig *config);
12-
#endif
1310

1411
PyAPI_FUNC(int) PyErr_WarnEx(
1512
PyObject *category,

Modules/main.c

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,27 +1151,6 @@ pymain_get_program_name(_PyMain *pymain)
11511151
}
11521152

11531153

1154-
/* Initialize the main interpreter.
1155-
*
1156-
* Replaces previous call to Py_Initialize()
1157-
*
1158-
* Return 0 on success.
1159-
* Set pymain->err and return -1 on error.
1160-
*/
1161-
static int
1162-
pymain_init_main_interpreter(_PyMain *pymain)
1163-
{
1164-
_PyInitError err;
1165-
1166-
err = _Py_InitializeMainInterpreter(&pymain->config);
1167-
if (_Py_INIT_FAILED(err)) {
1168-
pymain->err = err;
1169-
return -1;
1170-
}
1171-
return 0;
1172-
}
1173-
1174-
11751154
static void
11761155
pymain_header(_PyMain *pymain)
11771156
{
@@ -2357,7 +2336,9 @@ pymain_init_python_main(_PyMain *pymain)
23572336
return -1;
23582337
}
23592338

2360-
if (pymain_init_main_interpreter(pymain)) {
2339+
err = _Py_InitializeMainInterpreter(&pymain->config);
2340+
if (_Py_INIT_FAILED(err)) {
2341+
pymain->err = err;
23612342
return -1;
23622343
}
23632344

Python/_warnings.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ create_filter(PyObject *category, _Py_Identifier *id)
11731173

11741174

11751175
static PyObject *
1176-
init_filters(const _PyCoreConfig *config)
1176+
init_filters(void)
11771177
{
11781178
#ifdef Py_DEBUG
11791179
/* Py_DEBUG builds show all warnings by default */
@@ -1218,8 +1218,8 @@ static struct PyModuleDef warningsmodule = {
12181218
};
12191219

12201220

1221-
PyObject*
1222-
_PyWarnings_InitWithConfig(const _PyCoreConfig *config)
1221+
PyMODINIT_FUNC
1222+
_PyWarnings_Init(void)
12231223
{
12241224
PyObject *m;
12251225

@@ -1228,7 +1228,7 @@ _PyWarnings_InitWithConfig(const _PyCoreConfig *config)
12281228
return NULL;
12291229

12301230
if (_PyRuntime.warnings.filters == NULL) {
1231-
_PyRuntime.warnings.filters = init_filters(config);
1231+
_PyRuntime.warnings.filters = init_filters();
12321232
if (_PyRuntime.warnings.filters == NULL)
12331233
return NULL;
12341234
}
@@ -1259,12 +1259,3 @@ _PyWarnings_InitWithConfig(const _PyCoreConfig *config)
12591259
_PyRuntime.warnings.filters_version = 0;
12601260
return m;
12611261
}
1262-
1263-
1264-
PyMODINIT_FUNC
1265-
_PyWarnings_Init(void)
1266-
{
1267-
PyInterpreterState *interp = PyThreadState_GET()->interp;
1268-
const _PyCoreConfig *config = &interp->core_config;
1269-
return _PyWarnings_InitWithConfig(config);
1270-
}

Python/pylifecycle.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ _Py_InitializeCore(const _PyCoreConfig *core_config)
737737
}
738738

739739
/* Initialize _warnings. */
740-
if (_PyWarnings_InitWithConfig(&interp->core_config) == NULL) {
740+
if (_PyWarnings_Init() == NULL) {
741741
return _Py_INIT_ERR("can't initialize warnings");
742742
}
743743

@@ -847,7 +847,9 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config)
847847
}
848848

849849
/* Initialize warnings. */
850-
if (PySys_HasWarnOptions()) {
850+
if (interp->config.warnoptions != NULL &&
851+
PyList_Size(interp->config.warnoptions) > 0)
852+
{
851853
PyObject *warnings_module = PyImport_ImportModule("warnings");
852854
if (warnings_module == NULL) {
853855
fprintf(stderr, "'import warnings' failed; traceback:\n");
@@ -1021,9 +1023,15 @@ Py_FinalizeEx(void)
10211023

10221024
/* Copy the core config, PyInterpreterState_Delete() free
10231025
the core config memory */
1026+
#ifdef Py_REF_DEBUG
10241027
int show_ref_count = interp->core_config.show_ref_count;
1028+
#endif
1029+
#ifdef Py_TRACE_REFS
10251030
int dump_refs = interp->core_config.dump_refs;
1031+
#endif
1032+
#ifdef WITH_PYMALLOC
10261033
int malloc_stats = interp->core_config.malloc_stats;
1034+
#endif
10271035

10281036
/* Remaining threads (e.g. daemon threads) will automatically exit
10291037
after taking the GIL (in PyEval_RestoreThread()). */

0 commit comments

Comments
 (0)