Skip to content

Commit cb60ed4

Browse files
VigilansSkylion007
andauthored
Fix enum value's __int__ returning non-int when underlying type is bool or of char type (#1334)
* Use equivalent_integer for enum's Scalar decision * Add test for char underlying enum * Support translating bool type in enum's Scalar * Add test for bool underlying enum * Fix comment in test * Switch from `PYBIND11_CPP20` macro to `PYBIND11_HAS_U8STRING` * Refine tests Co-authored-by: Aaron Gokaslan <[email protected]>
1 parent 930bb16 commit cb60ed4

File tree

3 files changed

+108
-2
lines changed

3 files changed

+108
-2
lines changed

include/pybind11/pybind11.h

+19-2
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,19 @@ struct enum_base {
18141814
handle m_parent;
18151815
};
18161816

1817+
template <bool is_signed, size_t length> struct equivalent_integer {};
1818+
template <> struct equivalent_integer<true, 1> { using type = int8_t; };
1819+
template <> struct equivalent_integer<false, 1> { using type = uint8_t; };
1820+
template <> struct equivalent_integer<true, 2> { using type = int16_t; };
1821+
template <> struct equivalent_integer<false, 2> { using type = uint16_t; };
1822+
template <> struct equivalent_integer<true, 4> { using type = int32_t; };
1823+
template <> struct equivalent_integer<false, 4> { using type = uint32_t; };
1824+
template <> struct equivalent_integer<true, 8> { using type = int64_t; };
1825+
template <> struct equivalent_integer<false, 8> { using type = uint64_t; };
1826+
1827+
template <typename IntLike>
1828+
using equivalent_integer_t = typename equivalent_integer<std::is_signed<IntLike>::value, sizeof(IntLike)>::type;
1829+
18171830
PYBIND11_NAMESPACE_END(detail)
18181831

18191832
/// Binds C++ enumerations and enumeration classes to Python
@@ -1824,13 +1837,17 @@ template <typename Type> class enum_ : public class_<Type> {
18241837
using Base::attr;
18251838
using Base::def_property_readonly;
18261839
using Base::def_property_readonly_static;
1827-
using Scalar = typename std::underlying_type<Type>::type;
1840+
using Underlying = typename std::underlying_type<Type>::type;
1841+
// Scalar is the integer representation of underlying type
1842+
using Scalar = detail::conditional_t<detail::any_of<
1843+
detail::is_std_char_type<Underlying>, std::is_same<Underlying, bool>
1844+
>::value, detail::equivalent_integer_t<Underlying>, Underlying>;
18281845

18291846
template <typename... Extra>
18301847
enum_(const handle &scope, const char *name, const Extra&... extra)
18311848
: class_<Type>(scope, name, extra...), m_base(*this, scope) {
18321849
constexpr bool is_arithmetic = detail::any_of<std::is_same<arithmetic, Extra>...>::value;
1833-
constexpr bool is_convertible = std::is_convertible<Type, Scalar>::value;
1850+
constexpr bool is_convertible = std::is_convertible<Type, Underlying>::value;
18341851
m_base.init(is_arithmetic, is_convertible);
18351852

18361853
def(init([](Scalar i) { return static_cast<Type>(i); }), arg("value"));

tests/test_enum.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,65 @@ TEST_SUBMODULE(enums, m) {
8484
.value("ONE", SimpleEnum::THREE)
8585
.export_values();
8686
});
87+
88+
// test_enum_scalar
89+
enum UnscopedUCharEnum : unsigned char {};
90+
enum class ScopedShortEnum : short {};
91+
enum class ScopedLongEnum : long {};
92+
enum UnscopedUInt64Enum : std::uint64_t {};
93+
static_assert(py::detail::all_of<
94+
std::is_same<py::enum_<UnscopedUCharEnum>::Scalar, unsigned char>,
95+
std::is_same<py::enum_<ScopedShortEnum>::Scalar, short>,
96+
std::is_same<py::enum_<ScopedLongEnum>::Scalar, long>,
97+
std::is_same<py::enum_<UnscopedUInt64Enum>::Scalar, std::uint64_t>
98+
>::value, "Error during the deduction of enum's scalar type with normal integer underlying");
99+
100+
// test_enum_scalar_with_char_underlying
101+
enum class ScopedCharEnum : char { Zero, Positive };
102+
enum class ScopedWCharEnum : wchar_t { Zero, Positive };
103+
enum class ScopedChar32Enum : char32_t { Zero, Positive };
104+
enum class ScopedChar16Enum : char16_t { Zero, Positive };
105+
106+
// test the scalar of char type enums according to chapter 'Character types'
107+
// from https://en.cppreference.com/w/cpp/language/types
108+
static_assert(py::detail::any_of<
109+
std::is_same<py::enum_<ScopedCharEnum>::Scalar, signed char>, // e.g. gcc on x86
110+
std::is_same<py::enum_<ScopedCharEnum>::Scalar, unsigned char> // e.g. arm linux
111+
>::value, "char should be cast to either signed char or unsigned char");
112+
static_assert(
113+
sizeof(py::enum_<ScopedWCharEnum>::Scalar) == 2 ||
114+
sizeof(py::enum_<ScopedWCharEnum>::Scalar) == 4
115+
, "wchar_t should be either 16 bits (Windows) or 32 (everywhere else)");
116+
static_assert(py::detail::all_of<
117+
std::is_same<py::enum_<ScopedChar32Enum>::Scalar, std::uint_least32_t>,
118+
std::is_same<py::enum_<ScopedChar16Enum>::Scalar, std::uint_least16_t>
119+
>::value, "char32_t, char16_t (and char8_t)'s size, signedness, and alignment is determined");
120+
#if defined(PYBIND11_HAS_U8STRING)
121+
enum class ScopedChar8Enum : char8_t { Zero, Positive };
122+
static_assert(std::is_same<py::enum_<ScopedChar8Enum>::Scalar, unsigned char>::value);
123+
#endif
124+
125+
// test_char_underlying_enum
126+
py::enum_<ScopedCharEnum>(m, "ScopedCharEnum")
127+
.value("Zero", ScopedCharEnum::Zero)
128+
.value("Positive", ScopedCharEnum::Positive);
129+
py::enum_<ScopedWCharEnum>(m, "ScopedWCharEnum")
130+
.value("Zero", ScopedWCharEnum::Zero)
131+
.value("Positive", ScopedWCharEnum::Positive);
132+
py::enum_<ScopedChar32Enum>(m, "ScopedChar32Enum")
133+
.value("Zero", ScopedChar32Enum::Zero)
134+
.value("Positive", ScopedChar32Enum::Positive);
135+
py::enum_<ScopedChar16Enum>(m, "ScopedChar16Enum")
136+
.value("Zero", ScopedChar16Enum::Zero)
137+
.value("Positive", ScopedChar16Enum::Positive);
138+
139+
// test_bool_underlying_enum
140+
enum class ScopedBoolEnum : bool { FALSE, TRUE };
141+
142+
// bool is unsigned (std::is_signed returns false) and 1-byte long, so represented with u8
143+
static_assert(std::is_same<py::enum_<ScopedBoolEnum>::Scalar, std::uint8_t>::value, "");
144+
145+
py::enum_<ScopedBoolEnum>(m, "ScopedBoolEnum")
146+
.value("FALSE", ScopedBoolEnum::FALSE)
147+
.value("TRUE", ScopedBoolEnum::TRUE);
87148
}

