diff options
| author | Antoine Pitrou <solipsis@pitrou.net> | 2013-08-01 21:04:50 +0200 |
|---|---|---|
| committer | Antoine Pitrou <solipsis@pitrou.net> | 2013-08-01 21:04:50 +0200 |
| commit | 932ff8368289b53c4ca37167747bb71d146fc6ea (patch) | |
| tree | 31f508a77d07676df88535e20bfe555eb662f0a2 /Modules/_io/textio.c | |
| parent | 2d350fd8af29eada0c3f264a91df6ab4af4a05fd (diff) | |
| download | cpython-git-932ff8368289b53c4ca37167747bb71d146fc6ea.tar.gz | |
Issue #18608: Avoid keeping a strong reference to the locale module inside the _io module.
Diffstat (limited to 'Modules/_io/textio.c')
| -rw-r--r-- | Modules/_io/textio.c | 48 |
1 files changed, 21 insertions, 27 deletions
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index 5d2438d812..7b8de90020 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -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); |
