clang-tidy readability-qualified-auto (#3702)

* Adding readability-qualified-auto to .clang-tidy

Ported from @henryiii's 287527f705

* fix: support Python < 3.6

Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
This commit is contained in:
Ralf W. Grosse-Kunstleve 2022-02-09 06:24:57 -08:00 committed by GitHub
parent b4f5350d0d
commit 7769e7719c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 109 additions and 106 deletions

View File

@ -40,6 +40,7 @@ readability-implicit-bool-conversion,
readability-make-member-function-const, readability-make-member-function-const,
readability-misplaced-array-index, readability-misplaced-array-index,
readability-non-const-parameter, readability-non-const-parameter,
readability-qualified-auto,
readability-redundant-function-ptr-dereference, readability-redundant-function-ptr-dereference,
readability-redundant-smartptr-get, readability-redundant-smartptr-get,
readability-redundant-string-cstr, readability-redundant-string-cstr,

View File

@ -311,7 +311,7 @@ struct type_record {
bool is_final : 1; bool is_final : 1;
PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *)) { PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *)) {
auto base_info = detail::get_type_info(base, false); auto *base_info = detail::get_type_info(base, false);
if (!base_info) { if (!base_info) {
std::string tname(base.name()); std::string tname(base.name());
detail::clean_type_id(tname); detail::clean_type_id(tname);

View File

@ -251,7 +251,7 @@ public:
} }
/* Check if this is a C++ type */ /* Check if this is a C++ type */
auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr()); const auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr());
if (bases.size() == 1) { // Only allowing loading from a single-value type if (bases.size() == 1) { // Only allowing loading from a single-value type
value = values_and_holders(reinterpret_cast<instance *>(h.ptr())).begin()->value_ptr(); value = values_and_holders(reinterpret_cast<instance *>(h.ptr())).begin()->value_ptr();
return true; return true;
@ -306,7 +306,7 @@ public:
#else #else
// Alternate approach for CPython: this does the same as the above, but optimized // Alternate approach for CPython: this does the same as the above, but optimized
// using the CPython API so as to avoid an unneeded attribute lookup. // using the CPython API so as to avoid an unneeded attribute lookup.
else if (auto tp_as_number = src.ptr()->ob_type->tp_as_number) { else if (auto *tp_as_number = src.ptr()->ob_type->tp_as_number) {
if (PYBIND11_NB_BOOL(tp_as_number)) { if (PYBIND11_NB_BOOL(tp_as_number)) {
res = (*PYBIND11_NB_BOOL(tp_as_number))(src.ptr()); res = (*PYBIND11_NB_BOOL(tp_as_number))(src.ptr());
} }

View File

@ -65,7 +65,7 @@ inline PyTypeObject *make_static_property_type() {
issue no Python C API calls which could potentially invoke the issue no Python C API calls which could potentially invoke the
garbage collector (the GC will call type_traverse(), which will in garbage collector (the GC will call type_traverse(), which will in
turn find the newly constructed type in an invalid state) */ turn find the newly constructed type in an invalid state) */
auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0); auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
if (!heap_type) { if (!heap_type) {
pybind11_fail("make_static_property_type(): error allocating type!"); pybind11_fail("make_static_property_type(): error allocating type!");
} }
@ -75,7 +75,7 @@ inline PyTypeObject *make_static_property_type() {
heap_type->ht_qualname = name_obj.inc_ref().ptr(); heap_type->ht_qualname = name_obj.inc_ref().ptr();
#endif #endif
auto type = &heap_type->ht_type; auto *type = &heap_type->ht_type;
type->tp_name = name; type->tp_name = name;
type->tp_base = type_incref(&PyProperty_Type); type->tp_base = type_incref(&PyProperty_Type);
type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
@ -130,7 +130,7 @@ extern "C" inline int pybind11_meta_setattro(PyObject* obj, PyObject* name, PyOb
// 1. `Type.static_prop = value` --> descr_set: `Type.static_prop.__set__(value)` // 1. `Type.static_prop = value` --> descr_set: `Type.static_prop.__set__(value)`
// 2. `Type.static_prop = other_static_prop` --> setattro: replace existing `static_prop` // 2. `Type.static_prop = other_static_prop` --> setattro: replace existing `static_prop`
// 3. `Type.regular_attribute = value` --> setattro: regular attribute assignment // 3. `Type.regular_attribute = value` --> setattro: regular attribute assignment
const auto static_prop = (PyObject *) get_internals().static_property_type; auto *const static_prop = (PyObject *) get_internals().static_property_type;
const auto call_descr_set = (descr != nullptr) && (value != nullptr) const auto call_descr_set = (descr != nullptr) && (value != nullptr)
&& (PyObject_IsInstance(descr, static_prop) != 0) && (PyObject_IsInstance(descr, static_prop) != 0)
&& (PyObject_IsInstance(value, static_prop) == 0); && (PyObject_IsInstance(value, static_prop) == 0);
@ -179,7 +179,7 @@ extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, P
} }
// This must be a pybind11 instance // This must be a pybind11 instance
auto instance = reinterpret_cast<detail::instance *>(self); auto *instance = reinterpret_cast<detail::instance *>(self);
// Ensure that the base __init__ function(s) were called // Ensure that the base __init__ function(s) were called
for (const auto &vh : values_and_holders(instance)) { for (const auto &vh : values_and_holders(instance)) {
@ -245,7 +245,7 @@ inline PyTypeObject* make_default_metaclass() {
issue no Python C API calls which could potentially invoke the issue no Python C API calls which could potentially invoke the
garbage collector (the GC will call type_traverse(), which will in garbage collector (the GC will call type_traverse(), which will in
turn find the newly constructed type in an invalid state) */ turn find the newly constructed type in an invalid state) */
auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0); auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
if (!heap_type) { if (!heap_type) {
pybind11_fail("make_default_metaclass(): error allocating metaclass!"); pybind11_fail("make_default_metaclass(): error allocating metaclass!");
} }
@ -255,7 +255,7 @@ inline PyTypeObject* make_default_metaclass() {
heap_type->ht_qualname = name_obj.inc_ref().ptr(); heap_type->ht_qualname = name_obj.inc_ref().ptr();
#endif #endif
auto type = &heap_type->ht_type; auto *type = &heap_type->ht_type;
type->tp_name = name; type->tp_name = name;
type->tp_base = type_incref(&PyType_Type); type->tp_base = type_incref(&PyType_Type);
type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
@ -285,7 +285,7 @@ inline PyTypeObject* make_default_metaclass() {
inline void traverse_offset_bases(void *valueptr, const detail::type_info *tinfo, instance *self, inline void traverse_offset_bases(void *valueptr, const detail::type_info *tinfo, instance *self,
bool (*f)(void * /*parentptr*/, instance * /*self*/)) { bool (*f)(void * /*parentptr*/, instance * /*self*/)) {
for (handle h : reinterpret_borrow<tuple>(tinfo->type->tp_bases)) { for (handle h : reinterpret_borrow<tuple>(tinfo->type->tp_bases)) {
if (auto parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) { if (auto *parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) {
for (auto &c : parent_tinfo->implicit_casts) { for (auto &c : parent_tinfo->implicit_casts) {
if (c.first == tinfo->cpptype) { if (c.first == tinfo->cpptype) {
auto *parentptr = c.second(valueptr); auto *parentptr = c.second(valueptr);
@ -344,7 +344,7 @@ inline PyObject *make_new_instance(PyTypeObject *type) {
} }
#endif #endif
PyObject *self = type->tp_alloc(type, 0); PyObject *self = type->tp_alloc(type, 0);
auto inst = reinterpret_cast<instance *>(self); auto *inst = reinterpret_cast<instance *>(self);
// Allocate the value/holder internals: // Allocate the value/holder internals:
inst->allocate_layout(); inst->allocate_layout();
@ -369,14 +369,14 @@ extern "C" inline int pybind11_object_init(PyObject *self, PyObject *, PyObject
inline void add_patient(PyObject *nurse, PyObject *patient) { inline void add_patient(PyObject *nurse, PyObject *patient) {
auto &internals = get_internals(); auto &internals = get_internals();
auto instance = reinterpret_cast<detail::instance *>(nurse); auto *instance = reinterpret_cast<detail::instance *>(nurse);
instance->has_patients = true; instance->has_patients = true;
Py_INCREF(patient); Py_INCREF(patient);
internals.patients[nurse].push_back(patient); internals.patients[nurse].push_back(patient);
} }
inline void clear_patients(PyObject *self) { inline void clear_patients(PyObject *self) {
auto instance = reinterpret_cast<detail::instance *>(self); auto *instance = reinterpret_cast<detail::instance *>(self);
auto &internals = get_internals(); auto &internals = get_internals();
auto pos = internals.patients.find(self); auto pos = internals.patients.find(self);
assert(pos != internals.patients.end()); assert(pos != internals.patients.end());
@ -394,7 +394,7 @@ inline void clear_patients(PyObject *self) {
/// Clears all internal data from the instance and removes it from registered instances in /// Clears all internal data from the instance and removes it from registered instances in
/// preparation for deallocation. /// preparation for deallocation.
inline void clear_instance(PyObject *self) { inline void clear_instance(PyObject *self) {
auto instance = reinterpret_cast<detail::instance *>(self); auto *instance = reinterpret_cast<detail::instance *>(self);
// Deallocate any values/holders, if present: // Deallocate any values/holders, if present:
for (auto &v_h : values_and_holders(instance)) { for (auto &v_h : values_and_holders(instance)) {
@ -435,7 +435,7 @@ inline void clear_instance(PyObject *self) {
extern "C" inline void pybind11_object_dealloc(PyObject *self) { extern "C" inline void pybind11_object_dealloc(PyObject *self) {
clear_instance(self); clear_instance(self);
auto type = Py_TYPE(self); auto *type = Py_TYPE(self);
type->tp_free(self); type->tp_free(self);
#if PY_VERSION_HEX < 0x03080000 #if PY_VERSION_HEX < 0x03080000
@ -464,7 +464,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
issue no Python C API calls which could potentially invoke the issue no Python C API calls which could potentially invoke the
garbage collector (the GC will call type_traverse(), which will in garbage collector (the GC will call type_traverse(), which will in
turn find the newly constructed type in an invalid state) */ turn find the newly constructed type in an invalid state) */
auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0); auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
if (!heap_type) { if (!heap_type) {
pybind11_fail("make_object_base_type(): error allocating type!"); pybind11_fail("make_object_base_type(): error allocating type!");
} }
@ -474,7 +474,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
heap_type->ht_qualname = name_obj.inc_ref().ptr(); heap_type->ht_qualname = name_obj.inc_ref().ptr();
#endif #endif
auto type = &heap_type->ht_type; auto *type = &heap_type->ht_type;
type->tp_name = name; type->tp_name = name;
type->tp_base = type_incref(&PyBaseObject_Type); type->tp_base = type_incref(&PyBaseObject_Type);
type->tp_basicsize = static_cast<ssize_t>(sizeof(instance)); type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
@ -538,7 +538,7 @@ extern "C" inline int pybind11_clear(PyObject *self) {
/// Give instances of this type a `__dict__` and opt into garbage collection. /// Give instances of this type a `__dict__` and opt into garbage collection.
inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) { inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) {
auto type = &heap_type->ht_type; auto *type = &heap_type->ht_type;
type->tp_flags |= Py_TPFLAGS_HAVE_GC; type->tp_flags |= Py_TPFLAGS_HAVE_GC;
type->tp_dictoffset = type->tp_basicsize; // place dict at the end type->tp_dictoffset = type->tp_basicsize; // place dict at the end
type->tp_basicsize += (ssize_t)sizeof(PyObject *); // and allocate enough space for it type->tp_basicsize += (ssize_t)sizeof(PyObject *); // and allocate enough space for it
@ -639,7 +639,7 @@ inline PyObject* make_new_python_type(const type_record &rec) {
} }
} }
auto full_name = c_str( const auto *full_name = c_str(
#if !defined(PYPY_VERSION) #if !defined(PYPY_VERSION)
module_ ? str(module_).cast<std::string>() + "." + rec.name : module_ ? str(module_).cast<std::string>() + "." + rec.name :
#endif #endif
@ -656,17 +656,16 @@ inline PyObject* make_new_python_type(const type_record &rec) {
auto &internals = get_internals(); auto &internals = get_internals();
auto bases = tuple(rec.bases); auto bases = tuple(rec.bases);
auto base = (bases.empty()) ? internals.instance_base auto *base = (bases.empty()) ? internals.instance_base : bases[0].ptr();
: bases[0].ptr();
/* Danger zone: from now (and until PyType_Ready), make sure to /* Danger zone: from now (and until PyType_Ready), make sure to
issue no Python C API calls which could potentially invoke the issue no Python C API calls which could potentially invoke the
garbage collector (the GC will call type_traverse(), which will in garbage collector (the GC will call type_traverse(), which will in
turn find the newly constructed type in an invalid state) */ turn find the newly constructed type in an invalid state) */
auto metaclass = rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr() auto *metaclass
: internals.default_metaclass; = rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr() : internals.default_metaclass;
auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0); auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
if (!heap_type) { if (!heap_type) {
pybind11_fail(std::string(rec.name) + ": Unable to create type object!"); pybind11_fail(std::string(rec.name) + ": Unable to create type object!");
} }
@ -676,7 +675,7 @@ inline PyObject* make_new_python_type(const type_record &rec) {
heap_type->ht_qualname = qualname.inc_ref().ptr(); heap_type->ht_qualname = qualname.inc_ref().ptr();
#endif #endif
auto type = &heap_type->ht_type; auto *type = &heap_type->ht_type;
type->tp_name = full_name; type->tp_name = full_name;
type->tp_doc = tp_doc; type->tp_doc = tp_doc;
type->tp_base = type_incref((PyTypeObject *)base); type->tp_base = type_incref((PyTypeObject *)base);

View File

@ -301,7 +301,7 @@ bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
template <class T, template <class T,
enable_if_t<!std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0> enable_if_t<!std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
bool handle_nested_exception(const T &exc, const std::exception_ptr &p) { bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
if (auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) { if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
return handle_nested_exception(*nep, p); return handle_nested_exception(*nep, p);
} }
return false; return false;
@ -338,7 +338,7 @@ inline void translate_exception(std::exception_ptr p) {
return; return;
} catch (const builtin_exception &e) { } catch (const builtin_exception &e) {
// Could not use template since it's an abstract class. // Could not use template since it's an abstract class.
if (auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) { if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) {
handle_nested_exception(*nep, p); handle_nested_exception(*nep, p);
} }
e.set_error(); e.set_error();

View File

@ -109,7 +109,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_
auto const &type_dict = get_internals().registered_types_py; auto const &type_dict = get_internals().registered_types_py;
for (size_t i = 0; i < check.size(); i++) { for (size_t i = 0; i < check.size(); i++) {
auto type = check[i]; auto *type = check[i];
// Ignore Python2 old-style class super type: // Ignore Python2 old-style class super type:
if (!PyType_Check((PyObject *) type)) { if (!PyType_Check((PyObject *) type)) {
continue; continue;
@ -178,7 +178,7 @@ inline const std::vector<detail::type_info *> &all_type_info(PyTypeObject *type)
* `all_type_info` instead if you want to support multiple bases. * `all_type_info` instead if you want to support multiple bases.
*/ */
PYBIND11_NOINLINE detail::type_info* get_type_info(PyTypeObject *type) { PYBIND11_NOINLINE detail::type_info* get_type_info(PyTypeObject *type) {
auto &bases = all_type_info(type); const auto &bases = all_type_info(type);
if (bases.empty()) { if (bases.empty()) {
return nullptr; return nullptr;
} }
@ -210,10 +210,10 @@ inline detail::type_info *get_global_type_info(const std::type_index &tp) {
/// Return the type info for a given C++ type; on lookup failure can either throw or return nullptr. /// Return the type info for a given C++ type; on lookup failure can either throw or return nullptr.
PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_index &tp, PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_index &tp,
bool throw_if_missing = false) { bool throw_if_missing = false) {
if (auto ltype = get_local_type_info(tp)) { if (auto *ltype = get_local_type_info(tp)) {
return ltype; return ltype;
} }
if (auto gtype = get_global_type_info(tp)) { if (auto *gtype = get_global_type_info(tp)) {
return gtype; return gtype;
} }
@ -235,7 +235,7 @@ PYBIND11_NOINLINE handle find_registered_python_instance(void *src,
const detail::type_info *tinfo) { const detail::type_info *tinfo) {
auto it_instances = get_internals().registered_instances.equal_range(src); auto it_instances = get_internals().registered_instances.equal_range(src);
for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) { for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) {
for (auto instance_type : detail::all_type_info(Py_TYPE(it_i->second))) { for (auto *instance_type : detail::all_type_info(Py_TYPE(it_i->second))) {
if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype)) { if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype)) {
return handle((PyObject *) it_i->second).inc_ref(); return handle((PyObject *) it_i->second).inc_ref();
} }
@ -397,7 +397,7 @@ PYBIND11_NOINLINE value_and_holder instance::get_value_and_holder(const type_inf
} }
PYBIND11_NOINLINE void instance::allocate_layout() { PYBIND11_NOINLINE void instance::allocate_layout() {
auto &tinfo = all_type_info(Py_TYPE(this)); const auto &tinfo = all_type_info(Py_TYPE(this));
const size_t n_types = tinfo.size(); const size_t n_types = tinfo.size();
@ -421,7 +421,7 @@ PYBIND11_NOINLINE void instance::allocate_layout() {
// values that tracks whether each associated holder has been initialized. Each [block] is // values that tracks whether each associated holder has been initialized. Each [block] is
// padded, if necessary, to an integer multiple of sizeof(void *). // padded, if necessary, to an integer multiple of sizeof(void *).
size_t space = 0; size_t space = 0;
for (auto t : tinfo) { for (auto *t : tinfo) {
space += 1; // value pointer space += 1; // value pointer
space += t->holder_size_in_ptrs; // holder instance space += t->holder_size_in_ptrs; // holder instance
} }
@ -582,7 +582,7 @@ public:
} }
auto inst = reinterpret_steal<object>(make_new_instance(tinfo->type)); auto inst = reinterpret_steal<object>(make_new_instance(tinfo->type));
auto wrapper = reinterpret_cast<instance *>(inst.ptr()); auto *wrapper = reinterpret_cast<instance *>(inst.ptr());
wrapper->owned = false; wrapper->owned = false;
void *&valueptr = values_and_holders(wrapper).begin()->value_ptr(); void *&valueptr = values_and_holders(wrapper).begin()->value_ptr();
@ -656,7 +656,7 @@ public:
auto *&vptr = v_h.value_ptr(); auto *&vptr = v_h.value_ptr();
// Lazy allocation for unallocated values: // Lazy allocation for unallocated values:
if (vptr == nullptr) { if (vptr == nullptr) {
auto *type = v_h.type ? v_h.type : typeinfo; const auto *type = v_h.type ? v_h.type : typeinfo;
if (type->operator_new) { if (type->operator_new) {
vptr = type->operator_new(type->type_size); vptr = type->operator_new(type->type_size);
} else { } else {
@ -675,7 +675,7 @@ public:
value = vptr; value = vptr;
} }
bool try_implicit_casts(handle src, bool convert) { bool try_implicit_casts(handle src, bool convert) {
for (auto &cast : typeinfo->implicit_casts) { for (const auto &cast : typeinfo->implicit_casts) {
type_caster_generic sub_caster(*cast.first); type_caster_generic sub_caster(*cast.first);
if (sub_caster.load(src, convert)) { if (sub_caster.load(src, convert)) {
value = cast.second(sub_caster.value); value = cast.second(sub_caster.value);
@ -718,7 +718,7 @@ public:
return false; return false;
} }
if (auto result = foreign_typeinfo->module_local_load(src.ptr(), foreign_typeinfo)) { if (auto *result = foreign_typeinfo->module_local_load(src.ptr(), foreign_typeinfo)) {
value = result; value = result;
return true; return true;
} }
@ -750,7 +750,7 @@ public:
} }
// Case 2: We have a derived class // Case 2: We have a derived class
if (PyType_IsSubtype(srctype, typeinfo->type)) { if (PyType_IsSubtype(srctype, typeinfo->type)) {
auto &bases = all_type_info(srctype); const auto &bases = all_type_info(srctype);
bool no_cpp_mi = typeinfo->simple_type; bool no_cpp_mi = typeinfo->simple_type;
// Case 2a: the python type is a Python-inherited derived class that inherits from just // Case 2a: the python type is a Python-inherited derived class that inherits from just
@ -767,7 +767,7 @@ public:
// we can find an exact match (or, for a simple C++ type, an inherited match); if so, we // we can find an exact match (or, for a simple C++ type, an inherited match); if so, we
// can safely reinterpret_cast to the relevant pointer. // can safely reinterpret_cast to the relevant pointer.
if (bases.size() > 1) { if (bases.size() > 1) {
for (auto base : bases) { for (auto *base : bases) {
if (no_cpp_mi ? PyType_IsSubtype(base->type, typeinfo->type) : base->type == typeinfo->type) { if (no_cpp_mi ? PyType_IsSubtype(base->type, typeinfo->type) : base->type == typeinfo->type) {
this_.load_value(reinterpret_cast<instance *>(src.ptr())->get_value_and_holder(base)); this_.load_value(reinterpret_cast<instance *>(src.ptr())->get_value_and_holder(base));
return true; return true;
@ -785,7 +785,7 @@ public:
// Perform an implicit conversion // Perform an implicit conversion
if (convert) { if (convert) {
for (auto &converter : typeinfo->implicit_conversions) { for (const auto &converter : typeinfo->implicit_conversions) {
auto temp = reinterpret_steal<object>(converter(src.ptr(), typeinfo->type)); auto temp = reinterpret_steal<object>(converter(src.ptr(), typeinfo->type));
if (load_impl<ThisT>(temp, false)) { if (load_impl<ThisT>(temp, false)) {
loader_life_support::add_patient(temp); loader_life_support::add_patient(temp);
@ -799,7 +799,7 @@ public:
// Failed to match local typeinfo. Try again with global. // Failed to match local typeinfo. Try again with global.
if (typeinfo->module_local) { if (typeinfo->module_local) {
if (auto gtype = get_global_type_info(*typeinfo->cpptype)) { if (auto *gtype = get_global_type_info(*typeinfo->cpptype)) {
typeinfo = gtype; typeinfo = gtype;
return load(src, false); return load(src, false);
} }
@ -970,7 +970,7 @@ public:
// polymorphic type (using RTTI by default, but can be overridden by specializing // polymorphic type (using RTTI by default, but can be overridden by specializing
// polymorphic_type_hook). If the instance isn't derived, returns the base version. // polymorphic_type_hook). If the instance isn't derived, returns the base version.
static std::pair<const void *, const type_info *> src_and_type(const itype *src) { static std::pair<const void *, const type_info *> src_and_type(const itype *src) {
auto &cast_type = typeid(itype); const auto &cast_type = typeid(itype);
const std::type_info *instance_type = nullptr; const std::type_info *instance_type = nullptr;
const void *vsrc = polymorphic_type_hook<itype>::get(src, instance_type); const void *vsrc = polymorphic_type_hook<itype>::get(src, instance_type);
if (instance_type && !same_type(cast_type, *instance_type)) { if (instance_type && !same_type(cast_type, *instance_type)) {

View File

@ -157,7 +157,7 @@ inline void set_interpreter_argv(int argc, const char *const *argv, bool add_pro
widened_argv[ii] = widened_argv_entries.back().get(); widened_argv[ii] = widened_argv_entries.back().get();
} }
auto pysys_argv = widened_argv.get(); auto *pysys_argv = widened_argv.get();
#else #else
// python 2.x // python 2.x
std::vector<std::string> strings{safe_argv, safe_argv + argv_size}; std::vector<std::string> strings{safe_argv, safe_argv + argv_size};

View File

@ -46,10 +46,10 @@ public:
captured variables), in which case the roundtrip can be avoided. captured variables), in which case the roundtrip can be avoided.
*/ */
if (auto cfunc = func.cpp_function()) { if (auto cfunc = func.cpp_function()) {
auto cfunc_self = PyCFunction_GET_SELF(cfunc.ptr()); auto *cfunc_self = PyCFunction_GET_SELF(cfunc.ptr());
if (isinstance<capsule>(cfunc_self)) { if (isinstance<capsule>(cfunc_self)) {
auto c = reinterpret_borrow<capsule>(cfunc_self); auto c = reinterpret_borrow<capsule>(cfunc_self);
auto rec = (function_record *) c; auto *rec = (function_record *) c;
while (rec != nullptr) { while (rec != nullptr) {
if (rec->is_stateless if (rec->is_stateless

View File

@ -137,6 +137,8 @@ public:
auto &internals = detail::get_internals(); auto &internals = detail::get_internals();
tstate = PyEval_SaveThread(); tstate = PyEval_SaveThread();
if (disassoc) { if (disassoc) {
// Python >= 3.7 can remove this, it's an int before 3.7
// NOLINTNEXTLINE(readability-qualified-auto)
auto key = internals.tstate; auto key = internals.tstate;
PYBIND11_TLS_DELETE_VALUE(key); PYBIND11_TLS_DELETE_VALUE(key);
} }
@ -160,6 +162,8 @@ public:
PyEval_RestoreThread(tstate); PyEval_RestoreThread(tstate);
} }
if (disassoc) { if (disassoc) {
// Python >= 3.7 can remove this, it's an int before 3.7
// NOLINTNEXTLINE(readability-qualified-auto)
auto key = detail::get_internals().tstate; auto key = detail::get_internals().tstate;
PYBIND11_TLS_REPLACE_VALUE(key, tstate); PYBIND11_TLS_REPLACE_VALUE(key, tstate);
} }

View File

@ -1130,7 +1130,7 @@ public:
static constexpr int value = values[detail::is_fmt_numeric<T>::index]; static constexpr int value = values[detail::is_fmt_numeric<T>::index];
static pybind11::dtype dtype() { static pybind11::dtype dtype() {
if (auto ptr = npy_api::get().PyArray_DescrFromType_(value)) { if (auto *ptr = npy_api::get().PyArray_DescrFromType_(value)) {
return reinterpret_steal<pybind11::dtype>(ptr); return reinterpret_steal<pybind11::dtype>(ptr);
} }
pybind11_fail("Unsupported buffer format!"); pybind11_fail("Unsupported buffer format!");
@ -1200,7 +1200,7 @@ PYBIND11_NOINLINE void register_structured_dtype(
formats.append(field.descr); formats.append(field.descr);
offsets.append(pybind11::int_(field.offset)); offsets.append(pybind11::int_(field.offset));
} }
auto dtype_ptr auto *dtype_ptr
= pybind11::dtype(std::move(names), std::move(formats), std::move(offsets), itemsize) = pybind11::dtype(std::move(names), std::move(formats), std::move(offsets), itemsize)
.release() .release()
.ptr(); .ptr();
@ -1699,7 +1699,7 @@ private:
} }
/* Call the function */ /* Call the function */
auto mutable_data = returned_array::mutable_data(result); auto *mutable_data = returned_array::mutable_data(result);
if (trivial == broadcast_trivial::non_trivial) { if (trivial == broadcast_trivial::non_trivial) {
apply_broadcast(buffers, params, mutable_data, size, shape, i_seq, vi_seq, bi_seq); apply_broadcast(buffers, params, mutable_data, size, shape, i_seq, vi_seq, bi_seq);
} else { } else {

View File

@ -162,7 +162,7 @@ protected:
/* Store the function including any extra state it might have (e.g. a lambda capture object) */ /* Store the function including any extra state it might have (e.g. a lambda capture object) */
// The unique_ptr makes sure nothing is leaked in case of an exception. // The unique_ptr makes sure nothing is leaked in case of an exception.
auto unique_rec = make_function_record(); auto unique_rec = make_function_record();
auto rec = unique_rec.get(); auto *rec = unique_rec.get();
/* Store the capture object directly in the function record if there is enough space */ /* Store the capture object directly in the function record if there is enough space */
if (PYBIND11_SILENCE_MSVC_C4127(sizeof(capture) <= sizeof(rec->data))) { if (PYBIND11_SILENCE_MSVC_C4127(sizeof(capture) <= sizeof(rec->data))) {
@ -220,8 +220,8 @@ protected:
process_attributes<Extra...>::precall(call); process_attributes<Extra...>::precall(call);
/* Get a pointer to the capture object */ /* Get a pointer to the capture object */
auto data = (sizeof(capture) <= sizeof(call.func.data) const auto *data = (sizeof(capture) <= sizeof(call.func.data) ? &call.func.data
? &call.func.data : call.func.data[0]); : call.func.data[0]);
auto *cap = const_cast<capture *>(reinterpret_cast<const capture *>(data)); auto *cap = const_cast<capture *>(reinterpret_cast<const capture *>(data));
/* Override policy for rvalues -- usually to enforce rvp::move on an rvalue */ /* Override policy for rvalues -- usually to enforce rvp::move on an rvalue */
@ -287,12 +287,12 @@ protected:
class strdup_guard { class strdup_guard {
public: public:
~strdup_guard() { ~strdup_guard() {
for (auto s : strings) { for (auto *s : strings) {
std::free(s); std::free(s);
} }
} }
char *operator()(const char *s) { char *operator()(const char *s) {
auto t = PYBIND11_COMPAT_STRDUP(s); auto *t = PYBIND11_COMPAT_STRDUP(s);
strings.push_back(t); strings.push_back(t);
return t; return t;
} }
@ -309,7 +309,7 @@ protected:
// Do NOT receive `unique_rec` by value. If this function fails to move out the unique_ptr, // Do NOT receive `unique_rec` by value. If this function fails to move out the unique_ptr,
// we do not want this to destuct the pointer. `initialize` (the caller) still relies on the // we do not want this to destuct the pointer. `initialize` (the caller) still relies on the
// pointee being alive after this call. Only move out if a `capsule` is going to keep it alive. // pointee being alive after this call. Only move out if a `capsule` is going to keep it alive.
auto rec = unique_rec.get(); auto *rec = unique_rec.get();
// Keep track of strdup'ed strings, and clean them up as long as the function's capsule // Keep track of strdup'ed strings, and clean them up as long as the function's capsule
// has not taken ownership yet (when `unique_rec.release()` is called). // has not taken ownership yet (when `unique_rec.release()` is called).
@ -355,7 +355,7 @@ protected:
std::string signature; std::string signature;
size_t type_index = 0, arg_index = 0; size_t type_index = 0, arg_index = 0;
bool is_starred = false; bool is_starred = false;
for (auto *pc = text; *pc != '\0'; ++pc) { for (const auto *pc = text; *pc != '\0'; ++pc) {
const auto c = *pc; const auto c = *pc;
if (c == '{') { if (c == '{') {
@ -396,7 +396,7 @@ protected:
if (!t) { if (!t) {
pybind11_fail("Internal error while parsing type signature (1)"); pybind11_fail("Internal error while parsing type signature (1)");
} }
if (auto tinfo = detail::get_type_info(*t)) { if (auto *tinfo = detail::get_type_info(*t)) {
handle th((PyObject *) tinfo->type); handle th((PyObject *) tinfo->type);
signature += signature +=
th.attr("__module__").cast<std::string>() + "." + th.attr("__module__").cast<std::string>() + "." +
@ -535,7 +535,7 @@ protected:
} }
// Then specific overload signatures // Then specific overload signatures
bool first_user_def = true; bool first_user_def = true;
for (auto it = chain_start; it != nullptr; it = it->next) { for (auto *it = chain_start; it != nullptr; it = it->next) {
if (options::show_function_signatures()) { if (options::show_function_signatures()) {
if (index > 0) { if (index > 0) {
signatures += "\n"; signatures += "\n";
@ -652,8 +652,8 @@ protected:
return nullptr; return nullptr;
} }
const auto tinfo = get_type_info((PyTypeObject *) overloads->scope.ptr()); auto *const tinfo = get_type_info((PyTypeObject *) overloads->scope.ptr());
const auto pi = reinterpret_cast<instance *>(parent.ptr()); auto *const pi = reinterpret_cast<instance *>(parent.ptr());
self_value_and_holder = pi->get_value_and_holder(tinfo, true); self_value_and_holder = pi->get_value_and_holder(tinfo, true);
// If this value is already registered it must mean __init__ is invoked multiple times; // If this value is already registered it must mean __init__ is invoked multiple times;
@ -1207,7 +1207,7 @@ public:
/* m_clear */ nullptr, /* m_clear */ nullptr,
/* m_free */ nullptr /* m_free */ nullptr
}; };
auto m = PyModule_Create(def); auto *m = PyModule_Create(def);
#else #else
// Ignore module_def *def; only necessary for Python 3 // Ignore module_def *def; only necessary for Python 3
(void) def; (void) def;
@ -1317,7 +1317,7 @@ protected:
void mark_parents_nonsimple(PyTypeObject *value) { void mark_parents_nonsimple(PyTypeObject *value) {
auto t = reinterpret_borrow<tuple>(value->tp_bases); auto t = reinterpret_borrow<tuple>(value->tp_bases);
for (handle h : t) { for (handle h : t) {
auto tinfo2 = get_type_info((PyTypeObject *) h.ptr()); auto *tinfo2 = get_type_info((PyTypeObject *) h.ptr());
if (tinfo2) { if (tinfo2) {
tinfo2->simple_type = false; tinfo2->simple_type = false;
} }
@ -1329,7 +1329,7 @@ protected:
buffer_info *(*get_buffer)(PyObject *, void *), buffer_info *(*get_buffer)(PyObject *, void *),
void *get_buffer_data) { void *get_buffer_data) {
auto *type = (PyHeapTypeObject*) m_ptr; auto *type = (PyHeapTypeObject*) m_ptr;
auto tinfo = detail::get_type_info(&type->ht_type); auto *tinfo = detail::get_type_info(&type->ht_type);
if (!type->ht_type.tp_as_buffer) { if (!type->ht_type.tp_as_buffer) {
pybind11_fail("To be able to register buffer protocol support for the type '" pybind11_fail("To be able to register buffer protocol support for the type '"
@ -2304,7 +2304,7 @@ template <typename InputType, typename OutputType> void implicitly_convertible()
return result; return result;
}; };
if (auto tinfo = detail::get_type_info(typeid(OutputType))) { if (auto *tinfo = detail::get_type_info(typeid(OutputType))) {
tinfo->implicit_conversions.push_back(implicit_caster); tinfo->implicit_conversions.push_back(implicit_caster);
} else { } else {
pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>()); pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>());
@ -2568,7 +2568,7 @@ PYBIND11_NAMESPACE_END(detail)
:return: The Python method by this name from the object or an empty function wrapper. :return: The Python method by this name from the object or an empty function wrapper.
\endrst */ \endrst */
template <class T> function get_override(const T *this_ptr, const char *name) { template <class T> function get_override(const T *this_ptr, const char *name) {
auto tinfo = detail::get_type_info(typeid(T)); auto *tinfo = detail::get_type_info(typeid(T));
return tinfo ? detail::get_type_override(this_ptr, tinfo, name) : function(); return tinfo ? detail::get_type_override(this_ptr, tinfo, name) : function();
} }

View File

@ -1524,7 +1524,7 @@ public:
/// Get the pointer the capsule holds. /// Get the pointer the capsule holds.
template<typename T = void> template<typename T = void>
T* get_pointer() const { T* get_pointer() const {
auto name = this->name(); const auto *name = this->name();
T *result = static_cast<T *>(PyCapsule_GetPointer(m_ptr, name)); T *result = static_cast<T *>(PyCapsule_GetPointer(m_ptr, name));
if (!result) { if (!result) {
PyErr_Clear(); PyErr_Clear();

View File

@ -68,7 +68,7 @@ public:
PyObject* native = nullptr; PyObject* native = nullptr;
if constexpr (std::is_same_v<typename T::value_type, char>) { if constexpr (std::is_same_v<typename T::value_type, char>) {
if (PyUnicode_FSConverter(buf, &native) != 0) { if (PyUnicode_FSConverter(buf, &native) != 0) {
if (auto c_str = PyBytes_AsString(native)) { if (auto *c_str = PyBytes_AsString(native)) {
// AsString returns a pointer to the internal buffer, which // AsString returns a pointer to the internal buffer, which
// must not be free'd. // must not be free'd.
value = c_str; value = c_str;
@ -76,7 +76,7 @@ public:
} }
} else if constexpr (std::is_same_v<typename T::value_type, wchar_t>) { } else if constexpr (std::is_same_v<typename T::value_type, wchar_t>) {
if (PyUnicode_FSDecoder(buf, &native) != 0) { if (PyUnicode_FSDecoder(buf, &native) != 0) {
if (auto c_str = PyUnicode_AsWideCharString(native, nullptr)) { if (auto *c_str = PyUnicode_AsWideCharString(native, nullptr)) {
// AsWideCharString returns a new string that must be free'd. // AsWideCharString returns a new string that must be free'd.
value = c_str; // Copies the string. value = c_str; // Copies the string.
PyMem_Free(c_str); PyMem_Free(c_str);

View File

@ -464,7 +464,7 @@ class_<Vector, holder_type> bind_vector(handle scope, std::string const &name, A
// If the value_type is unregistered (e.g. a converting type) or is itself registered // If the value_type is unregistered (e.g. a converting type) or is itself registered
// module-local then make the vector binding module-local as well: // module-local then make the vector binding module-local as well:
using vtype = typename Vector::value_type; using vtype = typename Vector::value_type;
auto vtype_info = detail::get_type_info(typeid(vtype)); auto *vtype_info = detail::get_type_info(typeid(vtype));
bool local = !vtype_info || vtype_info->module_local; bool local = !vtype_info || vtype_info->module_local;
Class_ cl(scope, name.c_str(), pybind11::module_local(local), std::forward<Args>(args)...); Class_ cl(scope, name.c_str(), pybind11::module_local(local), std::forward<Args>(args)...);
@ -651,7 +651,7 @@ class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args&&.
// If either type is a non-module-local bound type then make the map binding non-local as well; // If either type is a non-module-local bound type then make the map binding non-local as well;
// otherwise (e.g. both types are either module-local or converting) the map will be // otherwise (e.g. both types are either module-local or converting) the map will be
// module-local. // module-local.
auto tinfo = detail::get_type_info(typeid(MappedType)); auto *tinfo = detail::get_type_info(typeid(MappedType));
bool local = !tinfo || tinfo->module_local; bool local = !tinfo || tinfo->module_local;
if (local) { if (local) {
tinfo = detail::get_type_info(typeid(KeyType)); tinfo = detail::get_type_info(typeid(KeyType));

View File

@ -89,7 +89,7 @@ TEST_SUBMODULE(buffers, m) {
throw std::runtime_error("Incompatible buffer format!"); throw std::runtime_error("Incompatible buffer format!");
} }
auto v = new Matrix(info.shape[0], info.shape[1]); auto *v = new Matrix(info.shape[0], info.shape[1]);
memcpy(v->data(), info.ptr, sizeof(float) * (size_t) (v->rows() * v->cols())); memcpy(v->data(), info.ptr, sizeof(float) * (size_t) (v->rows() * v->cols()));
return v; return v;
})) }))

View File

@ -277,7 +277,7 @@ TEST_SUBMODULE(builtin_casters, m) {
m.def("refwrap_list", [](bool copy) { m.def("refwrap_list", [](bool copy) {
static IncType x1(1), x2(2); static IncType x1(1), x2(2);
py::list l; py::list l;
for (auto &f : {std::ref(x1), std::ref(x2)}) { for (const auto &f : {std::ref(x1), std::ref(x2)}) {
l.append(py::cast(f, copy ? py::return_value_policy::copy l.append(py::cast(f, copy ? py::return_value_policy::copy
: py::return_value_policy::reference)); : py::return_value_policy::reference));
} }

View File

@ -95,7 +95,7 @@ TEST_SUBMODULE(call_policies, m) {
// but it's unclear how to test it without `PyGILState_GetThisThreadState`. // but it's unclear how to test it without `PyGILState_GetThisThreadState`.
auto report_gil_status = []() { auto report_gil_status = []() {
auto is_gil_held = false; auto is_gil_held = false;
if (auto tstate = py::detail::get_thread_state_unchecked()) { if (auto *tstate = py::detail::get_thread_state_unchecked()) {
is_gil_held = (tstate == PyGILState_GetThisThreadState()); is_gil_held = (tstate == PyGILState_GetThisThreadState());
} }

View File

@ -148,7 +148,7 @@ TEST_SUBMODULE(callbacks, m) {
py::arg("expect_none") = false); py::arg("expect_none") = false);
m.def("test_dummy_function", [](const std::function<int(int)> &f) -> std::string { m.def("test_dummy_function", [](const std::function<int(int)> &f) -> std::string {
using fn_type = int (*)(int); using fn_type = int (*)(int);
auto result = f.target<fn_type>(); const auto *result = f.target<fn_type>();
if (!result) { if (!result) {
auto r = f(1); auto r = f(1);
return "can't convert to function pointer: eval(1) = " + std::to_string(r); return "can't convert to function pointer: eval(1) = " + std::to_string(r);

View File

@ -255,7 +255,7 @@ TEST_SUBMODULE(class_, m) {
return py::str().release().ptr(); return py::str().release().ptr();
}; };
auto def = new PyMethodDef{"f", f, METH_VARARGS, nullptr}; auto *def = new PyMethodDef{"f", f, METH_VARARGS, nullptr};
py::capsule def_capsule(def, [](void *ptr) { delete reinterpret_cast<PyMethodDef *>(ptr); }); py::capsule def_capsule(def, [](void *ptr) { delete reinterpret_cast<PyMethodDef *>(ptr); });
return py::reinterpret_steal<py::object>(PyCFunction_NewEx(def, def_capsule.ptr(), m.ptr())); return py::reinterpret_steal<py::object>(PyCFunction_NewEx(def, def_capsule.ptr(), m.ptr()));
}()); }());

View File

@ -240,8 +240,8 @@ TEST_CASE("Subinterpreter") {
REQUIRE(has_pybind11_internals_static()); REQUIRE(has_pybind11_internals_static());
/// Create and switch to a subinterpreter. /// Create and switch to a subinterpreter.
auto main_tstate = PyThreadState_Get(); auto *main_tstate = PyThreadState_Get();
auto sub_tstate = Py_NewInterpreter(); auto *sub_tstate = Py_NewInterpreter();
// Subinterpreters get their own copy of builtins. detail::get_internals() still // Subinterpreters get their own copy of builtins. detail::get_internals() still
// works by returning from the static variable, i.e. all interpreters share a single // works by returning from the static variable, i.e. all interpreters share a single

View File

@ -89,7 +89,7 @@ template<typename... Ix> arr data_t(const arr_t& a, Ix... index) {
} }
template<typename... Ix> arr& mutate_data(arr& a, Ix... index) { template<typename... Ix> arr& mutate_data(arr& a, Ix... index) {
auto ptr = (uint8_t *) a.mutable_data(index...); auto *ptr = (uint8_t *) a.mutable_data(index...);
for (py::ssize_t i = 0; i < a.nbytes() - a.offset_at(index...); i++) { for (py::ssize_t i = 0; i < a.nbytes() - a.offset_at(index...); i++) {
ptr[i] = (uint8_t) (ptr[i] * 2); ptr[i] = (uint8_t) (ptr[i] * 2);
} }

View File

@ -165,7 +165,7 @@ template <typename S>
py::array_t<S, 0> create_recarray(size_t n) { py::array_t<S, 0> create_recarray(size_t n) {
auto arr = mkarray_via_buffer<S>(n); auto arr = mkarray_via_buffer<S>(n);
auto req = arr.request(); auto req = arr.request();
auto ptr = static_cast<S*>(req.ptr); auto *ptr = static_cast<S *>(req.ptr);
for (size_t i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
SET_TEST_VALS(ptr[i], i); SET_TEST_VALS(ptr[i], i);
} }
@ -175,7 +175,7 @@ py::array_t<S, 0> create_recarray(size_t n) {
template <typename S> template <typename S>
py::list print_recarray(py::array_t<S, 0> arr) { py::list print_recarray(py::array_t<S, 0> arr) {
const auto req = arr.request(); const auto req = arr.request();
const auto ptr = static_cast<S*>(req.ptr); auto *const ptr = static_cast<S *>(req.ptr);
auto l = py::list(); auto l = py::list();
for (py::ssize_t i = 0; i < req.size; i++) { for (py::ssize_t i = 0; i < req.size; i++) {
std::stringstream ss; std::stringstream ss;
@ -192,8 +192,8 @@ py::array_t<int32_t, 0> test_array_ctors(int i) {
std::vector<py::ssize_t> shape { 3, 2 }; std::vector<py::ssize_t> shape { 3, 2 };
std::vector<py::ssize_t> strides { 8, 4 }; std::vector<py::ssize_t> strides { 8, 4 };
auto ptr = data.data(); auto *ptr = data.data();
auto vptr = (void *) ptr; auto *vptr = (void *) ptr;
auto dtype = py::dtype("int32"); auto dtype = py::dtype("int32");
py::buffer_info buf_ndim1(vptr, 4, "i", 6); py::buffer_info buf_ndim1(vptr, 4, "i", 6);
@ -328,7 +328,7 @@ TEST_SUBMODULE(numpy_dtypes, m) {
m.def("create_rec_nested", [](size_t n) { // test_signature m.def("create_rec_nested", [](size_t n) { // test_signature
py::array_t<NestedStruct, 0> arr = mkarray_via_buffer<NestedStruct>(n); py::array_t<NestedStruct, 0> arr = mkarray_via_buffer<NestedStruct>(n);
auto req = arr.request(); auto req = arr.request();
auto ptr = static_cast<NestedStruct*>(req.ptr); auto *ptr = static_cast<NestedStruct *>(req.ptr);
for (size_t i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
SET_TEST_VALS(ptr[i].a, i); SET_TEST_VALS(ptr[i].a, i);
SET_TEST_VALS(ptr[i].b, i + 1); SET_TEST_VALS(ptr[i].b, i + 1);
@ -339,7 +339,7 @@ TEST_SUBMODULE(numpy_dtypes, m) {
m.def("create_rec_partial_nested", [](size_t n) { m.def("create_rec_partial_nested", [](size_t n) {
py::array_t<PartialNestedStruct, 0> arr = mkarray_via_buffer<PartialNestedStruct>(n); py::array_t<PartialNestedStruct, 0> arr = mkarray_via_buffer<PartialNestedStruct>(n);
auto req = arr.request(); auto req = arr.request();
auto ptr = static_cast<PartialNestedStruct*>(req.ptr); auto *ptr = static_cast<PartialNestedStruct *>(req.ptr);
for (size_t i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
SET_TEST_VALS(ptr[i].a, i); SET_TEST_VALS(ptr[i].a, i);
} }
@ -397,14 +397,14 @@ TEST_SUBMODULE(numpy_dtypes, m) {
m.def("test_dtype_ctors", &test_dtype_ctors); m.def("test_dtype_ctors", &test_dtype_ctors);
m.def("test_dtype_kind", [dtype_names]() { m.def("test_dtype_kind", [dtype_names]() {
py::list list; py::list list;
for (auto &dt_name : dtype_names) { for (const auto &dt_name : dtype_names) {
list.append(py::dtype(dt_name).kind()); list.append(py::dtype(dt_name).kind());
} }
return list; return list;
}); });
m.def("test_dtype_char_", [dtype_names]() { m.def("test_dtype_char_", [dtype_names]() {
py::list list; py::list list;
for (auto &dt_name : dtype_names) { for (const auto &dt_name : dtype_names) {
list.append(py::dtype(dt_name).char_()); list.append(py::dtype(dt_name).char_());
} }
return list; return list;
@ -430,7 +430,7 @@ TEST_SUBMODULE(numpy_dtypes, m) {
py::array_t<StringStruct, 0> arr = mkarray_via_buffer<StringStruct>(non_empty ? 4 : 0); py::array_t<StringStruct, 0> arr = mkarray_via_buffer<StringStruct>(non_empty ? 4 : 0);
if (non_empty) { if (non_empty) {
auto req = arr.request(); auto req = arr.request();
auto ptr = static_cast<StringStruct*>(req.ptr); auto *ptr = static_cast<StringStruct *>(req.ptr);
for (py::ssize_t i = 0; i < req.size * req.itemsize; i++) { for (py::ssize_t i = 0; i < req.size * req.itemsize; i++) {
static_cast<char *>(req.ptr)[i] = 0; static_cast<char *>(req.ptr)[i] = 0;
} }
@ -450,7 +450,7 @@ TEST_SUBMODULE(numpy_dtypes, m) {
// test_array_array // test_array_array
m.def("create_array_array", [](size_t n) { m.def("create_array_array", [](size_t n) {
py::array_t<ArrayStruct, 0> arr = mkarray_via_buffer<ArrayStruct>(n); py::array_t<ArrayStruct, 0> arr = mkarray_via_buffer<ArrayStruct>(n);
auto ptr = (ArrayStruct *) arr.mutable_data(); auto *ptr = (ArrayStruct *) arr.mutable_data();
for (size_t i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < 3; j++) { for (size_t j = 0; j < 3; j++) {
for (size_t k = 0; k < 4; k++) { for (size_t k = 0; k < 4; k++) {
@ -476,7 +476,7 @@ TEST_SUBMODULE(numpy_dtypes, m) {
// test_enum_array // test_enum_array
m.def("create_enum_array", [](size_t n) { m.def("create_enum_array", [](size_t n) {
py::array_t<EnumStruct, 0> arr = mkarray_via_buffer<EnumStruct>(n); py::array_t<EnumStruct, 0> arr = mkarray_via_buffer<EnumStruct>(n);
auto ptr = (EnumStruct *) arr.mutable_data(); auto *ptr = (EnumStruct *) arr.mutable_data();
for (size_t i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
ptr[i].e1 = static_cast<E1>(-1 + ((int) i % 2) * 2); ptr[i].e1 = static_cast<E1>(-1 + ((int) i % 2) * 2);
ptr[i].e2 = static_cast<E2>(1 + (i % 2)); ptr[i].e2 = static_cast<E2>(1 + (i % 2));
@ -488,7 +488,7 @@ TEST_SUBMODULE(numpy_dtypes, m) {
// test_complex_array // test_complex_array
m.def("create_complex_array", [](size_t n) { m.def("create_complex_array", [](size_t n) {
py::array_t<ComplexStruct, 0> arr = mkarray_via_buffer<ComplexStruct>(n); py::array_t<ComplexStruct, 0> arr = mkarray_via_buffer<ComplexStruct>(n);
auto ptr = (ComplexStruct *) arr.mutable_data(); auto *ptr = (ComplexStruct *) arr.mutable_data();
for (size_t i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
ptr[i].cflt.real(float(i)); ptr[i].cflt.real(float(i));
ptr[i].cflt.imag(float(i) + 0.25f); ptr[i].cflt.imag(float(i) + 0.25f);

View File

@ -146,7 +146,7 @@ TEST_SUBMODULE(pytypes, m) {
m.def("return_capsule_with_name_and_destructor", []() { m.def("return_capsule_with_name_and_destructor", []() {
auto capsule = py::capsule((void *) 12345, "pointer type description", [](PyObject *ptr) { auto capsule = py::capsule((void *) 12345, "pointer type description", [](PyObject *ptr) {
if (ptr) { if (ptr) {
auto name = PyCapsule_GetName(ptr); const auto *name = PyCapsule_GetName(ptr);
py::print("destructing capsule ({}, '{}')"_s.format( py::print("destructing capsule ({}, '{}')"_s.format(
(size_t) PyCapsule_GetPointer(ptr, name), name (size_t) PyCapsule_GetPointer(ptr, name), name
)); ));

View File

@ -113,7 +113,7 @@ public:
static void cleanupAllInstances() { static void cleanupAllInstances() {
auto tmp = std::move(myobject4_instances); auto tmp = std::move(myobject4_instances);
myobject4_instances.clear(); myobject4_instances.clear();
for (auto o : tmp) { for (auto *o : tmp) {
delete o; delete o;
} }
} }
@ -141,7 +141,7 @@ public:
static void cleanupAllInstances() { static void cleanupAllInstances() {
auto tmp = std::move(myobject4a_instances); auto tmp = std::move(myobject4a_instances);
myobject4a_instances.clear(); myobject4a_instances.clear();
for (auto o : tmp) { for (auto *o : tmp) {
delete o; delete o;
} }
} }

View File

@ -41,7 +41,7 @@ public:
}; };
template <class Container> Container *one_to_n(int n) { template <class Container> Container *one_to_n(int n) {
auto v = new Container(); auto *v = new Container();
for (int i = 1; i <= n; i++) { for (int i = 1; i <= n; i++) {
v->emplace_back(i); v->emplace_back(i);
} }
@ -49,7 +49,7 @@ template <class Container> Container *one_to_n(int n) {
} }
template <class Map> Map *times_ten(int n) { template <class Map> Map *times_ten(int n) {
auto m = new Map(); auto *m = new Map();
for (int i = 1; i <= n; i++) { for (int i = 1; i <= n; i++) {
m->emplace(int(i), E_nc(10 * i)); m->emplace(int(i), E_nc(10 * i));
} }
@ -57,7 +57,7 @@ template <class Map> Map *times_ten(int n) {
} }
template <class NestMap> NestMap *times_hundred(int n) { template <class NestMap> NestMap *times_hundred(int n) {
auto m = new NestMap(); auto *m = new NestMap();
for (int i = 1; i <= n; i++) { for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) { for (int j = 1; j <= n; j++) {
(*m)[i].emplace(int(j * 10), E_nc(100 * j)); (*m)[i].emplace(int(j * 10), E_nc(100 * j));
@ -99,9 +99,8 @@ TEST_SUBMODULE(stl_binders, m) {
m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>); m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>);
// Issue #1885: binding nested std::map<X, Container<E>> with E non-copyable // Issue #1885: binding nested std::map<X, Container<E>> with E non-copyable
py::bind_map<std::map<int, std::vector<E_nc>>>(m, "MapVecENC"); py::bind_map<std::map<int, std::vector<E_nc>>>(m, "MapVecENC");
m.def("get_nvnc", [](int n) m.def("get_nvnc", [](int n) {
{ auto *m = new std::map<int, std::vector<E_nc>>();
auto m = new std::map<int, std::vector<E_nc>>();
for (int i = 1; i <= n; i++) { for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) { for (int j = 1; j <= n; j++) {
(*m)[i].emplace_back(j); (*m)[i].emplace_back(j);