-
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
Improve Python 3.11 support #3694
Conversation
Okay, I have narrowed down the problem. It causes an MRO issue under these conditions:
This causes the multiple inheritance code complain similar to issue #597 . We fixed that one by ensuring all classes have the same metaclass. Something breaks this fix in 3.11. |
tests/test_multiple_inheritance.cpp
Outdated
@@ -204,7 +204,9 @@ TEST_SUBMODULE(multiple_inheritance, m) { | |||
m.def("i801e_b2", []() -> I801B2 * { return new I801E(); }); | |||
|
|||
// test_mi_static_properties | |||
py::class_<Vanilla>(m, "Vanilla").def(py::init<>()).def("vanilla", &Vanilla::vanilla); | |||
py::class_<Vanilla>(m, "Vanilla", py::dynamic_attr()) |
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.
Making all bases in the hierarchy either static or dynamic_attrs works, but is not a tenable solution.
…test-311-frame-changes
@@ -514,7 +515,14 @@ PYBIND11_NOINLINE std::string error_string() { | |||
errorString += " " + handle(f_code->co_filename).cast<std::string>() + "(" | |||
+ std::to_string(lineno) | |||
+ "): " + handle(f_code->co_name).cast<std::string>() + "\n"; | |||
frame = frame->f_back; | |||
# if PY_VERSION_HEX >= 0x030900B1 |
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.
Nit: Maybe we should add a backport section details/common.h . I just wasn't sure if the recommend static inline stuff would cause any nasty ODR / LTO issues so I left it as is.
This fixes compiling scipy with py311. |
I looked a bit but have questions for my understanding: Is it true that
It would be useful to explain in a couple sentences in the PR description. |
@rwgk This fixes 3.11 compatability. The issue is the new API returns a strong reference (instead of a weak reference) which means we need to decref it. The easiest way to handle this was to have the old code path make a strong reference too. I'll edit the PR description. |
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 changes LGTM, too, but I'm wondering about
- @Skylion007 I didn't see edits to the PR description, would be nice to have them.
- the valgrind error looks like it needs attention
- is it OK to ignore the upstream failure?
Thanks for pointing out that issue @henryiii. Looks good to me now. |
I'll run some quick interactive tests in with the Google toolchain. |
I did a manual & embarrassingly simple leak check (diff below + ASAN testing was happy, too. +++ b/tests/test_exceptions.py
@@ -246,6 +246,7 @@ def test_throw_nested_exception():
# This can often happen if you wrap a pybind11 class in a Python wrapper
def test_invalid_repr():
+ while True:
class MyRepr:
def __repr__(self):
raise AttributeError("Example error") |
* Test out Python 3.11 migration * Clean up a bit * Remove todo * Test workaround * Fix potential bug uncovered in 3.11 * Try to fix it more * last ditch fix * Revert. Tp-traverse isn't the problem * Test workaround * Try this hack * Revert MRO changes * Use f_back properly * Qualify auto * Update include/pybind11/pybind11.h * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Simplify code slightly * Ensure co_varnames decref if dict_getitem throws * Eager decref f_code Co-authored-by: Henry Schreiner <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Test out Python 3.11 migration * Clean up a bit * Remove todo * Test workaround * Fix potential bug uncovered in 3.11 * Try to fix it more * last ditch fix * Revert. Tp-traverse isn't the problem * Test workaround * Try this hack * Revert MRO changes * Use f_back properly * Qualify auto * Update include/pybind11/pybind11.h * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Simplify code slightly * Ensure co_varnames decref if dict_getitem throws * Eager decref f_code Co-authored-by: Henry Schreiner <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Backport of pybind#3694
Description
Suggested changelog entry: