Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions cmake/nanobind-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ endfunction()

function (nanobind_build_library TARGET_NAME)
cmake_parse_arguments(PARSE_ARGV 1 ARG
"AS_SYSINCLUDE" "" "")
"AS_SYSINCLUDE;NOSTRIP;NOLTO" "" "")

if (TARGET ${TARGET_NAME})
return()
Expand Down Expand Up @@ -218,9 +218,14 @@ function (nanobind_build_library TARGET_NAME)
nanobind_link_options(${TARGET_NAME})
target_compile_definitions(${TARGET_NAME} PRIVATE -DNB_BUILD)
target_compile_definitions(${TARGET_NAME} PUBLIC -DNB_SHARED)
nanobind_lto(${TARGET_NAME})

nanobind_strip(${TARGET_NAME})
if (NOT ${ARG_NOLTO})
nanobind_lto(${TARGET_NAME})
endif()

if (NOT ${ARG_NOSTRIP})
nanobind_strip(${TARGET_NAME})
endif()
elseif(NOT WIN32 AND NOT APPLE)
target_compile_options(${TARGET_NAME} PUBLIC $<${NB_OPT_SIZE}:-ffunction-sections -fdata-sections>)
target_link_options(${TARGET_NAME} PUBLIC $<${NB_OPT_SIZE}:-Wl,--gc-sections>)
Expand Down
20 changes: 10 additions & 10 deletions include/nanobind/eigen/sparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ template <typename T> struct type_caster<T, enable_if_t<is_eigen_sparse_matrix_v
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
object obj = borrow(src);

try {
if (true) {
object matrix_type =
module_::import_("scipy.sparse")
.attr(RowMajor ? "csr_matrix" : "csc_matrix");
Expand Down Expand Up @@ -90,7 +90,7 @@ template <typename T> struct type_caster<T, enable_if_t<is_eigen_sparse_matrix_v
indices_caster.value.data(),
data_caster.value.data());
return true;
} catch (const python_error &) {
} if (false) {
return false;
}
}
Expand Down Expand Up @@ -121,9 +121,9 @@ template <typename T> struct type_caster<T, enable_if_t<is_eigen_sparse_matrix_v
}

