@@ -221,6 +221,92 @@ unicode_copycharacters(PyObject *self, PyObject *args)
221
221
}
222
222
223
223
224
+ static PyObject *
225
+ test_unicodewriter (PyObject * self , PyObject * Py_UNUSED (args ))
226
+ {
227
+ PyUnicodeWriter * writer = PyUnicodeWriter_Create ();
228
+ if (writer == NULL ) {
229
+ return NULL ;
230
+ }
231
+
232
+ // test PyUnicodeWriter_SetOverallocate()
233
+ PyUnicodeWriter_SetOverallocate (writer , 1 );
234
+
235
+ // test PyUnicodeWriter_WriteStr()
236
+ PyObject * str = PyUnicode_FromString ("var" );
237
+ if (str == NULL ) {
238
+ goto error ;
239
+ }
240
+ int ret = PyUnicodeWriter_WriteStr (writer , str );
241
+ Py_CLEAR (str );
242
+ if (ret < 0 ) {
243
+ goto error ;
244
+ }
245
+
246
+ // test PyUnicodeWriter_WriteChar()
247
+ if (PyUnicodeWriter_WriteChar (writer , '=' ) < 0 ) {
248
+ goto error ;
249
+ }
250
+
251
+ // test PyUnicodeWriter_WriteSubstring()
252
+ str = PyUnicode_FromString ("[value]" );
253
+ if (str == NULL ) {
254
+ goto error ;
255
+ }
256
+ ret = PyUnicodeWriter_WriteSubstring (writer , str , 1 , 6 );
257
+ Py_CLEAR (str );
258
+ if (ret < 0 ) {
259
+ goto error ;
260
+ }
261
+
262
+ PyObject * result = PyUnicodeWriter_Finish (writer );
263
+ if (result == NULL ) {
264
+ return NULL ;
265
+ }
266
+ assert (PyUnicode_EqualToUTF8 (result , "var=value" ));
267
+ Py_DECREF (result );
268
+
269
+ Py_RETURN_NONE ;
270
+
271
+ error :
272
+ PyUnicodeWriter_Free (writer );
273
+ return NULL ;
274
+ }
275
+
276
+
277
+ static PyObject *
278
+ test_unicodewriter_format (PyObject * self , PyObject * Py_UNUSED (args ))
279
+ {
280
+ PyUnicodeWriter * writer = PyUnicodeWriter_Create ();
281
+ if (writer == NULL ) {
282
+ return NULL ;
283
+ }
284
+
285
+ // test PyUnicodeWriter_Format()
286
+ if (PyUnicodeWriter_Format (writer , "%s %i" , "Hello" , 123 ) < 0 ) {
287
+ goto error ;
288
+ }
289
+
290
+ // test PyUnicodeWriter_WriteChar()
291
+ if (PyUnicodeWriter_WriteChar (writer , '.' ) < 0 ) {
292
+ goto error ;
293
+ }
294
+
295
+ PyObject * result = PyUnicodeWriter_Finish (writer );
296
+ if (result == NULL ) {
297
+ return NULL ;
298
+ }
299
+ assert (PyUnicode_EqualToUTF8 (result , "Hello 123." ));
300
+ Py_DECREF (result );
301
+
302
+ Py_RETURN_NONE ;
303
+
304
+ error :
305
+ PyUnicodeWriter_Free (writer );
306
+ return NULL ;
307
+ }
308
+
309
+
224
310
static PyMethodDef TestMethods [] = {
225
311
{"unicode_new" , unicode_new , METH_VARARGS },
226
312
{"unicode_fill" , unicode_fill , METH_VARARGS },
@@ -229,6 +315,8 @@ static PyMethodDef TestMethods[] = {
229
315
{"unicode_asucs4copy" , unicode_asucs4copy , METH_VARARGS },
230
316
{"unicode_asutf8" , unicode_asutf8 , METH_VARARGS },
231
317
{"unicode_copycharacters" , unicode_copycharacters , METH_VARARGS },
318
+ {"test_unicodewriter" , test_unicodewriter , METH_NOARGS },
319
+ {"test_unicodewriter_format" , test_unicodewriter_format , METH_NOARGS },
232
320
{NULL },
233
321
};
234
322
0 commit comments