Skip to content

Support PyTypeObject #1692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
asadchev opened this issue Feb 12, 2019 · 0 comments · Fixed by #2364
Closed

Support PyTypeObject #1692

asadchev opened this issue Feb 12, 2019 · 0 comments · Fixed by #2364

Comments

@asadchev
Copy link

I couldnt find if PyBind supports type objects. If not, then something like this is useful:

  struct typeobject : py::handle {
    typeobject(PyTypeObject *type)
      : py::handle((PyObject*)type)
    {
    }
    typeobject static float_() { return typeobject(&PyFloat_Type); }
    typeobject static int_() { return typeobject(&PyInt_Type); }
    typeobject static str() { return typeobject(&PyString_Type); }
  };

or

  py::handle typeobject(PyTypeObject *type) {
    return py::handle((PyObject*)type);
  }

  template<typename T>
  py::handle typeobject();

  template<>
  py::handle typeobject<py::float_>() { return typeobject(&PyFloat_Type); }

  template<>
  py::handle typeobject<py::int_>() { return typeobject(&PyInt_Type); }

  template<>
  py::handle typeobject<py::str>() { return typeobject(&PyString_Type); }

for example handling type arguments
module.def("foo", py::arg("bar") = typeobject::str());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant