Skip to content
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

Enum with underlying char not supported in 2.3.0? #1820

Closed
bwlee opened this issue Jun 26, 2019 · 2 comments · Fixed by #1334
Closed

Enum with underlying char not supported in 2.3.0? #1820

bwlee opened this issue Jun 26, 2019 · 2 comments · Fixed by #1334
Labels

Comments

@bwlee
Copy link

bwlee commented Jun 26, 2019

Just upgrade from 2.2.4 to 2.3.0, nothing else changed, but got error when import package in python as below

libc++abi.dylib: terminating with uncaught exception of type pybind11::error_already_set: TypeError: __int__ returned non-int (type str)

it seems enum definition not supported. eg.

enum class A: char { First='1', Second = '2', }

@bwlee
Copy link
Author

bwlee commented Jun 26, 2019

Hi, I have tried a work-around by modify method "int" in pybind11.h

@line 1555 ~ 1558

        def("__int__", [](Type value) { return int(static_cast<Scalar>(value)); });
        #if PY_MAJOR_VERSION < 3
            def("__long__", [](Type value) { return long(static_cast<Scalar>(value)); });
        #endif

but it smells not well.

Is there any better solutions? @wjakob

@wojdyr
Copy link
Contributor

wojdyr commented Aug 12, 2019

I've also spotted it when upgrading to 2.3.0.
But the only problem I came across was using such enums as a default arg.
For example:

#include <pybind11/pybind11.h>
namespace py = pybind11;

enum class X : char { X1, X2 };
bool foo(X x) { return x == X::X1; }

PYBIND11_MODULE(example, m) {
    py::enum_<X>(m, "X").value("X1", X::X1).value("X2", X::X2);
    m.def("foo", &foo, py::arg("x")=X::X1);
}

In my case the simplest workaround is to change char to signed char (or unsigned char).

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

Successfully merging a pull request may close this issue.

3 participants