Skip to content

Commit

Permalink
Minor refactoring of Object/abstract.c (UNARY_FUNC macro and more cas…
Browse files Browse the repository at this point in the history
…es for BINARY_FUNC) (pythonGH-112145)

* Use BINARY_FUNC macro for some remaining ops
* Add UNARY_FUNC macro to define unary PyNumber_* functions
  • Loading branch information
skirpichev authored Dec 5, 2023
1 parent aa5bee3 commit 9f92b31
Showing 1 changed file with 25 additions and 90 deletions.
115 changes: 25 additions & 90 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -1180,29 +1180,10 @@ PyNumber_Multiply(PyObject *v, PyObject *w)
return result;
}

PyObject *
PyNumber_MatrixMultiply(PyObject *v, PyObject *w)
{
return binary_op(v, w, NB_SLOT(nb_matrix_multiply), "@");
}

PyObject *
PyNumber_FloorDivide(PyObject *v, PyObject *w)
{
return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
}

PyObject *
PyNumber_TrueDivide(PyObject *v, PyObject *w)
{
return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
}

PyObject *
PyNumber_Remainder(PyObject *v, PyObject *w)
{
return binary_op(v, w, NB_SLOT(nb_remainder), "%");
}
BINARY_FUNC(PyNumber_MatrixMultiply, nb_matrix_multiply, "@")
BINARY_FUNC(PyNumber_FloorDivide, nb_floor_divide, "//")
BINARY_FUNC(PyNumber_TrueDivide, nb_true_divide, "/")
BINARY_FUNC(PyNumber_Remainder, nb_remainder, "%")

PyObject *
PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
Expand Down Expand Up @@ -1379,73 +1360,27 @@ _PyNumber_InPlacePowerNoMod(PyObject *lhs, PyObject *rhs)

/* Unary operators and functions */

PyObject *
PyNumber_Negative(PyObject *o)
{
if (o == NULL) {
return null_error();
}

PyNumberMethods *m = Py_TYPE(o)->tp_as_number;
if (m && m->nb_negative) {
PyObject *res = (*m->nb_negative)(o);
assert(_Py_CheckSlotResult(o, "__neg__", res != NULL));
return res;
}

return type_error("bad operand type for unary -: '%.200s'", o);
}

PyObject *
PyNumber_Positive(PyObject *o)
{
if (o == NULL) {
return null_error();
}

PyNumberMethods *m = Py_TYPE(o)->tp_as_number;
if (m && m->nb_positive) {
PyObject *res = (*m->nb_positive)(o);
assert(_Py_CheckSlotResult(o, "__pos__", res != NULL));
return res;
}

return type_error("bad operand type for unary +: '%.200s'", o);
}

PyObject *
PyNumber_Invert(PyObject *o)
{
if (o == NULL) {
return null_error();
}

PyNumberMethods *m = Py_TYPE(o)->tp_as_number;
if (m && m->nb_invert) {
PyObject *res = (*m->nb_invert)(o);
assert(_Py_CheckSlotResult(o, "__invert__", res != NULL));
return res;
}

return type_error("bad operand type for unary ~: '%.200s'", o);
}

PyObject *
PyNumber_Absolute(PyObject *o)
{
if (o == NULL) {
return null_error();
}

PyNumberMethods *m = Py_TYPE(o)->tp_as_number;
if (m && m->nb_absolute) {
PyObject *res = m->nb_absolute(o);
assert(_Py_CheckSlotResult(o, "__abs__", res != NULL));
return res;
}

return type_error("bad operand type for abs(): '%.200s'", o);
}
#define UNARY_FUNC(func, op, meth_name, descr) \
PyObject * \
func(PyObject *o) { \
if (o == NULL) { \
return null_error(); \
} \
\
PyNumberMethods *m = Py_TYPE(o)->tp_as_number; \
if (m && m->op) { \
PyObject *res = (*m->op)(o); \
assert(_Py_CheckSlotResult(o, #meth_name, res != NULL)); \
return res; \
} \
\
return type_error("bad operand type for "descr": '%.200s'", o); \
}

UNARY_FUNC(PyNumber_Negative, nb_negative, __neg__, "unary -")
UNARY_FUNC(PyNumber_Positive, nb_positive, __pow__, "unary +")
UNARY_FUNC(PyNumber_Invert, nb_invert, __invert__, "unary ~")
UNARY_FUNC(PyNumber_Absolute, nb_absolute, __abs__, "abs()")


int
Expand Down

0 comments on commit 9f92b31

Please sign in to comment.