Add support for os.PathLike inputs in the parsers

Resolve: #974
This commit is contained in:
Romain Thomas
2023-10-21 05:23:42 +02:00
parent dffcff5df7
commit a27bd6a644
24 changed files with 201 additions and 60 deletions
+15 -7
View File
@@ -19,6 +19,7 @@
#include <nanobind/stl/string.h>
#include <nanobind/stl/vector.h>
#include "pyutils.hpp"
#include "pyIOStream.hpp"
#include "LIEF/MachO/Parser.hpp"
@@ -60,21 +61,28 @@ void create<Parser>(nb::module_& m) {
nb::rv_policy::take_ownership);
m.def("parse",
[] (nb::object byteio, const ParserConfig& config) -> nb::object {
if (auto stream = PyIOStream::from_python(std::move(byteio))) {
[] (nb::object obj, const ParserConfig& config) -> nb::object {
if (auto path_str = path_to_str(obj)) {
return nb::cast(MachO::Parser::parse(std::move(*path_str)));
}
if (auto stream = PyIOStream::from_python(obj)) {
auto ptr = std::make_unique<PyIOStream>(std::move(*stream));
return nb::cast(MachO::Parser::parse(std::move(ptr), config));
}
logging::log(logging::LOG_ERR, "Can't create a LIEF stream interface over the provided io");
logging::log(logging::LOG_ERR,
"LIEF parser interface does not support Python object: " +
type2str(obj));
return nb::none();
},
R"delim(
Parse the given binary from a Python IO interface and return a :class:`~lief.MachO.FatBinary` object
Parse the given binary from the given input and return a :class:`~lief.MachO.FatBinary` object
One can configure the parsing with the ``config`` parameter. See :class:`~lief.MachO.ParserConfig`
One can configure the parser with the ``config`` parameter. See :class:`~lief.MachO.ParserConfig`
)delim"_doc,
"io"_a, "config"_a = ParserConfig::quick(),
"parse(io: io.IOBase, config: lief.MachO.ParserConfig = ...) -> lief.MachO.FatBinary | None"_p,
"obj"_a, "config"_a = ParserConfig::quick(),
"parse(obj: io.IOBase | os.PathLike, config: lief.MachO.ParserConfig = ...) -> lief.MachO.FatBinary | None"_p,
nb::rv_policy::take_ownership);
}