Skip to content

py::cast<T> where T is a subclass of py::object is unchecked #1108

@ezyang

Description

@ezyang

Issue description

I expect something like py::cast<py::bytes>(x) to check if the py::object x is actually bytes. It does not.

Reproducible example code

#include <pybind11/pybind11.h>
#include <iostream>
namespace py = pybind11;
int main() {
  Py_Initialize();
  auto gil = PyGILState_Ensure();
  PyObject *l = PyFloat_FromDouble(3.14);
  //PyObject *l = PyBytes_FromString("foo");
  auto n = py::cast<py::bytes>(l);
  std::cerr << "No problem yet...\n";
  std::cerr << static_cast<std::string>(n);
  PyGILState_Release(gil);
  return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions