|
|
@ -79,45 +79,37 @@ PyObject* PyUpb_UpbToPy(upb_MessageValue val, const upb_FieldDef* f, |
|
|
|
static bool PyUpb_GetInt64(PyObject* obj, int64_t* val) { |
|
|
|
static bool PyUpb_GetInt64(PyObject* obj, int64_t* val) { |
|
|
|
// We require that the value is either an integer or has an __index__
|
|
|
|
// We require that the value is either an integer or has an __index__
|
|
|
|
// conversion.
|
|
|
|
// conversion.
|
|
|
|
if (!PyIndex_Check(obj)) { |
|
|
|
obj = PyNumber_Index(obj); |
|
|
|
PyErr_Format(PyExc_TypeError, "Expected integer: %S", obj); |
|
|
|
if (!obj) return false; |
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// If the value is already a Python long, PyLong_AsLongLong() retrieves it.
|
|
|
|
// If the value is already a Python long, PyLong_AsLongLong() retrieves it.
|
|
|
|
// Otherwise is converts to integer using __int__.
|
|
|
|
// Otherwise is converts to integer using __int__.
|
|
|
|
*val = PyLong_AsLongLong(obj); |
|
|
|
*val = PyLong_AsLongLong(obj); |
|
|
|
if (!PyErr_Occurred()) return true; |
|
|
|
bool ok = true; |
|
|
|
if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
|
|
|
if (PyErr_Occurred()) { |
|
|
|
// Rewrite OverflowError -> ValueError.
|
|
|
|
assert(PyErr_ExceptionMatches(PyExc_OverflowError)); |
|
|
|
// But don't rewrite other errors such as TypeError.
|
|
|
|
|
|
|
|
PyErr_Clear(); |
|
|
|
PyErr_Clear(); |
|
|
|
PyErr_Format(PyExc_ValueError, "Value out of range: %S", obj); |
|
|
|
PyErr_Format(PyExc_ValueError, "Value out of range: %S", obj); |
|
|
|
|
|
|
|
ok = false; |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
Py_DECREF(obj); |
|
|
|
|
|
|
|
return ok; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static bool PyUpb_GetUint64(PyObject* obj, uint64_t* val) { |
|
|
|
static bool PyUpb_GetUint64(PyObject* obj, uint64_t* val) { |
|
|
|
// We require that the value is either an integer or has an __index__
|
|
|
|
// We require that the value is either an integer or has an __index__
|
|
|
|
// conversion.
|
|
|
|
// conversion.
|
|
|
|
if (!PyIndex_Check(obj)) { |
|
|
|
obj = PyNumber_Index(obj); |
|
|
|
PyErr_Format(PyExc_TypeError, "Expected integer: %S", obj); |
|
|
|
if (!obj) return false; |
|
|
|
return false; |
|
|
|
*val = PyLong_AsUnsignedLongLong(obj); |
|
|
|
} |
|
|
|
bool ok = true; |
|
|
|
if (PyLong_Check(obj)) { |
|
|
|
if (PyErr_Occurred()) { |
|
|
|
*val = PyLong_AsUnsignedLongLong(obj); |
|
|
|
assert(PyErr_ExceptionMatches(PyExc_OverflowError)); |
|
|
|
} else if (PyIndex_Check(obj)) { |
|
|
|
PyErr_Clear(); |
|
|
|
PyObject* casted = PyNumber_Long(obj); |
|
|
|
PyErr_Format(PyExc_ValueError, "Value out of range: %S", obj); |
|
|
|
if (!casted) return false; |
|
|
|
ok = false; |
|
|
|
*val = PyLong_AsUnsignedLongLong(casted); |
|
|
|
|
|
|
|
Py_DECREF(casted); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
PyErr_Format(PyExc_TypeError, "Expected integer: %S", obj); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (!PyErr_Occurred()) return true; |
|
|
|
Py_DECREF(obj); |
|
|
|
PyErr_Clear(); |
|
|
|
return ok; |
|
|
|
PyErr_Format(PyExc_ValueError, "Value out of range: %S", obj); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static bool PyUpb_GetInt32(PyObject* obj, int32_t* val) { |
|
|
|
static bool PyUpb_GetInt32(PyObject* obj, int32_t* val) { |
|
|
|