Skip to content

Commit

Permalink
Fix typing
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Oct 22, 2023
1 parent c6db728 commit e5ef1da
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 104 deletions.
4 changes: 2 additions & 2 deletions api/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
6 changes: 5 additions & 1 deletion api/python/lief/PE.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions api/python/src/Abstract/pySection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Section>(nb::module_& m) {
Expand Down
28 changes: 16 additions & 12 deletions api/python/src/MachO/objects/pyRelocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<>
Expand Down
1 change: 1 addition & 0 deletions api/python/src/PE/objects/signature/pySignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <sstream>
#include <nanobind/stl/string.h>
#include <nanobind/stl/vector.h>
#include <nanobind/stl/unique_ptr.h>

#include "enums_wrapper.hpp"

Expand Down
36 changes: 12 additions & 24 deletions api/python/src/pyErr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,24 @@
namespace LIEF::py {

template<class RetTy>
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<class T>
struct type_caster<LIEF::py::typing_error<T>> {
using typing_type = typename LIEF::py::typing_error<T>;
NB_TYPE_CASTER(LIEF::py::typing_error<T>,
const_name("Union[") +
make_caster<typename typing_type::value_type>::Name +
const_name(", ") +
make_caster<typename typing_type::err_type>::Name +
const_name("]"));
static constexpr auto Name = nanobind::detail::const_name("Union[") +
nanobind::detail::make_caster<value_type>::Name +
nanobind::detail::const_name(", ") +
nanobind::detail::make_caster<err_type>::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<T> &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 <class Func, typename... Ts,
Expand Down
10 changes: 6 additions & 4 deletions api/python/src/pySafeString.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
#include <string>
#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);
Expand Down
48 changes: 11 additions & 37 deletions api/python/src/typing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,16 @@
#include <nanobind/nanobind.h>

#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<Class_> { \
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
22 changes: 4 additions & 18 deletions api/python/src/typing/InputParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<LIEF::py::typing::InputParser> {
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
Binary file not shown.

0 comments on commit e5ef1da

Please sign in to comment.