@@ -1180,29 +1180,10 @@ PyNumber_Multiply(PyObject *v, PyObject *w)
1180
1180
return result ;
1181
1181
}
1182
1182
1183
- PyObject *
1184
- PyNumber_MatrixMultiply (PyObject * v , PyObject * w )
1185
- {
1186
- return binary_op (v , w , NB_SLOT (nb_matrix_multiply ), "@" );
1187
- }
1188
-
1189
- PyObject *
1190
- PyNumber_FloorDivide (PyObject * v , PyObject * w )
1191
- {
1192
- return binary_op (v , w , NB_SLOT (nb_floor_divide ), "//" );
1193
- }
1194
-
1195
- PyObject *
1196
- PyNumber_TrueDivide (PyObject * v , PyObject * w )
1197
- {
1198
- return binary_op (v , w , NB_SLOT (nb_true_divide ), "/" );
1199
- }
1200
-
1201
- PyObject *
1202
- PyNumber_Remainder (PyObject * v , PyObject * w )
1203
- {
1204
- return binary_op (v , w , NB_SLOT (nb_remainder ), "%" );
1205
- }
1183
+ BINARY_FUNC (PyNumber_MatrixMultiply , nb_matrix_multiply , "@" )
1184
+ BINARY_FUNC (PyNumber_FloorDivide , nb_floor_divide , "//" )
1185
+ BINARY_FUNC (PyNumber_TrueDivide , nb_true_divide , "/" )
1186
+ BINARY_FUNC (PyNumber_Remainder , nb_remainder , "%" )
1206
1187
1207
1188
PyObject *
1208
1189
PyNumber_Power (PyObject * v , PyObject * w , PyObject * z )
@@ -1379,73 +1360,27 @@ _PyNumber_InPlacePowerNoMod(PyObject *lhs, PyObject *rhs)
1379
1360
1380
1361
/* Unary operators and functions */
1381
1362
1382
- PyObject *
1383
- PyNumber_Negative (PyObject * o )
1384
- {
1385
- if (o == NULL ) {
1386
- return null_error ();
1387
- }
1388
-
1389
- PyNumberMethods * m = Py_TYPE (o )-> tp_as_number ;
1390
- if (m && m -> nb_negative ) {
1391
- PyObject * res = (* m -> nb_negative )(o );
1392
- assert (_Py_CheckSlotResult (o , "__neg__" , res != NULL ));
1393
- return res ;
1394
- }
1395
-
1396
- return type_error ("bad operand type for unary -: '%.200s'" , o );
1397
- }
1398
-
1399
- PyObject *
1400
- PyNumber_Positive (PyObject * o )
1401
- {
1402
- if (o == NULL ) {
1403
- return null_error ();
1404
- }
1405
-
1406
- PyNumberMethods * m = Py_TYPE (o )-> tp_as_number ;
1407
- if (m && m -> nb_positive ) {
1408
- PyObject * res = (* m -> nb_positive )(o );
1409
- assert (_Py_CheckSlotResult (o , "__pos__" , res != NULL ));
1410
- return res ;
1411
- }
1412
-
1413
- return type_error ("bad operand type for unary +: '%.200s'" , o );
1414
- }
1415
-
1416
- PyObject *
1417
- PyNumber_Invert (PyObject * o )
1418
- {
1419
- if (o == NULL ) {
1420
- return null_error ();
1421
- }
1422
-
1423
- PyNumberMethods * m = Py_TYPE (o )-> tp_as_number ;
1424
- if (m && m -> nb_invert ) {
1425
- PyObject * res = (* m -> nb_invert )(o );
1426
- assert (_Py_CheckSlotResult (o , "__invert__" , res != NULL ));
1427
- return res ;
1428
- }
1429
-
1430
- return type_error ("bad operand type for unary ~: '%.200s'" , o );
1431
- }
1432
-
1433
- PyObject *
1434
- PyNumber_Absolute (PyObject * o )
1435
- {
1436
- if (o == NULL ) {
1437
- return null_error ();
1438
- }
1439
-
1440
- PyNumberMethods * m = Py_TYPE (o )-> tp_as_number ;
1441
- if (m && m -> nb_absolute ) {
1442
- PyObject * res = m -> nb_absolute (o );
1443
- assert (_Py_CheckSlotResult (o , "__abs__" , res != NULL ));
1444
- return res ;
1445
- }
1446
-
1447
- return type_error ("bad operand type for abs(): '%.200s'" , o );
1448
- }
1363
+ #define UNARY_FUNC (func , op , meth_name , descr ) \
1364
+ PyObject * \
1365
+ func(PyObject *o) { \
1366
+ if (o == NULL) { \
1367
+ return null_error(); \
1368
+ } \
1369
+ \
1370
+ PyNumberMethods *m = Py_TYPE(o)->tp_as_number; \
1371
+ if (m && m->op) { \
1372
+ PyObject *res = (*m->op)(o); \
1373
+ assert(_Py_CheckSlotResult(o, #meth_name, res != NULL)); \
1374
+ return res; \
1375
+ } \
1376
+ \
1377
+ return type_error("bad operand type for "descr": '%.200s'", o); \
1378
+ }
1379
+
1380
+ UNARY_FUNC (PyNumber_Negative , nb_negative , __neg__ , "unary -" )
1381
+ UNARY_FUNC (PyNumber_Positive , nb_positive , __pow__ , "unary +" )
1382
+ UNARY_FUNC (PyNumber_Invert , nb_invert , __invert__ , "unary ~" )
1383
+ UNARY_FUNC (PyNumber_Absolute , nb_absolute , __abs__ , "abs()" )
1449
1384
1450
1385
1451
1386
int
0 commit comments