| 169 | } |
| 170 | |
| 171 | Result<OwnedRef> PyObjectToPyInt(PyObject* obj) { |
| 172 | // Try to call __index__ or __int__ on `obj` |
| 173 | // (starting from Python 3.10, the latter isn't done anymore by PyLong_AsLong*). |
| 174 | OwnedRef ref(PyNumber_Index(obj)); |
| 175 | if (ref) { |
| 176 | return std::move(ref); |
| 177 | } |
| 178 | PyErr_Clear(); |
| 179 | const auto nb = Py_TYPE(obj)->tp_as_number; |
| 180 | if (nb && nb->nb_int) { |
| 181 | ref.reset(nb->nb_int(obj)); |
| 182 | if (!ref) { |
| 183 | RETURN_IF_PYERROR(); |
| 184 | } |
| 185 | ARROW_DCHECK(ref); |
| 186 | return std::move(ref); |
| 187 | } |
| 188 | return Status::TypeError( |
| 189 | "object of type ", |
| 190 | PyObject_StdStringRepr(reinterpret_cast<PyObject*>(Py_TYPE(obj))), |
| 191 | " cannot be converted to int"); |
| 192 | } |
| 193 | |
| 194 | // Extract C signed int from Python object |
| 195 | template <typename Int, enable_if_t<std::is_signed<Int>::value, Int> = 0> |
no test coverage detected