[BUG]: Exception thrown from C++ library function could not be correctly converted to the registered class with clang-19 compiler

Abierto
#5,773 1 comentario 1 reacción 0 asignados Ver en GitHub

Los mantenedores suelen responder en 3 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
cpp, python

Línea de trabajo

Comienza con el ejemplo mínimo adjunto, especialmente con pyexcept_test.cpp, libpyexcept.so y el script de prueba de Python. Reproduce la diferencia entre clang-19.1.5 con libc++ y GCC 14.1.0, comparando los casos de biblioteca compartida y lambda relacionados con register_exception. El trabajo se considera terminado cuando se haya identificado el problema de conversión de la biblioteca compartida y se haya cubierto el comportamiento corregido con una prueba de regresión.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

3.0.0

Problem description

Recently I have encountered some issue while working with the pybind11 exception binding. The exception defined in C++ and registered by pybind11 could not be caught as the defined exception class, but only as its parent exception class (e.g., RuntimeError).

This issue raises while I am using LLVM/clang 19.1.5 compiler (along with libc++), but GCC 14.1.0 compiler works fine. The testing platform is CentOS 7. For a minimal example, please see the attachment.

Minimal example

In the minimal example (see attachment), an exception class is defined in C++, inheriting from standard std::runtime_error:

class PyTestException: public std::runtime_error
{
public:
    using runtime_error::runtime_error;
};

A function simply throwing this exception is also defined:

void throw_pyexcept()
{
    throw PyTestException("Exception from C++");
}

The exception class and the function throw_pyexcept make up a shared library called libpyexcept.so.

Then, in pyexcept_test.cpp, the pybind11 binding to the exception is registered, the function defined above is also bound. Meanwhile, another binding function that simply throw the exception using lambda expression is also defined.

PYBIND11_MODULE(pyexcept_test, m) {
    pybind11::register_exception<PyTestException>(m, "PyTestException", PyExc_RuntimeError);

    m.def("throw_pyexcept", &throw_pyexcept);
    m.def("throw_pyexcept_from_lambda", [](){throw PyTestException("Exception from lambda");});
}

After installing this package to local Python environment, the following test script is executed:

from pyexcept_test.pyexcept_test import PyTestException, throw_pyexcept, throw_pyexcept_from_lambda

print('Testing throw_pyexcept')

try:
    throw_pyexcept()
except PyTestException:
    print('PyTestException')
except RuntimeError:
    print('RuntimeError')
except Exception:
    print('Exception')
    
print('Testing throw_pyexcept_from_lambda')

try:
    throw_pyexcept_from_lambda()
except PyTestException:
    print('PyTestException')
except RuntimeError:
    print('RuntimeError')
except Exception:
    print('Exception')
    
print('Finished')

The expected output of this script should be like:

Testing throw_pyexcept
PyTestException
Testing throw_pyexcept_from_lambda
PyTestException
Finished

Because the thrown exception is exactly PyTestException. It is exactly this case if GCC 14.1.0 compiler is used. However, when LLVM Clang 19.1.5 compiler is used, the result is different:

Testing throw_pyexcept
RuntimeError
Testing throw_pyexcept_from_lambda
PyTestException
Finished

The PyTestException thrown by the throw_pyexcept C++ function defined in the shared library is caught as RuntimeError, not PyTestException, while the one thrown by lambda expression is correctly caught.

More information

I have also tested that if the throw_pyexcept function was defined in a static library (not shared), the exception could be correctly caught.

I wonder if there was any wrong made in my code, or with my testing environment.

Any suggestions are welcomed.

Thank you in advance for your time!

test_exception.zip

Reproducible example code
Please see the attachment for the reproducible minimal example code.
Is this a regression? Put the last known working version here if it is.

Not a regression

Lenguaje dominante
C++
Estrellas
18k
Forks
2.3k
Merge medio
5 d 7 h
PR fusionados (30 d)
53

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de pybind/pybind11

Todos los issues de pybind/pybind11

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.