Switch the bindings to nanobind (and update the CI)

This commit is contained in:
Romain Thomas
2023-07-09 21:33:43 +02:00
parent 7e387675ac
commit e37ffa9ec8
377 changed files with 12540 additions and 15730 deletions
+28 -48
View File
@@ -13,68 +13,48 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "pyART.hpp"
#include "LIEF/ART/Parser.hpp"
#include "LIEF/ART/File.hpp"
#include "LIEF/logging.hpp"
#include "ART/pyART.hpp"
#include "pyIOStream.hpp"
#include <string>
#include <memory>
namespace LIEF {
namespace ART {
#include <nanobind/stl/string.h>
#include <nanobind/stl/vector.h>
#include <nanobind/stl/unique_ptr.h>
namespace LIEF::ART::py {
template<>
void create<Parser>(py::module& m) {
void create<Parser>(nb::module_& m) {
using namespace LIEF::py;
// Parser (Parser)
m.def("parse",
static_cast<std::unique_ptr<File> (*) (const std::string&)>(&Parser::parse),
"Parse the given filename and return an " RST_CLASS_REF(lief.ART.File) " object",
nb::overload_cast<const std::string&>(&Parser::parse),
"Parse the given filename and return an " RST_CLASS_REF(lief.ART.File) " object"_doc,
"filename"_a,
py::return_value_policy::take_ownership);
nb::rv_policy::take_ownership);
m.def("parse",
static_cast<std::unique_ptr<File> (*) (std::vector<uint8_t>, const std::string&)>(&Parser::parse),
"Parse the given raw data and return an " RST_CLASS_REF(lief.ART.File) " object",
nb::overload_cast<std::vector<uint8_t>, const std::string&>(&Parser::parse),
"Parse the given raw data and return an " RST_CLASS_REF(lief.ART.File) " object"_doc,
"raw"_a, "name"_a = "",
py::return_value_policy::take_ownership);
nb::rv_policy::take_ownership);
m.def("parse",
[] (py::object byteio, const std::string& name) {
const auto& io = py::module::import("io");
const auto& RawIOBase = io.attr("RawIOBase");
const auto& BufferedIOBase = io.attr("BufferedIOBase");
const auto& TextIOBase = io.attr("TextIOBase");
py::object rawio;
if (py::isinstance(byteio, RawIOBase)) {
rawio = byteio;
}
else if (py::isinstance(byteio, BufferedIOBase)) {
rawio = byteio.attr("raw");
}
else if (py::isinstance(byteio, TextIOBase)) {
rawio = byteio.attr("buffer").attr("raw");
}
else {
throw py::type_error(py::repr(byteio).cast<std::string>().c_str());
}
std::string raw_str = static_cast<py::bytes>(rawio.attr("readall")());
std::vector<uint8_t> raw = {
std::make_move_iterator(std::begin(raw_str)),
std::make_move_iterator(std::end(raw_str))};
return LIEF::ART::Parser::parse(std::move(raw), name);
},
"io"_a, "name"_a = "",
py::return_value_policy::take_ownership);
}
[] (nb::object byteio, const std::string& name) -> nb::object {
if (auto stream = PyIOStream::from_python(std::move(byteio))) {
auto ptr = std::make_unique<PyIOStream>(std::move(*stream));
return nb::cast(Parser::parse(stream->content(), name));
}
logging::log(logging::LOG_ERR, "Can't create a LIEF stream interface over the provided io");
return nb::none();
}, "io"_a, "name"_a = "", nb::rv_policy::take_ownership);
}
}