Skip to content

Commit 493a98e

Browse files
committed
CI fixes
1 parent 1f47d3f commit 493a98e

File tree

9 files changed

+209
-150
lines changed

9 files changed

+209
-150
lines changed

.clang-tidy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,4 @@ CheckOptions:
7878
value: true
7979

8080
HeaderFilterRegex: 'pybind11/.*h'
81+
ExcludeHeaderFilterRegex: 'pybind11/detail/pymetabind.h'

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ci:
1919
autoupdate_schedule: monthly
2020

2121
# third-party content
22-
exclude: ^tools/JoinPaths.cmake$
22+
exclude: ^(tools/JoinPaths.cmake|include/pybind11/detail/pymetabind.h)$
2323

2424
repos:
2525

include/pybind11/cast.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,10 +864,11 @@ struct holder_helper {
864864

865865
struct holder_caster_foreign_helpers {
866866
struct py_deleter {
867-
void operator()(const void *) noexcept {
867+
void operator()(const void *) const noexcept {
868868
// Don't run the deleter if the interpreter has been shut down
869-
if (!Py_IsInitialized())
869+
if (Py_IsInitialized() == 0) {
870870
return;
871+
}
871872
gil_scoped_acquire guard;
872873
Py_DECREF(o);
873874
}

include/pybind11/detail/foreign.h

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,15 @@ inline void *foreign_cb_from_python(pymb_binding *binding,
8484
type_caster_generic caster{static_cast<const type_info *>(binding->context)};
8585
void *ret = nullptr;
8686
try {
87-
if (caster.load(pyobj, convert)) {
87+
if (caster.load(pyobj, convert != 0)) {
8888
ret = caster.value;
8989
}
9090
} catch (...) {
9191
translate_exception(std::current_exception());
9292
PyErr_WriteUnraisable(pyobj);
9393
}
9494
if (keep_referenced) {
95+
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
9596
for (PyObject *item : holder->list_patients()) {
9697
keep_referenced(keep_referenced_ctx, item);
9798
}
@@ -103,7 +104,7 @@ inline PyObject *foreign_cb_to_python(pymb_binding *binding,
103104
void *cobj,
104105
enum pymb_rv_policy rvp_,
105106
PyObject *parent) noexcept {
106-
auto *ti = static_cast<const type_info *>(binding->context);
107+
const auto *ti = static_cast<const type_info *>(binding->context);
107108
if (cobj == nullptr) {
108109
return none().release().ptr();
109110
}
@@ -177,8 +178,9 @@ inline void foreign_cb_translate_exception(const void *eptr) {
177178
// frameworks' exceptions), it's the second-last one and should
178179
// be skipped too. We don't want mutual recursion between
179180
// different frameworks' translators.
180-
if (!get_foreign_internals().exc_frameworks.empty())
181+
if (!get_foreign_internals().exc_frameworks.empty()) {
181182
++leader;
183+
}
182184

183185
for (; leader != exception_translators.end(); ++it, ++leader) {
184186
try {
@@ -237,8 +239,9 @@ inline void foreign_cb_remove_foreign_binding(pymb_binding *binding) noexcept {
237239
should_remove_auto &= (it->second != binding->native_type);
238240
foreign_internals.manual_imports.erase(it);
239241
}
240-
if (should_remove_auto)
242+
if (should_remove_auto) {
241243
remove_from_type((const std::type_info *) binding->native_type);
244+
}
242245
});
243246
}
244247

@@ -255,15 +258,17 @@ inline void foreign_cb_add_foreign_framework(pymb_framework *framework) noexcept
255258
// translator).
256259
auto leader = exception_translators.begin();
257260
auto trailer = exception_translators.before_begin();
258-
while (++leader != exception_translators.end())
261+
while (++leader != exception_translators.end()) {
259262
++trailer;
263+
}
260264
exception_translators.insert_after(trailer, foreign_exception_translator);
261265
}
262266
// Add the new framework at the end of the list
263267
auto leader = foreign_internals.exc_frameworks.begin();
264268
auto trailer = leader;
265-
while (++leader != foreign_internals.exc_frameworks.end())
269+
while (++leader != foreign_internals.exc_frameworks.end()) {
266270
++trailer;
271+
}
267272
foreign_internals.exc_frameworks.insert_after(trailer, framework);
268273
});
269274
}
@@ -274,13 +279,15 @@ inline void foreign_cb_add_foreign_framework(pymb_framework *framework) noexcept
274279
// Advertise our existence, and the above callbacks, to other frameworks
275280
PYBIND11_NOINLINE bool foreign_internals::initialize() {
276281
bool inited_by_us = with_internals([&](internals &) {
277-
if (registry)
282+
if (registry) {
278283
return false;
284+
}
279285
registry = pymb_get_registry();
280-
if (!registry)
286+
if (!registry) {
281287
throw error_already_set();
288+
}
282289

283-
self = std::make_unique<pymb_framework>();
290+
self.reset(new pymb_framework{});
284291
self->name = "pybind11 " PYBIND11_ABI_TAG;
285292
// TODO: pybind11 does leak some bindings; there should be a way to
286293
// indicate that (so that eg nanobind can disable its leak detection)
@@ -315,27 +322,32 @@ inline foreign_internals::~foreign_internals() = default;
315322
PYBIND11_NOINLINE void import_foreign_type(type pytype, const std::type_info *cpptype) {
316323
auto &foreign_internals = get_foreign_internals();
317324
pymb_binding *binding = pymb_get_binding(pytype.ptr());
318-
if (!binding)
325+
if (!binding) {
319326
pybind11_fail("pybind11::import_foreign_type(): type does not define "
320327
"a __pymetabind_binding__");
321-
if (binding->framework == foreign_internals.self.get())
328+
}
329+
if (binding->framework == foreign_internals.self.get()) {
322330
pybind11_fail("pybind11::import_foreign_type(): type is not foreign");
331+
}
323332
if (!cpptype) {
324-
if (binding->framework->abi_lang != pymb_abi_lang_cpp)
333+
if (binding->framework->abi_lang != pymb_abi_lang_cpp) {
325334
pybind11_fail("pybind11::import_foreign_type(): type is not "
326335
"written in C++, so you must specify a C++ type");
327-
if (binding->framework->abi_extra != foreign_internals.self->abi_extra)
336+
}
337+
if (binding->framework->abi_extra != foreign_internals.self->abi_extra) {
328338
pybind11_fail("pybind11::import_foreign_type(): type has "
329339
"incompatible C++ ABI with this module");
340+
}
330341
cpptype = (const std::type_info *) binding->native_type;
331342
}
332343

333344
auto result = foreign_internals.manual_imports.emplace(binding, cpptype);
334345
if (!result.second) {
335-
auto *existing = (const std::type_info *) result.first->second;
336-
if (existing != cpptype && *existing != *cpptype)
346+
const auto *existing = (const std::type_info *) result.first->second;
347+
if (existing != cpptype && *existing != *cpptype) {
337348
pybind11_fail("pybind11::import_foreign_type(): type was "
338349
"already imported as a different C++ type");
350+
}
339351
}
340352
import_foreign_binding(binding, cpptype);
341353
}
@@ -346,13 +358,15 @@ PYBIND11_NOINLINE void import_foreign_type(type pytype, const std::type_info *cp
346358
PYBIND11_NOINLINE void foreign_enable_import_all() {
347359
auto &foreign_internals = get_foreign_internals();
348360
bool proceed = with_internals([&](internals &) {
349-
if (foreign_internals.import_all)
361+
if (foreign_internals.import_all) {
350362
return false;
363+
}
351364
foreign_internals.import_all = true;
352365
return true;
353366
});
354-
if (!proceed)
367+
if (!proceed) {
355368
return;
369+
}
356370
if (foreign_internals.initialize_if_needed()) {
357371
// pymb_add_framework tells us about every existing type when we
358372
// register, so if we register with import enabled, we're done
@@ -361,11 +375,13 @@ PYBIND11_NOINLINE void foreign_enable_import_all() {
361375
// If we enable import after registering, we have to iterate over the
362376
// list of types ourselves. Do this without the internals lock held so
363377
// we can reuse the pymb callback functions. foreign_internals registry +
364-
// self never change once they're non-null, so we can accesss them
378+
// self never change once they're non-null, so we can access them
365379
// without locking here.
366380
pymb_lock_registry(foreign_internals.registry);
381+
// NOLINTNEXTLINE(modernize-use-auto)
367382
PYMB_LIST_FOREACH(struct pymb_binding *, binding, foreign_internals.registry->bindings) {
368-
if (binding->framework != foreign_internals.self.get() && pymb_try_ref_binding(binding)) {
383+
if (binding->framework != foreign_internals.self.get() &&
384+
pymb_try_ref_binding(binding) != 0) {
369385
foreign_cb_add_foreign_binding(binding);
370386
pymb_unref_binding(binding);
371387
}
@@ -379,9 +395,11 @@ PYBIND11_NOINLINE void foreign_enable_import_all() {
379395
PYBIND11_NOINLINE void export_type_to_foreign(type_info *ti) {
380396
auto &foreign_internals = get_foreign_internals();
381397
auto range = foreign_internals.bindings.equal_range(*ti->cpptype);
382-
for (auto it = range.first; it != range.second; ++it)
383-
if (it->second->framework == foreign_internals.self.get())
398+
for (auto it = range.first; it != range.second; ++it) {
399+
if (it->second->framework == foreign_internals.self.get()) {
384400
return; // already exported
401+
}
402+
}
385403

386404
auto *binding = new pymb_binding{};
387405
binding->framework = foreign_internals.self.get();
@@ -391,7 +409,7 @@ PYBIND11_NOINLINE void export_type_to_foreign(type_info *ti) {
391409
binding->context = ti;
392410

393411
capsule tie_lifetimes((void *) binding, [](void *p) {
394-
pymb_binding *binding = (pymb_binding *) p;
412+
auto *binding = (pymb_binding *) p;
395413
pymb_remove_binding(get_foreign_internals().registry, binding);
396414
free(const_cast<char *>(binding->source_name));
397415
delete binding;
@@ -407,14 +425,16 @@ PYBIND11_NOINLINE void export_type_to_foreign(type_info *ti) {
407425
PYBIND11_NOINLINE void foreign_enable_export_all() {
408426
auto &foreign_internals = get_foreign_internals();
409427
bool proceed = with_internals([&](internals &) {
410-
if (foreign_internals.export_all)
428+
if (foreign_internals.export_all) {
411429
return false;
430+
}
412431
foreign_internals.export_all = true;
413432
foreign_internals.export_type_to_foreign = &detail::export_type_to_foreign;
414433
return true;
415434
});
416-
if (!proceed)
435+
if (!proceed) {
417436
return;
437+
}
418438
foreign_internals.initialize_if_needed();
419439
with_internals([&](internals &internals) {
420440
for (const auto &entry : internals.registered_types_cpp) {
@@ -434,15 +454,18 @@ PYBIND11_NOINLINE void *try_foreign_bindings(const std::type_info *type,
434454
auto &foreign_internals = get_foreign_internals();
435455

436456
PYBIND11_LOCK_INTERNALS(internals);
457+
(void) internals; // suppress unused warning on non-ft builds
437458
auto range = foreign_internals.bindings.equal_range(*type);
438459

439-
if (range.first == range.second)
460+
if (range.first == range.second) {
440461
return nullptr; // no foreign bindings
462+
}
441463

442464
if (std::next(range.first) == range.second) {
443465
// Single binding - check that it's not our own
444466
auto *binding = range.first->second;
445-
if (binding->framework != foreign_internals.self.get() && pymb_try_ref_binding(binding)) {
467+
if (binding->framework != foreign_internals.self.get() &&
468+
pymb_try_ref_binding(binding) != 0) {
446469
#ifdef Py_GIL_DISABLED
447470
// attempt() might execute Python code; drop the internals lock
448471
// to avoid a deadlock
@@ -459,11 +482,13 @@ PYBIND11_NOINLINE void *try_foreign_bindings(const std::type_info *type,
459482
#ifndef Py_GIL_DISABLED
460483
for (auto it = range.first; it != range.second; ++it) {
461484
auto *binding = it->second;
462-
if (binding->framework != foreign_internals.self.get() && pymb_try_ref_binding(binding)) {
485+
if (binding->framework != foreign_internals.self.get() &&
486+
pymb_try_ref_binding(binding) != 0) {
463487
void *result = attempt(closure, binding);
464488
pymb_unref_binding(binding);
465-
if (result)
489+
if (result) {
466490
return result;
491+
}
467492
}
468493
}
469494
return nullptr;
@@ -486,8 +511,10 @@ PYBIND11_NOINLINE void *try_foreign_bindings(const std::type_info *type,
486511
// our scratch storage
487512
for (auto it = range.first; it != range.second; ++it) {
488513
auto *binding = it->second;
489-
if (binding->framework != foreign_internals.self.get() && pymb_try_ref_binding(binding))
514+
if (binding->framework != foreign_internals.self.get() &&
515+
pymb_try_ref_binding(binding) != 0) {
490516
*scratch_tail++ = binding;
517+
}
491518
}
492519

493520
// Drop the lock and proceed using only our saved binding pointers.
@@ -496,8 +523,9 @@ PYBIND11_NOINLINE void *try_foreign_bindings(const std::type_info *type,
496523
lock.unlock();
497524
void *result = nullptr;
498525
while (scratch != scratch_tail) {
499-
if (!result)
526+
if (!result) {
500527
result = attempt(closure, *scratch);
528+
}
501529
pymb_unref_binding(*scratch);
502530
++scratch;
503531
}
@@ -509,10 +537,12 @@ PYBIND11_NAMESPACE_END(detail)
509537

510538
inline void set_foreign_type_defaults(bool export_all, bool import_all) {
511539
auto &foreign_internals = detail::get_foreign_internals();
512-
if (import_all && !foreign_internals.import_all)
540+
if (import_all && !foreign_internals.import_all) {
513541
detail::foreign_enable_import_all();
514-
if (export_all && !foreign_internals.export_all)
542+
}
543+
if (export_all && !foreign_internals.export_all) {
515544
detail::foreign_enable_export_all();
545+
}
516546
}
517547

518548
template <class T = void>
@@ -521,14 +551,15 @@ inline void import_foreign_type(type pytype) {
521551
auto &foreign_internals = detail::get_foreign_internals();
522552
foreign_internals.initialize_if_needed();
523553
detail::with_internals(
524-
[&](detail::internals &) { detail::import_foreign_type(pytype, cpptype); });
554+
[&](detail::internals &) { detail::import_foreign_type(std::move(pytype), cpptype); });
525555
}
526556

527557
inline void export_type_to_foreign(type ty) {
528558
detail::type_info *ti = detail::get_type_info((PyTypeObject *) ty.ptr());
529-
if (!ti)
559+
if (!ti) {
530560
pybind11_fail("pybind11::export_type_to_foreign: not a "
531561
"pybind11 registered type");
562+
}
532563
auto &foreign_internals = detail::get_foreign_internals();
533564
foreign_internals.initialize_if_needed();
534565
detail::with_internals([&](detail::internals &) { detail::export_type_to_foreign(ti); });

include/pybind11/detail/internals.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,14 @@ struct foreign_internals {
363363

364364
// Returns true if we initialized, false if someone else already did.
365365
inline bool initialize_if_needed() {
366-
if (registry)
366+
if (registry) {
367367
return false;
368+
}
368369
return initialize();
369370
}
370371

371372
private:
372-
inline bool initialize();
373+
bool initialize();
373374
};
374375

375376
// the internals struct (above) is shared between all the modules. local_internals are only

0 commit comments

Comments
 (0)