-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Allow NULL value in pybind11_meta_setattro #2629
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
Conversation
5c32889
to
ceb2878
Compare
tests/test_methods_and_attributes.py
Outdated
@@ -171,6 +171,13 @@ def test_static_properties(): | |||
assert m.TestPropertiesOverride().def_readonly == 99 | |||
assert m.TestPropertiesOverride.def_readonly_static == 99 | |||
|
|||
# Static attributes can be deleted | |||
del m.TestPropertiesOverride.def_readonly_static | |||
assert not hasattr(m.TestPropertiesOverride, "def_readonly_static") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this check fails because:
>>> import pybind11_tests
>>> m = pybind11_tests.methods_and_attributes
>>> m.TestPropertiesOverride.def_readonly_static
99
>>> del m.TestPropertiesOverride.def_readonly_static
>>> hasattr(m.TestPropertiesOverride, 'def_readonly_static')
True
>>> m.TestPropertiesOverride.def_readonly_static
1
>>> del m.TestPropertiesOverride.def_readonly_static
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: def_readonly_static
Can anyone make sense of it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latter value, 1, seems to come from m.TestProperties
, a base class of m.TestPropertiesOverride
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right.
>>> m = pybind11_tests.methods_and_attributes
>>> del m.TestPropertiesOverride.def_readonly_static
>>> m.TestPropertiesOverride.def_readonly_static
1
>>> del m.TestProperties.def_readonly_static
>>> m.TestPropertiesOverride.def_readonly_static
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'pybind11_tests.methods_and_attributes.TestProperti' has no attribute 'def_readonly_static'
This makes me even less sure if this is the right course of action.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we see/mimic how this would react in Python?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just tried this out:
>>> class Base:
... answer = 6 * 9
...
>>> class Derived(Base):
... answer = 42
...
>>> Derived.answer
42
>>> del Derived.answer
>>> Derived.answer
54
>>> del Derived.answer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: answer
>>> del Base.answer
>>> Derived.answer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'Derived' has no attribute 'answer'
>>>
So even without any properties, static attributes in pure Python would match our behavior, no?
So ... not a bug, just counter-intuitive Python behaviour?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless there's a reason to match non-static properties (or something that needs to be kept alive or so), I'm fine with this. It matches pure Python behavior, and as far as I know, that's what we most of the time aim for.
ceb2878
to
f4e1d2f
Compare
2.6.1 or 2.7? |
That was fast... :) |
Hahahaha, I was just thinking I missed a PR or a few somewhere, and was already looking around ;-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My vote is to include this one in 2.6.1.
f4e1d2f
to
b7ca21e
Compare
Merged! Good catch, @fried, and thanks for fixing, @bstaletic! |
Description
Python says we have to handle
NULL
values inpybind11_meta_setattro
as "delete property".https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_setattro
However, pybind11 currently doesn't allow users to
del
ete non-static properties. I'm not really sure what's the right course of action, this PR is just the first thing that worked, in the sense of not segfaulting as in #2009.Fixes #2009
Suggested changelog entry: