Skip to content

Handle result from PyObject_VisitManagedDict#6032

Open
maxbachmann wants to merge 3 commits intopybind:masterfrom
maxbachmann:patch-1
Open

Handle result from PyObject_VisitManagedDict#6032
maxbachmann wants to merge 3 commits intopybind:masterfrom
maxbachmann:patch-1

Conversation

@maxbachmann
Copy link
Copy Markdown

Without this the relationship between __dict__ and self isn't found.

@rwgk
Copy link
Copy Markdown
Collaborator

rwgk commented Apr 8, 2026

How much trouble is it to add a test?

If adding a test is too much trouble: could you please explain in the PR description what the problem was, and why this is the correct fix?

I'm totally willing to believe this fix is needed and correct, but without any test or explanation it's very opaque.

@maxbachmann
Copy link
Copy Markdown
Author

maxbachmann commented Apr 8, 2026

This is the same handling as PY_VISIT.

#define Py_VISIT(op)                                                    \
    do {                                                                \
        if (op) {                                                       \
            int vret = visit(_PyObject_CAST(op), arg);                  \
            if (vret)                                                   \
                return vret;                                            \
        }                                                               \
    } while (0)

I had the same bug in the Python wrapper I am implementing at my company and so checked whether others had this problem as well.

For us this fixes the following case:

    instance = m.DynamicAttr()
    instance.a = "test"
    assert instance in gc.get_referrers(instance.__dict__)

For pybind11 this unit test still fails due to python/cpython#130327. It should presumably work once we upgrade to 3.14.4 which was released yesterday.

@maxbachmann
Copy link
Copy Markdown
Author

Worth noting that cpython doesn't always properly handle the result either (python/cpython#148275).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants