@@ -1724,6 +1724,102 @@ test_get_constant(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
17241724}
17251725
17261726
1727+ static PyObject *
1728+ test_unicodewriter (PyObject * Py_UNUSED (self ), PyObject * Py_UNUSED (args ))
1729+ {
1730+ PyUnicodeWriter * writer = PyUnicodeWriter_Create ();
1731+ if (writer == NULL ) {
1732+ return NULL ;
1733+ }
1734+ int ret ;
1735+
1736+ // test PyUnicodeWriter_SetOverallocate()
1737+ PyUnicodeWriter_SetOverallocate (writer , 1 );
1738+
1739+ // test PyUnicodeWriter_WriteString()
1740+ PyObject * str = PyUnicode_FromString ("var" );
1741+ if (str == NULL ) {
1742+ goto error ;
1743+ }
1744+ ret = PyUnicodeWriter_WriteString (writer , str );
1745+ Py_CLEAR (str );
1746+ if (ret < 0 ) {
1747+ goto error ;
1748+ }
1749+
1750+ // test PyUnicodeWriter_WriteChar()
1751+ if (PyUnicodeWriter_WriteChar (writer , '=' ) < 0 ) {
1752+ goto error ;
1753+ }
1754+
1755+ // test PyUnicodeWriter_WriteSubstring()
1756+ str = PyUnicode_FromString ("[long]" );
1757+ if (str == NULL ) {
1758+ goto error ;
1759+ }
1760+ ret = PyUnicodeWriter_WriteSubstring (writer , str , 1 , 5 );
1761+ Py_CLEAR (str );
1762+ if (ret < 0 ) {
1763+ goto error ;
1764+ }
1765+
1766+ // test PyUnicodeWriter_WriteUTF8()
1767+ if (PyUnicodeWriter_WriteUTF8 (writer , " valu\xC3\xA9" , -1 ) < 0 ) {
1768+ goto error ;
1769+ }
1770+
1771+ {
1772+ PyObject * result = PyUnicodeWriter_Finish (writer );
1773+ if (result == NULL ) {
1774+ return NULL ;
1775+ }
1776+ assert (PyUnicode_EqualToUTF8 (result , "var=long valu\xC3\xA9" ));
1777+ Py_DECREF (result );
1778+ }
1779+
1780+ Py_RETURN_NONE ;
1781+
1782+ error :
1783+ PyUnicodeWriter_Free (writer );
1784+ return NULL ;
1785+ }
1786+
1787+
1788+ static PyObject *
1789+ test_unicodewriter_format (PyObject * Py_UNUSED (self ), PyObject * Py_UNUSED (args ))
1790+ {
1791+ PyUnicodeWriter * writer = PyUnicodeWriter_Create ();
1792+ if (writer == NULL ) {
1793+ return NULL ;
1794+ }
1795+
1796+ // test PyUnicodeWriter_Format()
1797+ if (PyUnicodeWriter_Format (writer , "%s %i" , "Hello" , 123 ) < 0 ) {
1798+ goto error ;
1799+ }
1800+
1801+ // test PyUnicodeWriter_WriteChar()
1802+ if (PyUnicodeWriter_WriteChar (writer , '.' ) < 0 ) {
1803+ goto error ;
1804+ }
1805+
1806+ {
1807+ PyObject * result = PyUnicodeWriter_Finish (writer );
1808+ if (result == NULL ) {
1809+ return NULL ;
1810+ }
1811+ assert (PyUnicode_EqualToUTF8 (result , "Hello 123." ));
1812+ Py_DECREF (result );
1813+ }
1814+
1815+ Py_RETURN_NONE ;
1816+
1817+ error :
1818+ PyUnicodeWriter_Free (writer );
1819+ return NULL ;
1820+ }
1821+
1822+
17271823static struct PyMethodDef methods [] = {
17281824 {"test_object" , test_object , METH_NOARGS , _Py_NULL },
17291825 {"test_py_is" , test_py_is , METH_NOARGS , _Py_NULL },
@@ -1762,6 +1858,8 @@ static struct PyMethodDef methods[] = {
17621858 {"test_time" , test_time , METH_NOARGS , _Py_NULL },
17631859#endif
17641860 {"test_get_constant" , test_get_constant , METH_NOARGS , _Py_NULL },
1861+ {"test_unicodewriter" , test_unicodewriter , METH_NOARGS , _Py_NULL },
1862+ {"test_unicodewriter_format" , test_unicodewriter_format , METH_NOARGS , _Py_NULL },
17651863 {_Py_NULL , _Py_NULL , 0 , _Py_NULL }
17661864};
17671865
0 commit comments