Skip to content

Segfault when __repr__ throws #2383

Closed
@henryiii

Description

@henryiii

Issue description

If a __repr__ throws, then passing an invalid object causes a segfault.

Reproducible example code

diff --git a/tests/test_exceptions.cpp b/tests/test_exceptions.cpp
index 372d0ae..8244322 100644
--- a/tests/test_exceptions.cpp
+++ b/tests/test_exceptions.cpp
@@ -218,4 +218,7 @@ TEST_SUBMODULE(exceptions, m) {
         }
     });

+    // Test repr that cannot be displayed
+    m.def("simple_bool_passthrough", [](bool x){return x;});
+
 }
diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py
index b794e3d..64bdc63 100644
--- a/tests/test_exceptions.py
+++ b/tests/test_exceptions.py
@@ -179,3 +179,14 @@ def test_nested_throws(capture):
     with pytest.raises(m.MyException5) as excinfo:
         m.try_catch(m.MyException, pycatch, m.MyException, m.throws5)
     assert str(excinfo.value) == "this is a helper-defined translated exception"
+
+
+# This can often happen if you wrap a pybind11 class in a Python wrapper
+def test_invalid_repr():
+
+    class MyRepr(object):
+        def __repr__(self):
+            raise AttributeError("Example error")
+
+
+    m.simple_bool_passthrough(MyRepr())

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions