MCPcopy Create free account
hub / github.com/apache/arrow / PyObjectToPyInt

Function PyObjectToPyInt

python/pyarrow/src/arrow/python/helpers.cc:171–192  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

169}
170
171Result<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
195template <typename Int, enable_if_t<std::is_signed<Int>::value, Int> = 0>

Callers 1

CIntFromPythonImplFunction · 0.85

Calls 3

PyObject_StdStringReprFunction · 0.85
TypeErrorFunction · 0.50
resetMethod · 0.45

Tested by

no test coverage detected