-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add error_scope to py::class_::dealloc() to protect destructor calls #2342
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 assert that the
PyPrintDestructor()actually gets called before the error gets handled? We need to make sure we're testing the situation we think we're testing.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.
Sounds tricky, because you'd have to somehow know what is "before" and insert something there.
My opinion: if the test fails without the change to pybind11.h we're good.
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.
Yes, the "before" does fail as described in #1878
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.
OK, if it's fine with you two, why not :-)
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.
Actually, it's probably OK here, but in an
assert, pytest rewrites the AST (as we've seen before), so it is slightly tricky: https://github.com/pybind/pybind11/pull/2291/files#r453234693There 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.
Since
throw_something()is actuallyPyPrintDestructor.throw_something(self)under the hood, it's impossible for thePyPrintDestructor()to be not constructed before the exception is thrown. Python needs a reference toselfbefore it can make the method call.Given the subtlety of #1878 I think any additional assertions etc. may mean we create another reference to PyPrintDestructor which would change the timing of the destructor and not trigger the error.
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.
I just spent about one hour to convince myself that this PR is safe to merge beyond a reasonable doubt. See approach below. (In the unlikely case that there are issues that escaped the automatic and my manual testing, it's a super easy rollback.)
I will merge this PR now. @jbarlow83 Thank you very much for the contribution!
Approach:
master.clang++with-std=c++11and-std=c++2a(i.e. 4 builds).pybind11.hand rebuilt the 4 combinations.test_class.pyFAILS in all 4 builds. The error messages differ between Python 2 and 3, but NOT between-std=c++11and-std=c++2a. The two kinds of error messages are pasted below.PYTHON 2 ERROR
PYTHON3 ERROR
For completeness: