5
5
*/
6
6
7
7
#define PY_SSIZE_T_CLEAN
8
- #define NEEDS_PY_IDENTIFIER
9
8
#include "Python.h"
10
9
#include "structmember.h" // PyMemberDef
11
10
#include "multibytecodec.h"
@@ -19,6 +18,7 @@ typedef struct {
19
18
PyTypeObject * reader_type ;
20
19
PyTypeObject * writer_type ;
21
20
PyTypeObject * multibytecodec_type ;
21
+ PyObject * str_write ;
22
22
} _multibytecodec_state ;
23
23
24
24
static _multibytecodec_state *
@@ -72,8 +72,6 @@ static PyObject *multibytecodec_encode(MultibyteCodec *,
72
72
73
73
#define MBENC_RESET MBENC_MAX<<1 /* reset after an encoding session */
74
74
75
- _Py_IDENTIFIER (write );
76
-
77
75
static PyObject *
78
76
make_tuple (PyObject * object , Py_ssize_t len )
79
77
{
@@ -1715,15 +1713,15 @@ static PyType_Spec reader_spec = {
1715
1713
1716
1714
static int
1717
1715
mbstreamwriter_iwrite (MultibyteStreamWriterObject * self ,
1718
- PyObject * unistr )
1716
+ PyObject * unistr , PyObject * str_write )
1719
1717
{
1720
1718
PyObject * str , * wr ;
1721
1719
1722
1720
str = encoder_encode_stateful (STATEFUL_ECTX (self ), unistr , 0 );
1723
1721
if (str == NULL )
1724
1722
return -1 ;
1725
1723
1726
- wr = _PyObject_CallMethodIdOneArg (self -> stream , & PyId_write , str );
1724
+ wr = _PyObject_CallMethodOneArg (self -> stream , str_write , str );
1727
1725
Py_DECREF (str );
1728
1726
if (wr == NULL )
1729
1727
return -1 ;
@@ -1735,32 +1733,38 @@ mbstreamwriter_iwrite(MultibyteStreamWriterObject *self,
1735
1733
/*[clinic input]
1736
1734
_multibytecodec.MultibyteStreamWriter.write
1737
1735
1736
+ cls: defining_class
1738
1737
strobj: object
1739
1738
/
1740
1739
[clinic start generated code]*/
1741
1740
1742
1741
static PyObject *
1743
- _multibytecodec_MultibyteStreamWriter_write (MultibyteStreamWriterObject * self ,
1744
- PyObject * strobj )
1745
- /*[clinic end generated code: output=e13ae841c895251e input=551dc4c018c10a2b]*/
1742
+ _multibytecodec_MultibyteStreamWriter_write_impl (MultibyteStreamWriterObject * self ,
1743
+ PyTypeObject * cls ,
1744
+ PyObject * strobj )
1745
+ /*[clinic end generated code: output=68ade3aea26410ac input=199f26f68bd8425a]*/
1746
1746
{
1747
- if (mbstreamwriter_iwrite (self , strobj ))
1747
+ _multibytecodec_state * state = PyType_GetModuleState (cls );
1748
+ assert (state != NULL );
1749
+ if (mbstreamwriter_iwrite (self , strobj , state -> str_write )) {
1748
1750
return NULL ;
1749
- else
1750
- Py_RETURN_NONE ;
1751
+ }
1752
+ Py_RETURN_NONE ;
1751
1753
}
1752
1754
1753
1755
/*[clinic input]
1754
1756
_multibytecodec.MultibyteStreamWriter.writelines
1755
1757
1758
+ cls: defining_class
1756
1759
lines: object
1757
1760
/
1758
1761
[clinic start generated code]*/
1759
1762
1760
1763
static PyObject *
1761
- _multibytecodec_MultibyteStreamWriter_writelines (MultibyteStreamWriterObject * self ,
1762
- PyObject * lines )
1763
- /*[clinic end generated code: output=e5c4285ac8e7d522 input=57797fe7008d4e96]*/
1764
+ _multibytecodec_MultibyteStreamWriter_writelines_impl (MultibyteStreamWriterObject * self ,
1765
+ PyTypeObject * cls ,
1766
+ PyObject * lines )
1767
+ /*[clinic end generated code: output=b4c99d2cf23ffb88 input=a6d5fe7c74972a34]*/
1764
1768
{
1765
1769
PyObject * strobj ;
1766
1770
int i , r ;
@@ -1771,13 +1775,15 @@ _multibytecodec_MultibyteStreamWriter_writelines(MultibyteStreamWriterObject *se
1771
1775
return NULL ;
1772
1776
}
1773
1777
1778
+ _multibytecodec_state * state = PyType_GetModuleState (cls );
1779
+ assert (state != NULL );
1774
1780
for (i = 0 ; i < PySequence_Length (lines ); i ++ ) {
1775
1781
/* length can be changed even within this loop */
1776
1782
strobj = PySequence_GetItem (lines , i );
1777
1783
if (strobj == NULL )
1778
1784
return NULL ;
1779
1785
1780
- r = mbstreamwriter_iwrite (self , strobj );
1786
+ r = mbstreamwriter_iwrite (self , strobj , state -> str_write );
1781
1787
Py_DECREF (strobj );
1782
1788
if (r == -1 )
1783
1789
return NULL ;
@@ -1791,11 +1797,16 @@ _multibytecodec_MultibyteStreamWriter_writelines(MultibyteStreamWriterObject *se
1791
1797
1792
1798
/*[clinic input]
1793
1799
_multibytecodec.MultibyteStreamWriter.reset
1800
+
1801
+ cls: defining_class
1802
+ /
1803
+
1794
1804
[clinic start generated code]*/
1795
1805
1796
1806
static PyObject *
1797
- _multibytecodec_MultibyteStreamWriter_reset_impl (MultibyteStreamWriterObject * self )
1798
- /*[clinic end generated code: output=8f54a4d9b03db5ff input=b56dbcbaf35cc10c]*/
1807
+ _multibytecodec_MultibyteStreamWriter_reset_impl (MultibyteStreamWriterObject * self ,
1808
+ PyTypeObject * cls )
1809
+ /*[clinic end generated code: output=32ef224c2a38aa3d input=28af6a9cd38d1979]*/
1799
1810
{
1800
1811
PyObject * pwrt ;
1801
1812
@@ -1814,10 +1825,14 @@ _multibytecodec_MultibyteStreamWriter_reset_impl(MultibyteStreamWriterObject *se
1814
1825
return NULL ;
1815
1826
1816
1827
assert (PyBytes_Check (pwrt ));
1828
+
1829
+ _multibytecodec_state * state = PyType_GetModuleState (cls );
1830
+ assert (state != NULL );
1831
+
1817
1832
if (PyBytes_Size (pwrt ) > 0 ) {
1818
1833
PyObject * wr ;
1819
1834
1820
- wr = _PyObject_CallMethodIdOneArg (self -> stream , & PyId_write , pwrt );
1835
+ wr = _PyObject_CallMethodOneArg (self -> stream , state -> str_write , pwrt );
1821
1836
if (wr == NULL ) {
1822
1837
Py_DECREF (pwrt );
1823
1838
return NULL ;
@@ -1989,6 +2004,7 @@ _multibytecodec_clear(PyObject *mod)
1989
2004
Py_CLEAR (state -> decoder_type );
1990
2005
Py_CLEAR (state -> reader_type );
1991
2006
Py_CLEAR (state -> writer_type );
2007
+ Py_CLEAR (state -> str_write );
1992
2008
return 0 ;
1993
2009
}
1994
2010
@@ -2017,6 +2033,10 @@ static int
2017
2033
_multibytecodec_exec (PyObject * mod )
2018
2034
{
2019
2035
_multibytecodec_state * state = _multibytecodec_get_state (mod );
2036
+ state -> str_write = PyUnicode_InternFromString ("write" );
2037
+ if (state -> str_write == NULL ) {
2038
+ return -1 ;
2039
+ }
2020
2040
CREATE_TYPE (mod , state -> multibytecodec_type , & multibytecodec_spec );
2021
2041
CREATE_TYPE (mod , state -> encoder_type , & encoder_spec );
2022
2042
CREATE_TYPE (mod , state -> decoder_type , & decoder_spec );
0 commit comments