[BUG]: Exception thrown from C++ library function could not be correctly converted to the registered class with clang-19 compiler
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
- Área
- backend-api-design
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
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
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!
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
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de pybind/pybind11
-
triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
Los mantenedores suelen responder en 3 días
-
triage
Dificultad 1/5 Menos de una hora Aptitud para principiantes 62/100
pybind/pybind11#5604 · 1 comentario ·
Los mantenedores suelen responder en 3 días
-
triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
Los mantenedores suelen responder en 3 días
-
docs
Dificultad 2/5 1-3 horas Aptitud para principiantes 60/100
pybind/pybind11#747 · 2 comentarios ·
Los mantenedores suelen responder en 3 días
-
array_t::at() returns a misaligned reference when the stride is not a multiple of alignof(T)Posiblemente ocupada @henryiii la tomó hoy. Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
Los mantenedores suelen responder en 3 días
Todos los issues de pybind/pybind11
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
patrick-hudson/put.gd-desktop#2044 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
uxlfoundation/oneDNN#6604 · 1 comentario ·
Los mantenedores suelen responder en 2 días
-
[Bug]: RoundToNearest truncates in plugins compiled with spcomp 1.13Posiblemente ocupada @fmu1337 la tomó hoy. AbiertoBug
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
alliedmodders/sourcemod#2627 ·
Los mantenedores suelen responder en 1 día
-
`Number` enum is not exportedAbierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 75/100
Los mantenedores suelen responder en 1 día