Skip to content

Commit 1958388

Browse files
committed
Change some names containing 'foreign' to say 'interop' instead, so that we use 'foreign' specifically for types/frameworks that don't use our internals, rather than the concept of sharing with them
1 parent d0a02e2 commit 1958388

File tree

8 files changed

+155
-149
lines changed

8 files changed

+155
-149
lines changed

include/pybind11/detail/class.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ extern "C" inline void pybind11_meta_dealloc(PyObject *obj) {
226226
} else {
227227
internals.registered_types_cpp.erase(tindex);
228228
}
229-
get_foreign_internals().copy_move_ctors.erase(tindex);
229+
get_interop_internals().copy_move_ctors.erase(tindex);
230230
internals.registered_types_py.erase(tinfo->type);
231231

232232
// Actually just `std::erase_if`, but that's only available in C++20

include/pybind11/detail/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@
354354
#define PYBIND11_ENSURE_INTERNALS_READY \
355355
{ \
356356
pybind11::detail::get_internals_pp_manager().unref(); \
357-
pybind11::detail::get_foreign_internals_pp_manager().unref(); \
357+
pybind11::detail::get_interop_internals_pp_manager().unref(); \
358358
pybind11::detail::get_internals(); \
359359
}
360360

include/pybind11/detail/foreign.h

Lines changed: 100 additions & 100 deletions
Large diffs are not rendered by default.

