Skip to content

Commit 957a23b

Browse files
committed
Issue python#17408: Avoid using an obsolete instance of the copyreg module when the interpreter is shutdown and then started again.
1 parent 5de183a commit 957a23b

4 files changed

Lines changed: 22 additions & 8 deletions

File tree

‎Include/pythonrun.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ PyAPI_FUNC(void) PyFloat_Fini(void);
219219
PyAPI_FUNC(void) PyOS_FiniInterrupts(void);
220220
PyAPI_FUNC(void) _PyGC_Fini(void);
221221
PyAPI_FUNC(void) PySlice_Fini(void);
222+
PyAPI_FUNC(void) _PyType_Fini(void);
222223

223224
PyAPI_DATA(PyThreadState *) _Py_Finalizing;
224225
#endif

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.3.2?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #17408: Avoid using an obsolete instance of the copyreg module when
16+
the interpreter is shutdown and then started again.
17+
1518
- Issue #17863: In the interactive console, don't loop forever if the encoding
1619
can't be fetched from stdin.
1720

‎Objects/typeobject.c‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#include <ctype.h>
88

99

10+
/* Cached lookup of the copyreg module, for faster __reduce__ calls */
11+
12+
static PyObject *cached_copyreg_module = NULL;
13+
1014
/* Support type attribute cache */
1115

1216
/* The cache can keep references to the names alive for longer than
@@ -68,6 +72,15 @@ PyType_ClearCache(void)
6872
return cur_version_tag;
6973
}
7074

75+
void
76+
_PyType_Fini(void)
77+
{
78+
PyType_ClearCache();
79+
/* Need to forget our obsolete instance of the copyreg module at
80+
* interpreter shutdown (issue #17408). */
81+
Py_CLEAR(cached_copyreg_module);
82+
}
83+
7184
void
7285
PyType_Modified(PyTypeObject *type)
7386
{
@@ -3339,19 +3352,18 @@ static PyObject *
33393352
import_copyreg(void)
33403353
{
33413354
static PyObject *copyreg_str;
3342-
static PyObject *mod_copyreg = NULL;
33433355

33443356
if (!copyreg_str) {
33453357
copyreg_str = PyUnicode_InternFromString("copyreg");
33463358
if (copyreg_str == NULL)
33473359
return NULL;
33483360
}
3349-
if (!mod_copyreg) {
3350-
mod_copyreg = PyImport_Import(copyreg_str);
3361+
if (!cached_copyreg_module) {
3362+
cached_copyreg_module = PyImport_Import(copyreg_str);
33513363
}
33523364

3353-
Py_XINCREF(mod_copyreg);
3354-
return mod_copyreg;
3365+
Py_XINCREF(cached_copyreg_module);
3366+
return cached_copyreg_module;
33553367
}
33563368

33573369
static PyObject *

‎Python/pythonrun.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,6 @@ Py_Finalize(void)
506506
/* Disable signal handling */
507507
PyOS_FiniInterrupts();
508508

509-
/* Clear type lookup cache */
510-
PyType_ClearCache();
511-
512509
/* Collect garbage. This may call finalizers; it's nice to call these
513510
* before all modules are destroyed.
514511
* XXX If a __del__ or weakref callback is triggered here, and tries to
@@ -614,6 +611,7 @@ Py_Finalize(void)
614611
PyFloat_Fini();
615612
PyDict_Fini();
616613
PySlice_Fini();
614+
_PyType_Fini();
617615

618616
/* Cleanup Unicode implementation */
619617
_PyUnicode_Fini();

0 commit comments

Comments
 (0)