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.
This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-08-31 08:52:04 -07:00 committed by GitHub
parent a46f623782
commit 49173e472f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import pytest import pytest
import env
from pybind11_tests import enums as m from pybind11_tests import enums as m
@ -238,20 +239,26 @@ def test_duplicate_enum_name():
def test_char_underlying_enum(): # Issue #1331/PR #1334: def test_char_underlying_enum(): # Issue #1331/PR #1334:
assert type(m.ScopedCharEnum.Positive.__int__()) is int assert type(m.ScopedCharEnum.Positive.__int__()) is int
assert int(m.ScopedChar16Enum.Zero) == 0 # int() call should successfully return assert int(m.ScopedChar16Enum.Zero) == 0
assert hash(m.ScopedChar32Enum.Positive) == 1 assert hash(m.ScopedChar32Enum.Positive) == 1
assert m.ScopedCharEnum.Positive.__getstate__() == 1 # return type is long in py2.x if env.PY2:
assert m.ScopedCharEnum.Positive.__getstate__() == 1 # long
else:
assert type(m.ScopedCharEnum.Positive.__getstate__()) is int
assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive
with pytest.raises(TypeError): with pytest.raises(TypeError):
# Enum should construct with a int, even with char underlying type # Even if the underlying type is char, only an int can be used to construct the enum:
m.ScopedWCharEnum("0") m.ScopedCharEnum("0")
def test_bool_underlying_enum(): def test_bool_underlying_enum():
assert type(m.ScopedBoolEnum.TRUE.__int__()) is int assert type(m.ScopedBoolEnum.TRUE.__int__()) is int
assert int(m.ScopedBoolEnum.FALSE) == 0 assert int(m.ScopedBoolEnum.FALSE) == 0
assert hash(m.ScopedBoolEnum.TRUE) == 1 assert hash(m.ScopedBoolEnum.TRUE) == 1
assert m.ScopedBoolEnum.TRUE.__getstate__() == 1 if env.PY2:
assert m.ScopedBoolEnum.TRUE.__getstate__() == 1 # long
else:
assert type(m.ScopedBoolEnum.TRUE.__getstate__()) is int
assert m.ScopedBoolEnum(1) == m.ScopedBoolEnum.TRUE assert m.ScopedBoolEnum(1) == m.ScopedBoolEnum.TRUE
# Enum could construct with a bool # Enum could construct with a bool
# (bool is a strict subclass of int, and False will be converted to 0) # (bool is a strict subclass of int, and False will be converted to 0)