-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Allow python builtins to be used as callbacks #1413
Conversation
5ef30fd
to
4197abb
Compare
The change looks good to me. There's one minor change needed in the added python test to fix the flake warning (two blank lines needed before the added test). |
Great, I'll find a moment to tidy that up at the weekend! |
4197abb
to
1c4261b
Compare
@jagerman should be all good now. Thanks |
1c4261b
to
a9ccb02
Compare
@jagerman just wanted to make sure this wasn't forgotten - I still run into this every so often. |
I just ran into the same problem. Many thanks! |
what about progress on this one? |
We're in the middle of cleaning up issues and PRs. Give us a little bit of time, please. If you want to help this PR progress, fixing the merge conflicts would help us greatly! :-) |
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.
Looks great! Is the original author still around? If someone rebases on current master and the CI comes back green I think we should merge this straightaway.
for more information, see https://pre-commit.ci
include/pybind11/functional.h
Outdated
#if defined(PYPY_VERSION) | ||
// PyPy will segfault otherwise when passing in raw builtin functions. | ||
return false; | ||
//pybind11_fail("Passing raw builtin functions not supported with PyPy. Wrap in function."); |
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.
Do a fail her also caused a segfault. Weird.
Now it segfaults on every PyPy EXCEPT Ubuntu. So weird... |
include/pybind11/functional.h
Outdated
#if defined(PYPY_VERSION) | ||
// PyPy will segfault otherwise when passing in raw builtin functions. | ||
// This will lead to a TypeError instead. | ||
return false; |
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 it looks like this test case ALWAYS segfaulted on non-ubuntu PyPy then which means that that the code is still better than before. I am really stumped on how to avoid this segfault though.
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 counted, we have 19 xfail for PyPy already.
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.
PyPy has bugs. If we aren't adding a new one or making it worse (which it appears we are not), then I'm happy with it if it passes Google's testing.
👋 original author still here! Sorry it's taken me a couple of days to reply. It looks like you've got this one under control without me, so I won't push anything unless you ask me to. These days I don't use C++ much any more, I tend to use Rust instead. I'm still very much working in the Python + native domain though, I am a maintainer of PyO3. I still watch progress here closely; I like to think that both projects can benefit from sharing ideas and pushing the ecosystem forward. Sadly we're still some way behind in PyO3 so I don't think there's much learning from PyO3 that would interest you yet. Do feel free to ping me any time (on this issue or any other) if you want my input as a "neighbour" 😄 |
I imported this PR into the Google environment and ran ASAN & MSAN. Both are happy. I'm now submitting this PR to our global testing system. I'll report on the results tomorrow. |
EDIT: SEE NEXT COMMENT Unfortunately there is a problem, triggered by code that's not open source, so I cannot easily point or share. It's a compilation failure (using some very modern clang version). I'll share the full traceback non-publicly. Here is the result of
|
WAIT: after reporting the above I tried to reproduce the errors interactively, but it works! |
It turns out to be some kind of Windows build. |
In the meantime the author of #1883 gave me some pointers, including how to reproduce the error interactively. Theory: we could be up against a bug in the Windows clang compiler. I'll try to get the attention of the team maintaining that compiler. |
I'll split off the bugprone checks into their own PR. |
* Allow python builtins to be used as callbacks * Try to fix pypy segfault * Add expected fail for PyPy * Fix typo * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add more info to xfail * Add env * Try returning false * Try removing the move for pypy * Fix bugs * Try removing move * Just keep ignoring for PyPy * Add back xfail * Fix ctors * Revert change of std::move * Change to skip * Fix bug and edit comments * Remove clang-tidy bugprone fix skip bug Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
At the moment when passing python builtins like
sum()
as callback functions to c++, the std::function caster will fail withRuntimeError: Unable to extract capsule contents
. This is because of the machinery to extract stateless c++ function pointers to avoid the c++ -> python -> c++ overhead.I've added a simple check that the definition being extracted is indeed a capsule and an accompanying test that I can now use
sum()
as a callback. Hope you find this useful.Suggested changelog entry: