Skip to content

Commit

Permalink
remove type_slot_callbacks feature, minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wjakob committed Apr 19, 2024
1 parent d25bde8 commit 96e1c0d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 50 deletions.
18 changes: 0 additions & 18 deletions docs/api_core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1876,24 +1876,6 @@ declarations in generated :ref:`stubs <stubs>`,
zero-initialized ``PyType_Slot`` element. See :ref:`Customizing type creation
<typeslots>` for more information about this feature.

.. cpp:struct:: type_slots_callback

.. cpp:function:: type_slots_callback(void (* callback)(detail::type_init_data * , PyType_Slot * &slots, size_t max_slots) noexcept)

This is an alternative to `type_slots` that provides a callback
which will be invoked during type creation to populate the type's
list of slots. It is used by `enum_`. It can be used in addition to
the `type_slots` annotation; if both are provided,
`type_slots_callback` runs first (so `type_slots` can override its choices).

The callback should execute ``*slots++ = {Py_tp_foo, (void *) handle_foo};``
at most *max_slots* times.

Information about the type under construction is available via the first
parameter received by the callback, but be aware that this is an internal
type which is not subject to nanobind's usual semantic versioning guarantees.
See ``include/nanobind/nb_class.h`` for more details.

.. cpp:struct:: template <typename T> intrusive_ptr

nanobind provides a custom interface for intrusive reference-counted C++
Expand Down
17 changes: 2 additions & 15 deletions include/nanobind/nb_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ enum class type_init_flags : uint32_t {
/// Is the 'base_py' field of the type_init_data structure set?
has_base_py = (1 << 22),

/// This type provides extra PyType_Slot fields via the 'type_slots'
/// and/or 'type_slots_callback' members of type_init_data
/// This type provides extra PyType_Slot fields
has_type_slots = (1 << 23),

all_init_flags = (0x1f << 19)
Expand Down Expand Up @@ -135,7 +134,6 @@ struct type_init_data : type_data {
PyTypeObject *base_py;
const char *doc;
const PyType_Slot *type_slots;
void (*type_slots_callback)(const type_init_data *d, PyType_Slot *&slots, size_t max_slots);
size_t supplement;
};

Expand All @@ -150,21 +148,10 @@ NB_INLINE void type_extra_apply(type_init_data &t, const char *doc) {
}

NB_INLINE void type_extra_apply(type_init_data &t, type_slots c) {
if ((t.flags & (uint32_t) type_init_flags::has_type_slots) == 0) {
t.flags |= (uint32_t) type_init_flags::has_type_slots;
t.type_slots_callback = nullptr;
}
t.flags |= (uint32_t) type_init_flags::has_type_slots;
t.type_slots = c.value;
}

NB_INLINE void type_extra_apply(type_init_data &t, type_slots_callback c) {
if ((t.flags & (uint32_t) type_init_flags::has_type_slots) == 0) {
t.flags |= (uint32_t) type_init_flags::has_type_slots;
t.type_slots = nullptr;
}
t.type_slots_callback = c.callback;
}

template <typename T>
NB_INLINE void type_extra_apply(type_init_data &t, intrusive_ptr<T> ip) {
t.flags |= (uint32_t) type_flags::intrusive_ptr;
Expand Down
23 changes: 6 additions & 17 deletions src/nb_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ void nb_type_unregister(type_data *t) noexcept {
}
}


check(!fail,
"nanobind::detail::nb_type_unregister(\"%s\"): could not "
"find type!", t->name);
Expand Down Expand Up @@ -980,22 +979,12 @@ PyObject *nb_type_new(const type_init_data *t) noexcept {

if (has_type_slots) {
size_t num_avail = nb_extra_slots;
if (t->type_slots_callback) {
PyType_Slot* first_new = s;
t->type_slots_callback(t, s, num_avail);
check(first_new + num_avail >= s,
"nanobind::detail::nb_type_new(\"%s\"): type_slots_callback "
"overflowed the slots array!", t_name);
num_avail -= (s - first_new);
}
if (t->type_slots) {
size_t i = 0;
while (t->type_slots[i].slot) {
check(i != num_avail,
"nanobind::detail::nb_type_new(\"%s\"): ran out of "
"type slots!", t_name);
*s++ = t->type_slots[i++];
}
size_t i = 0;
while (t->type_slots[i].slot) {
check(i != num_avail,
"nanobind::detail::nb_type_new(\"%s\"): ran out of "
"type slots!", t_name);
*s++ = t->type_slots[i++];
}
}

Expand Down

0 comments on commit 96e1c0d

Please sign in to comment.