Skip to content

Casting from an unregistered type does not throw. #2336

Description

@nyibbang

Hello,

Issue description

I have this issue on pybind11 v2.5.0.

In the documentation, in object.rst, section Casting back and forth, the following is stated:

When conversion fails, both directions throw the exception cast_error.

But when trying to cast an unregistered custom type, it does not. Instead it seems to be setting an error (PyErr_Occurred returns a TypeError) and it returns a None object.

Reproducible example code

#include<pybind11/pybind11.h>
#include<pybind11/embed.h>
#include<cassert>structF {};
intmain() {
pybind11::scoped_interpreter interp;
F f;
try {
auto obj = pybind11::cast(f);
}
catch(...) {
return0;
}
assert(((void)"cast did not throw an exception", false));
return -1;
}

According to the documentation, we should not get an assertion failure with this code.

Activity

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions