summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergey B Kirpichev <skirpichev@gmail.com>2023-12-05 07:34:13 (GMT)
committerGitHub <noreply@github.com>2023-12-05 07:34:13 (GMT)
commit9f92b31339945da55559747c420e170c968e9e2b (patch)
tree8d00987768fd5fb0c5801bd78cb7e368d7baa133
parentaa5bee30abb28d73a838399f4c3a8bcdc5108fe3 (diff)
downloadcpython-9f92b31339945da55559747c420e170c968e9e2b.zip
cpython-9f92b31339945da55559747c420e170c968e9e2b.tar.gz
cpython-9f92b31339945da55559747c420e170c968e9e2b.tar.bz2
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
-rw-r--r--Objects/abstract.c115
1 files changed, 25 insertions, 90 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 43842fb..1ec5c5b 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -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)
@@ -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