tests/test_enum.py

+28
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,16 @@ def test_binary_operators():
218218
def test_enum_to_int():
219219
m.test_enum_to_int(m.Flags.Read)
220220
m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode)
221+
m.test_enum_to_int(m.ScopedCharEnum.Positive)
222+
m.test_enum_to_int(m.ScopedBoolEnum.TRUE)
221223
m.test_enum_to_uint(m.Flags.Read)
222224
m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode)
225+
m.test_enum_to_uint(m.ScopedCharEnum.Positive)
226+
m.test_enum_to_uint(m.ScopedBoolEnum.TRUE)
223227
m.test_enum_to_long_long(m.Flags.Read)
224228
m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode)
229+
m.test_enum_to_long_long(m.ScopedCharEnum.Positive)
230+
m.test_enum_to_long_long(m.ScopedBoolEnum.TRUE)
225231

226232

227233
def test_duplicate_enum_name():
@@ -230,6 +236,28 @@ def test_duplicate_enum_name():
230236
assert str(excinfo.value) == 'SimpleEnum: element "ONE" already exists!'
231237

232238

239+
def test_char_underlying_enum(): # Issue #1331/PR #1334:
240+
assert type(m.ScopedCharEnum.Positive.__int__()) is int
241+
assert int(m.ScopedChar16Enum.Zero) == 0 # int() call should successfully return
242+
assert hash(m.ScopedChar32Enum.Positive) == 1
243+
assert m.ScopedCharEnum.Positive.__getstate__() == 1 # return type is long in py2.x
244+
assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive
245+
with pytest.raises(TypeError):
246+
# Enum should construct with a int, even with char underlying type
247+
m.ScopedWCharEnum("0")
248+
249+
250+
def test_bool_underlying_enum():
251+
assert type(m.ScopedBoolEnum.TRUE.__int__()) is int
252+
assert int(m.ScopedBoolEnum.FALSE) == 0
253+
assert hash(m.ScopedBoolEnum.TRUE) == 1
254+
assert m.ScopedBoolEnum.TRUE.__getstate__() == 1
255+
assert m.ScopedBoolEnum(1) == m.ScopedBoolEnum.TRUE
256+
# Enum could construct with a bool
257+
# (bool is a strict subclass of int, and False will be converted to 0)
258+
assert m.ScopedBoolEnum(False) == m.ScopedBoolEnum.FALSE
259+
260+
233261
def test_docstring_signatures():
234262
for enum_type in [m.ScopedEnum, m.UnscopedEnum]:
235263
for attr in enum_type.__dict__.values():

0 commit comments

Comments
 (0)