changeset: 83613:0b34fd75b937 parent: 83610:193f25352e1c parent: 83612:8c1385205a35 user: Antoine Pitrou date: Sat May 04 20:46:19 2013 +0200 files: Misc/NEWS Objects/typeobject.c Python/pythonrun.c description: Issue #17408: Avoid using an obsolete instance of the copyreg module when the interpreter is shutdown and then started again. diff -r 193f25352e1c -r 0b34fd75b937 Include/pythonrun.h --- a/Include/pythonrun.h Sat May 04 20:18:53 2013 +0200 +++ b/Include/pythonrun.h Sat May 04 20:46:19 2013 +0200 @@ -219,6 +219,7 @@ PyAPI_FUNC(void) PyOS_FiniInterrupts(void); PyAPI_FUNC(void) _PyGC_Fini(void); PyAPI_FUNC(void) PySlice_Fini(void); +PyAPI_FUNC(void) _PyType_Fini(void); PyAPI_DATA(PyThreadState *) _Py_Finalizing; #endif diff -r 193f25352e1c -r 0b34fd75b937 Misc/NEWS --- a/Misc/NEWS Sat May 04 20:18:53 2013 +0200 +++ b/Misc/NEWS Sat May 04 20:46:19 2013 +0200 @@ -10,6 +10,9 @@ Core and Builtins ----------------- +- Issue #17408: Avoid using an obsolete instance of the copyreg module when + the interpreter is shutdown and then started again. + - Issue #5845: Enable tab-completion in the interactive interpreter by default, thanks to a new sys.__interactivehook__. diff -r 193f25352e1c -r 0b34fd75b937 Objects/typeobject.c --- a/Objects/typeobject.c Sat May 04 20:18:53 2013 +0200 +++ b/Objects/typeobject.c Sat May 04 20:46:19 2013 +0200 @@ -7,6 +7,10 @@ #include +/* Cached lookup of the copyreg module, for faster __reduce__ calls */ + +static PyObject *cached_copyreg_module = NULL; + /* Support type attribute cache */ /* The cache can keep references to the names alive for longer than @@ -69,6 +73,15 @@ } void +_PyType_Fini(void) +{ + PyType_ClearCache(); + /* Need to forget our obsolete instance of the copyreg module at + * interpreter shutdown (issue #17408). */ + Py_CLEAR(cached_copyreg_module); +} + +void PyType_Modified(PyTypeObject *type) { /* Invalidate any cached data for the specified type and all @@ -3339,19 +3352,18 @@ import_copyreg(void) { static PyObject *copyreg_str; - static PyObject *mod_copyreg = NULL; if (!copyreg_str) { copyreg_str = PyUnicode_InternFromString("copyreg"); if (copyreg_str == NULL) return NULL; } - if (!mod_copyreg) { - mod_copyreg = PyImport_Import(copyreg_str); - } - - Py_XINCREF(mod_copyreg); - return mod_copyreg; + if (!cached_copyreg_module) { + cached_copyreg_module = PyImport_Import(copyreg_str); + } + + Py_XINCREF(cached_copyreg_module); + return cached_copyreg_module; } static PyObject * diff -r 193f25352e1c -r 0b34fd75b937 Python/pythonrun.c --- a/Python/pythonrun.c Sat May 04 20:18:53 2013 +0200 +++ b/Python/pythonrun.c Sat May 04 20:46:19 2013 +0200 @@ -524,9 +524,6 @@ /* Disable signal handling */ PyOS_FiniInterrupts(); - /* Clear type lookup cache */ - PyType_ClearCache(); - /* Collect garbage. This may call finalizers; it's nice to call these * before all modules are destroyed. * XXX If a __del__ or weakref callback is triggered here, and tries to @@ -632,6 +629,7 @@ PyFloat_Fini(); PyDict_Fini(); PySlice_Fini(); + _PyType_Fini(); /* Cleanup Unicode implementation */ _PyUnicode_Fini();