Skip to content
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

Segfault when __repr__ throws #2383

Closed
henryiii opened this issue Aug 11, 2020 · 3 comments · Fixed by #2389
Closed

Segfault when __repr__ throws #2383

henryiii opened this issue Aug 11, 2020 · 3 comments · Fixed by #2389
Labels

Comments

@henryiii
Copy link
Collaborator

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())
@YannickJadoul
Copy link
Collaborator

YannickJadoul commented Aug 11, 2020

This feels familiar, but I can't remember whether that's because a) we talked about it on Gitter or in an issue, in the context of something else, b) there's an open issue about it, or c) there's an open PR dealing with it.

EDIT: I was/am probably thinking about #2317

@jbarlow83
Copy link
Contributor

jbarlow83 commented Aug 12, 2020

I thought it might be due there not being a try block around the call to py::repr in pybind11.h:231 but it seems to be something else.

@henryiii
Copy link
Collaborator Author

Line 779, actually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants