Skip to content

Commit fd2ebb1

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 89f05a7 commit fd2ebb1

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
@@ -1786,7 +1786,7 @@ static void
17861786
_bufferedwriter_set_append(buffered *self)
17871787
{
17881788
PyObject *mode = _PyObject_GetAttrId(self->raw, &PyId_mode);
1789-
if (mode != NULL) {
1789+
if (mode != NULL && PyUnicode_Check(mode)) {
17901790
if (PyUnicode_FindChar(mode, 'a', 0,
17911791
PyUnicode_GET_LENGTH(mode), 1) != -1) {
17921792
self->appending = 1;
@@ -1797,6 +1797,9 @@ _bufferedwriter_set_append(buffered *self)
17971797
Py_DECREF(mode);
17981798
}
17991799
else {
1800+
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1801+
PyErr_Clear();
1802+
}
18001803
/* Raw fileobj has no mode string so as far as we can know it has
18011804
normal write behavior */
18021805
self->appending = 0;

0 commit comments

Comments
 (0)