Skip to content

Commit bec8c78

Browse files
authored
bpo-43510: Fix emitting EncodingWarning from _io module. (GH-25146)
I forget to check PyErr_WarnEx() return value. But it will fail when -Werror is used.
1 parent 8bbfeb3 commit bec8c78

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

Modules/_io/_iomodule.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,10 @@ _io_text_encoding_impl(PyObject *module, PyObject *encoding, int stacklevel)
532532
if (encoding == NULL || encoding == Py_None) {
533533
PyInterpreterState *interp = _PyInterpreterState_GET();
534534
if (_PyInterpreterState_GetConfig(interp)->warn_default_encoding) {
535-
PyErr_WarnEx(PyExc_EncodingWarning,
536-
"'encoding' argument not specified", stacklevel);
535+
if (PyErr_WarnEx(PyExc_EncodingWarning,
536+
"'encoding' argument not specified", stacklevel)) {
537+
return NULL;
538+
}
537539
}
538540
Py_INCREF(_PyIO_str_locale);
539541
return _PyIO_str_locale;

Modules/_io/textio.c

+13-11
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,19 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
10851085
self->ok = 0;
10861086
self->detached = 0;
10871087

1088+
if (encoding == NULL) {
1089+
PyInterpreterState *interp = _PyInterpreterState_GET();
1090+
if (_PyInterpreterState_GetConfig(interp)->warn_default_encoding) {
1091+
if (PyErr_WarnEx(PyExc_EncodingWarning,
1092+
"'encoding' argument not specified", 1)) {
1093+
return -1;
1094+
}
1095+
}
1096+
}
1097+
else if (strcmp(encoding, "locale") == 0) {
1098+
encoding = NULL;
1099+
}
1100+
10881101
if (errors == Py_None) {
10891102
errors = _PyUnicode_FromId(&PyId_strict); /* borrowed */
10901103
if (errors == NULL) {
@@ -1123,17 +1136,6 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
11231136
self->encodefunc = NULL;
11241137
self->b2cratio = 0.0;
11251138

1126-
if (encoding == NULL) {
1127-
PyInterpreterState *interp = _PyInterpreterState_GET();
1128-
if (_PyInterpreterState_GetConfig(interp)->warn_default_encoding) {
1129-
PyErr_WarnEx(PyExc_EncodingWarning,
1130-
"'encoding' argument not specified", 1);
1131-
}
1132-
}
1133-
else if (strcmp(encoding, "locale") == 0) {
1134-
encoding = NULL;
1135-
}
1136-
11371139
if (encoding == NULL) {
11381140
/* Try os.device_encoding(fileno) */
11391141
PyObject *fileno;

0 commit comments

Comments
 (0)