@@ -1752,95 +1752,13 @@ 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 ) ) {
18441762 bytes_len = PyUnicode_GET_LENGTH (b );
18451763 }
18461764 else {
@@ -1860,7 +1778,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
18601778 while (self -> pending_bytes != NULL ) {
18611779 if (_textiowrapper_writeflush (self ) < 0 ) {
18621780 Py_DECREF (b );
1863- return NULL ;
1781+ return -1 ;
18641782 }
18651783 }
18661784 }
@@ -1874,7 +1792,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
18741792 PyObject * list = PyList_New (2 );
18751793 if (list == NULL ) {
18761794 Py_DECREF (b );
1877- return NULL ;
1795+ return -1 ;
18781796 }
18791797 // Since Python 3.12, allocating GC object won't trigger GC and release
18801798 // GIL. See https://github.com/python/cpython/issues/97922
@@ -1886,7 +1804,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
18861804 else {
18871805 if (PyList_Append (self -> pending_bytes , b ) < 0 ) {
18881806 Py_DECREF (b );
1889- return NULL ;
1807+ return -1 ;
18901808 }
18911809 Py_DECREF (b );
18921810 }
@@ -1900,18 +1818,18 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
19001818 if (self -> pending_bytes_count >= self -> chunk_size || needflush ||
19011819 text_needflush ) {
19021820 if (_textiowrapper_writeflush (self ) < 0 )
1903- return NULL ;
1821+ return -1 ;
19041822 }
19051823
19061824 if (needflush ) {
19071825 PyObject * buffer = buffer_access_safe (self );
19081826 if (buffer == NULL ) {
1909- return NULL ;
1827+ return -1 ;
19101828 }
19111829 int res = _PyFile_Flush (buffer );
19121830 Py_DECREF (buffer );
19131831 if (res < 0 ) {
1914- return NULL ;
1832+ return -1 ;
19151833 }
19161834 }
19171835
@@ -1921,15 +1839,112 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
19211839 }
19221840
19231841 if (self -> decoder ) {
1924- ret = PyObject_CallMethodNoArgs (self -> decoder , & _Py_ID (reset ));
1842+ PyObject * ret = PyObject_CallMethodNoArgs (self -> decoder , & _Py_ID (reset ));
19251843 if (ret == NULL )
1926- return NULL ;
1844+ return -1 ;
19271845 Py_DECREF (ret );
19281846 }
19291847
1848+ return 0 ;
1849+ }
1850+
1851+ static PyObject *
1852+ textiowrapper_write_text (textio * self , PyObject * text )
1853+ {
1854+ PyObject * b ;
1855+ Py_ssize_t textlen ;
1856+ int haslf = 0 ;
1857+ int needflush = 0 , text_needflush = 0 ;
1858+
1859+ Py_INCREF (text );
1860+
1861+ textlen = PyUnicode_GET_LENGTH (text );
1862+
1863+ if ((self -> writetranslate && self -> writenl != NULL ) || self -> line_buffering )
1864+ if (PyUnicode_FindChar (text , '\n' , 0 , PyUnicode_GET_LENGTH (text ), 1 ) != -1 )
1865+ haslf = 1 ;
1866+
1867+ if (haslf && self -> writetranslate && self -> writenl != NULL ) {
1868+ PyObject * newtext = _PyObject_CallMethod (text , & _Py_ID (replace ),
1869+ "ss" , "\n" , self -> writenl );
1870+ Py_DECREF (text );
1871+ if (newtext == NULL )
1872+ return NULL ;
1873+ if (!PyUnicode_Check (newtext )) {
1874+ PyErr_Format (PyExc_TypeError ,
1875+ "replace() should return a str, not %T" , newtext );
1876+ Py_DECREF (newtext );
1877+ return NULL ;
1878+ }
1879+ /* replace() may have closed, detached or reinitialized the stream. */
1880+ if (textiowrapper_check_write (self ) == NULL ) {
1881+ Py_DECREF (newtext );
1882+ return NULL ;
1883+ }
1884+ text = newtext ;
1885+ }
1886+
1887+ if (self -> write_through )
1888+ text_needflush = 1 ;
1889+ if (self -> line_buffering &&
1890+ (haslf ||
1891+ PyUnicode_FindChar (text , '\r' , 0 , PyUnicode_GET_LENGTH (text ), 1 ) != -1 ))
1892+ needflush = 1 ;
1893+
1894+ /* XXX What if we were just reading? */
1895+ if (self -> encodefunc != NULL ) {
1896+ if (PyUnicode_IS_ASCII (text ) &&
1897+ // See bpo-43260
1898+ PyUnicode_GET_LENGTH (text ) <= self -> chunk_size &&
1899+ is_asciicompat_encoding (self -> encodefunc )) {
1900+ b = Py_NewRef (text );
1901+ }
1902+ else {
1903+ b = (* self -> encodefunc )((PyObject * ) self , text );
1904+ }
1905+ self -> encoding_start_of_stream = 0 ;
1906+ }
1907+ else {
1908+ /* A codec callback may reconfigure the stream. */
1909+ PyObject * encoder = Py_NewRef (self -> encoder );
1910+ b = PyObject_CallMethodOneArg (encoder , & _Py_ID (encode ), text );
1911+ Py_DECREF (encoder );
1912+ }
1913+
1914+ Py_DECREF (text );
1915+ if (b == NULL )
1916+ return NULL ;
1917+ if (b != text && !PyBytes_Check (b )) {
1918+ PyErr_Format (PyExc_TypeError ,
1919+ "encoder should return a bytes object, not '%.200s'" ,
1920+ Py_TYPE (b )-> tp_name );
1921+ Py_DECREF (b );
1922+ return NULL ;
1923+ }
1924+
1925+ if (textiowrapper_write_bytes (self , b , needflush , text_needflush ) < 0 ) {
1926+ return NULL ;
1927+ }
19301928 return PyLong_FromSsize_t (textlen );
19311929}
19321930
1931+ /*[clinic input]
1932+ @critical_section
1933+ _io.TextIOWrapper.write
1934+ text: unicode
1935+ /
1936+ [clinic start generated code]*/
1937+
1938+ static PyObject *
1939+ _io_TextIOWrapper_write_impl (textio * self , PyObject * text )
1940+ /*[clinic end generated code: output=d2deb0d50771fcec input=73ec95c5c4a3489c]*/
1941+ {
1942+ if (textiowrapper_check_write (self ) == NULL ) {
1943+ return NULL ;
1944+ }
1945+ return textiowrapper_write_text (self , text );
1946+ }
1947+
19331948/* Steal a reference to chars and store it in the decoded_char buffer;
19341949 */
19351950static void
0 commit comments