Skip to content

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

Open

Description

@CDK6182CHR

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

Activity

  1. added
    triage
    on Jul 28, 2025
  2. henryiii commented on Nov 23, 2026

    @henryiii
    Collaborator

    🤖 AI text below 🤖

    This is probably a symbol visibility problem and not a pybind11 bug. I read the attached example but did not build it.

    PyTestException is defined fully in the header, so each binary that uses it emits its own copy of the typeinfo. pybind11_add_module compiles the module with -fvisibility=hidden. Thus the copy in pyexcept_test is hidden, and the dynamic linker cannot merge it with the copy in libpyexcept.so. The process then has two different typeinfo objects for the same class.

    • libc++ on Linux compares typeinfo by address. The exception thrown from libpyexcept.so does not match the catch (const PyTestException &) in the translator that register_exception installs in the module. The exception then goes to the std::runtime_error translator, and you get RuntimeError.
    • libstdc++ compares typeinfo by name, so GCC works.
    • The lambda throws from inside the module, so it uses the same typeinfo as the translator. A static library also puts the throw inside the module.

    To fix this, give the class default visibility in the header:

    class __attribute__((visibility("default"))) PyTestException : public std::runtime_error
    {
    public:
        using runtime_error::runtime_error;
    };

    This is necessary for each exception type (and each type used with dynamic_cast) that goes across a shared library boundary. See also the visibility FAQ entry.

    Please tell us if this fixes the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions