diff --git a/api/python/CMakeLists.txt b/api/python/CMakeLists.txt index b41968d6d9..4d0886b350 100644 --- a/api/python/CMakeLists.txt +++ b/api/python/CMakeLists.txt @@ -36,8 +36,8 @@ endmacro() if (LIEF_EXTERNAL_NANOBINDS) find_package(nanobind REQUIRED) else() - set(NANOBIND_VERSION 1.6.2.r20.g46ed29e) - set(NANOBIND_SHA256 SHA256=fd91abf745de6b67edf87f9549df5380b666a66218b0bf170e27313d3a726ee7) + set(NANOBIND_VERSION 1.7.0.r7.gfd1f04b) + set(NANOBIND_SHA256 SHA256=c6b1e0459559fc753ec6ef0911744f5fbc5f995e868dcb1ecef8f0c6c3d376d2) set(NANOBIND_URL "${THIRD_PARTY_DIRECTORY}/nanobind-${NANOBIND_VERSION}.zip" CACHE STRING "URL to the Nanobind") FetchContent_Declare(nanobind URL ${NANOBIND_URL} diff --git a/api/python/lief/PE.pyi b/api/python/lief/PE.pyi index 5d0a543440..3d16f2d00b 100644 --- a/api/python/lief/PE.pyi +++ b/api/python/lief/PE.pyi @@ -2458,8 +2458,12 @@ class Signature(lief.Object): def find_crt_subject(self, subject: str) -> lief.PE.x509: ... @overload def find_crt_subject(self, subject: str, serialno: list[int]) -> lief.PE.x509: ... + @overload + @staticmethod + def parse(path: str) -> Optional[lief.PE.Signature]: ... + @overload @staticmethod - def parse(self, *args, **kwargs) -> Any: ... + def parse(raw: list[int], skip_header: bool = ...) -> Optional[lief.PE.Signature]: ... @property def certificates(self) -> lief.PE.Signature.it_const_crt: ... @property diff --git a/api/python/src/Abstract/pySection.cpp b/api/python/src/Abstract/pySection.cpp index a89fa823f7..a46fa8e1af 100644 --- a/api/python/src/Abstract/pySection.cpp +++ b/api/python/src/Abstract/pySection.cpp @@ -28,15 +28,16 @@ namespace LIEF::py { -struct search_result { + +struct search_result : public nanobind::object { LIEF_PY_DEFAULT_CTOR(search_result, nanobind::object); - LIEF_PY_DEFAULT_WRAPPER(search_result); -}; -} -LIEF_PY_DEFAULT_NB_CASTER(LIEF::py::search_result, "Optional[int]") + NB_OBJECT_DEFAULT(search_result, object, "Optional[int]", check) -namespace LIEF::py { + static bool check(handle h) { + return true; + } +}; template<> void create
(nb::module_& m) { diff --git a/api/python/src/MachO/objects/pyRelocation.cpp b/api/python/src/MachO/objects/pyRelocation.cpp index f06462bbc6..ff6c14070f 100644 --- a/api/python/src/MachO/objects/pyRelocation.cpp +++ b/api/python/src/MachO/objects/pyRelocation.cpp @@ -26,22 +26,26 @@ #include "typing.hpp" namespace LIEF::MachO::py { -struct relocations_typing { - LIEF_PY_DEFAULT_CTOR(relocations_typing, nb::object); - LIEF_PY_DEFAULT_WRAPPER(relocations_typing); + +struct relocations_typing : public nanobind::object { + LIEF_PY_DEFAULT_CTOR(relocations_typing, nb::object); + NB_OBJECT_DEFAULT(relocations_typing, object, + "Union[" + "lief.MachO.X86_RELOCATION, " + "lief.MachO.X86_64_RELOCATION, " + "lief.MachO.PPC_RELOCATION, " + "lief.MachO.ARM_RELOCATION, " + "lief.MachO.ARM64_RELOCATION, " + "lief.MachO.REBASE_TYPES, " + "]", check) + + static bool check(handle h) { + return true; + } }; } -LIEF_PY_DEFAULT_NB_CASTER(LIEF::MachO::py::relocations_typing, - "Union[" "lief.MachO.X86_RELOCATION, " - "lief.MachO.X86_64_RELOCATION, " - "lief.MachO.PPC_RELOCATION, " - "lief.MachO.ARM_RELOCATION, " - "lief.MachO.ARM64_RELOCATION, " - "lief.MachO.REBASE_TYPES, " - "]"); - namespace LIEF::MachO::py { template<> diff --git a/api/python/src/PE/objects/signature/pySignature.cpp b/api/python/src/PE/objects/signature/pySignature.cpp index 00cfb43bb9..0ab1b0feae 100644 --- a/api/python/src/PE/objects/signature/pySignature.cpp +++ b/api/python/src/PE/objects/signature/pySignature.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "enums_wrapper.hpp" diff --git a/api/python/src/pyErr.hpp b/api/python/src/pyErr.hpp index b0c861cd88..a8f9df528b 100644 --- a/api/python/src/pyErr.hpp +++ b/api/python/src/pyErr.hpp @@ -24,36 +24,24 @@ namespace LIEF::py { template -struct typing_error { +struct typing_error : public nanobind::object { using value_type = typename RetTy::value_type; using err_type = typename RetTy::error_type; - LIEF_PY_DEFAULT_CTOR(typing_error, nanobind::object); - LIEF_PY_DEFAULT_WRAPPER(typing_error); -}; -} -NAMESPACE_BEGIN(NB_NAMESPACE) -NAMESPACE_BEGIN(detail) -template -struct type_caster> { - using typing_type = typename LIEF::py::typing_error; - NB_TYPE_CASTER(LIEF::py::typing_error, - const_name("Union[") + - make_caster::Name + - const_name(", ") + - make_caster::Name + - const_name("]")); + static constexpr auto Name = nanobind::detail::const_name("Union[") + + nanobind::detail::make_caster::Name + + nanobind::detail::const_name(", ") + + nanobind::detail::make_caster::Name + + nanobind::detail::const_name("]"); - bool from_python(handle src, uint8_t, cleanup_list *) noexcept { - return false; - } - static handle from_cpp(const LIEF::py::typing_error &value, rv_policy, - cleanup_list *) noexcept { - return value.obj; + LIEF_PY_DEFAULT_CTOR(typing_error, nanobind::object); + NB_OBJECT_DEFAULT_NONAME(typing_error, object, check) + + static bool check(handle h) { + return true; } }; -NAMESPACE_END(detail) -NAMESPACE_END(NB_NAMESPACE) +} namespace LIEF::py { template #include "typing.hpp" -struct safe_string_t { +struct safe_string_t : public nanobind::object { LIEF_PY_DEFAULT_CTOR(safe_string_t, nanobind::str); LIEF_PY_DEFAULT_CTOR(safe_string_t, nanobind::bytes); - LIEF_PY_DEFAULT_WRAPPER(safe_string_t); -}; + NB_OBJECT_DEFAULT(safe_string_t, object, "Union[str, bytes]", check) -LIEF_PY_DEFAULT_NB_CASTER(safe_string_t, "Union[str, bytes]"); + static bool check(handle h) { + return true; + } +}; namespace LIEF::py { safe_string_t safe_string(const std::string& str); diff --git a/api/python/src/typing.hpp b/api/python/src/typing.hpp index 59c9c18b7e..fd71d34f0c 100644 --- a/api/python/src/typing.hpp +++ b/api/python/src/typing.hpp @@ -18,42 +18,16 @@ #include #define LIEF_PY_DEFAULT_CTOR(Class_, Ty) \ - Class_(Ty obj_) : obj(std::move(obj_)) {} - -#define LIEF_PY_DEFAULT_WRAPPER(Class_) \ - Class_(const Class_&) = default; \ - Class_(Class_&&) = default; \ - \ - Class_& operator=(const Class_&) = default; \ - Class_& operator=(Class_&&) = default; \ - \ - operator nanobind::object () const { \ - return obj; \ - } \ - \ - operator nanobind::object && () { \ - return std::move(obj); \ - } \ - \ - Class_() : obj(nanobind::none()) {} \ - nanobind::object obj; - - -#define LIEF_PY_DEFAULT_NB_CASTER(Class_, Typing) \ -NAMESPACE_BEGIN(NB_NAMESPACE) \ -NAMESPACE_BEGIN(detail) \ -template <> struct type_caster { \ - NB_TYPE_CASTER(Class_, const_name(Typing)); \ - \ - bool from_python(handle src, uint8_t, cleanup_list *) noexcept { \ - return false; \ - } \ - static handle from_cpp(const Class_ &value, rv_policy, \ - cleanup_list *) noexcept { \ - return value.obj; \ - } \ -}; \ -NAMESPACE_END(detail) \ -NAMESPACE_END(NB_NAMESPACE) + Class_(Ty obj_) : nanobind::object(obj_, nanobind::detail::borrow_t{}) {} +#define NB_OBJECT_DEFAULT_NONAME(Type, Parent, Check) \ +public: \ + NB_INLINE Type(handle h, ::nanobind::detail::borrow_t) \ + : Parent(h, ::nanobind::detail::borrow_t{}) { } \ + NB_INLINE Type(handle h, ::nanobind::detail::steal_t) \ + : Parent(h, ::nanobind::detail::steal_t{}) { } \ + NB_INLINE static bool check_(handle h) { \ + return Check(h.ptr()); \ + } \ + NB_INLINE Type() : Parent() {} #endif diff --git a/api/python/src/typing/InputParser.hpp b/api/python/src/typing/InputParser.hpp index 89f36ad36d..f7a45f38a5 100644 --- a/api/python/src/typing/InputParser.hpp +++ b/api/python/src/typing/InputParser.hpp @@ -18,28 +18,14 @@ #include "typing.hpp" namespace LIEF::py::typing { -struct InputParser { +struct InputParser : public nanobind::object { LIEF_PY_DEFAULT_CTOR(InputParser, nanobind::object); - LIEF_PY_DEFAULT_WRAPPER(InputParser); -}; -} -NAMESPACE_BEGIN(NB_NAMESPACE) -NAMESPACE_BEGIN(detail) -template <> struct type_caster { - using Type = LIEF::py::typing::InputParser; - NB_TYPE_CASTER(LIEF::py::typing::InputParser, const_name("Union[io.IOBase | os.PathLike]")); + NB_OBJECT_DEFAULT(InputParser, object, "Union[io.IOBase | os.PathLike]", check) - bool from_python(handle src, uint8_t, cleanup_list *) noexcept { - value = nanobind::object(src, nanobind::detail::borrow_t()); + static bool check(handle h) { return true; } - static handle from_cpp(const Type &value, rv_policy, - cleanup_list *) noexcept { - return nanobind::none(); - } }; -NAMESPACE_END(detail) -NAMESPACE_END(NB_NAMESPACE) - +} #endif diff --git a/third-party/nanobind-1.6.2.r20.g46ed29e.zip b/third-party/nanobind-1.7.0.r7.gfd1f04b.zip similarity index 91% rename from third-party/nanobind-1.6.2.r20.g46ed29e.zip rename to third-party/nanobind-1.7.0.r7.gfd1f04b.zip index c392b89d52..096ced5fb1 100644 Binary files a/third-party/nanobind-1.6.2.r20.g46ed29e.zip and b/third-party/nanobind-1.7.0.r7.gfd1f04b.zip differ