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