Skip to content

Commit 9f92b31

Browse files
authored
Minor refactoring of Object/abstract.c (UNARY_FUNC macro and more cases for BINARY_FUNC) (GH-112145)
* Use BINARY_FUNC macro for some remaining ops * Add UNARY_FUNC macro to define unary PyNumber_* functions
1 parent aa5bee3 commit 9f92b31

File tree

1 file changed

+25
-90
lines changed

1 file changed

+25
-90
lines changed

Objects/abstract.c

Lines changed: 25 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,29 +1180,10 @@ PyNumber_Multiply(PyObject *v, PyObject *w)
11801180
return result;
11811181
}
11821182

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, "%")
12061187

12071188
PyObject *
12081189
PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
@@ -1379,73 +1360,27 @@ _PyNumber_InPlacePowerNoMod(PyObject *lhs, PyObject *rhs)
13791360

13801361
/* Unary operators and functions */
13811362

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()")
14491384

14501385

14511386
int

0 commit comments

Comments
 (0)