|
24 | 24 | // qualified names for the conversion functions and the enum type. |
25 | 25 |
|
26 | 26 | //! str + int + enum form. |
27 | | -#define DUCKDB_PY_ENUM_STRING_INT_CASTER(EnumType, FromStringFn, FromIntegerFn, NameLiteral) \ |
| 27 | +#define DUCKDB_PY_ENUM_STRING_INT_CASTER(EnumType, FromStringFn, FromIntegerFn, NameLiteral) \ |
28 | 28 | namespace nanobind { \ |
29 | 29 | namespace detail { \ |
30 | 30 | template <> \ |
31 | 31 | struct type_caster<EnumType> { \ |
32 | 32 | NB_TYPE_CASTER(EnumType, const_name(NameLiteral)) \ |
33 | | - bool from_python(handle src, uint8_t, cleanup_list *) noexcept { \ |
34 | | - try { \ |
35 | | - if (nanobind::isinstance<nanobind::str>(src)) { \ |
36 | | - value = FromStringFn(nanobind::cast<std::string>(src)); \ |
37 | | - return true; \ |
38 | | - } \ |
39 | | - if (nanobind::isinstance<nanobind::int_>(src)) { \ |
40 | | - value = FromIntegerFn(nanobind::cast<int64_t>(src)); \ |
41 | | - return true; \ |
42 | | - } \ |
43 | | - /* Registered nb::enum_ instances aren't int subclasses (unlike pybind11's), so accept a member */ \ |
44 | | - /* of the registered enum by reading its integer .value. */ \ |
45 | | - nanobind::handle enum_type = nanobind::type<EnumType>(); \ |
46 | | - if (enum_type.is_valid() && PyObject_IsInstance(src.ptr(), enum_type.ptr()) == 1) { \ |
47 | | - value = FromIntegerFn(nanobind::cast<int64_t>(src.attr("value"))); \ |
48 | | - return true; \ |
49 | | - } \ |
50 | | - } catch (...) { \ |
51 | | - return false; \ |
52 | | - } \ |
53 | | - return false; \ |
54 | | - } \ |
55 | | - static handle from_cpp(EnumType src, rv_policy, cleanup_list *) noexcept { \ |
56 | | - return nanobind::int_((int64_t)src).release(); \ |
57 | | - } \ |
58 | | - }; \ |
59 | | - } /* namespace detail */ \ |
| 33 | + bool from_python(handle src, uint8_t, cleanup_list *) noexcept { \ |
| 34 | + try { \ |
| 35 | + if (nanobind::isinstance<nanobind::str>(src)) { \ |
| 36 | + value = FromStringFn(nanobind::cast<std::string>(src)); \ |
| 37 | + return true; \ |
| 38 | + } \ |
| 39 | + if (nanobind::isinstance<nanobind::int_>(src)) { \ |
| 40 | + value = FromIntegerFn(nanobind::cast<int64_t>(src)); \ |
| 41 | + return true; \ |
| 42 | + } \ |
| 43 | + /* Registered nb::enum_ instances aren't int subclasses (unlike pybind11's), so accept a member */ \ |
| 44 | + /* of the registered enum by reading its integer .value. */ \ |
| 45 | + nanobind::handle enum_type = nanobind::type<EnumType>(); \ |
| 46 | + if (enum_type.is_valid() && PyObject_IsInstance(src.ptr(), enum_type.ptr()) == 1) { \ |
| 47 | + value = FromIntegerFn(nanobind::cast<int64_t>(src.attr("value"))); \ |
| 48 | + return true; \ |
| 49 | + } \ |
| 50 | + } catch (...) { \ |
| 51 | + return false; \ |
| 52 | + } \ |
| 53 | + return false; \ |
| 54 | + } \ |
| 55 | + static handle from_cpp(EnumType src, rv_policy, cleanup_list *) noexcept { \ |
| 56 | + return nanobind::int_((int64_t)src).release(); \ |
| 57 | + } \ |
| 58 | + }; \ |
| 59 | + } /* namespace detail */ \ |
60 | 60 | } /* namespace nanobind */ |
61 | 61 |
|
62 | 62 | //! str + enum form (no integer accepted). |
63 | | -#define DUCKDB_PY_ENUM_STRING_CASTER(EnumType, FromStringFn, NameLiteral) \ |
| 63 | +#define DUCKDB_PY_ENUM_STRING_CASTER(EnumType, FromStringFn, NameLiteral) \ |
64 | 64 | namespace nanobind { \ |
65 | 65 | namespace detail { \ |
66 | 66 | template <> \ |
67 | 67 | struct type_caster<EnumType> { \ |
68 | 68 | NB_TYPE_CASTER(EnumType, const_name(NameLiteral)) \ |
69 | | - bool from_python(handle src, uint8_t, cleanup_list *) noexcept { \ |
70 | | - try { \ |
71 | | - if (nanobind::isinstance<nanobind::str>(src)) { \ |
72 | | - value = FromStringFn(nanobind::cast<std::string>(src)); \ |
73 | | - return true; \ |
74 | | - } \ |
75 | | - } catch (...) { \ |
76 | | - return false; \ |
77 | | - } \ |
78 | | - return false; \ |
79 | | - } \ |
80 | | - static handle from_cpp(EnumType src, rv_policy, cleanup_list *) noexcept { \ |
81 | | - return nanobind::int_((int64_t)src).release(); \ |
82 | | - } \ |
83 | | - }; \ |
84 | | - } /* namespace detail */ \ |
| 69 | + bool from_python(handle src, uint8_t, cleanup_list *) noexcept { \ |
| 70 | + try { \ |
| 71 | + if (nanobind::isinstance<nanobind::str>(src)) { \ |
| 72 | + value = FromStringFn(nanobind::cast<std::string>(src)); \ |
| 73 | + return true; \ |
| 74 | + } \ |
| 75 | + /* Registered nb::enum_ instances aren't int subclasses; accept a member of the registered enum */ \ |
| 76 | + /* by reading its integer .value (this enum has no FromInteger, so cast the int directly). */ \ |
| 77 | + nanobind::handle enum_type = nanobind::type<EnumType>(); \ |
| 78 | + if (enum_type.is_valid() && PyObject_IsInstance(src.ptr(), enum_type.ptr()) == 1) { \ |
| 79 | + value = (EnumType)nanobind::cast<int64_t>(src.attr("value")); \ |
| 80 | + return true; \ |
| 81 | + } \ |
| 82 | + } catch (...) { \ |
| 83 | + return false; \ |
| 84 | + } \ |
| 85 | + return false; \ |
| 86 | + } \ |
| 87 | + static handle from_cpp(EnumType src, rv_policy, cleanup_list *) noexcept { \ |
| 88 | + return nanobind::int_((int64_t)src).release(); \ |
| 89 | + } \ |
| 90 | + }; \ |
| 91 | + } /* namespace detail */ \ |
85 | 92 | } /* namespace nanobind */ |
0 commit comments