Skip to content

Commit

Permalink
Fix for bad this behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Oct 1, 2017
1 parent b02e440 commit 8e279ac
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
13 changes: 6 additions & 7 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ template <typename Type> class enum_ : public class_<Type> {

template <typename... Extra>
enum_(const handle &scope, const char *name, const Extra&... extra)
: class_<Type>(scope, name, extra...), m_entries(), m_parent(scope), m_name(name) {
: class_<Type>(scope, name, extra...), m_entries(), m_parent(scope) {

constexpr bool is_arithmetic = detail::any_of<std::is_same<arithmetic, Extra>...>::value;

Expand All @@ -1377,14 +1377,14 @@ template <typename Type> class enum_ : public class_<Type> {
return m;
}, return_value_policy::copy);
def(init([](Scalar i) { return static_cast<Type>(i); }));
def(init([this, m_entries_ptr](std::string value) -> Type {
def(init([name, m_entries_ptr](std::string value) -> Type {
for (const auto &kv : reinterpret_borrow<dict>(m_entries_ptr)) {
std::string key = cast<str>(kv.first);
if(value == key || key == m_name + "::" + value) {
return cast<Type>(kv.second);
std::string key = pybind11::cast<pybind11::str>(kv.first);
if (value == key) {
return pybind11::cast<Type>(kv.second);
}
}
throw value_error("\"" + value + "\" is not a valid value for enum type " + m_name);
throw value_error("\"" + value + "\" is not a valid value for enum type " + name);
}));
def("__int__", [](Type value) { return (Scalar) value; });
#if PY_MAJOR_VERSION < 3
Expand Down Expand Up @@ -1445,7 +1445,6 @@ template <typename Type> class enum_ : public class_<Type> {
private:
dict m_entries;
handle m_parent;
std::string m_name;
};

NAMESPACE_BEGIN(detail)
Expand Down
1 change: 1 addition & 0 deletions tests/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def test_unscoped_enum():
assert not (m.UnscopedEnum.ETwo < m.UnscopedEnum.EOne)
assert not (2 < m.UnscopedEnum.EOne)


def test_converstion_enum():
assert m.test_conversion_enum(m.ConversionEnum.Convert1) == "ConversionEnum::Convert1"
assert m.test_conversion_enum(m.ConversionEnum("Convert1")) == "ConversionEnum::Convert1"
Expand Down

0 comments on commit 8e279ac

Please sign in to comment.