Skip to content

Fix assertion failure for unions (#1685) #1709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ class class_ : public detail::generic_type {

template <typename C, typename D, typename... Extra>
class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) {
static_assert(std::is_base_of<C, type>::value, "def_readwrite() requires a class member (or base class member)");
static_assert(std::is_same<C, type>::value || std::is_base_of<C, type>::value, "def_readwrite() requires a class member (or base class member)");
cpp_function fget([pm](const type &c) -> const D &{ return c.*pm; }, is_method(*this)),
fset([pm](type &c, const D &value) { c.*pm = value; }, is_method(*this));
def_property(name, fget, fset, return_value_policy::reference_internal, extra...);
Expand All @@ -1194,7 +1194,7 @@ class class_ : public detail::generic_type {

template <typename C, typename D, typename... Extra>
class_ &def_readonly(const char *name, const D C::*pm, const Extra& ...extra) {
static_assert(std::is_base_of<C, type>::value, "def_readonly() requires a class member (or base class member)");
static_assert(std::is_same<C, type>::value || std::is_base_of<C, type>::value, "def_readonly() requires a class member (or base class member)");
cpp_function fget([pm](const type &c) -> const D &{ return c.*pm; }, is_method(*this));
def_property_readonly(name, fget, return_value_policy::reference_internal, extra...);
return *this;
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ set(PYBIND11_TEST_FILES
test_stl.cpp
test_stl_binders.cpp
test_tagbased_polymorphic.cpp
test_union.cpp
test_virtual_functions.cpp
)

Expand Down
22 changes: 22 additions & 0 deletions tests/test_union.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
tests/test_class.cpp -- test py::class_ definitions and basic functionality

Copyright (c) 2019 Roland Dreier <[email protected]>

All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
*/

#include "pybind11_tests.h"

TEST_SUBMODULE(union_, m) {
union TestUnion {
int value_int;
unsigned value_uint;
};

py::class_<TestUnion>(m, "TestUnion")
.def(py::init<>())
.def_readonly("as_int", &TestUnion::value_int)
.def_readwrite("as_uint", &TestUnion::value_uint);
}
8 changes: 8 additions & 0 deletions tests/test_union.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pybind11_tests import union_ as m


def test_union():
instance = m.TestUnion()

instance.as_uint = 10
assert instance.as_int == 10