]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43510: Fix emitting EncodingWarning from _io module. (GH-25146)
authorInada Naoki <songofacandy@gmail.com>
Fri, 2 Apr 2021 08:38:59 +0000 (17:38 +0900)
committerGitHub <noreply@github.com>
Fri, 2 Apr 2021 08:38:59 +0000 (17:38 +0900)
I forget to check PyErr_WarnEx() return value. But it will fail when -Werror is used.

Modules/_io/_iomodule.c
Modules/_io/textio.c

index 652c2ce5b0d61f329d07c9fe7fe3b6c9e55c1088..170dea41e8abdd2093aed569b622ef02b7408fe5 100644 (file)
@@ -532,8 +532,10 @@ _io_text_encoding_impl(PyObject *module, PyObject *encoding, int stacklevel)
     if (encoding == NULL || encoding == Py_None) {
         PyInterpreterState *interp = _PyInterpreterState_GET();
         if (_PyInterpreterState_GetConfig(interp)->warn_default_encoding) {
-            PyErr_WarnEx(PyExc_EncodingWarning,
-                         "'encoding' argument not specified", stacklevel);
+            if (PyErr_WarnEx(PyExc_EncodingWarning,
+                             "'encoding' argument not specified", stacklevel)) {
+                return NULL;
+            }
         }
         Py_INCREF(_PyIO_str_locale);
         return _PyIO_str_locale;
index 6f89a879c9c2bf233cf85d00b2331d894c43b1af..eb05ae1a16eb03adbdb01c0e1bcd769621d10bc3 100644 (file)
@@ -1085,6 +1085,19 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
     self->ok = 0;
     self->detached = 0;
 
+    if (encoding == NULL) {
+        PyInterpreterState *interp = _PyInterpreterState_GET();
+        if (_PyInterpreterState_GetConfig(interp)->warn_default_encoding) {
+            if (PyErr_WarnEx(PyExc_EncodingWarning,
+                             "'encoding' argument not specified", 1)) {
+                return -1;
+            }
+        }
+    }
+    else if (strcmp(encoding, "locale") == 0) {
+        encoding = NULL;
+    }
+
     if (errors == Py_None) {
         errors = _PyUnicode_FromId(&PyId_strict); /* borrowed */
         if (errors == NULL) {
@@ -1123,17 +1136,6 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
     self->encodefunc = NULL;
     self->b2cratio = 0.0;
 
-    if (encoding == NULL) {
-        PyInterpreterState *interp = _PyInterpreterState_GET();
-        if (_PyInterpreterState_GetConfig(interp)->warn_default_encoding) {
-            PyErr_WarnEx(PyExc_EncodingWarning,
-                         "'encoding' argument not specified", 1);
-        }
-    }
-    else if (strcmp(encoding, "locale") == 0) {
-        encoding = NULL;
-    }
-
     if (encoding == NULL) {
         /* Try os.device_encoding(fileno) */
         PyObject *fileno;