object matrix_type;
try {
if (true) {
matrix_type = module_::import_("scipy.sparse").attr(RowMajor ? "csr_matrix" : "csc_matrix");
} catch (python_error &e) {
} if (false) { shim::exception_placeholder e;
e.restore();
return handle();
}
Expand All @@ -143,12 +143,12 @@ template <typename T> struct type_caster<T, enable_if_t<is_eigen_sparse_matrix_v
StorageIndexNDArray outer_indices(src->outerIndexPtr(), 1, outer_indices_shape, owner);
StorageIndexNDArray inner_indices(src->innerIndexPtr(), 1, data_shape, owner);

try {
if (true) {
return matrix_type(nanobind::make_tuple(
std::move(data), std::move(inner_indices), std::move(outer_indices)),
nanobind::make_tuple(rows, cols))
.release();
} catch (python_error &e) {
} if (false) { shim::exception_placeholder e;
e.restore();
return handle();
}
Expand Down Expand Up @@ -187,7 +187,7 @@ struct type_caster<Eigen::Map<T>, enable_if_t<is_eigen_sparse_matrix_v<T>>> {
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
flags = ~(uint8_t) cast_flags::convert;

try {
if (true) {
object matrix_type =
module_::import_("scipy.sparse")
.attr(RowMajor ? "csr_matrix" : "csc_matrix");
Expand Down Expand Up @@ -216,7 +216,7 @@ struct type_caster<Eigen::Map<T>, enable_if_t<is_eigen_sparse_matrix_v<T>>> {
rows = cast<Index>(shape_o[0]);
cols = cast<Index>(shape_o[1]);
nnz = cast<Index>(src.attr("nnz"));
} catch (const python_error &) {
} if (false) {
return false;
}

Expand All @@ -234,7 +234,7 @@ struct type_caster<Eigen::Map<T>, enable_if_t<is_eigen_sparse_matrix_v<T>>> {
}

object matrix_type;
try {
if (true) {
matrix_type = module_::import_("scipy.sparse")
.attr(RowMajor ? "csr_matrix" : "csc_matrix");

Expand All @@ -255,7 +255,7 @@ struct type_caster<Eigen::Map<T>, enable_if_t<is_eigen_sparse_matrix_v<T>>> {
cast(outer_indices, rv_policy::reference)),
nanobind::make_tuple(rows, cols))
.release();
} catch (python_error &e) {
} if (false) { shim::exception_placeholder e;
e.restore();
return handle();
}
Expand Down
37 changes: 37 additions & 0 deletions include/nanobind/exception_shim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <cstdio>
#include <cstdlib>

#define throwShim(X) shim::throw_(X, __FILE__, __LINE__)

namespace shim {

template<typename T>
[[noreturn]] inline void throw_(T &&Exception, const char *Filename, unsigned Line) {
fprintf(stderr, "Exception thrown at %s:%d\n%s\n", Filename, Line, Exception.what());
std::abort();
}

class exception_placeholder {
public:
exception_placeholder() = default;
~exception_placeholder() = default;

template<typename T>
exception_placeholder& operator=(const T &) { std::abort(); }
void restore() { std::abort(); }
const char *what() { return ""; }
};

}

NAMESPACE_BEGIN(NB_NAMESPACE)
NAMESPACE_BEGIN(detail)

inline bool set_builtin_exception_status(shim::exception_placeholder &) {
std::abort();
}

NAMESPACE_END(detail)
NAMESPACE_END(NB_NAMESPACE)
2 changes: 1 addition & 1 deletion include/nanobind/make_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ typed<iterator, ValueType> make_iterator_impl(handle scope, const char *name,

if (s.it == s.end) {
s.first_or_done = true;
throw stop_iteration();
throwShim(stop_iteration());
}

return Access()(s.it);
Expand Down
3 changes: 2 additions & 1 deletion include/nanobind/nanobind.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@
#include <exception>
#include <stdexcept>
#include <type_traits>
#include <typeinfo>
#include <utility>
#include <new>
#include "typeinfo_shim.h"

// Implementation. The nb_*.h files should only be included through nanobind.h
// IWYU pragma: begin_exports
#include "nb_defs.h"
#include "exception_shim.h"
#include "nb_enums.h"
#include "nb_traits.h"
#include "nb_tuple.h"
Expand Down
2 changes: 1 addition & 1 deletion include/nanobind/nb_attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ struct func_data_prelim_base {
const char *descr;

/// C++ types referenced by 'descr'
const std::type_info **descr_types;
const shim::type_info **descr_types;

/// Supplementary flags
uint32_t flags;
Expand Down
14 changes: 7 additions & 7 deletions include/nanobind/nb_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,13 @@ template <typename T>
struct type_caster<T, enable_if_t<std::is_enum_v<T>>> {
NB_INLINE bool from_python(handle src, uint8_t flags, cleanup_list *) noexcept {
int64_t result;
bool rv = enum_from_python(&typeid(T), src.ptr(), &result, flags);
bool rv = enum_from_python(&typeidShim<T>(), src.ptr(), &result, flags);
value = (T) result;
return rv;
}

NB_INLINE static handle from_cpp(T src, rv_policy, cleanup_list *) noexcept {
return enum_from_cpp(&typeid(T), (int64_t) src);
return enum_from_cpp(&typeidShim<T>(), (int64_t) src);
}

NB_TYPE_CASTER(T, const_name<T>())
Expand Down Expand Up @@ -316,7 +316,7 @@ template <> struct type_caster<char> {
if (can_cast<char>())
return value[0];
else
throw next_overload();
throwShim(next_overload());
}
};

Expand Down Expand Up @@ -465,7 +465,7 @@ template <typename Type_> struct type_caster_base : type_caster_base_tag {

NB_INLINE bool from_python(handle src, uint8_t flags,
cleanup_list *cleanup) noexcept {
return nb_type_get(&typeid(Type), src.ptr(), flags, cleanup,
return nb_type_get(&typeidShim<Type>(), src.ptr(), flags, cleanup,
(void **) &value);
}

Expand All @@ -479,7 +479,7 @@ template <typename Type_> struct type_caster_base : type_caster_base_tag {
ptr = (Type *) &value;

policy = infer_policy<T>(policy);
const std::type_info *type = &typeid(Type);
const shim::type_info *type = &typeidShim<Type>();

constexpr bool has_type_hook =
!std::is_base_of_v<std::false_type, type_hook<Type>>;
Expand All @@ -489,8 +489,8 @@ template <typename Type_> struct type_caster_base : type_caster_base_tag {
if constexpr (!std::is_polymorphic_v<Type>) {
return nb_type_put(type, ptr, policy, cleanup);
} else {
const std::type_info *type_p =
(!has_type_hook && ptr) ? &typeid(*ptr) : nullptr;
const shim::type_info *type_p =
(!has_type_hook && ptr) ? &typeidShim(*ptr) : nullptr;
return nb_type_put_p(type, type_p, ptr, policy, cleanup);
}
}
Expand Down
24 changes: 12 additions & 12 deletions include/nanobind/nb_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ struct nb_alias_chain;

// Implicit conversions for C++ type bindings, used in type_data below
struct implicit_t {
const std::type_info **cpp;
const shim::type_info **cpp;
bool (**py)(PyTypeObject *, PyObject *, cleanup_list *) noexcept;
};

Expand All @@ -112,7 +112,7 @@ struct type_data {
uint32_t align : 8;
uint32_t flags : 24;
const char *name;
const std::type_info *type;
const shim::type_info *type;
PyTypeObject *type_py;
nb_alias_chain *alias_chain;
#if defined(Py_LIMITED_API)
Expand All @@ -135,7 +135,7 @@ struct type_data {
/// Information about a type that is only relevant when it is being created
struct type_init_data : type_data {
PyObject *scope;
const std::type_info *base;
const shim::type_info *base;
PyTypeObject *base_py;
const char *doc;
const PyType_Slot *type_slots;
Expand Down Expand Up @@ -210,7 +210,7 @@ enum class enum_flags : uint32_t {
};

struct enum_init_data {
const std::type_info *type;
const shim::type_info *type;
PyObject *scope;
const char *name;
const char *docstr;
Expand Down Expand Up @@ -289,7 +289,7 @@ NAMESPACE_END(detail)
inline bool type_check(handle h) { return detail::nb_type_check(h.ptr()); }
inline size_t type_size(handle h) { return detail::nb_type_size(h.ptr()); }
inline size_t type_align(handle h) { return detail::nb_type_align(h.ptr()); }
inline const std::type_info& type_info(handle h) { return *detail::nb_type_info(h.ptr()); }
inline const shim::type_info& type_info(handle h) { return *detail::nb_type_info(h.ptr()); }
template <typename T>
inline T &type_supplement(handle h) { return *(T *) detail::nb_type_supplement(h.ptr()); }
inline str type_name(handle h) { return steal<str>(detail::nb_type_name(h.ptr())); }
Expand Down Expand Up @@ -401,7 +401,7 @@ template <typename Arg> struct init_implicit : def_visitor<init_implicit<Arg>> {
return Caster().from_python(
src, detail::cast_flags::convert, cleanup);
},
&typeid(Type));
&typeidShim<Type>());
}
}
};
Expand All @@ -416,7 +416,7 @@ namespace detail {
cpp_function_def(
[](handle type) {
if (!type_check(type))
throw cast_error();
throwShim(cast_error());
return inst_alloc(type);
},
scope(cls), name("__new__"));
Expand Down Expand Up @@ -564,10 +564,10 @@ class class_ : public object {
d.size = (uint32_t) sizeof(Alias);
d.name = name;
d.scope = scope.ptr();
d.type = &typeid(T);
d.type = &typeidShim<T>();

if constexpr (!std::is_same_v<Base, T>) {
d.base = &typeid(Base);
d.base = &typeidShim<Base>();
d.flags |= (uint32_t) detail::type_init_flags::has_base;
}

Expand Down Expand Up @@ -776,7 +776,7 @@ template <typename T> class enum_ : public object {
template <typename... Extra>
NB_INLINE enum_(handle scope, const char *name, const Extra &... extra) {
detail::enum_init_data ed { };
ed.type = &typeid(T);
ed.type = &typeidShim<T>();
ed.scope = scope.ptr();
ed.name = name;
ed.flags = std::is_signed_v<Underlying>
Expand Down Expand Up @@ -847,15 +847,15 @@ template <typename Source, typename Target> void implicitly_convertible() {
"unless it is opaque.");

if constexpr (detail::is_base_caster_v<Caster>) {
detail::implicitly_convertible(&typeid(Source), &typeid(Target));
detail::implicitly_convertible(&typeidShim<Source>(), &typeidShim<Target>());
} else {
detail::implicitly_convertible(
[](PyTypeObject *, PyObject *src,
detail::cleanup_list *cleanup) noexcept -> bool {
return Caster().from_python(src, detail::cast_flags::convert,
cleanup);
},
&typeid(Target));
&typeidShim<Target>());
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions include/nanobind/nb_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@
# endif
#endif

#if defined(__GNUC__) && !defined(_WIN32)
# define NB_NAMESPACE nanobind __attribute__((visibility("hidden")))
#else
# define NB_NAMESPACE nanobind
#endif
#define NB_NAMESPACE nanobind

#if defined(__GNUC__)
# define NB_UNLIKELY(x) __builtin_expect(bool(x), 0)
Expand Down Expand Up @@ -175,16 +171,16 @@
static void nanobind_##name##_exec_impl(nanobind::module_); \
static int nanobind_##name##_exec(PyObject *m) { \
nanobind::detail::nb_module_exec(NB_DOMAIN_STR, m); \
try { \
if (true) { \
nanobind_##name##_exec_impl( \
nanobind::borrow<nanobind::module_>(m)); \
return 0; \
} catch (nanobind::python_error &e) { \
} if (false) { shim::exception_placeholder e; \
e.restore(); \
nanobind::chain_error( \
PyExc_ImportError, \
"Encountered an error while initializing the extension."); \
} catch (const std::exception &e) { \
} if (false) { shim::exception_placeholder e; \
PyErr_SetString(PyExc_ImportError, e.what()); \
} \
return -1; \
Expand Down
4 changes: 2 additions & 2 deletions include/nanobind/nb_descr.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ struct descr {
constexpr size_t type_count() const { return sizeof...(Ts); }
constexpr size_t size() const { return N; }

NB_INLINE void put_types(const std::type_info **out) const {
NB_INLINE void put_types(const shim::type_info **out) const {
size_t ctr = 0;
((out[ctr++] = &typeid(Ts)), ...);
((out[ctr++] = &typeidShim<Ts>()), ...);
out[ctr++] = nullptr;
}
};
Expand Down
Loading
Loading