Skip to content

Commit

Permalink
pythonGH-94857: fix test_io refleak (pythonGH-94858)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaraditya303 authored Jul 18, 2022
1 parent ae0be5a commit 631160c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix refleak in ``_io.TextIOWrapper.reconfigure``. Patch by Kumar Aditya.
7 changes: 6 additions & 1 deletion Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1247,13 +1247,16 @@ textiowrapper_change_encoding(textio *self, PyObject *encoding,
if (errors == Py_None) {
errors = self->errors;
}
Py_INCREF(encoding);
}
else {
if (_PyUnicode_EqualToASCIIString(encoding, "locale")) {
encoding = _Py_GetLocaleEncodingObject();
if (encoding == NULL) {
return -1;
}
} else {
Py_INCREF(encoding);
}
if (errors == Py_None) {
errors = &_Py_ID(strict);
Expand All @@ -1262,23 +1265,25 @@ textiowrapper_change_encoding(textio *self, PyObject *encoding,

const char *c_errors = PyUnicode_AsUTF8(errors);
if (c_errors == NULL) {
Py_DECREF(encoding);
return -1;
}

// Create new encoder & decoder
PyObject *codec_info = _PyCodec_LookupTextEncoding(
PyUnicode_AsUTF8(encoding), "codecs.open()");
if (codec_info == NULL) {
Py_DECREF(encoding);
return -1;
}
if (_textiowrapper_set_decoder(self, codec_info, c_errors) != 0 ||
_textiowrapper_set_encoder(self, codec_info, c_errors) != 0) {
Py_DECREF(codec_info);
Py_DECREF(encoding);
return -1;
}
Py_DECREF(codec_info);

Py_INCREF(encoding);
Py_INCREF(errors);
Py_SETREF(self->encoding, encoding);
Py_SETREF(self->errors, errors);
Expand Down

0 comments on commit 631160c

Please sign in to comment.