Skip to content

Commit 49173e4

Browse files
authored
Minor follow-on to PR #1334 (Fix enum value's __int__ returning non-int when underlying type is bool or of char type) (#3232)
* Minor tweaks. * Restoring tests/pybind11_tests.h version from master, removing just the comment and empty line that was added in PR #3087; those were made obsolete by the pragma cleanup that concluded with PR #3186. * More-to-the-point test for Python 3.
1 parent a46f623 commit 49173e4

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

tests/test_enum.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
import pytest
33

4+
import env
45
from pybind11_tests import enums as m
56

67

@@ -238,20 +239,26 @@ def test_duplicate_enum_name():
238239

239240
def test_char_underlying_enum(): # Issue #1331/PR #1334:
240241
assert type(m.ScopedCharEnum.Positive.__int__()) is int
241-
assert int(m.ScopedChar16Enum.Zero) == 0 # int() call should successfully return
242+
assert int(m.ScopedChar16Enum.Zero) == 0
242243
assert hash(m.ScopedChar32Enum.Positive) == 1
243-
assert m.ScopedCharEnum.Positive.__getstate__() == 1 # return type is long in py2.x
244+
if env.PY2:
245+
assert m.ScopedCharEnum.Positive.__getstate__() == 1 # long
246+
else:
247+
assert type(m.ScopedCharEnum.Positive.__getstate__()) is int
244248
assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive
245249
with pytest.raises(TypeError):
246-
# Enum should construct with a int, even with char underlying type
247-
m.ScopedWCharEnum("0")
250+
# Even if the underlying type is char, only an int can be used to construct the enum:
251+
m.ScopedCharEnum("0")
248252

249253

250254
def test_bool_underlying_enum():
251255
assert type(m.ScopedBoolEnum.TRUE.__int__()) is int
252256
assert int(m.ScopedBoolEnum.FALSE) == 0
253257
assert hash(m.ScopedBoolEnum.TRUE) == 1
254-
assert m.ScopedBoolEnum.TRUE.__getstate__() == 1
258+
if env.PY2:
259+
assert m.ScopedBoolEnum.TRUE.__getstate__() == 1 # long
260+
else:
261+
assert type(m.ScopedBoolEnum.TRUE.__getstate__()) is int
255262
assert m.ScopedBoolEnum(1) == m.ScopedBoolEnum.TRUE
256263
# Enum could construct with a bool
257264
# (bool is a strict subclass of int, and False will be converted to 0)

0 commit comments

Comments
 (0)