]> granicus.if.org Git - python/commitdiff
Issue #17408: Avoid using an obsolete instance of the copyreg module when the interpr...
authorAntoine Pitrou <solipsis@pitrou.net>
Sat, 4 May 2013 18:45:02 +0000 (20:45 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Sat, 4 May 2013 18:45:02 +0000 (20:45 +0200)
Include/pythonrun.h
Misc/NEWS
Objects/typeobject.c
Python/pythonrun.c

index 4d24b2df7dce38144ccbf9064d7633c7e9a7dd11..e8a582d50a5c4ce1f82ade3b6089044df0dcaacf 100644 (file)
@@ -219,6 +219,7 @@ PyAPI_FUNC(void) PyFloat_Fini(void);
 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
index 1bb73554c6ab8a4d674976ba421f82ea1b3f9e0d..6a3f17c9b559ab550ce41a9f69153139577c3b9c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.3.2?
 Core and Builtins
 -----------------
 
+- Issue #17408: Avoid using an obsolete instance of the copyreg module when
+  the interpreter is shutdown and then started again.
+
 - Issue #17863: In the interactive console, don't loop forever if the encoding
   can't be fetched from stdin.
 
index 6ece741833a03459e543c18d0453ed45eeb2ba5e..f40dd10a34041a581a7ed36235d35cae26225c1e 100644 (file)
@@ -7,6 +7,10 @@
 #include <ctype.h>
 
 
+/* 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
@@ -68,6 +72,15 @@ PyType_ClearCache(void)
     return cur_version_tag;
 }
 
+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)
 {
@@ -3339,19 +3352,18 @@ static PyObject *
 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);
+    if (!cached_copyreg_module) {
+        cached_copyreg_module = PyImport_Import(copyreg_str);
     }
 
-    Py_XINCREF(mod_copyreg);
-    return mod_copyreg;
+    Py_XINCREF(cached_copyreg_module);
+    return cached_copyreg_module;
 }
 
 static PyObject *
index ee6071e631e54a0d1edf7433bfbb34fb6943b76b..ddda4a4cd08c076f7c4b3078da93acf1256b7b38 100644 (file)
@@ -506,9 +506,6 @@ Py_Finalize(void)
     /* 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
@@ -614,6 +611,7 @@ Py_Finalize(void)
     PyFloat_Fini();
     PyDict_Fini();
     PySlice_Fini();
+    _PyType_Fini();
 
     /* Cleanup Unicode implementation */
     _PyUnicode_Fini();