11#include <stddef.h> // ptrdiff_t
22
33#include "parts.h"
4+ #include "util.h"
45
56static struct PyModuleDef * _testcapimodule = NULL ; // set at initialization
67
@@ -101,7 +102,6 @@ test_widechar(PyObject *self, PyObject *Py_UNUSED(ignored))
101102 Py_RETURN_NONE ;
102103}
103104
104- #define NULLABLE (x ) do { if (x == Py_None) x = NULL; } while (0);
105105
106106static PyObject *
107107unicode_copy (PyObject * unicode )
@@ -347,13 +347,8 @@ unicode_substring(PyObject *self, PyObject *args)
347347static PyObject *
348348unicode_getlength (PyObject * self , PyObject * arg )
349349{
350- Py_ssize_t result ;
351-
352350 NULLABLE (arg );
353- result = PyUnicode_GetLength (arg );
354- if (result == -1 )
355- return NULL ;
356- return PyLong_FromSsize_t (result );
351+ RETURN_SIZE (PyUnicode_GetLength (arg ));
357352}
358353
359354/* Test PyUnicode_ReadChar() */
@@ -482,16 +477,12 @@ static PyObject *
482477unicode_aswidechar_null (PyObject * self , PyObject * args )
483478{
484479 PyObject * unicode ;
485- Py_ssize_t buflen , size ;
480+ Py_ssize_t buflen ;
486481
487482 if (!PyArg_ParseTuple (args , "On" , & unicode , & buflen ))
488483 return NULL ;
489484 NULLABLE (unicode );
490- size = PyUnicode_AsWideChar (unicode , NULL , buflen );
491- if (size == -1 ) {
492- return NULL ;
493- }
494- return PyLong_FromSsize_t (size );
485+ RETURN_SIZE (PyUnicode_AsWideChar (unicode , NULL , buflen ));
495486}
496487
497488/* Test PyUnicode_AsWideCharString() */
@@ -1293,17 +1284,13 @@ unicode_count(PyObject *self, PyObject *args)
12931284 PyObject * substr ;
12941285 Py_ssize_t start ;
12951286 Py_ssize_t end ;
1296- Py_ssize_t result ;
12971287
12981288 if (!PyArg_ParseTuple (args , "OOnn" , & str , & substr , & start , & end ))
12991289 return NULL ;
13001290
13011291 NULLABLE (str );
13021292 NULLABLE (substr );
1303- result = PyUnicode_Count (str , substr , start , end );
1304- if (result == -1 )
1305- return NULL ;
1306- return PyLong_FromSsize_t (result );
1293+ RETURN_SIZE (PyUnicode_Count (str , substr , start , end ));
13071294}
13081295
13091296/* Test PyUnicode_Find() */
@@ -1323,8 +1310,11 @@ unicode_find(PyObject *self, PyObject *args)
13231310 NULLABLE (str );
13241311 NULLABLE (substr );
13251312 result = PyUnicode_Find (str , substr , start , end , direction );
1326- if (result == -2 )
1313+ if (result == -2 ) {
1314+ assert (PyErr_Occurred ());
13271315 return NULL ;
1316+ }
1317+ assert (!PyErr_Occurred ());
13281318 return PyLong_FromSsize_t (result );
13291319}
13301320
@@ -1337,17 +1327,13 @@ unicode_tailmatch(PyObject *self, PyObject *args)
13371327 Py_ssize_t start ;
13381328 Py_ssize_t end ;
13391329 int direction ;
1340- Py_ssize_t result ;
13411330
13421331 if (!PyArg_ParseTuple (args , "OOnni" , & str , & substr , & start , & end , & direction ))
13431332 return NULL ;
13441333
13451334 NULLABLE (str );
13461335 NULLABLE (substr );
1347- result = PyUnicode_Tailmatch (str , substr , start , end , direction );
1348- if (result == -1 )
1349- return NULL ;
1350- return PyLong_FromSsize_t (result );
1336+ RETURN_SIZE (PyUnicode_Tailmatch (str , substr , start , end , direction ));
13511337}
13521338
13531339/* Test PyUnicode_FindChar() */
@@ -1366,10 +1352,12 @@ unicode_findchar(PyObject *self, PyObject *args)
13661352 }
13671353 NULLABLE (str );
13681354 result = PyUnicode_FindChar (str , (Py_UCS4 )ch , start , end , direction );
1369- if (result == -2 )
1355+ if (result == -2 ) {
1356+ assert (PyErr_Occurred ());
13701357 return NULL ;
1371- else
1372- return PyLong_FromSsize_t (result );
1358+ }
1359+ assert (!PyErr_Occurred ());
1360+ return PyLong_FromSsize_t (result );
13731361}
13741362
13751363/* Test PyUnicode_Replace() */
@@ -1407,6 +1395,7 @@ unicode_compare(PyObject *self, PyObject *args)
14071395 if (result == -1 && PyErr_Occurred ()) {
14081396 return NULL ;
14091397 }
1398+ assert (!PyErr_Occurred ());
14101399 return PyLong_FromLong (result );
14111400}
14121401
@@ -1467,32 +1456,21 @@ unicode_contains(PyObject *self, PyObject *args)
14671456{
14681457 PyObject * container ;
14691458 PyObject * element ;
1470- int result ;
14711459
14721460 if (!PyArg_ParseTuple (args , "OO" , & container , & element ))
14731461 return NULL ;
14741462
14751463 NULLABLE (container );
14761464 NULLABLE (element );
1477- result = PyUnicode_Contains (container , element );
1478- if (result == -1 && PyErr_Occurred ()) {
1479- return NULL ;
1480- }
1481- return PyLong_FromLong (result );
1465+ RETURN_INT (PyUnicode_Contains (container , element ));
14821466}
14831467
14841468/* Test PyUnicode_IsIdentifier() */
14851469static PyObject *
14861470unicode_isidentifier (PyObject * self , PyObject * arg )
14871471{
1488- int result ;
1489-
14901472 NULLABLE (arg );
1491- result = PyUnicode_IsIdentifier (arg );
1492- if (result == -1 && PyErr_Occurred ()) {
1493- return NULL ;
1494- }
1495- return PyLong_FromLong (result );
1473+ RETURN_INT (PyUnicode_IsIdentifier (arg ));
14961474}
14971475
14981476/* Test PyUnicode_CopyCharacters() */
0 commit comments