Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions python/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,35 @@ bool PyUpb_IsNumpyNdarray(PyObject* obj, const upb_FieldDef* f) {
return is_ndarray;
}

bool PyUpb_IsNumpyBoolScalar(PyObject* obj) {
PyObject* type_module_obj =
PyObject_GetAttrString((PyObject*)Py_TYPE(obj), "__module__");
bool is_numpy = !strcmp(PyUpb_GetStrData(type_module_obj), "numpy");
Py_DECREF(type_module_obj);
if (!is_numpy) {
return false;
}

PyObject* type_name_obj =
PyObject_GetAttrString((PyObject*)Py_TYPE(obj), "__name__");
bool is_bool = !strcmp(PyUpb_GetStrData(type_name_obj), "bool");
Py_DECREF(type_name_obj);
if (!is_bool) {
return false;
}
return true;
}

static bool PyUpb_GetBool(PyObject* obj, const upb_FieldDef* f, bool* val) {
if (PyUpb_IsNumpyNdarray(obj, f)) return false;
if (PyUpb_IsNumpyBoolScalar(obj)) {
*val = PyObject_IsTrue(obj);
return !PyErr_Occurred();
}
*val = PyLong_AsLong(obj);
return !PyErr_Occurred();
}

bool PyUpb_PyToUpb(PyObject* obj, const upb_FieldDef* f, upb_MessageValue* val,
upb_Arena* arena) {
switch (upb_FieldDef_CType(f)) {
Expand All @@ -230,9 +259,7 @@ bool PyUpb_PyToUpb(PyObject* obj, const upb_FieldDef* f, upb_MessageValue* val,
val->double_val = PyFloat_AsDouble(obj);
return !PyErr_Occurred();
case kUpb_CType_Bool:
if (PyUpb_IsNumpyNdarray(obj, f)) return false;
val->bool_val = PyLong_AsLong(obj);
return !PyErr_Occurred();
return PyUpb_GetBool(obj, f, &val->bool_val);
case kUpb_CType_Bytes: {
char* ptr;
Py_ssize_t size;
Expand Down
13 changes: 11 additions & 2 deletions python/google/protobuf/internal/type_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,21 @@ class BoolValueChecker(object):
"""Type checker used for bool fields."""

def CheckValue(self, proposed_value):
if not hasattr(proposed_value, '__index__') or (
type(proposed_value).__module__ == 'numpy' and
if not hasattr(proposed_value, '__index__'):
# Under NumPy 2.3, numpy.bool does not have an __index__ method.
if (type(proposed_value).__module__ == 'numpy' and
type(proposed_value).__name__ == 'bool'):
return bool(proposed_value)
message = ('%.1024r has type %s, but expected one of: %s' %
(proposed_value, type(proposed_value), (bool, int)))
raise TypeError(message)

if (type(proposed_value).__module__ == 'numpy' and
type(proposed_value).__name__ == 'ndarray'):
message = ('%.1024r has type %s, but expected one of: %s' %
(proposed_value, type(proposed_value), (bool, int)))
raise TypeError(message)

return bool(proposed_value)

def DefaultValue(self):
Expand Down
16 changes: 14 additions & 2 deletions python/google/protobuf/pyext/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,20 @@ bool CheckAndGetFloat(PyObject* arg, float* value) {

bool CheckAndGetBool(PyObject* arg, bool* value) {
long long_value = PyLong_AsLong(arg); // NOLINT
if (!strcmp(Py_TYPE(arg)->tp_name, "numpy.ndarray") ||
(long_value == -1 && PyErr_Occurred())) {
if (long_value == -1 && PyErr_Occurred()) {
// In NumPy 2.3, numpy.bool does not have an __index__ method and cannot
// be converted to a long using PyLong_AsLong.
if (!strcmp(Py_TYPE(arg)->tp_name, "numpy.bool")) {
PyErr_Clear();
int is_true = PyObject_IsTrue(arg);
if (is_true >= 0) {
*value = static_cast<bool>(is_true);
return true;
}
}
FormatTypeError(arg, "int, bool");
return false;
} else if (!strcmp(Py_TYPE(arg)->tp_name, "numpy.ndarray")) {
FormatTypeError(arg, "int, bool");
return false;
}
Expand Down
Loading