Skip to content

Commit f8743f8

Browse files
committed
Add the extract PyObject fields to support extending Exceptions
Any pybind11 type that extends PyExc_Exception has to have a instance struct that contains the same initial layout. And in pybind11 currently, the instance struct just has PyObject_HEAD. That means if you don't change the instance struct, this will all compile, but when python seeks into this object, it will do with the assumption that those extra fields exist and then it will seek right off the end of viable memory and you'll get all sorts of fun segfaults. So this change adds those extra fields to every class_ in pybind11. It does not seem to break normal classes to have these extra fields and it definitely seems to make exceptions work correctly. I am not super happy about this approach, so consider a proof of concept that should probably be improved.
1 parent 1c418f3 commit f8743f8

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

include/pybind11/detail/common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,10 @@ struct nonsimple_values_and_holders {
409409
/// The 'instance' type which needs to be standard layout (need to be able to use 'offsetof')
410410
struct instance {
411411
PyObject_HEAD
412+
// Necessary to support exceptions.
413+
PyObject *dict;
414+
PyObject *args;
415+
PyObject *message;
412416
/// Storage for pointers and holder; see simple_layout, below, for a description
413417
union {
414418
void *simple_value_holder[1 + instance_simple_holder_in_ptrs()];

0 commit comments

Comments
 (0)