Skip to content

Commit

Permalink
Merge pull request #3242 from Starbuck5/remove-unnecessary-num-calls-…
Browse files Browse the repository at this point in the history
…and-checks

Remove unnecessary calls to PyNumber_Index, PyLong_Checks
  • Loading branch information
ankith26 authored Nov 27, 2024
2 parents e3255f0 + b7eed5f commit 2913d6f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 29 deletions.
20 changes: 4 additions & 16 deletions src_c/pixelarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -1666,15 +1666,9 @@ _pxarray_subscript(pgPixelArrayObject *array, PyObject *op)
return _pxarray_subscript_internal(array, start, stop, step, 0, dim1,
1);
}
else if (PyIndex_Check(op) || PyLong_Check(op)) {
Py_ssize_t i;
PyObject *val = PyNumber_Index(op);
if (!val) {
return 0;
}
else if (PyIndex_Check(op)) {
/* A simple index. */
i = PyNumber_AsSsize_t(val, PyExc_IndexError);
Py_DECREF(val);
Py_ssize_t i = PyNumber_AsSsize_t(op, PyExc_IndexError);
if (i == -1 && PyErr_Occurred()) {
return 0;
}
Expand Down Expand Up @@ -1828,15 +1822,9 @@ _pxarray_ass_subscript(pgPixelArrayObject *array, PyObject *op,
Py_DECREF(tmparray);
return retval;
}
else if (PyIndex_Check(op) || PyLong_Check(op)) {
Py_ssize_t i;
PyObject *val = PyNumber_Index(op);
if (!val) {
return -1;
}
else if (PyIndex_Check(op)) {
/* A simple index. */
i = PyNumber_AsSsize_t(val, PyExc_IndexError);
Py_DECREF(val);
Py_ssize_t i = PyNumber_AsSsize_t(op, PyExc_IndexError);
if (i == -1 && PyErr_Occurred()) {
return -1;
}
Expand Down
17 changes: 4 additions & 13 deletions src_c/rect_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2160,14 +2160,10 @@ RectExport_subscript(RectObject *self, PyObject *op)
PrimitiveType *data = (PrimitiveType *)&self->r;

if (PyIndex_Check(op)) {
PyObject *index = PyNumber_Index(op);
Py_ssize_t i;

if (index == NULL) {
Py_ssize_t i = PyNumber_AsSsize_t(op, NULL);
if (i == -1 && PyErr_Occurred()) {
return NULL;
}
i = PyNumber_AsSsize_t(index, NULL);
Py_DECREF(index);
return RectExport_item(self, i);
}
else if (op == Py_Ellipsis) {
Expand Down Expand Up @@ -2213,15 +2209,10 @@ RectExport_assSubscript(RectObject *self, PyObject *op, PyObject *value)
return -1;
}
if (PyIndex_Check(op)) {
PyObject *index;
Py_ssize_t i;

index = PyNumber_Index(op);
if (index == NULL) {
Py_ssize_t i = PyNumber_AsSsize_t(op, NULL);
if (i == -1 && PyErr_Occurred()) {
return -1;
}
i = PyNumber_AsSsize_t(index, NULL);
Py_DECREF(index);
return RectExport_assItem(self, i, value);
}
else if (op == Py_Ellipsis) {
Expand Down

0 comments on commit 2913d6f

Please sign in to comment.