[Python-checkins] cpython: Issue #18608: Avoid keeping a strong reference to the locale module inside the

antoine.pitrou python-checkins at python.org
Thu Aug 1 21:05:18 CEST 2013


http://hg.python.org/cpython/rev/1c9aa4f68f2b
changeset:   84954:1c9aa4f68f2b
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Thu Aug 01 21:04:50 2013 +0200
summary:
  Issue #18608: Avoid keeping a strong reference to the locale module inside the _io module.

files:
  Misc/NEWS               |   3 +
  Modules/_io/_iomodule.c |  25 +++++++++++++++
  Modules/_io/_iomodule.h |   2 +
  Modules/_io/textio.c    |  48 ++++++++++++----------------
  4 files changed, 51 insertions(+), 27 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -179,6 +179,9 @@
 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.
 
diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c
--- a/Modules/_io/_iomodule.c
+++ b/Modules/_io/_iomodule.c
@@ -533,6 +533,31 @@
 }
 
 
+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);
diff --git a/Modules/_io/_iomodule.h b/Modules/_io/_iomodule.h
--- a/Modules/_io/_iomodule.h
+++ b/Modules/_io/_iomodule.h
@@ -137,6 +137,8 @@
 #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;
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -917,35 +917,29 @@
         }
     }
     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;
+        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
-                goto use_locale;
+                goto error;
         }
-        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;
-            }
-            else if (!PyUnicode_Check(self->encoding))
-                Py_CLEAR(self->encoding);
-        }
+        else if (!PyUnicode_Check(self->encoding))
+            Py_CLEAR(self->encoding);
     }
     if (self->encoding != NULL) {
         encoding = _PyUnicode_AsString(self->encoding);

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list