Skip to content

Casting from an unregistered type does not throw. #2336

Open
@nyibbang

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>

struct F {};

int main() {
  pybind11::scoped_interpreter interp;
  F f;
  try {
    auto obj = pybind11::cast(f);
  }
  catch(...) {
    return 0;
  }
  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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions