Skip to content

Instantly share code, notes, and snippets.

@killeent
Created June 2, 2017 20:33
Show Gist options
  • Save killeent/4c7b49017dfabffb976e36b1f4138c32 to your computer and use it in GitHub Desktop.
Save killeent/4c7b49017dfabffb976e36b1f4138c32 to your computer and use it in GitHub Desktop.
// Case 1: arg is a non-tuple sequence object
if (PySequence_Check(arg) && !PyTuple_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, ndarray, or LongTensor
if (PyTuple_Check(arg)) {
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(arg); ++i) {
PyObject *item = PyTuple_GET_ITEM(arg, i);
if (PySequence_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
if (THPIndexTensor_Check(item)) return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment