@@ -307,58 +307,73 @@ BytesWriter_append_internal(BytesWriterObject *self, uint8_t value) {
307307}
308308
309309static PyObject *
310- BytesWriter_truncate (PyObject * self , PyObject * const * args , size_t nargs ) {
311- if (unlikely (nargs != 1 )) {
312- PyErr_Format (PyExc_TypeError ,
313- "truncate() takes exactly 1 argument (%zu given)" , nargs );
310+ BytesWriter_append (PyObject * self , PyObject * const * args , size_t nargs , PyObject * kwnames ) {
311+ static const char * const kwlist [] = {"value" , 0 };
312+ static CPyArg_Parser parser = {"O:append" , kwlist , 0 };
313+ PyObject * value ;
314+ if (unlikely (!CPyArg_ParseStackAndKeywordsSimple (args , nargs , kwnames , & parser , & value ))) {
314315 return NULL ;
315316 }
316317 if (!check_bytes_writer (self )) {
317318 return NULL ;
318319 }
319-
320- PyObject * size_obj = args [0 ];
321- int overflow ;
322- long long size = PyLong_AsLongLongAndOverflow (size_obj , & overflow );
323-
324- if (size == -1 && PyErr_Occurred ()) {
320+ uint8_t unboxed = CPyLong_AsUInt8 (value );
321+ if (unlikely (unboxed == CPY_LL_UINT_ERROR && PyErr_Occurred ())) {
322+ CPy_TypeError ("u8" , value );
325323 return NULL ;
326324 }
327- if (overflow != 0 || size < 0 ) {
328- PyErr_SetString (PyExc_ValueError , "size must be non-negative" );
325+ if (unlikely (BytesWriter_append_internal ((BytesWriterObject * )self , unboxed ) == CPY_NONE_ERROR )) {
329326 return NULL ;
330327 }
328+ Py_INCREF (Py_None );
329+ return Py_None ;
330+ }
331331
332+ static char
333+ BytesWriter_truncate_internal (PyObject * self , int64_t size ) {
332334 BytesWriterObject * writer = (BytesWriterObject * )self ;
333335 Py_ssize_t current_size = writer -> len ;
334336
337+ // Validate size is non-negative
338+ if (size < 0 ) {
339+ PyErr_SetString (PyExc_ValueError , "size must be non-negative" );
340+ return CPY_NONE_ERROR ;
341+ }
342+
343+ // Validate size doesn't exceed current size
335344 if (size > current_size ) {
336345 PyErr_SetString (PyExc_ValueError , "size cannot be larger than current buffer size" );
337- return NULL ;
346+ return CPY_NONE_ERROR ;
338347 }
339348
340349 writer -> len = size ;
341- Py_INCREF (Py_None );
342- return Py_None ;
350+ return CPY_NONE ;
343351}
344352
345353static PyObject *
346- BytesWriter_append (PyObject * self , PyObject * const * args , size_t nargs , PyObject * kwnames ) {
347- static const char * const kwlist [] = {"value" , 0 };
348- static CPyArg_Parser parser = {"O:append" , kwlist , 0 };
349- PyObject * value ;
350- if (unlikely (!CPyArg_ParseStackAndKeywordsSimple (args , nargs , kwnames , & parser , & value ))) {
354+ BytesWriter_truncate (PyObject * self , PyObject * const * args , size_t nargs ) {
355+ if (unlikely (nargs != 1 )) {
356+ PyErr_Format (PyExc_TypeError ,
357+ "truncate() takes exactly 1 argument (%zu given)" , nargs );
351358 return NULL ;
352359 }
353360 if (!check_bytes_writer (self )) {
354361 return NULL ;
355362 }
356- uint8_t unboxed = CPyLong_AsUInt8 (value );
357- if (unlikely (unboxed == CPY_LL_UINT_ERROR && PyErr_Occurred ())) {
358- CPy_TypeError ("u8" , value );
363+
364+ PyObject * size_obj = args [0 ];
365+ int overflow ;
366+ long long size = PyLong_AsLongLongAndOverflow (size_obj , & overflow );
367+
368+ if (size == -1 && PyErr_Occurred ()) {
359369 return NULL ;
360370 }
361- if (unlikely (BytesWriter_append_internal ((BytesWriterObject * )self , unboxed ) == CPY_NONE_ERROR )) {
371+ if (overflow != 0 ) {
372+ PyErr_SetString (PyExc_ValueError , "integer out of range" );
373+ return NULL ;
374+ }
375+
376+ if (unlikely (BytesWriter_truncate_internal (self , size ) == CPY_NONE_ERROR )) {
362377 return NULL ;
363378 }
364379 Py_INCREF (Py_None );
@@ -376,27 +391,6 @@ BytesWriter_len_internal(PyObject *self) {
376391 return writer -> len << 1 ;
377392}
378393
379- static char
380- BytesWriter_truncate_internal (PyObject * self , int64_t size ) {
381- BytesWriterObject * writer = (BytesWriterObject * )self ;
382- Py_ssize_t current_size = writer -> len ;
383-
384- // Validate size is non-negative
385- if (size < 0 ) {
386- PyErr_SetString (PyExc_ValueError , "size must be non-negative" );
387- return CPY_NONE_ERROR ;
388- }
389-
390- // Validate size doesn't exceed current size
391- if (size > current_size ) {
392- PyErr_SetString (PyExc_ValueError , "size cannot be larger than current buffer size" );
393- return CPY_NONE_ERROR ;
394- }
395-
396- writer -> len = size ;
397- return CPY_NONE ;
398- }
399-
400394static PyMethodDef librt_strings_module_methods [] = {
401395 {NULL , NULL , 0 , NULL }
402396};
0 commit comments