Skip to content

Commit 06b673a

Browse files
authored
Allow NULL value in pybind11_meta_setattro (#2629)
1 parent 3e4d54b commit 06b673a

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

include/pybind11/detail/class.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ extern "C" inline int pybind11_meta_setattro(PyObject* obj, PyObject* name, PyOb
129129
// 2. `Type.static_prop = other_static_prop` --> setattro: replace existing `static_prop`
130130
// 3. `Type.regular_attribute = value` --> setattro: regular attribute assignment
131131
const auto static_prop = (PyObject *) get_internals().static_property_type;
132-
const auto call_descr_set = descr && PyObject_IsInstance(descr, static_prop)
132+
const auto call_descr_set = descr && value && PyObject_IsInstance(descr, static_prop)
133133
&& !PyObject_IsInstance(value, static_prop);
134134
if (call_descr_set) {
135135
// Call `static_property.__set__()` instead of replacing the `static_property`.

tests/test_methods_and_attributes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ def test_static_properties():
171171
assert m.TestPropertiesOverride().def_readonly == 99
172172
assert m.TestPropertiesOverride.def_readonly_static == 99
173173

174+
# Only static attributes can be deleted
175+
del m.TestPropertiesOverride.def_readonly_static
176+
assert (
177+
hasattr(m.TestPropertiesOverride, "def_readonly_static")
178+
and m.TestPropertiesOverride.def_readonly_static
179+
is m.TestProperties.def_readonly_static
180+
)
181+
assert "def_readonly_static" not in m.TestPropertiesOverride.__dict__
182+
properties_override = m.TestPropertiesOverride()
183+
with pytest.raises(AttributeError) as excinfo:
184+
del properties_override.def_readonly
185+
assert "can't delete attribute" in str(excinfo.value)
186+
174187

175188
def test_static_cls():
176189
"""Static property getter and setters expect the type object as the their only argument"""

0 commit comments

Comments
 (0)