include/pybind11/detail/internals.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ using copy_or_move_ctor = void *(*) (const void *);
311311
// version, it would be good for performance to also add a flag to `type_info`
312312
// indicating whether any foreign bindings are also known for its C++ type;
313313
// that way we can avoid an extra lookup when conversion to a native type fails.
314-
struct foreign_internals {
314+
struct interop_internals {
315315
// Registered foreign bindings for each C++ type.
316316
// Protected by internals::mutex.
317317
type_multimap<pymb_binding *> bindings;
@@ -337,34 +337,34 @@ struct foreign_internals {
337337
std::unique_ptr<pymb_framework> self;
338338

339339
// Remember the C++ type associated with each binding by
340-
// import_type_from_foreign(), so we can clean up `bindings` properly.
340+
// import_for_interop(), so we can clean up `bindings` properly.
341341
// Protected by internals::mutex.
342342
std::unordered_map<pymb_binding *, const std::type_info *> manual_imports;
343343

344-
// Pointer to `detail::export_type_to_foreign` in foreign.h, or nullptr if
344+
// Pointer to `detail::export_for_interop` in foreign.h, or nullptr if
345345
// export_all is false. This indirection is vital to avoid having every
346346
// compilation unit with a py::class_ pull in the callback methods in
347347
// foreign.h. Instead, only compilation units that call
348-
// set_foreign_type_defaults(), import_foreign_type(), or
349-
// export_type_to_foreign() will emit that code.
350-
void (*export_type_to_foreign)(type_info *);
348+
// interoperate_by_default(), import_for_interop(), or
349+
// export_for_interop() will emit that code.
350+
void (*export_for_interop)(type_info *);
351351

352352
// Should we automatically advertise our types to other binding frameworks,
353-
// or only when requested via pybind11::export_type_to_foreign()?
353+
// or only when requested via pybind11::export_for_interop()?
354354
// Never becomes false once it is set to true.
355355
bool export_all = false;
356356

357357
// Should we automatically use types advertised by other frameworks as
358358
// a fallback when we can't do a cast using pybind11 types, or only when
359-
// requested via pybind11::import_foreign_type()?
359+
// requested via pybind11::import_for_interop()?
360360
// Never becomes false once it is set to true.
361361
bool import_all = false;
362362

363363
// Are there any entries in `bindings` that don't correspond to our
364364
// own types?
365365
bool imported_any = false;
366366

367-
inline ~foreign_internals();
367+
inline ~interop_internals();
368368

369369
// Returns true if we initialized, false if someone else already did.
370370
inline bool initialize_if_needed() {
@@ -778,17 +778,17 @@ inline auto with_exception_translators(const F &cb)
778778
local_internals.registered_exception_translators);
779779
}
780780

781-
inline internals_pp_manager<foreign_internals> &get_foreign_internals_pp_manager() {
782-
static internals_pp_manager<foreign_internals> foreign_internals_pp_manager(
783-
PYBIND11_INTERNALS_ID "foreign", nullptr);
784-
return foreign_internals_pp_manager;
781+
inline internals_pp_manager<interop_internals> &get_interop_internals_pp_manager() {
782+
static internals_pp_manager<interop_internals> interop_internals_pp_manager(
783+
PYBIND11_INTERNALS_ID "interop", nullptr);
784+
return interop_internals_pp_manager;
785785
}
786786

787-
inline foreign_internals &get_foreign_internals() {
788-
auto &ppmgr = get_foreign_internals_pp_manager();
787+
inline interop_internals &get_interop_internals() {
788+
auto &ppmgr = get_interop_internals_pp_manager();
789789
auto &ptr = *ppmgr.get_pp();
790790
if (!ptr) {
791-
ptr.reset(new foreign_internals());
791+
ptr.reset(new interop_internals());
792792
}
793793
return *ptr;
794794
}

include/pybind11/detail/type_caster_base.h

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@ PYBIND11_NOINLINE handle get_type_handle(const std::type_info &tp,
265265
return handle((PyObject *) type_info->type);
266266
}
267267
if (foreign_ok) {
268-
auto &foreign_internals = detail::get_foreign_internals();
269-
if (foreign_internals.imported_any) {
268+
auto &interop_internals = detail::get_interop_internals();
269+
if (interop_internals.imported_any) {
270270
handle ret = with_internals([&](internals &) {
271-
auto range = foreign_internals.bindings.equal_range(tp);
271+
auto range = interop_internals.bindings.equal_range(tp);
272272
if (range.first != range.second) {
273273
return handle((PyObject *) range.first->second->pytype);
274274
}
@@ -1052,7 +1052,7 @@ class type_caster_generic {
10521052
if (!srcs.result.second) {
10531053
// No pybind11 type info. See if we can use another framework's
10541054
// type to complete this cast. Set srcs.used_foreign if so.
1055-
if (get_foreign_internals().imported_any) {
1055+
if (get_interop_internals().imported_any) {
10561056
if (handle ret = cast_foreign(srcs, policy, parent)) {
10571057
return ret;
10581058
}
@@ -1215,10 +1215,11 @@ class type_caster_generic {
12151215
return nullptr;
12161216
}
12171217

1218-
/// Try to load as a type exposed by a different binding framework.
1218+
/// Try to load as a type exposed by a different binding framework (which
1219+
/// might be an ABI-incompatible version of pybind11).
12191220
bool try_load_other_framework(handle src, bool convert) {
1220-
auto &foreign_internals = get_foreign_internals();
1221-
if (!foreign_internals.imported_any || !cpptype || src.is_none()) {
1221+
auto &interop_internals = get_interop_internals();
1222+
if (!interop_internals.imported_any || !cpptype || src.is_none()) {
12221223
return false;
12231224
}
12241225

@@ -1245,30 +1246,35 @@ class type_caster_generic {
12451246
return false;
12461247
}
12471248

1248-
/// Try to load with foreign typeinfo, if available. Used when there is no
1249-
/// native typeinfo, or when the native one wasn't able to produce a value.
1250-
PYBIND11_NOINLINE bool try_load_foreign(handle src, bool convert) {
1251-
constexpr auto *local_key = PYBIND11_MODULE_LOCAL_ID;
1252-
const auto pytype = type::handle_of(src);
1253-
if (!hasattr(pytype, local_key)) {
1254-
return try_load_other_framework(src, convert);
1255-
}
1256-
1257-
type_info *foreign_typeinfo = reinterpret_borrow<capsule>(getattr(pytype, local_key));
1258-
// Only consider this foreign loader if actually foreign and is a loader of the correct cpp
1259-
// type
1260-
if (foreign_typeinfo->module_local_load == &local_load
1261-
|| (cpptype && !same_type(*cpptype, *foreign_typeinfo->cpptype))) {
1249+
/// Try to load as a type bound as py::module_local() in a different (but
1250+
/// ABI-compatible) pybind11 module.
1251+
bool try_load_other_module_local(handle src, type_info *remote_typeinfo) {
1252+
// Only consider this loader if it's not ours and it loads the correct cpp type
1253+
if (remote_typeinfo->module_local_load == &local_load
1254+
|| (cpptype && !same_type(*cpptype, *remote_typeinfo->cpptype))) {
12621255
return false;
12631256
}
12641257

1265-
if (auto *result = foreign_typeinfo->module_local_load(src.ptr(), foreign_typeinfo)) {
1258+
if (auto *result = remote_typeinfo->module_local_load(src.ptr(), remote_typeinfo)) {
12661259
value = result;
12671260
return true;
12681261
}
12691262
return false;
12701263
}
12711264

1265+
/// Try to load with foreign typeinfo, if available. Used when there is no
1266+
/// native typeinfo, or when the native one wasn't able to produce a value.
1267+
PYBIND11_NOINLINE bool try_load_foreign(handle src, bool convert) {
1268+
constexpr auto *local_key = PYBIND11_MODULE_LOCAL_ID;
1269+
const auto pytype = type::handle_of(src);
1270+
if (hasattr(pytype, local_key)) {
1271+
return try_load_other_module_local(
1272+
src, reinterpret_borrow<capsule>(getattr(pytype, local_key)));
1273+
} else {
1274+
return try_load_other_framework(src, convert);
1275+
}
1276+
}
1277+
12721278
// Implementation of `load`; this takes the type of `this` so that it can dispatch the relevant
12731279
// bits of code between here and copyable_holder_caster where the two classes need different
12741280
// logic (without having to resort to virtual inheritance).

include/pybind11/embed.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ inline void finalize_interpreter() {
245245
if (detail::get_num_interpreters_seen() > 1) {
246246
detail::get_internals_pp_manager().unref();
247247
detail::get_local_internals_pp_manager().unref();
248-
detail::get_foreign_internals_pp_manager().unref();
248+
detail::get_interop_internals_pp_manager().unref();
249249

250250
// We know there can be no other interpreter alive now, so we can lower the count
251251
detail::get_num_interpreters_seen() = 1;
@@ -257,7 +257,7 @@ inline void finalize_interpreter() {
257257
// and check it after Py_Finalize().
258258
detail::get_internals_pp_manager().get_pp();
259259
detail::get_local_internals_pp_manager().get_pp();
260-
detail::get_foreign_internals_pp_manager().get_pp();
260+
detail::get_interop_internals_pp_manager().get_pp();
261261

262262
Py_Finalize();
263263

@@ -266,7 +266,7 @@ inline void finalize_interpreter() {
266266
// interpreter
267267
detail::get_internals_pp_manager().destroy();
268268
detail::get_local_internals_pp_manager().destroy();
269-
detail::get_foreign_internals_pp_manager().destroy();
269+
detail::get_interop_internals_pp_manager().destroy();
270270

271271
// We know there is no interpreter alive now, so we can reset the count
272272
detail::get_num_interpreters_seen() = 0;

include/pybind11/pybind11.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,9 +1648,9 @@ class generic_type : public object {
16481648
internals.registered_types_py[(PyTypeObject *) m_ptr] = {tinfo};
16491649
PYBIND11_WARNING_POP
16501650

1651-
auto &foreign_internals = get_foreign_internals();
1652-
if (foreign_internals.export_all) {
1653-
foreign_internals.export_type_to_foreign(tinfo);
1651+
auto &interop_internals = get_interop_internals();
1652+
if (interop_internals.export_all) {
1653+
interop_internals.export_for_interop(tinfo);
16541654
}
16551655
});
16561656

@@ -2136,7 +2136,7 @@ class class_ : public detail::generic_type {
21362136
generic_type::initialize(record);
21372137

21382138
with_internals([&](internals &internals) {
2139-
get_foreign_internals().copy_move_ctors.emplace(
2139+
get_interop_internals().copy_move_ctors.emplace(
21402140
*record.type, detail::type_caster_base<type_>::copy_and_move_ctors());
21412141
if (has_alias) {
21422142
auto &instances = record.module_local ? get_local_internals().registered_types_cpp

include/pybind11/subinterpreter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class subinterpreter {
180180
// internals themselves.
181181
detail::get_internals_pp_manager().get_pp();
182182
detail::get_local_internals_pp_manager().get_pp();
183-
detail::get_foreign_internals_pp_manager().get_pp();
183+
detail::get_interop_internals_pp_manager().get_pp();
184184

185185
// End it
186186
Py_EndInterpreter(destroy_tstate);
@@ -189,7 +189,7 @@ class subinterpreter {
189189
// py::capsule calls `get_internals()` during destruction), so we destroy afterward.
190190
detail::get_internals_pp_manager().destroy();
191191
detail::get_local_internals_pp_manager().destroy();
192-
detail::get_foreign_internals_pp_manager().destroy();
192+
detail::get_interop_internals_pp_manager().destroy();
193193

194194
// switch back to the old tstate and old GIL (if there was one)
195195
if (switch_back)

0 commit comments

Comments
 (0)