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

Function PyObjectToPyInt

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

Source from the content-addressed store, hash-verified

170}
171
172Result<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
196template <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