-
Notifications
You must be signed in to change notification settings - Fork 2.2k
check for already existing enum value added; added test #1453
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
check for already existing enum value added; added test #1453
Conversation
That error on Travis is definitely unrelated to your changes. As for your PR, it looks good. I personally like |
So this PR will be accepted after travis fix? About the travis issue - looking at last successful build and the next one, the difference seems to be in python version: |
I can't say anything about this getting merged because I don't have write access to the repo. |
include/pybind11/pybind11.h
Outdated
m_entries[pybind11::str(name)] = std::make_pair(v, doc); | ||
auto name_converted = pybind11::str(name); | ||
if (m_entries.contains(name_converted)) | ||
throw value_error("Enum error - element with provided name already exist"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's put the name into this to make it easier to figure out which enum is the problem:
throw value_error("Enum error - element " + std::string(name) + " already exists");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! The change is already on branch, so I think that now after travis fix, this can be merged to master.
The change looks good to me, but the test case needs to be moved (this really isn't related to the embedded interpreter). My apologies; I should have mentioned this earlier, but didn't notice until just now. For tests like this that raise an exception during binding you need to defer the registration via a callable function, like this (in // test_duplicate_enum_name
m.def("register_bad_enum", [m]() {
py::enum_<SimpleEnum>(m, "SimpleEnum")
.value("ONE", SimpleEnum::ONE) //NOTE: all value function calls are called with the same first parameter value
.value("ONE", SimpleEnum::TWO)
.value("ONE", SimpleEnum::THREE)
.export_values();
}); and then call/catch/check the error message from def test_duplicate_enum_name():
with pytest.raises(ValueError) as excinfo:
m.register_bad_enum()
assert str(excinfo.value) == "Enum error - element with name: ONE already exists" |
Ok, i've moved the test case. Wow, I didn't even knew that it's possible to defer the registration. Nice trick, thanks! |
So... now this pull request will be accepted? Or something is missing? |
Merged, thank you! |
* check for already existing enum value added; added test * added enum value name to exception message * test for defining enum with multiple identical names moved to test_enum.cpp/py
Fix for #1451 (Defining enum with mutiple identical values should cause an error)
Now trying to add already existing value throws an exception.