Skip to content

Commit 176e4ae

Browse files
committed
fix possible unhandled AttributeError (e.g. when the raw file does not have a mode attribute) and ensure that the mode is in fact a string
1 parent e8a2d25 commit 176e4ae

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Modules/_io/bufferedio.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1945,7 +1945,7 @@ static void
19451945
_bufferedwriter_set_append(buffered *self)
19461946
{
19471947
PyObject *mode = _PyObject_GetAttrId(self->raw, &PyId_mode);
1948-
if (mode != NULL) {
1948+
if (mode != NULL && PyUnicode_Check(mode)) {
19491949
if (PyUnicode_FindChar(mode, 'a', 0,
19501950
PyUnicode_GET_LENGTH(mode), 1) != -1) {
19511951
self->appending = 1;
@@ -1956,6 +1956,9 @@ _bufferedwriter_set_append(buffered *self)
19561956
Py_DECREF(mode);
19571957
}
19581958
else {
1959+
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1960+
PyErr_Clear();
1961+
}
19591962
/* Raw fileobj has no mode string so as far as we can know it has
19601963
normal write behavior */
19611964
self->appending = 0;

0 commit comments

Comments
 (0)