mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
py::class_<T>'s `def_property` and `def_property_static` can now take a `nullptr` as the getter to allow a write-only property to be established (mirroring Python's `property()` built-in when `None` is given for the getter). This also updates properties to use the new nullptr constructor internally.
This commit is contained in:
committed by
Jason Rhinelander
parent
71178922fd
commit
0a0758ce3a
@@ -98,6 +98,21 @@ def test_properties():
|
||||
instance.def_property = 3
|
||||
assert instance.def_property == 3
|
||||
|
||||
with pytest.raises(AttributeError) as excinfo:
|
||||
dummy = instance.def_property_writeonly # noqa: F841 unused var
|
||||
assert "unreadable attribute" in str(excinfo)
|
||||
|
||||
instance.def_property_writeonly = 4
|
||||
assert instance.def_property_readonly == 4
|
||||
|
||||
with pytest.raises(AttributeError) as excinfo:
|
||||
dummy = instance.def_property_impossible # noqa: F841 unused var
|
||||
assert "unreadable attribute" in str(excinfo)
|
||||
|
||||
with pytest.raises(AttributeError) as excinfo:
|
||||
instance.def_property_impossible = 5
|
||||
assert "can't set attribute" in str(excinfo)
|
||||
|
||||
|
||||
def test_static_properties():
|
||||
assert m.TestProperties.def_readonly_static == 1
|
||||
@@ -108,13 +123,27 @@ def test_static_properties():
|
||||
m.TestProperties.def_readwrite_static = 2
|
||||
assert m.TestProperties.def_readwrite_static == 2
|
||||
|
||||
assert m.TestProperties.def_property_readonly_static == 2
|
||||
with pytest.raises(AttributeError) as excinfo:
|
||||
m.TestProperties.def_property_readonly_static = 3
|
||||
dummy = m.TestProperties.def_writeonly_static # noqa: F841 unused var
|
||||
assert "unreadable attribute" in str(excinfo)
|
||||
|
||||
m.TestProperties.def_writeonly_static = 3
|
||||
assert m.TestProperties.def_readonly_static == 3
|
||||
|
||||
assert m.TestProperties.def_property_readonly_static == 3
|
||||
with pytest.raises(AttributeError) as excinfo:
|
||||
m.TestProperties.def_property_readonly_static = 99
|
||||
assert "can't set attribute" in str(excinfo)
|
||||
|
||||
m.TestProperties.def_property_static = 3
|
||||
assert m.TestProperties.def_property_static == 3
|
||||
m.TestProperties.def_property_static = 4
|
||||
assert m.TestProperties.def_property_static == 4
|
||||
|
||||
with pytest.raises(AttributeError) as excinfo:
|
||||
dummy = m.TestProperties.def_property_writeonly_static
|
||||
assert "unreadable attribute" in str(excinfo)
|
||||
|
||||
m.TestProperties.def_property_writeonly_static = 5
|
||||
assert m.TestProperties.def_property_static == 5
|
||||
|
||||
# Static property read and write via instance
|
||||
instance = m.TestProperties()
|
||||
@@ -127,6 +156,13 @@ def test_static_properties():
|
||||
assert m.TestProperties.def_readwrite_static == 2
|
||||
assert instance.def_readwrite_static == 2
|
||||
|
||||
with pytest.raises(AttributeError) as excinfo:
|
||||
dummy = instance.def_property_writeonly_static # noqa: F841 unused var
|
||||
assert "unreadable attribute" in str(excinfo)
|
||||
|
||||
instance.def_property_writeonly_static = 4
|
||||
assert instance.def_property_static == 4
|
||||
|
||||
# It should be possible to override properties in derived classes
|
||||
assert m.TestPropertiesOverride().def_readonly == 99
|
||||
assert m.TestPropertiesOverride.def_readonly_static == 99
|
||||
|
||||
Reference in New Issue
Block a user