@@ -1752,95 +1752,14 @@ textiowrapper_check_write(textio *self)
17521752 return Py_None ;
17531753}
17541754
1755- /*[clinic input]
1756- @critical_section
1757- _io.TextIOWrapper.write
1758- text: unicode
1759- /
1760- [clinic start generated code]*/
1761-
1762- static PyObject *
1763- _io_TextIOWrapper_write_impl (textio * self , PyObject * text )
1764- /*[clinic end generated code: output=d2deb0d50771fcec input=73ec95c5c4a3489c]*/
1755+ /* Consume b, which is either bytes or an ASCII string. */
1756+ static int
1757+ textiowrapper_write_bytes (textio * self , PyObject * b ,
1758+ int needflush , int text_needflush )
17651759{
1766- PyObject * ret ;
1767- PyObject * b ;
1768- Py_ssize_t textlen ;
1769- int haslf = 0 ;
1770- int needflush = 0 , text_needflush = 0 ;
1771-
1772- if (textiowrapper_check_write (self ) == NULL ) {
1773- return NULL ;
1774- }
1775-
1776- Py_INCREF (text );
1777-
1778- textlen = PyUnicode_GET_LENGTH (text );
1779-
1780- if ((self -> writetranslate && self -> writenl != NULL ) || self -> line_buffering )
1781- if (PyUnicode_FindChar (text , '\n' , 0 , PyUnicode_GET_LENGTH (text ), 1 ) != -1 )
1782- haslf = 1 ;
1783-
1784- if (haslf && self -> writetranslate && self -> writenl != NULL ) {
1785- PyObject * newtext = _PyObject_CallMethod (text , & _Py_ID (replace ),
1786- "ss" , "\n" , self -> writenl );
1787- Py_DECREF (text );
1788- if (newtext == NULL )
1789- return NULL ;
1790- if (!PyUnicode_Check (newtext )) {
1791- PyErr_Format (PyExc_TypeError ,
1792- "replace() should return a str, not %T" , newtext );
1793- Py_DECREF (newtext );
1794- return NULL ;
1795- }
1796- /* replace() may have closed, detached or reinitialized the stream. */
1797- if (textiowrapper_check_write (self ) == NULL ) {
1798- Py_DECREF (newtext );
1799- return NULL ;
1800- }
1801- text = newtext ;
1802- }
1803-
1804- if (self -> write_through )
1805- text_needflush = 1 ;
1806- if (self -> line_buffering &&
1807- (haslf ||
1808- PyUnicode_FindChar (text , '\r' , 0 , PyUnicode_GET_LENGTH (text ), 1 ) != -1 ))
1809- needflush = 1 ;
1810-
1811- /* XXX What if we were just reading? */
1812- if (self -> encodefunc != NULL ) {
1813- if (PyUnicode_IS_ASCII (text ) &&
1814- // See bpo-43260
1815- PyUnicode_GET_LENGTH (text ) <= self -> chunk_size &&
1816- is_asciicompat_encoding (self -> encodefunc )) {
1817- b = Py_NewRef (text );
1818- }
1819- else {
1820- b = (* self -> encodefunc )((PyObject * ) self , text );
1821- }
1822- self -> encoding_start_of_stream = 0 ;
1823- }
1824- else {
1825- /* A codec callback may reconfigure the stream. */
1826- PyObject * encoder = Py_NewRef (self -> encoder );
1827- b = PyObject_CallMethodOneArg (encoder , & _Py_ID (encode ), text );
1828- Py_DECREF (encoder );
1829- }
1830-
1831- Py_DECREF (text );
1832- if (b == NULL )
1833- return NULL ;
1834- if (b != text && !PyBytes_Check (b )) {
1835- PyErr_Format (PyExc_TypeError ,
1836- "encoder should return a bytes object, not '%.200s'" ,
1837- Py_TYPE (b )-> tp_name );
1838- Py_DECREF (b );
1839- return NULL ;
1840- }
1841-
18421760 Py_ssize_t bytes_len ;
1843- if (b == text ) {
1761+ if (PyUnicode_Check (b )) {
1762+ assert (PyUnicode_IS_ASCII (b ));
18441763 bytes_len = PyUnicode_GET_LENGTH (b );
18451764 }
18461765 else {
@@ -1860,7 +1779,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
18601779 while (self -> pending_bytes != NULL ) {
18611780 if (_textiowrapper_writeflush (self ) < 0 ) {
18621781 Py_DECREF (b );
1863- return NULL ;
1782+ return -1 ;
18641783 }
18651784 }
18661785 }
@@ -1874,7 +1793,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
18741793 PyObject * list = PyList_New (2 );
18751794 if (list == NULL ) {
18761795 Py_DECREF (b );
1877- return NULL ;
1796+ return -1 ;
18781797 }
18791798 // Since Python 3.12, allocating GC object won't trigger GC and release
18801799 // GIL. See https://github.com/python/cpython/issues/97922
@@ -1886,7 +1805,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
18861805 else {
18871806 if (PyList_Append (self -> pending_bytes , b ) < 0 ) {
18881807 Py_DECREF (b );
1889- return NULL ;
1808+ return -1 ;
18901809 }
18911810 Py_DECREF (b );
18921811 }
@@ -1900,18 +1819,18 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
19001819 if (self -> pending_bytes_count >= self -> chunk_size || needflush ||
19011820 text_needflush ) {
19021821 if (_textiowrapper_writeflush (self ) < 0 )
1903- return NULL ;
1822+ return -1 ;
19041823 }
19051824
19061825 if (needflush ) {
19071826 PyObject * buffer = buffer_access_safe (self );
19081827 if (buffer == NULL ) {
1909- return NULL ;
1828+ return -1 ;
19101829 }
19111830 int res = _PyFile_Flush (buffer );
19121831 Py_DECREF (buffer );
19131832 if (res < 0 ) {
1914- return NULL ;
1833+ return -1 ;
19151834 }
19161835 }
19171836
@@ -1921,15 +1840,112 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
19211840 }
19221841
19231842 if (self -> decoder ) {
1924- ret = PyObject_CallMethodNoArgs (self -> decoder , & _Py_ID (reset ));
1843+ PyObject * ret = PyObject_CallMethodNoArgs (self -> decoder , & _Py_ID (reset ));
19251844 if (ret == NULL )
1926- return NULL ;
1845+ return -1 ;
19271846 Py_DECREF (ret );
19281847 }
19291848
1849+ return 0 ;
1850+ }
1851+
1852+ static PyObject *
1853+ textiowrapper_write_text (textio * self , PyObject * text )
1854+ {
1855+ PyObject * b ;
1856+ Py_ssize_t textlen ;
1857+ int haslf = 0 ;
1858+ int needflush = 0 , text_needflush = 0 ;
1859+
1860+ Py_INCREF (text );
1861+
1862+ textlen = PyUnicode_GET_LENGTH (text );
1863+
1864+ if ((self -> writetranslate && self -> writenl != NULL ) || self -> line_buffering )
1865+ if (PyUnicode_FindChar (text , '\n' , 0 , PyUnicode_GET_LENGTH (text ), 1 ) != -1 )
1866+ haslf = 1 ;
1867+
1868+ if (haslf && self -> writetranslate && self -> writenl != NULL ) {
1869+ PyObject * newtext = _PyObject_CallMethod (text , & _Py_ID (replace ),
1870+ "ss" , "\n" , self -> writenl );
1871+ Py_DECREF (text );
1872+ if (newtext == NULL )
1873+ return NULL ;
1874+ if (!PyUnicode_Check (newtext )) {
1875+ PyErr_Format (PyExc_TypeError ,
1876+ "replace() should return a str, not %T" , newtext );
1877+ Py_DECREF (newtext );
1878+ return NULL ;
1879+ }
1880+ /* replace() may have closed, detached or reinitialized the stream. */
1881+ if (textiowrapper_check_write (self ) == NULL ) {
1882+ Py_DECREF (newtext );
1883+ return NULL ;
1884+ }
1885+ text = newtext ;
1886+ }
1887+
1888+ if (self -> write_through )
1889+ text_needflush = 1 ;
1890+ if (self -> line_buffering &&
1891+ (haslf ||
1892+ PyUnicode_FindChar (text , '\r' , 0 , PyUnicode_GET_LENGTH (text ), 1 ) != -1 ))
1893+ needflush = 1 ;
1894+
1895+ /* XXX What if we were just reading? */
1896+ if (self -> encodefunc != NULL ) {
1897+ if (PyUnicode_IS_ASCII (text ) &&
1898+ // See bpo-43260
1899+ PyUnicode_GET_LENGTH (text ) <= self -> chunk_size &&
1900+ is_asciicompat_encoding (self -> encodefunc )) {
1901+ b = Py_NewRef (text );
1902+ }
1903+ else {
1904+ b = (* self -> encodefunc )((PyObject * ) self , text );
1905+ }
1906+ self -> encoding_start_of_stream = 0 ;
1907+ }
1908+ else {
1909+ /* A codec callback may reconfigure the stream. */
1910+ PyObject * encoder = Py_NewRef (self -> encoder );
1911+ b = PyObject_CallMethodOneArg (encoder , & _Py_ID (encode ), text );
1912+ Py_DECREF (encoder );
1913+ }
1914+
1915+ Py_DECREF (text );
1916+ if (b == NULL )
1917+ return NULL ;
1918+ if (b != text && !PyBytes_Check (b )) {
1919+ PyErr_Format (PyExc_TypeError ,
1920+ "encoder should return a bytes object, not '%.200s'" ,
1921+ Py_TYPE (b )-> tp_name );
1922+ Py_DECREF (b );
1923+ return NULL ;
1924+ }
1925+
1926+ if (textiowrapper_write_bytes (self , b , needflush , text_needflush ) < 0 ) {
1927+ return NULL ;
1928+ }
19301929 return PyLong_FromSsize_t (textlen );
19311930}
19321931
1932+ /*[clinic input]
1933+ @critical_section
1934+ _io.TextIOWrapper.write
1935+ text: unicode
1936+ /
1937+ [clinic start generated code]*/
1938+
1939+ static PyObject *
1940+ _io_TextIOWrapper_write_impl (textio * self , PyObject * text )
1941+ /*[clinic end generated code: output=d2deb0d50771fcec input=73ec95c5c4a3489c]*/
1942+ {
1943+ if (textiowrapper_check_write (self ) == NULL ) {
1944+ return NULL ;
1945+ }
1946+ return textiowrapper_write_text (self , text );
1947+ }
1948+
19331949/* Steal a reference to chars and store it in the decoded_char buffer;
19341950 */
19351951static void
0 commit comments