From d5483392e2d60e0c5523577a44f4a9796c31fd39 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sun, 29 Aug 2021 15:48:56 -0700 Subject: [PATCH 1/3] Minor tweaks. --- tests/pybind11_tests.h | 29 ++++++++++++++++------------- tests/test_enum.py | 8 ++++---- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/tests/pybind11_tests.h b/tests/pybind11_tests.h index 12d8a777fc..8cbfa650e9 100644 --- a/tests/pybind11_tests.h +++ b/tests/pybind11_tests.h @@ -1,10 +1,7 @@ #pragma once -// This must be kept first for MSVC 2015. -// Do not remove the empty line between the #includes. -#include - #include +#include #if defined(_MSC_VER) && _MSC_VER < 1910 // We get some really long type names here which causes MSVC 2015 to emit warnings @@ -29,13 +26,13 @@ class test_initializer { void test_submodule_##name(py::module_ &(variable)) /// Dummy type which is not exported anywhere -- something to trigger a conversion error -struct UnregisteredType { }; +struct UnregisteredType {}; /// A user-defined type which is exported and can be used by any test class UserType { public: UserType() = default; - UserType(int i) : i(i) { } + UserType(int i) : i(i) {} int value() const { return i; } void set(int set) { i = set; } @@ -49,7 +46,7 @@ class IncType : public UserType { public: using UserType::UserType; IncType() = default; - IncType(const IncType &other) : IncType(other.value() + 1) { } + IncType(const IncType &other) : IncType(other.value() + 1) {} IncType(IncType &&) = delete; IncType &operator=(const IncType &) = delete; IncType &operator=(IncType &&) = delete; @@ -61,16 +58,21 @@ union IntFloat { float f; }; -/// Custom cast-only type that casts to a string "rvalue" or "lvalue" depending on the cast context. -/// Used to test recursive casters (e.g. std::tuple, stl containers). +/// Custom cast-only type that casts to a string "rvalue" or "lvalue" depending on the cast +/// context. Used to test recursive casters (e.g. std::tuple, stl containers). struct RValueCaster {}; PYBIND11_NAMESPACE_BEGIN(pybind11) PYBIND11_NAMESPACE_BEGIN(detail) -template<> class type_caster { +template <> +class type_caster { public: PYBIND11_TYPE_CASTER(RValueCaster, _("RValueCaster")); - static handle cast(RValueCaster &&, return_value_policy, handle) { return py::str("rvalue").release(); } - static handle cast(const RValueCaster &, return_value_policy, handle) { return py::str("lvalue").release(); } + static handle cast(RValueCaster &&, return_value_policy, handle) { + return py::str("rvalue").release(); + } + static handle cast(const RValueCaster &, return_value_policy, handle) { + return py::str("lvalue").release(); + } }; PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(pybind11) @@ -84,5 +86,6 @@ void ignoreOldStyleInitWarnings(F &&body) { with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=message, category=FutureWarning) body() - )", py::dict(py::arg("body") = py::cpp_function(body))); + )", + py::dict(py::arg("body") = py::cpp_function(body))); } diff --git a/tests/test_enum.py b/tests/test_enum.py index 11cab6ddf5..8a5cf6702f 100644 --- a/tests/test_enum.py +++ b/tests/test_enum.py @@ -238,13 +238,13 @@ def test_duplicate_enum_name(): def test_char_underlying_enum(): # Issue #1331/PR #1334: 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 m.ScopedCharEnum.Positive.__getstate__() == 1 # return type is long in py2.x + assert m.ScopedCharEnum.Positive.__getstate__() == 1 assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive with pytest.raises(TypeError): - # Enum should construct with a int, even with char underlying type - m.ScopedWCharEnum("0") + # Even if the underlying type is char, only an int can be used to construct the enum: + m.ScopedCharEnum("0") def test_bool_underlying_enum(): From 12c1024b1852e26611428a6da0bef15dd1736c25 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sun, 29 Aug 2021 23:36:51 -0700 Subject: [PATCH 2/3] 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. --- tests/pybind11_tests.h | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/tests/pybind11_tests.h b/tests/pybind11_tests.h index 8cbfa650e9..8da0a670f2 100644 --- a/tests/pybind11_tests.h +++ b/tests/pybind11_tests.h @@ -1,7 +1,7 @@ #pragma once -#include #include +#include #if defined(_MSC_VER) && _MSC_VER < 1910 // We get some really long type names here which causes MSVC 2015 to emit warnings @@ -26,13 +26,13 @@ class test_initializer { void test_submodule_##name(py::module_ &(variable)) /// Dummy type which is not exported anywhere -- something to trigger a conversion error -struct UnregisteredType {}; +struct UnregisteredType { }; /// A user-defined type which is exported and can be used by any test class UserType { public: UserType() = default; - UserType(int i) : i(i) {} + UserType(int i) : i(i) { } int value() const { return i; } void set(int set) { i = set; } @@ -46,7 +46,7 @@ class IncType : public UserType { public: using UserType::UserType; IncType() = default; - IncType(const IncType &other) : IncType(other.value() + 1) {} + IncType(const IncType &other) : IncType(other.value() + 1) { } IncType(IncType &&) = delete; IncType &operator=(const IncType &) = delete; IncType &operator=(IncType &&) = delete; @@ -58,21 +58,16 @@ union IntFloat { float f; }; -/// Custom cast-only type that casts to a string "rvalue" or "lvalue" depending on the cast -/// context. Used to test recursive casters (e.g. std::tuple, stl containers). +/// Custom cast-only type that casts to a string "rvalue" or "lvalue" depending on the cast context. +/// Used to test recursive casters (e.g. std::tuple, stl containers). struct RValueCaster {}; PYBIND11_NAMESPACE_BEGIN(pybind11) PYBIND11_NAMESPACE_BEGIN(detail) -template <> -class type_caster { +template<> class type_caster { public: PYBIND11_TYPE_CASTER(RValueCaster, _("RValueCaster")); - static handle cast(RValueCaster &&, return_value_policy, handle) { - return py::str("rvalue").release(); - } - static handle cast(const RValueCaster &, return_value_policy, handle) { - return py::str("lvalue").release(); - } + static handle cast(RValueCaster &&, return_value_policy, handle) { return py::str("rvalue").release(); } + static handle cast(const RValueCaster &, return_value_policy, handle) { return py::str("lvalue").release(); } }; PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(pybind11) @@ -86,6 +81,5 @@ void ignoreOldStyleInitWarnings(F &&body) { with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=message, category=FutureWarning) body() - )", - py::dict(py::arg("body") = py::cpp_function(body))); + )", py::dict(py::arg("body") = py::cpp_function(body))); } From d6278efa2b57e0b777c3681ac081cb411427c873 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 31 Aug 2021 08:23:03 -0700 Subject: [PATCH 3/3] More-to-the-point test for Python 3. --- tests/test_enum.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/test_enum.py b/tests/test_enum.py index 8a5cf6702f..85302b0809 100644 --- a/tests/test_enum.py +++ b/tests/test_enum.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import pytest +import env from pybind11_tests import enums as m @@ -240,7 +241,10 @@ def test_char_underlying_enum(): # Issue #1331/PR #1334: assert type(m.ScopedCharEnum.Positive.__int__()) is int assert int(m.ScopedChar16Enum.Zero) == 0 assert hash(m.ScopedChar32Enum.Positive) == 1 - assert m.ScopedCharEnum.Positive.__getstate__() == 1 + 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 with pytest.raises(TypeError): # Even if the underlying type is char, only an int can be used to construct the enum: @@ -251,7 +255,10 @@ def test_bool_underlying_enum(): assert type(m.ScopedBoolEnum.TRUE.__int__()) is int assert int(m.ScopedBoolEnum.FALSE) == 0 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 # Enum could construct with a bool # (bool is a strict subclass of int, and False will be converted to 0)