Skip to content
Prev Previous commit
Next Next commit
Improve the new _PyNumber_*Op implementations
  • Loading branch information
brandtbucher committed Oct 27, 2021
commit a2df29ae881bb1acc0c9196ed29187644e02cad0
4 changes: 2 additions & 2 deletions Include/abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,9 @@ PyAPI_FUNC(PyObject *) PyNumber_InPlaceOr(PyObject *o1, PyObject *o2);
If n is not an int object, it is converted with PyNumber_Index first. */
PyAPI_FUNC(PyObject *) PyNumber_ToBase(PyObject *n, int base);

PyAPI_FUNC(PyObject *) _PyNumber_Op(PyObject *o1, PyObject *o2, int op);
PyAPI_FUNC(PyObject *) _PyNumber_Op(PyObject *o1, PyObject *o2, unsigned op);

PyAPI_FUNC(PyObject *) _PyNumber_InPlaceOp(PyObject *o1, PyObject *o2, int op);
PyAPI_FUNC(PyObject *) _PyNumber_InPlaceOp(PyObject *o1, PyObject *o2, unsigned op);


/* === Sequence protocol ================================================ */
Expand Down
12 changes: 7 additions & 5 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -1716,14 +1716,14 @@ PyNumber_ToBase(PyObject *n, int base)
}

typedef struct {
Comment thread
brandtbucher marked this conversation as resolved.
Outdated
const char *iname;
const char *name;
size_t islot;
size_t slot;
const char *iname;
size_t islot;
} nb_info;
Comment thread
brandtbucher marked this conversation as resolved.
Outdated

#define NB_INFO(name, slot) \
{name "=", name, NB_SLOT(nb_inplace_##slot), NB_SLOT(nb_##slot)}
{name, NB_SLOT(nb_##slot), name "=", NB_SLOT(nb_inplace_##slot)}

static nb_info nb_infos[] = {
Comment thread
brandtbucher marked this conversation as resolved.
Outdated
[NB_AND] = NB_INFO("&", and),
Expand All @@ -1740,15 +1740,17 @@ static nb_info nb_infos[] = {
#undef NB_INFO

PyObject *
_PyNumber_Op(PyObject *o1, PyObject *o2, int op)
_PyNumber_Op(PyObject *o1, PyObject *o2, unsigned op)
Comment thread
brandtbucher marked this conversation as resolved.
Outdated
{
assert(op < sizeof(nb_infos) / sizeof(nb_info));
nb_info *ni = &nb_infos[op];
return binary_op(o1, o2, ni->slot, ni->name);
}

PyObject *
_PyNumber_InPlaceOp(PyObject *o1, PyObject *o2, int op)
_PyNumber_InPlaceOp(PyObject *o1, PyObject *o2, unsigned op)
{
assert(op < sizeof(nb_infos) / sizeof(nb_info));
nb_info *ni = &nb_infos[op];
return binary_iop(o1, o2, ni->islot, ni->slot, ni->iname);
}
Expand Down