Skip to content

Instantly share code, notes, and snippets.

@killeent
Created April 26, 2017 18:25
Show Gist options
  • Save killeent/b579de22079bedd5f70ca861d6ae326f to your computer and use it in GitHub Desktop.
Save killeent/b579de22079bedd5f70ca861d6ae326f to your computer and use it in GitHub Desktop.
bool THPUtils_checkAdvancedIndexing(PyObject *arg) {
// Checks whether the specified selection object should trigger advanced
// indexing
// Case 1: arg is a non-tuple sequence object
if (PyList_Check(arg) || PyRange_Check(arg)) return true;
#ifdef WITH_NUMPY
// Case 2: arg is an nd-array with type integer or bool
if (PyArray_Check(arg) && (PyArray_TYPE((PyArrayObject*)arg) == NPY_INT64 || PyArray_TYPE((PyArrayObject*)arg) == NPY_BOOL)) return true;
#endif
// Case 3: arg is a tuple containing at least one sequence object or ndarray
if (PyTuple_Check(arg)) {
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(arg); ++i) {
PyObject *item = PyTuple_GET_ITEM(arg, i);
if (PyList_Check(item) || PyRange_Check(item) || PyTuple_Check(item)) {
return true;
}
#ifdef WITH_NUMPY
if (PyArray_Check(item) && (PyArray_TYPE((PyArrayObject*)item) == NPY_INT64 || PyArray_TYPE((PyArrayObject*)item) == NPY_BOOL)) return true;
#endif
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment