@@ -917,35 +917,29 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
917917 }
918918 }
919919 if (encoding == NULL && self -> encoding == NULL ) {
920- if (state -> locale_module == NULL ) {
921- state -> locale_module = PyImport_ImportModule ("locale" );
922- if (state -> locale_module == NULL )
923- goto catch_ImportError ;
924- else
925- goto use_locale ;
926- }
927- else {
928- use_locale :
929- self -> encoding = _PyObject_CallMethodId (
930- state -> locale_module , & PyId_getpreferredencoding , "O" , Py_False );
931- if (self -> encoding == NULL ) {
932- catch_ImportError :
933- /*
934- Importing locale can raise a ImportError because of
935- _functools, and locale.getpreferredencoding can raise a
936- ImportError if _locale is not available. These will happen
937- during module building.
938- */
939- if (PyErr_ExceptionMatches (PyExc_ImportError )) {
940- PyErr_Clear ();
941- self -> encoding = PyUnicode_FromString ("ascii" );
942- }
943- else
944- goto error ;
920+ PyObject * locale_module = _PyIO_get_locale_module (state );
921+ if (locale_module == NULL )
922+ goto catch_ImportError ;
923+ self -> encoding = _PyObject_CallMethodId (
924+ locale_module , & PyId_getpreferredencoding , "O" , Py_False );
925+ Py_DECREF (locale_module );
926+ if (self -> encoding == NULL ) {
927+ catch_ImportError :
928+ /*
929+ Importing locale can raise a ImportError because of
930+ _functools, and locale.getpreferredencoding can raise a
931+ ImportError if _locale is not available. These will happen
932+ during module building.
933+ */
934+ if (PyErr_ExceptionMatches (PyExc_ImportError )) {
935+ PyErr_Clear ();
936+ self -> encoding = PyUnicode_FromString ("ascii" );
945937 }
946- else if (! PyUnicode_Check ( self -> encoding ))
947- Py_CLEAR ( self -> encoding ) ;
938+ else
939+ goto error ;
948940 }
941+ else if (!PyUnicode_Check (self -> encoding ))
942+ Py_CLEAR (self -> encoding );
949943 }
950944 if (self -> encoding != NULL ) {
951945 encoding = _PyUnicode_AsString (self -> encoding );
0 commit comments