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

Throw an exception in Python bindings on Get() buffer type mismatch #3544

Merged
merged 3 commits into from
Mar 19, 2023

Conversation

eisenhauer
Copy link
Member

Relevant to issue #3538

anagainaru
anagainaru previously approved these changes Mar 19, 2023
Copy link
Contributor

@anagainaru anagainaru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could add a test for this

@eisenhauer
Copy link
Member Author

Good point on the test. Added test code modified from issue #3538 to use default file engine instead of streaming.

@eisenhauer eisenhauer merged commit 422a2a5 into ornladios:release_29 Mar 19, 2023
@eisenhauer eisenhauer deleted the PyException branch March 19, 2023 16:14
except Exception as e:
got_exception = 1
logging.info(str(e))
if not got_exception:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python has a more convenient construction for that:
try:
# Some Code....
except:
# Handling of exception (if required)
else:
# execute if no exception

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting... My knowledge of Python is pretty superficial, but that would eliminate a variable...

@@ -129,6 +129,12 @@ void Engine::Get(Variable variable, pybind11::array &array, const Mode launch)
#define declare_type(T) \
else if (type == helper::GetDataType<T>()) \
{ \
if (!array.dtype().is(pybind11::dtype::of<T>())) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the type and size of received data is known, we could just reallocate the buffer to the received type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we have other options. We could also redefine Get() so that it simply returned a buffer of the right type instead of asking the user to provide a buffer that might potentially be of the wrong type. (See notes in issue #3538.) The current structure is likely more the result of directly translating the C++ bindings into Python without a lot of thought of what a redesign should look like. A redesign might be desirable, but not for this release. For this, I did the minimum that would have let us know about a problem that came up in that issue. Want to create an issue for rethinking the Python APIs?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably it should be improved rather on the core level, not in the python binding.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to hear ideas about that, but I do think that Python has specific capabilities (and flexibilities) that aren't present in C++, C, and Fortran, specifically regarding type flexibility. There will be things that we could do in Python that simply aren't easily replicated in type-safe languages (at least not efficiently) and those are the sorts of things that could be more easily addressed in bindings, and would likely be difficult to mirror all the way into core.

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 this pull request may close these issues.

4 participants