Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
const char *iname;
const char *name;
size_t islot;
size_t slot;
const char *iname;
size_t islot;
} nb_info;

#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[] = {
[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)
{
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