Skip to content

Commit

Permalink
removed superfluous nb_enum metaclass
Browse files Browse the repository at this point in the history
  • Loading branch information
wjakob committed Apr 12, 2023
1 parent 9ae3205 commit 9c19850
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 35 deletions.
22 changes: 18 additions & 4 deletions docs/changelog.rst
Expand Up @@ -5,7 +5,21 @@
Changelog
#########

nanobind uses a `semantic versioning <http://semver.org>`__ policy.
nanobind uses a `semantic versioning <http://semver.org>`__ policy for its API.
It also has a separate ABI version that is *not* subject to semantic
versioning.

The ABI version is relevant whenever a type binding from one extension module
should be visible in another (also nanobind-based) extension module. In this
case, both modules must use the same nanobind ABI version, or they will be
isolated from each other. Releases that don't explicitly mention an ABI version
below inherit that of the preceding release.

Version 1.2.0 (TBA)
-------------------

* Removed the superfluous ``nb_enum`` metaclass.
* ABI version 8.

Version 1.1.1 (April 6, 2023)
-----------------------------
Expand Down Expand Up @@ -171,7 +185,7 @@ Version 0.2.0 (March 3, 2023)
(commit `fe4965
<https://github.com/wjakob/nanobind/commit/fe4965369435bf7c0925bddf610553d0bb516e27>`__).
* Various minor fixes and improvements.
* Internals ABI version bump.
* ABI version 7.

Version 0.1.0 (January 3, 2023)
-------------------------------
Expand Down Expand Up @@ -199,7 +213,7 @@ Version 0.1.0 (January 3, 2023)
discovered in this codebase. (commit `3b81b1
<https://github.com/wjakob/nanobind/commit/3b81b18577e243118a659b524d4de9500a320312>`__).
* Various minor fixes and improvements.
* Internals ABI version bump.
* ABI version 6.

Version 0.0.9 (Nov 23, 2022)
----------------------------
Expand Down Expand Up @@ -261,7 +275,7 @@ Version 0.0.8 (Oct 27, 2022)
extension modules in conjunction with ``typing.py`` (commit `5e11e80
<https://github.com/wjakob/nanobind/commit/5e11e8032f777c0a34abd437dc6e84a909907c91>`__).
* Various minor fixes and improvements.
* Internals ABI version bump.
* ABI version 5.

Version 0.0.7 (Oct 14, 2022)
----------------------------
Expand Down
2 changes: 1 addition & 1 deletion docs/faq.rst
Expand Up @@ -137,7 +137,7 @@ for ``T`` that says: "ignore ``T`` and don't use a type caster to handle it".
.. warning::

If your extension consistes of multiple source code files that involve
If your extension consists of multiple source code files that involve
overlapping use of type casters and bindings, you are *treading on thin
ice*. It is easy to violate the *One Definition Rule* (ODR) [`details
<https://en.wikipedia.org/wiki/One_Definition_Rule>`_] in such a case, which
Expand Down
24 changes: 3 additions & 21 deletions src/nb_internals.cpp
Expand Up @@ -196,20 +196,6 @@ static PyType_Spec nb_type_spec = {
/* .slots = */ nb_type_slots
};

static PyType_Slot nb_enum_slots[] = {
{ Py_tp_base, nullptr },
{ 0, nullptr }
};

static PyType_Spec nb_enum_spec = {
/*.name = */"nanobind.nb_enum",
/*.basicsize = */0,
/*.itemsize = */0,
/*.flags = */Py_TPFLAGS_DEFAULT,
/*.slots = */nb_enum_slots
};


