Skip to content

[BUG]: Can't catch the exception in Python that raise in C++ using py::error_already_set() #5290

@QunBB

Description

@QunBB

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

2.10.0

Problem description

When i use the below reproducible example code, I can't catch the exception IndexError in Python that raise in C++.
The log is:

before exception

Process finished with exit code 139 (interrupted by signal 11:SIGSEGV)

Reproducible example code

m.def("test_exception", []() { 
        std::cout << "before exception" << std::endl;

        PyErr_SetString(PyExc_IndexError, "Here is an IndexError");
        throw py::error_already_set();
        
        std::cout << "after exception" << std::endl;
        });
test_exception()

Is this a regression? Put the last known working version here if it is.

Not a regression

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageNew bug, unverified

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions