@@ -288,6 +288,83 @@ writer_extend_array_steal(PyObject *self_raw, PyObject *tuple)
288288}
289289
290290
291+ // Test PyListWriter_FromFormat("i")
292+ static PyObject *
293+ writer_from_format_i (PyObject * self_raw , PyObject * arg )
294+ {
295+ WriterObject * self = (WriterObject * )self_raw ;
296+ if (writer_check (self ) < 0 ) {
297+ return NULL ;
298+ }
299+
300+ int value ;
301+ if (!PyArg_Parse (arg , "i" , & value )) {
302+ return NULL ;
303+ }
304+
305+ if (PyListWriter_FromFormat (self -> writer , "i" , value ) < 0 ) {
306+ return NULL ;
307+ }
308+ Py_RETURN_NONE ;
309+ }
310+
311+
312+ // Test PyListWriter_FromFormat("i")
313+ static PyObject *
314+ writer_from_format_i16 (PyObject * self_raw , PyObject * tuple )
315+ {
316+ WriterObject * self = (WriterObject * )self_raw ;
317+ if (writer_check (self ) < 0 ) {
318+ return NULL ;
319+ }
320+
321+ // Test size larger than small_array size in PyListWriter_FromFormat()
322+ int values [16 ];
323+ Py_ssize_t len = Py_ARRAY_LENGTH (values );
324+ if (!PyTuple_Check (tuple ) || PyTuple_GET_SIZE (tuple ) != len ) {
325+ PyErr_Format (PyExc_ValueError ,
326+ "expect a tuple of %zd integers" , len );
327+ return NULL ;
328+ }
329+ for (Py_ssize_t i = 0 ; i < len ; i ++ ) {
330+ int value = PyLong_AsInt (PyTuple_GET_ITEM (tuple , i ));
331+ if (value == -1 && PyErr_Occurred ()) {
332+ return NULL ;
333+ }
334+ values [i ] = value ;
335+ }
336+
337+ int res = PyListWriter_FromFormat (self -> writer ,
338+ "iiiiiiiiiiiiiiii" ,
339+ values [0 ], values [1 ], values [2 ], values [3 ],
340+ values [4 ], values [5 ], values [6 ], values [7 ],
341+ values [8 ], values [9 ], values [10 ], values [11 ],
342+ values [12 ], values [13 ], values [14 ], values [15 ]
343+ );
344+ if (res < 0 ) {
345+ return NULL ;
346+ }
347+ Py_RETURN_NONE ;
348+ }
349+
350+
351+ // Test PyListWriter_FromFormat() error
352+ static PyObject *
353+ writer_from_format_error (PyObject * self_raw , PyObject * Py_UNUSED (args ))
354+ {
355+ WriterObject * self = (WriterObject * )self_raw ;
356+ if (writer_check (self ) < 0 ) {
357+ return NULL ;
358+ }
359+
360+ int res = PyListWriter_FromFormat (self -> writer , "iiO" , 1 , 2 , NULL );
361+ if (res < 0 ) {
362+ return NULL ;
363+ }
364+ Py_RETURN_NONE ;
365+ }
366+
367+
291368// Test PyListWriter_Finish()
292369static PyObject *
293370writer_finish (PyObject * self_raw , PyObject * Py_UNUSED (args ))
@@ -324,6 +401,9 @@ static PyMethodDef writer_methods[] = {
324401 {"extend" , _PyCFunction_CAST (writer_extend ), METH_O },
325402 {"extend_array" , _PyCFunction_CAST (writer_extend_array ), METH_O },
326403 {"extend_array_steal" , _PyCFunction_CAST (writer_extend_array_steal ), METH_O },
404+ {"from_format_i" , _PyCFunction_CAST (writer_from_format_i ), METH_O },
405+ {"from_format_i16" , _PyCFunction_CAST (writer_from_format_i16 ), METH_O },
406+ {"from_format_error" , _PyCFunction_CAST (writer_from_format_error ), METH_NOARGS },
327407 {"finish" , _PyCFunction_CAST (writer_finish ), METH_NOARGS },
328408 {"discard" , _PyCFunction_CAST (writer_discard ), METH_NOARGS },
329409 {NULL , NULL } /* sentinel */
0 commit comments