1
1
#include <stddef.h> // ptrdiff_t
2
2
3
3
#include "parts.h"
4
+ #include "util.h"
4
5
5
6
static struct PyModuleDef * _testcapimodule = NULL ; // set at initialization
6
7
@@ -101,7 +102,6 @@ test_widechar(PyObject *self, PyObject *Py_UNUSED(ignored))
101
102
Py_RETURN_NONE ;
102
103
}
103
104
104
- #define NULLABLE (x ) do { if (x == Py_None) x = NULL; } while (0);
105
105
106
106
static PyObject *
107
107
unicode_copy (PyObject * unicode )
@@ -347,13 +347,8 @@ unicode_substring(PyObject *self, PyObject *args)
347
347
static PyObject *
348
348
unicode_getlength (PyObject * self , PyObject * arg )
349
349
{
350
- Py_ssize_t result ;
351
-
352
350
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 ));
357
352
}
358
353
359
354
/* Test PyUnicode_ReadChar() */
@@ -482,16 +477,12 @@ static PyObject *
482
477
unicode_aswidechar_null (PyObject * self , PyObject * args )
483
478
{
484
479
PyObject * unicode ;
485
- Py_ssize_t buflen , size ;
480
+ Py_ssize_t buflen ;
486
481
487
482
if (!PyArg_ParseTuple (args , "On" , & unicode , & buflen ))
488
483
return NULL ;
489
484
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 ));
495
486
}
496
487
497
488
/* Test PyUnicode_AsWideCharString() */
@@ -1293,17 +1284,13 @@ unicode_count(PyObject *self, PyObject *args)
1293
1284
PyObject * substr ;
1294
1285
Py_ssize_t start ;
1295
1286
Py_ssize_t end ;
1296
- Py_ssize_t result ;
1297
1287
1298
1288
if (!PyArg_ParseTuple (args , "OOnn" , & str , & substr , & start , & end ))
1299
1289
return NULL ;
1300
1290
1301
1291
NULLABLE (str );
1302
1292
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 ));
1307
1294
}
1308
1295
1309
1296
/* Test PyUnicode_Find() */
@@ -1323,8 +1310,11 @@ unicode_find(PyObject *self, PyObject *args)
1323
1310
NULLABLE (str );
1324
1311
NULLABLE (substr );
1325
1312
result = PyUnicode_Find (str , substr , start , end , direction );
1326
- if (result == -2 )
1313
+ if (result == -2 ) {
1314
+ assert (PyErr_Occurred ());
1327
1315
return NULL ;
1316
+ }
1317
+ assert (!PyErr_Occurred ());
1328
1318
return PyLong_FromSsize_t (result );
1329
1319
}
1330
1320
@@ -1337,17 +1327,13 @@ unicode_tailmatch(PyObject *self, PyObject *args)
1337
1327
Py_ssize_t start ;
1338
1328
Py_ssize_t end ;
1339
1329
int direction ;
1340
- Py_ssize_t result ;
1341
1330
1342
1331
if (!PyArg_ParseTuple (args , "OOnni" , & str , & substr , & start , & end , & direction ))
1343
1332
return NULL ;
1344
1333
1345
1334
NULLABLE (str );
1346
1335
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 ));
1351
1337
}
1352
1338
1353
1339
/* Test PyUnicode_FindChar() */
@@ -1366,10 +1352,12 @@ unicode_findchar(PyObject *self, PyObject *args)
1366
1352
}
1367
1353
NULLABLE (str );
1368
1354
result = PyUnicode_FindChar (str , (Py_UCS4 )ch , start , end , direction );
1369
- if (result == -2 )
1355
+ if (result == -2 ) {
1356
+ assert (PyErr_Occurred ());
1370
1357
return NULL ;
1371
- else
1372
- return PyLong_FromSsize_t (result );
1358
+ }
1359
+ assert (!PyErr_Occurred ());
1360
+ return PyLong_FromSsize_t (result );
1373
1361
}
1374
1362
1375
1363
/* Test PyUnicode_Replace() */
@@ -1407,6 +1395,7 @@ unicode_compare(PyObject *self, PyObject *args)
1407
1395
if (result == -1 && PyErr_Occurred ()) {
1408
1396
return NULL ;
1409
1397
}
1398
+ assert (!PyErr_Occurred ());
1410
1399
return PyLong_FromLong (result );
1411
1400
}
1412
1401
@@ -1467,32 +1456,21 @@ unicode_contains(PyObject *self, PyObject *args)
1467
1456
{
1468
1457
PyObject * container ;
1469
1458
PyObject * element ;
1470
- int result ;
1471
1459
1472
1460
if (!PyArg_ParseTuple (args , "OO" , & container , & element ))
1473
1461
return NULL ;
1474
1462
1475
1463
NULLABLE (container );
1476
1464
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 ));
1482
1466
}
1483
1467
1484
1468
/* Test PyUnicode_IsIdentifier() */
1485
1469
static PyObject *
1486
1470
unicode_isidentifier (PyObject * self , PyObject * arg )
1487
1471
{
1488
- int result ;
1489
-
1490
1472
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 ));
1496
1474
}
1497
1475
1498
1476
/* Test PyUnicode_CopyCharacters() */
0 commit comments