Skip to content

Commit 2ecbe8e

Browse files
committed
Self review, update pymetabind, resolve TODO
1 parent 8acf6d7 commit 2ecbe8e

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

include/pybind11/cast.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -876,20 +876,20 @@ struct holder_caster_foreign_helpers {
876876
PyObject *o;
877877
};
878878

879-
template <typename type>
880-
static bool try_shared_from_this(std::enable_shared_from_this<type> *value,
879+
template <typename base, typename type>
880+
static bool try_shared_from_this(std::enable_shared_from_this<base> *value,
881881
std::shared_ptr<type> *holder_out) {
882882
// object derives from enable_shared_from_this;
883883
// try to reuse an existing shared_ptr if one is known
884884
if (auto existing = try_get_shared_from_this(value)) {
885-
*holder_out = existing;
885+
*holder_out = std::static_pointer_cast<type>(existing);
886886
return true;
887887
}
888888
return false;
889889
}
890890

891891
template <typename type>
892-
static bool try_shared_from_this(type *, std::shared_ptr<type> *) {
892+
static bool try_shared_from_this(void *, std::shared_ptr<type> *) {
893893
return false;
894894
}
895895

include/pybind11/detail/foreign.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,8 @@ PYBIND11_NOINLINE bool foreign_internals::initialize() {
289289

290290
self.reset(new pymb_framework{});
291291
self->name = "pybind11 " PYBIND11_ABI_TAG;
292-
// TODO: pybind11 does leak some bindings; there should be a way to
293-
// indicate that (so that eg nanobind can disable its leak detection)
294-
// without promising to leak all bindings
295292
self->bindings_usable_forever = 0;
293+
self->leak_safe = 0;
296294
self->abi_lang = pymb_abi_lang_cpp;
297295
self->abi_extra = PYBIND11_PLATFORM_ABI_ID;
298296
self->from_python = foreign_cb_from_python;

include/pybind11/detail/pymetabind.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
* This functionality is intended to be used by the framework itself,
77
* rather than by users of the framework.
88
*
9-
* This is version 0.1 of pymetabind. Changelog:
9+
* This is version 0.1+dev of pymetabind. Changelog:
10+
*
11+
* Unreleased: Fix typo in Py_GIL_DISABLED. Add pymb_framework::leak_safe.
12+
* Add casts from PyTypeObject* to PyObject* where needed.
1013
*
1114
* Version 0.1: Initial draft. ABI may change without warning while we
1215
* 2025-08-16 prove out the concept. Please wait for a 1.0 release
@@ -195,7 +198,7 @@ struct pymb_registry {
195198
#endif
196199
};
197200

198-
#if defined(Py_GIL_DISALED)
201+
#if defined(Py_GIL_DISABLED)
199202
inline void pymb_lock_registry(struct pymb_registry* registry) {
200203
PyMutex_Lock(&registry->mutex);
201204
}
@@ -250,8 +253,15 @@ struct pymb_framework {
250253
// this framework's bindings in free-threaded builds.
251254
uint8_t bindings_usable_forever;
252255

256+
// Does this framework reliably deallocate all of its type and function
257+
// objects by the time the Python interpreter is finalized, in the absence
258+
// of bugs in user code? If not, it might cause leaks of other frameworks'
259+
// types or functions, via attributes or default argument values for
260+
// this framework's leaked objects.
261+
uint8_t leak_safe;
262+
253263
// Reserved for future extensions. Set to 0.
254-
uint8_t reserved[3];
264+
uint8_t reserved[2];
255265

256266
// The language to which this framework provides bindings: one of the
257267
// `pymb_abi_lang` enumerators.

0 commit comments

Comments
 (0)