-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐍 Be more lenient when accepting args to file I/O (#1819)
PyArbor I/O routines now try to cast `filename` arguments to a string. This allows passing `pathlib.Path` objects. Example ```py here = Path(__file__).parent cat = A.load_catalogue(here / 'local-catalogue.so') ```
- Loading branch information
1 parent
ba781cb
commit 2176e15
Showing
4 changed files
with
60 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
#include "strprintf.hpp" | ||
|
||
namespace pyarb { | ||
namespace util { | ||
|
||
namespace py = pybind11; | ||
|
||
inline | ||
std::string to_path(py::object fn) { | ||
if (py::isinstance<py::str>(fn)) { | ||
return std::string{py::str(fn)}; | ||
} | ||
else if (py::isinstance(fn, | ||
py::module_::import("pathlib").attr("Path"))) { | ||
return std::string{py::str(fn)}; | ||
} | ||
throw std::runtime_error( | ||
util::strprintf("Cannot convert objects of type '{}' to a path-like.", | ||
std::string{py::str(fn.get_type())})); | ||
} | ||
|
||
} | ||
} |