]> granicus.if.org Git - python/commitdiff
Issue #18608: Avoid keeping a strong reference to the locale module inside the _io...
authorAntoine Pitrou <solipsis@pitrou.net>
Thu, 1 Aug 2013 19:04:50 +0000 (21:04 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Thu, 1 Aug 2013 19:04:50 +0000 (21:04 +0200)
Misc/NEWS
Modules/_io/_iomodule.c
Modules/_io/_iomodule.h
Modules/_io/textio.c

index 0704c07deb1b28917cd5e990ef3488fff04f63fb..e6bbd30fa9d0d94cf834985947cb732b7f27bfd1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -179,6 +179,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #18608: Avoid keeping a strong reference to the locale module
+  inside the _io module.
+
 - Issue #18619: Fix atexit leaking callbacks registered from sub-interpreters,
   and make it GC-aware.
 
index 4a7e758cda5e657fbb1f525a4a9612f59c181cca..14457e80cbe39c2349b2bc5f9882f58ef164cb65 100644 (file)
@@ -533,6 +533,31 @@ _PyIO_ConvertSsize_t(PyObject *obj, void *result) {
 }
 
 
+PyObject *
+_PyIO_get_locale_module(_PyIO_State *state)
+{
+    PyObject *mod;
+    if (state->locale_module != NULL) {
+        assert(PyWeakref_CheckRef(state->locale_module));
+        mod = PyWeakref_GET_OBJECT(state->locale_module);
+        if (mod != Py_None) {
+            Py_INCREF(mod);
+            return mod;
+        }
+        Py_CLEAR(state->locale_module);
+    }
+    mod = PyImport_ImportModule("locale");
+    if (mod == NULL)
+        return NULL;
+    state->locale_module = PyWeakref_NewRef(mod, NULL);
+    if (state->locale_module == NULL) {
+        Py_DECREF(mod);
+        return NULL;
+    }
+    return mod;
+}
+
+
 static int
 iomodule_traverse(PyObject *mod, visitproc visit, void *arg) {
     _PyIO_State *state = IO_MOD_STATE(mod);
index ad1faa8c46e9d4e3a74f9daa8345190512e77f82..b90a658397dee2ec769a126e30c0e36fc76a4a52 100644 (file)
@@ -137,6 +137,8 @@ typedef struct {
 #define IO_MOD_STATE(mod) ((_PyIO_State *)PyModule_GetState(mod))
 #define IO_STATE IO_MOD_STATE(PyState_FindModule(&_PyIO_Module))
 
+extern PyObject *_PyIO_get_locale_module(_PyIO_State *);
+
 extern PyObject *_PyIO_str_close;
 extern PyObject *_PyIO_str_closed;
 extern PyObject *_PyIO_str_decode;
index 5d2438d812563e138c63472f333184afdad622cc..7b8de90020e9d65c700c9dc68dd5f466794cce86 100644 (file)
@@ -917,35 +917,29 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
         }
     }
     if (encoding == NULL && self->encoding == NULL) {
-        if (state->locale_module == NULL) {
-            state->locale_module = PyImport_ImportModule("locale");
-            if (state->locale_module == NULL)
-                goto catch_ImportError;
-            else
-                goto use_locale;
-        }
-        else {
-          use_locale:
-            self->encoding = _PyObject_CallMethodId(
-                state->locale_module, &PyId_getpreferredencoding, "O", Py_False);
-            if (self->encoding == NULL) {
-              catch_ImportError:
-                /*
-                 Importing locale can raise a ImportError because of
-                 _functools, and locale.getpreferredencoding can raise a
-                 ImportError if _locale is not available.  These will happen
-                 during module building.
-                */
-                if (PyErr_ExceptionMatches(PyExc_ImportError)) {
-                    PyErr_Clear();
-                    self->encoding = PyUnicode_FromString("ascii");
-                }
-                else
-                    goto error;
+        PyObject *locale_module = _PyIO_get_locale_module(state);
+        if (locale_module == NULL)
+            goto catch_ImportError;
+        self->encoding = _PyObject_CallMethodId(
+            locale_module, &PyId_getpreferredencoding, "O", Py_False);
+        Py_DECREF(locale_module);
+        if (self->encoding == NULL) {
+          catch_ImportError:
+            /*
+             Importing locale can raise a ImportError because of
+             _functools, and locale.getpreferredencoding can raise a
+             ImportError if _locale is not available.  These will happen
+             during module building.
+            */
+            if (PyErr_ExceptionMatches(PyExc_ImportError)) {
+                PyErr_Clear();
+                self->encoding = PyUnicode_FromString("ascii");
             }
-            else if (!PyUnicode_Check(self->encoding))
-                Py_CLEAR(self->encoding);
+            else
+                goto error;
         }
+        else if (!PyUnicode_Check(self->encoding))
+            Py_CLEAR(self->encoding);
     }
     if (self->encoding != NULL) {
         encoding = _PyUnicode_AsString(self->encoding);