Skip to content

Commit 2675738

Browse files
committed
Add UNARY_FUNC macro to define unary PyNumber_* functions
1 parent 9f5b7a1 commit 2675738

File tree

1 file changed

+21
-67
lines changed

1 file changed

+21
-67
lines changed

Objects/abstract.c

+21-67
Original file line numberDiff line numberDiff line change
@@ -1344,73 +1344,27 @@ _PyNumber_InPlacePowerNoMod(PyObject *lhs, PyObject *rhs)
13441344

13451345
/* Unary operators and functions */
13461346

1347-
PyObject *
1348-
PyNumber_Negative(PyObject *o)
1349-
{
1350-
if (o == NULL) {
1351-
return null_error();
1352-
}
1353-
1354-
PyNumberMethods *m = Py_TYPE(o)->tp_as_number;
1355-
if (m && m->nb_negative) {
1356-
PyObject *res = (*m->nb_negative)(o);
1357-
assert(_Py_CheckSlotResult(o, "__neg__", res != NULL));
1358-
return res;
1359-
}
1360-
1361-
return type_error("bad operand type for unary -: '%.200s'", o);
1362-
}
1363-
1364-
PyObject *
1365-
PyNumber_Positive(PyObject *o)
1366-
{
1367-
if (o == NULL) {
1368-
return null_error();
1369-
}
1370-
1371-
PyNumberMethods *m = Py_TYPE(o)->tp_as_number;
1372-
if (m && m->nb_positive) {
1373-
PyObject *res = (*m->nb_positive)(o);
1374-
assert(_Py_CheckSlotResult(o, "__pos__", res != NULL));
1375-
return res;
1376-
}
1377-
1378-
return type_error("bad operand type for unary +: '%.200s'", o);
1379-
}
1380-
1381-
PyObject *
1382-
PyNumber_Invert(PyObject *o)
1383-
{
1384-
if (o == NULL) {
1385-
return null_error();
1386-
}
1387-
1388-
PyNumberMethods *m = Py_TYPE(o)->tp_as_number;
1389-
if (m && m->nb_invert) {
1390-
PyObject *res = (*m->nb_invert)(o);
1391-
assert(_Py_CheckSlotResult(o, "__invert__", res != NULL));
1392-
return res;
1393-
}
1394-
1395-
return type_error("bad operand type for unary ~: '%.200s'", o);
1396-
}
1397-
1398-
PyObject *
1399-
PyNumber_Absolute(PyObject *o)
1400-
{
1401-
if (o == NULL) {
1402-
return null_error();
1403-
}
1404-
1405-
PyNumberMethods *m = Py_TYPE(o)->tp_as_number;
1406-
if (m && m->nb_absolute) {
1407-
PyObject *res = m->nb_absolute(o);
1408-
assert(_Py_CheckSlotResult(o, "__abs__", res != NULL));
1409-
return res;
1410-
}
1411-
1412-
return type_error("bad operand type for abs(): '%.200s'", o);
1413-
}
1347+
#define UNARY_FUNC(func, op, meth_name) \
1348+
PyObject * \
1349+
func(PyObject *o) { \
1350+
if (o == NULL) { \
1351+
return null_error(); \
1352+
} \
1353+
\
1354+
PyNumberMethods *m = Py_TYPE(o)->tp_as_number; \
1355+
if (m && m->op) { \
1356+
PyObject *res = (*m->op)(o); \
1357+
assert(_Py_CheckSlotResult(o, #meth_name, res != NULL)); \
1358+
return res; \
1359+
} \
1360+
\
1361+
return type_error("bad operand type for unary -: '%.200s'", o); \
1362+
}
1363+
1364+
UNARY_FUNC(PyNumber_Negative, nb_negative, __neg__)
1365+
UNARY_FUNC(PyNumber_Positive, nb_positive, __pow__)
1366+
UNARY_FUNC(PyNumber_Invert, nb_invert, __invert__)
1367+
UNARY_FUNC(PyNumber_Absolute, nb_absolute, __abs__)
14141368

14151369

14161370
int

0 commit comments

Comments
 (0)