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).

@maxbachmann
Copy link
Copy Markdown
Author

I was hoping that since 3.14.4 is released in https://github.com/actions/python-versions it would pick it up now, but apparently that isn't the case

@rwgk
Copy link
Copy Markdown
Collaborator

rwgk commented Apr 9, 2026

I was hoping that since 3.14.4 is released in https://github.com/actions/python-versions it would pick it up now, but apparently that isn't the case

In the past I've seen it taking a day or so. Do you see the option to "rerun failed tests"?

(I'll try to look at this PR carefully on the weekend.)

@maxbachmann
Copy link
Copy Markdown
Author

No I don't have that option.

@rwgk
Copy link
Copy Markdown
Collaborator

rwgk commented Apr 9, 2026

I just clicked the button, but it's still picking up: Successfully set up CPython (3.14.3)

Under

https://github.com/pybind/pybind11/actions/runs/24187486767/job/70595813802?pr=6032

I see:

  =================================== FAILURES ===================================
  ______________________________ test_get_referrers ______________________________
   
      def test_get_referrers():
          instance = m.DynamicAttr()
          instance.a = "test"
  >       assert instance in gc.get_referrers(instance.__dict__)
  E       AssertionError: assert <pybind11_tests.class_.DynamicAttr object at 0x7195a1b995e0> in []
  E        +  where [] = <built-in function get_referrers>({'a': 'test'})
  E        +    where <built-in function get_referrers> = gc.get_referrers
  E        +    and   {'a': 'test'} = <pybind11_tests.class_.DynamicAttr object at 0x7195a1b995e0>.__dict__
   
  instance   = <pybind11_tests.class_.DynamicAttr object at 0x7195a1b995e0>
   
  tests/test_class.py:52: AssertionError

I think it'll be best if you skip that test for Python versions between 3.14.0 and 3.14.3 (or similar; I'm not sure).

@rwgk
Copy link
Copy Markdown
Collaborator

rwgk commented Apr 9, 2026

If you need more reruns, tag me here with "another rerun needed" or similar. I cannot focus on this PR before the weekend, but I can do quick button clicks.

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