mirror of https://github.com/python/cpython
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
This commit is contained in:
parent
aa5bee30ab
commit
9f92b31339
|
@ -1180,29 +1180,10 @@ PyNumber_Multiply(PyObject *v, PyObject *w)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *
|
BINARY_FUNC(PyNumber_MatrixMultiply, nb_matrix_multiply, "@")
|
||||||
PyNumber_MatrixMultiply(PyObject *v, PyObject *w)
|
BINARY_FUNC(PyNumber_FloorDivide, nb_floor_divide, "//")
|
||||||
{
|
BINARY_FUNC(PyNumber_TrueDivide, nb_true_divide, "/")
|
||||||
return binary_op(v, w, NB_SLOT(nb_matrix_multiply), "@");
|
BINARY_FUNC(PyNumber_Remainder, nb_remainder, "%")
|
||||||
}
|
|
||||||
|
|
||||||
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), "%");
|
|
||||||
}
|
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
|
PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
|
||||||
|
@ -1379,73 +1360,27 @@ _PyNumber_InPlacePowerNoMod(PyObject *lhs, PyObject *rhs)
|
||||||
|
|
||||||
/* Unary operators and functions */
|
/* Unary operators and functions */
|
||||||
|
|
||||||
PyObject *
|
#define UNARY_FUNC(func, op, meth_name, descr) \
|
||||||
PyNumber_Negative(PyObject *o)
|
PyObject * \
|
||||||
{
|
func(PyObject *o) { \
|
||||||
if (o == NULL) {
|
if (o == NULL) { \
|
||||||
return null_error();
|
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); \
|
||||||
}
|
}
|
||||||
|
|
||||||
PyNumberMethods *m = Py_TYPE(o)->tp_as_number;
|
UNARY_FUNC(PyNumber_Negative, nb_negative, __neg__, "unary -")
|
||||||
if (m && m->nb_negative) {
|
UNARY_FUNC(PyNumber_Positive, nb_positive, __pow__, "unary +")
|
||||||
PyObject *res = (*m->nb_negative)(o);
|
UNARY_FUNC(PyNumber_Invert, nb_invert, __invert__, "unary ~")
|
||||||
assert(_Py_CheckSlotResult(o, "__neg__", res != NULL));
|
UNARY_FUNC(PyNumber_Absolute, nb_absolute, __abs__, "abs()")
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
Loading…
Reference in New Issue