-
Notifications
You must be signed in to change notification settings - Fork 62
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
🐍 Be more lenient when accepting args to file I/O #1819
Conversation
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.
One small suggestion to improve robustness.
python/cable_cell_io.cpp
Outdated
namespace py = pybind11; | ||
|
||
arborio::cable_cell_component load_component(py::object fn) { | ||
const std::string fname = py::str(fn); |
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.
Here we are using the py::object
as though it can be converted to a std::string
, without checking.
The errors when a type that can't be converted to string, or converts to an odd string, is passed.
How about writing a little helper function that tests the type and converts it to a string, or throws an exception with a helpful message?
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.
I thought you might bring up this argument. Essentially we are calling foo.__str__
here, which is essentially in line with Python's behaviour. We have two options:
- Restrict to a known good set of py-types (String, Path, ...) trying each in turn
- Accept all the odd things that can happen when calling eg
arbor.load_swc(42)
I tend to option 1 as well, but wanted to have a neutral verdict first.
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.
... option 1!
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.
The Pythonic way is to try and handle errors (which can just be "Unknown input provided, aborting...").
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.
Actually, we should be taking FileHandles, including StringIO. But that is probably not possible.
python/cable_cell_io.cpp
Outdated
std::ifstream fid{fname}; | ||
if (!fid.good()) { | ||
throw pyarb_error("Can't open file '{}'" + fname); | ||
throw pyarb_error(util::pprintf("Can't open file '{}'", fname)); |
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.
I'd be happy to have a new-style typed error here, but I'll leave that up to you.
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.
That's a good point, I should remember my own demands.
Co-authored-by: Brent Huisman <brenthuisman@users.noreply.github.com>
PyArbor I/O routines now try to cast
filename
arguments to a string.This allows passing
pathlib.Path
objects.Example