Skip to content

Commit 4173474

Browse files
committed
Fix comments, better handle if write returuns a big/small int
1 parent 947ff62 commit 4173474

1 file changed

Lines changed: 11 additions & 16 deletions

File tree

Modules/_io/textio.c

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,8 +1738,8 @@ _textiowrapper_writeflush(textio *self)
17381738

17391739
/* Try to determine bytes written from return value
17401740
1741-
XXX: On unexpected return this matches previous behavior and asumes
1742-
all data was written. */
1741+
XXX: Unexpected return: match previous behavior assume all data was
1742+
written. */
17431743
/* OPEN QUESTION: None is common in CPython, should this warn? */
17441744
if (ret == Py_None) {
17451745
Py_DECREF(b);
@@ -1762,34 +1762,29 @@ _textiowrapper_writeflush(textio *self)
17621762
/* Check for unexpected return values. */
17631763
/* Can't get out size follow return previous behavior. */
17641764
if (size == -1 && PyErr_Occurred()) {
1765-
if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
1766-
Py_DECREF(b);
1767-
Py_DECREF(ret);
1768-
return -1;
1769-
}
1770-
PyErr_Clear(); /* fall through */
1765+
/* Warn about the value, but do not error. */
1766+
PyObject *exc = PyErr_GetRaisedException()
17711767
PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1772-
"buffer.write returned value '%s' not specified by"
1773-
" BufferedIOBase or TextIOBase", ret);
1768+
"write returned value '%s' not specified by"
1769+
" BufferedIOBase or TextIOBase (%s)", ret, exc);
1770+
Py_DECREF(exc);
17741771
Py_DECREF(b);
17751772
Py_DECREF(ret);
17761773
return 0;
17771774
}
17781775
/* Negative count of bytes doesn't make sense. */
17791776
else if (size < 0) {
17801777
PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
1781-
"buffer.write returned negative count of bytes '%s'",
1782-
ret);
1778+
"write returned negative count of bytes '%s'", ret);
17831779
Py_DECREF(b);
17841780
Py_DECREF(ret);
17851781
return 0;
17861782
}
17871783
/* More written than passed in call. */
17881784
else if (size > bytes_to_write) {
17891785
PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
1790-
"buffer.write returned '%s' bytes written but was only"
1791-
" called with '%s' bytes to write",
1792-
ret, bytes_to_write);
1786+
"write returned '%s' bytes written but was only called"
1787+
" with '%s' bytes to write", ret, bytes_to_write);
17931788
Py_DECREF(b);
17941789
Py_DECREF(ret);
17951790
return 0;
@@ -1805,7 +1800,7 @@ _textiowrapper_writeflush(textio *self)
18051800
break;
18061801
}
18071802

1808-
/* Make a new PyByte to keep type for next call to write. */
1803+
/* Make a new PyBytes to keep type for next call to write. */
18091804
pending = PyBytes_FromStringAndSize(
18101805
PyBytes_AS_STRING(b) + size,
18111806
self->pending_bytes_count);

0 commit comments

Comments
 (0)