#if PY_VERSION_HEX >= 0x030C0000
static PyMemberDef nb_static_property_members[] = {
{ "__doc__", T_OBJECT, 0, 0, nullptr },
Expand Down Expand Up @@ -425,16 +411,12 @@ static void internals_make() {
int tp_itemsize = (int) PyType_Type.tp_itemsize;
int tp_basicsize = (int) PyType_Type.tp_basicsize;
#endif
nb_type_spec.basicsize = nb_enum_spec.basicsize = tp_basicsize
nb_type_spec.basicsize = tp_basicsize
+ (int) sizeof(type_data);
nb_type_spec.itemsize = nb_enum_spec.itemsize = tp_itemsize;
nb_type_spec.itemsize = tp_itemsize;
nb_type_slots[0].pfunc = &PyType_Type;
internals_p->nb_type = (PyTypeObject *) PyType_FromSpec(&nb_type_spec);

// Metaclass #2 (nb_enum)
nb_enum_slots[0].pfunc = internals_p->nb_type;
internals_p->nb_enum = (PyTypeObject *) PyType_FromSpec(&nb_enum_spec);

/// Static properties
#if defined(Py_LIMITED_API)
tp_basicsize = cast<int>(handle(&PyProperty_Type).attr("__basicsize__"));
Expand Down Expand Up @@ -465,7 +447,7 @@ static void internals_make() {

if (!internals_p->nb_func || !internals_p->nb_method ||
!internals_p->nb_bound_method || !internals_p->nb_type ||
!internals_p->nb_enum || !internals_p->nb_static_property ||
!internals_p->nb_static_property ||
!internals_p->nb_ndarray)
fail("nanobind::detail::internals_make(): type initialization failed!");

Expand Down
2 changes: 1 addition & 1 deletion src/nb_internals.h
Expand Up @@ -178,7 +178,7 @@ struct nb_internals {
PyObject *nb_module;

/// Registered metaclasses for nanobind classes and enumerations
PyTypeObject *nb_type, *nb_enum;
PyTypeObject *nb_type;

/// Types of nanobind functions and methods
PyTypeObject *nb_func, *nb_method, *nb_bound_method;
Expand Down
13 changes: 5 additions & 8 deletions src/nb_type.cpp
Expand Up @@ -445,8 +445,7 @@ PyObject *nb_type_new(const type_data *t) noexcept {

*s++ = { 0, nullptr };

PyTypeObject *metaclass = is_enum ? internals.nb_enum
: internals.nb_type;
PyTypeObject *metaclass = internals.nb_type;

#if PY_VERSION_HEX >= 0x030C0000
// Life is good, PyType_FromMetaclass() is available
Expand Down Expand Up @@ -785,8 +784,7 @@ bool nb_type_get(const std::type_info *cpp_type, PyObject *src, uint8_t flags,
PyTypeObject *src_type = Py_TYPE(src);
const std::type_info *cpp_type_src = nullptr;
const PyTypeObject *metaclass = Py_TYPE((PyObject *) src_type);
const bool src_is_nb_type = metaclass == internals.nb_type ||
metaclass == internals.nb_enum;
const bool src_is_nb_type = metaclass == internals.nb_type;

type_data *dst_type = nullptr;

Expand Down Expand Up @@ -867,7 +865,7 @@ void keep_alive(PyObject *nurse, PyObject *patient) {
nb_internals &internals = internals_get();
PyTypeObject *metaclass = Py_TYPE((PyObject *) Py_TYPE(nurse));

if (metaclass == internals.nb_type || metaclass == internals.nb_enum) {
if (metaclass == internals.nb_type) {
// Populate nanobind-internal data structures
keep_alive_set &keep_alive = internals.keep_alive[nurse];

Expand Down Expand Up @@ -909,7 +907,7 @@ void keep_alive(PyObject *nurse, void *payload,

nb_internals &internals = internals_get();

if (metaclass == internals.nb_type || metaclass == internals.nb_enum) {
if (metaclass == internals.nb_type) {
keep_alive_set &keep_alive = internals.keep_alive[nurse];
auto [it, success] = keep_alive.emplace(payload, callback);
if (!success)
Expand Down Expand Up @@ -1221,8 +1219,7 @@ bool nb_type_check(PyObject *t) noexcept {
nb_internals &internals = internals_get();
PyTypeObject *metaclass = Py_TYPE(t);

return metaclass == internals.nb_type ||
metaclass == internals.nb_enum;
return metaclass == internals.nb_type;
}

size_t nb_type_size(PyObject *t) noexcept {
Expand Down

0 comments on commit 9c19850

Please sign in to comment.