Skip to content

Commit 0db7f72

Browse files
maxbachmannpre-commit-ci[bot]rwgk
authored
Handle result from PyObject_VisitManagedDict (#6032)
* Handle result from PyObject_VisitManagedDict * add unit test * style: pre-commit fixes * use different variable name This avoids a warning on msvc about Py_Visit shadowing the vret variable. * skip test_get_referrers on unsupported runtimes The managed-dict referrer check is only known to work on CPython 3.13.13+ and 3.14.4+, while earlier releases and non-CPython interpreters can report different traversal behavior. Made-with: Cursor --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
1 parent 4158dcf commit 0db7f72

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

include/pybind11/detail/class.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,10 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
578578
/// dynamic_attr: Allow the garbage collector to traverse the internal instance `__dict__`.
579579
extern "C" inline int pybind11_traverse(PyObject *self, visitproc visit, void *arg) {
580580
#if PY_VERSION_HEX >= 0x030D0000
581-
PyObject_VisitManagedDict(self, visit, arg);
581+
int ret = PyObject_VisitManagedDict(self, visit, arg);
582+
if (ret) {
583+
return ret;
584+
}
582585
#else
583586
PyObject *&dict = *_PyObject_GetDictPtr(self);
584587
Py_VISIT(dict);

tests/test_class.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ TEST_SUBMODULE(class_, m) {
104104
~NoConstructorNew() { print_destroyed(this); }
105105
};
106106

107+
struct DynamicAttr {
108+
DynamicAttr() = default;
109+
};
110+
107111
py::class_<NoConstructor>(m, "NoConstructor")
108112
.def_static("new_instance", &NoConstructor::new_instance, "Return an instance");
109113

@@ -112,6 +116,8 @@ TEST_SUBMODULE(class_, m) {
112116
.def_static("__new__",
113117
[](const py::object &) { return NoConstructorNew::new_instance(); });
114118

119+
py::class_<DynamicAttr>(m, "DynamicAttr", py::dynamic_attr()).def(py::init<>());
120+
115121
// test_pass_unique_ptr
116122
struct ToBeHeldByUniquePtr {};
117123
py::class_<ToBeHeldByUniquePtr, std::unique_ptr<ToBeHeldByUniquePtr>>(m, "ToBeHeldByUniquePtr")

tests/test_class.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import gc
34
import sys
45
from unittest import mock
56

@@ -18,6 +19,13 @@ def refcount_immortal(ob: object) -> int:
1819
return sys.getrefcount(ob)
1920

2021

22+
MANAGED_DICT_GET_REFERRERS_SUPPORTED = (
23+
env.CPYTHON
24+
and sys.version_info >= (3, 13, 13)
25+
and (sys.version_info < (3, 14) or sys.version_info >= (3, 14, 4))
26+
)
27+
28+
2129
def test_obj_class_name():
2230
expected_name = "UserType" if env.PYPY else "pybind11_tests.UserType"
2331
assert m.obj_class_name(UserType(1)) == expected_name
@@ -45,6 +53,16 @@ def test_instance(msg):
4553
assert cstats.alive() == 0
4654

4755

56+
@pytest.mark.skipif(
57+
not MANAGED_DICT_GET_REFERRERS_SUPPORTED,
58+
reason="Requires CPython 3.13.13+ or 3.14.4+ managed dict traversal support",
59+
)
60+
def test_get_referrers():
61+
instance = m.DynamicAttr()
62+
instance.a = "test"
63+
assert instance in gc.get_referrers(instance.__dict__)
64+
65+
4866
def test_instance_new():
4967
instance = m.NoConstructorNew() # .__new__(m.NoConstructor.__class__)
5068

0 commit comments

Comments
 (0)