Skip to content

Commit 4fb9c85

Browse files
committed
Add unit tests
1 parent 2c5e718 commit 4fb9c85

17 files changed

+1552
-393
lines changed

docs/api_core.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3239,10 +3239,11 @@ additional information and caveats about this feature.
32393239

32403240
.. cpp:function:: void export_for_interop(handle type)
32413241

3242-
Make the Python type object *type*, which was bound in this nanobind domain,
3243-
be available for import by other binding libraries and other nanobind
3244-
domains. If they do so, then their bound functions will be able to accept
3245-
and return instances of *type*.
3242+
Make the Python type object *type*, which was created by a
3243+
:cpp:class:`nb::class_` or :cpp:class:`nb::enum_` binding statement in this
3244+
nanobind domain be available for import by other binding libraries and other
3245+
nanobind domains. If they do so, then their bound functions will be able to
3246+
accept and return instances of *type*.
32463247

32473248
Repeatedly exporting the same type is idempotent.
32483249

include/nanobind/nb_class.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ enum class enum_flags : uint32_t {
204204
is_signed = (1 << 2),
205205

206206
/// Is the underlying enumeration type Flag?
207-
is_flag = (1 << 3)
207+
is_flag = (1 << 3),
208+
209+
/// Was the enum successfully registered with nanobind?
210+
is_registered = (1 << 4),
208211
};
209212

210213
struct enum_init_data {
@@ -340,6 +343,12 @@ inline void interoperate_by_default(bool export_all = true,
340343
}
341344
template <class T = void>
342345
inline void import_for_interop(handle type) {
346+
if constexpr (!std::is_void_v<T>) {
347+
static_assert(
348+
detail::is_base_caster_v<detail::make_caster<T>>,
349+
"Types that are intercepted by a type caster cannot use the "
350+
"interoperability feature");
351+
}
343352
detail::nb_type_import(type.ptr(),
344353
std::is_void_v<T> ? nullptr : &typeid(T));
345354
}

src/nb_enum.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ PyObject *enum_create(enum_init_data *ed) noexcept {
6464
t->type = ed->type;
6565
t->type_py = (PyTypeObject *) result.ptr();
6666
t->flags = ed->flags;
67+
t->size = ed->size;
6768
t->enum_tbl.fwd = new enum_map();
6869
t->enum_tbl.rev = new enum_map();
6970
t->scope = ed->scope;
@@ -72,7 +73,8 @@ PyObject *enum_create(enum_init_data *ed) noexcept {
7273
type_init_data *t = (type_init_data *) p;
7374
delete (enum_map *) t->enum_tbl.fwd;
7475
delete (enum_map *) t->enum_tbl.rev;
75-
nb_type_unregister(t);
76+
if (t->flags & (uint32_t) enum_flags::is_registered)
77+
nb_type_unregister(t);
7678
free((char*) t->name);
7779
delete t;
7880
});
@@ -86,14 +88,15 @@ PyObject *enum_create(enum_init_data *ed) noexcept {
8688
return tp;
8789
}
8890

91+
t->flags |= (uint32_t) enum_flags::is_registered;
8992
result.attr("__nb_enum__") = tie_lifetimes;
9093

9194
make_immortal(result.ptr());
9295

9396
return result.release().ptr();
9497
}
9598

96-
static type_init_data *enum_get_type_data(handle tp) {
99+
type_init_data *enum_get_type_data(handle tp) {
97100
return (type_init_data *) (borrow<capsule>(handle(tp).attr("__nb_enum__"))).data();
98101
}
99102

0 commit comments

Comments
 (0)