Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/pybind11/attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ struct type_record {
+ (base_has_unique_ptr_holder ? "does not" : "does"));
}

bases.append((PyObject *) base_info->type);
bases.append(reinterpret_cast<PyObject *>(base_info->type));

#ifdef PYBIND11_BACKWARD_COMPATIBILITY_TP_DICTOFFSET
dynamic_attr |= base_info->type->tp_dictoffset != 0;
Expand Down Expand Up @@ -721,7 +721,9 @@ template <typename... Extra,
size_t self = constexpr_sum(std::is_same<is_method, Extra>::value...)>
constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs) {
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(nargs, has_args, has_kwargs);
return named == 0 || (self + named + size_t(has_args) + size_t(has_kwargs)) == nargs;
return named == 0
|| (self + named + static_cast<size_t>(has_args) + static_cast<size_t>(has_kwargs))
== nargs;
}

PYBIND11_NAMESPACE_END(detail)
Expand Down
7 changes: 4 additions & 3 deletions include/pybind11/buffer_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ struct buffer_info {
bool readonly = false)
: ptr(ptr), itemsize(itemsize), size(1), format(format), ndim(ndim),
shape(std::move(shape_in)), strides(std::move(strides_in)), readonly(readonly) {
if (ndim != (ssize_t) shape.size() || ndim != (ssize_t) strides.size()) {
if (ndim != static_cast<ssize_t>(shape.size())
|| ndim != static_cast<ssize_t>(strides.size())) {
pybind11_fail("buffer_info: ndim doesn't match shape and/or strides length");
}
for (size_t i = 0; i < (size_t) ndim; ++i) {
for (size_t i = 0; i < static_cast<size_t>(ndim); ++i) {
size *= shape[i];
}
}
Expand Down Expand Up @@ -195,7 +196,7 @@ struct compare_buffer_info {
template <typename T>
struct compare_buffer_info<T, detail::enable_if_t<std::is_integral<T>::value>> {
static bool compare(const buffer_info &b) {
return (size_t) b.itemsize == sizeof(T)
return static_cast<size_t>(b.itemsize) == sizeof(T)
&& (b.format == format_descriptor<T>::value
|| ((sizeof(T) == sizeof(long))
&& b.format == (std::is_unsigned<T>::value ? "L" : "l"))
Expand Down
5 changes: 3 additions & 2 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ class type_caster<void> : public type_caster<void_type> {
}

/* Check if this is a C++ type */
const auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr());
const auto &bases
= all_type_info(reinterpret_cast<PyTypeObject *>(type::handle_of(h).ptr()));
if (bases.size() == 1) { // Only allowing loading from a single-value type
value = values_and_holders(reinterpret_cast<instance *>(h.ptr())).begin()->value_ptr();
return true;
Expand Down Expand Up @@ -541,7 +542,7 @@ struct string_caster {

const auto *buffer
= reinterpret_cast<const CharT *>(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr()));
size_t length = (size_t) PYBIND11_BYTES_SIZE(utfNbytes.ptr()) / sizeof(CharT);
size_t length = static_cast<size_t>(PYBIND11_BYTES_SIZE(utfNbytes.ptr())) / sizeof(CharT);
// Skip BOM for UTF-16/32
if (UTF_N > 8) {
buffer++;
Expand Down
8 changes: 4 additions & 4 deletions include/pybind11/detail/argument_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ struct args_convert_vector {
new (&m_repr.hvector) typename repr_type::heap_vector(count, value);
} else {
auto &inline_arr = m_repr.iarray;
inline_arr.arr.fill(value ? std::size_t(-1) : 0);
inline_arr.arr.fill(value ? static_cast<std::size_t>(-1) : 0);
inline_arr.size = static_cast<decltype(inline_arr.size)>(count);
}
}
Expand Down Expand Up @@ -273,9 +273,9 @@ struct args_convert_vector {
assert(wbi.word < kWords);
assert(wbi.bit < kBitsPerWord);
if (b) {
ha.arr[wbi.word] |= (std::size_t(1) << wbi.bit);
ha.arr[wbi.word] |= (static_cast<std::size_t>(1) << wbi.bit);
} else {
ha.arr[wbi.word] &= ~(std::size_t(1) << wbi.bit);
ha.arr[wbi.word] &= ~(static_cast<std::size_t>(1) << wbi.bit);
}
assert(operator[](ha.size - 1) == b);
}
Expand All @@ -300,7 +300,7 @@ struct args_convert_vector {
const auto wbi = word_and_bit_index(idx);
assert(wbi.word < kWords);
assert(wbi.bit < kBitsPerWord);
return m_repr.iarray.arr[wbi.word] & (std::size_t(1) << wbi.bit);
return m_repr.iarray.arr[wbi.word] & (static_cast<std::size_t>(1) << wbi.bit);
}

PYBIND11_NOINLINE void move_to_heap_vector_with_reserved_size(std::size_t reserved_size) {
Expand Down
32 changes: 16 additions & 16 deletions include/pybind11/detail/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ inline PyTypeObject *make_static_property_type() {
issue no Python C API calls which could potentially invoke the
garbage collector (the GC will call type_traverse(), which will in
turn find the newly constructed type in an invalid state) */
auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
auto *heap_type = reinterpret_cast<PyHeapTypeObject *>(PyType_Type.tp_alloc(&PyType_Type, 0));
if (!heap_type) {
pybind11_fail("make_static_property_type(): error allocating type!");
}
Expand All @@ -98,7 +98,7 @@ inline PyTypeObject *make_static_property_type() {
pybind11_fail("make_static_property_type(): failure in PyType_Ready()!");
}

setattr((PyObject *) type, "__module__", str(PYBIND11_DUMMY_MODULE_NAME));
setattr(reinterpret_cast<PyObject *>(type), "__module__", str(PYBIND11_DUMMY_MODULE_NAME));
PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);

return type;
Expand Down Expand Up @@ -265,7 +265,7 @@ inline PyTypeObject *make_default_metaclass() {
issue no Python C API calls which could potentially invoke the
garbage collector (the GC will call type_traverse(), which will in
turn find the newly constructed type in an invalid state) */
auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
auto *heap_type = reinterpret_cast<PyHeapTypeObject *>(PyType_Type.tp_alloc(&PyType_Type, 0));
if (!heap_type) {
pybind11_fail("make_default_metaclass(): error allocating metaclass!");
}
Expand All @@ -291,7 +291,7 @@ inline PyTypeObject *make_default_metaclass() {
pybind11_fail("make_default_metaclass(): failure in PyType_Ready()!");
}

setattr((PyObject *) type, "__module__", str(PYBIND11_DUMMY_MODULE_NAME));
setattr(reinterpret_cast<PyObject *>(type), "__module__", str(PYBIND11_DUMMY_MODULE_NAME));
PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);

return type;
Expand All @@ -306,7 +306,7 @@ inline void traverse_offset_bases(void *valueptr,
instance *self,
bool (*f)(void * /*parentptr*/, instance * /*self*/)) {
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(reinterpret_cast<PyTypeObject *>(h.ptr()))) {
for (auto &c : parent_tinfo->implicit_casts) {
if (c.first == tinfo->cpptype) {
auto *parentptr = c.second(valueptr);
Expand Down Expand Up @@ -530,7 +530,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
issue no Python C API calls which could potentially invoke the
garbage collector (the GC will call type_traverse(), which will in
turn find the newly constructed type in an invalid state) */
auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
auto *heap_type = reinterpret_cast<PyHeapTypeObject *>(metaclass->tp_alloc(metaclass, 0));
if (!heap_type) {
pybind11_fail("make_object_base_type(): error allocating type!");
}
Expand All @@ -557,11 +557,11 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
pybind11_fail("PyType_Ready failed in make_object_base_type(): " + error_string());
}

setattr((PyObject *) type, "__module__", str(PYBIND11_DUMMY_MODULE_NAME));
setattr(reinterpret_cast<PyObject *>(type), "__module__", str(PYBIND11_DUMMY_MODULE_NAME));
PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);

assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
return (PyObject *) heap_type;
return reinterpret_cast<PyObject *>(heap_type);
}

/// dynamic_attr: Allow the garbage collector to traverse the internal instance `__dict__`.
Expand Down Expand Up @@ -746,7 +746,7 @@ inline PyObject *make_new_python_type(const type_record &rec) {
/* Allocate memory for docstring (Python will free this later on) */
size_t size = std::strlen(rec.doc) + 1;
#if PY_VERSION_HEX >= 0x030D0000
tp_doc = (char *) PyMem_MALLOC(size);
tp_doc = static_cast<char *>(PyMem_MALLOC(size));
#else
tp_doc = (char *) PyObject_MALLOC(size);
#endif
Expand All @@ -761,10 +761,10 @@ inline PyObject *make_new_python_type(const type_record &rec) {
issue no Python C API calls which could potentially invoke the
garbage collector (the GC will call type_traverse(), which will in
turn find the newly constructed type in an invalid state) */
auto *metaclass
= rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr() : internals.default_metaclass;
auto *metaclass = rec.metaclass.ptr() ? reinterpret_cast<PyTypeObject *>(rec.metaclass.ptr())
: internals.default_metaclass;

auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
auto *heap_type = reinterpret_cast<PyHeapTypeObject *>(metaclass->tp_alloc(metaclass, 0));
if (!heap_type) {
pybind11_fail(std::string(rec.name) + ": Unable to create type object!");
}
Expand All @@ -777,7 +777,7 @@ inline PyObject *make_new_python_type(const type_record &rec) {
auto *type = &heap_type->ht_type;
type->tp_name = full_name;
type->tp_doc = tp_doc;
type->tp_base = type_incref((PyTypeObject *) base);
type->tp_base = type_incref(reinterpret_cast<PyTypeObject *>(base));
type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
if (!bases.empty()) {
type->tp_bases = bases.release().ptr();
Expand Down Expand Up @@ -818,18 +818,18 @@ inline PyObject *make_new_python_type(const type_record &rec) {

/* Register type with the parent scope */
if (rec.scope) {
setattr(rec.scope, rec.name, (PyObject *) type);
setattr(rec.scope, rec.name, reinterpret_cast<PyObject *>(type));
} else {
Py_INCREF(type); // Keep it alive forever (reference leak)
}

if (module_) { // Needed by pydoc
setattr((PyObject *) type, "__module__", module_);
setattr(reinterpret_cast<PyObject *>(type), "__module__", module_);
}

PYBIND11_SET_OLDPY_QUALNAME(type, qualname);

return (PyObject *) type;
return reinterpret_cast<PyObject *>(type);
}

PYBIND11_NAMESPACE_END(detail)
Expand Down
4 changes: 2 additions & 2 deletions include/pybind11/detail/cpp_conduit.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ inline bool type_is_managed_by_our_internals(PyTypeObject *type_obj) {
return bool(internals.registered_types_py.find(type_obj)
!= internals.registered_types_py.end());
#else
return bool(type_obj->tp_new == pybind11_object_new);
return (type_obj->tp_new == pybind11_object_new);
#endif
}

inline bool is_instance_method_of_type(PyTypeObject *type_obj, PyObject *attr_name) {
PyObject *descr = _PyType_Lookup(type_obj, attr_name);
return bool((descr != nullptr) && PyInstanceMethod_Check(descr));
return ((descr != nullptr) && PyInstanceMethod_Check(descr));
}

inline object try_get_cpp_conduit_method(PyObject *obj) {
Expand Down
4 changes: 2 additions & 2 deletions include/pybind11/detail/function_record_pyobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ inline bool is_function_record_PyObject(PyObject *obj) {

inline function_record *function_record_ptr_from_PyObject(PyObject *obj) {
if (is_function_record_PyObject(obj)) {
return ((detail::function_record_PyObject *) obj)->cpp_func_rec;
return (reinterpret_cast<detail::function_record_PyObject *>(obj))->cpp_func_rec;
}
return nullptr;
}
Expand All @@ -137,7 +137,7 @@ inline object function_record_PyObject_New() {
throw error_already_set();
}
py_func_rec->cpp_func_rec = nullptr; // For clarity/purity. Redundant in practice.
return reinterpret_steal<object>((PyObject *) py_func_rec);
return reinterpret_steal<object>(reinterpret_cast<PyObject *>(py_func_rec));
}

PYBIND11_NAMESPACE_BEGIN(function_record_PyTypeObject_methods)
Expand Down
4 changes: 2 additions & 2 deletions include/pybind11/detail/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,9 @@ void setstate(value_and_holder &v_h, std::pair<T, O> &&result, bool need_alias)
return;
}
// Our tests never run into an unset dict, but being careful here for now (see #5658)
auto dict = getattr((PyObject *) v_h.inst, "__dict__", none());
auto dict = getattr(reinterpret_cast<PyObject *>(v_h.inst), "__dict__", none());
if (dict.is_none()) {
setattr((PyObject *) v_h.inst, "__dict__", d);
setattr(reinterpret_cast<PyObject *>(v_h.inst), "__dict__", d);
} else {
// Keep the original object dict and just update it
if (PyDict_Update(dict.ptr(), d.ptr()) < 0) {
Expand Down
5 changes: 2 additions & 3 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -606,9 +606,8 @@ class internals_pp_manager {
// this could be called without an active interpreter, just use what was cached
if (!tstate || tstate->interp == last_istate_tls()) {
auto tpp = internals_p_tls();
if (tpp) {
delete tpp;
}

delete tpp;
}
unref();
return;
Expand Down
12 changes: 6 additions & 6 deletions include/pybind11/detail/type_caster_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_
assert(bases.empty());
std::vector<PyTypeObject *> check;
for (handle parent : reinterpret_borrow<tuple>(t->tp_bases)) {
check.push_back((PyTypeObject *) parent.ptr());
check.push_back(reinterpret_cast<PyTypeObject *>(parent.ptr()));
}
auto const &type_dict = get_internals().registered_types_py;
for (size_t i = 0; i < check.size(); i++) {
Expand Down Expand Up @@ -168,7 +168,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_
i--;
}
for (handle parent : reinterpret_borrow<tuple>(type->tp_bases)) {
check.push_back((PyTypeObject *) parent.ptr());
check.push_back(reinterpret_cast<PyTypeObject *>(parent.ptr()));
}
}
}
Expand Down Expand Up @@ -286,7 +286,7 @@ PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_info &tp,

PYBIND11_NOINLINE handle get_type_handle(const std::type_info &tp, bool throw_if_missing) {
detail::type_info *type_info = get_type_info(tp, throw_if_missing);
return handle(type_info ? ((PyObject *) type_info->type) : nullptr);
return handle(type_info ? (reinterpret_cast<PyObject *>(type_info->type)) : nullptr);
}

inline bool try_incref(PyObject *obj) {
Expand Down Expand Up @@ -506,7 +506,7 @@ PYBIND11_NOINLINE void instance::allocate_layout() {
// efficient for small allocations like the one we're doing here;
// for larger allocations they are just wrappers around malloc.
// TODO: is this still true for pure Python 3.6?
nonsimple.values_and_holders = (void **) PyMem_Calloc(space, sizeof(void *));
nonsimple.values_and_holders = static_cast<void **>(PyMem_Calloc(space, sizeof(void *)));
if (!nonsimple.values_and_holders) {
throw std::bad_alloc();
}
Expand Down Expand Up @@ -537,7 +537,7 @@ PYBIND11_NOINLINE handle get_object_handle(const void *ptr, const detail::type_i
for (auto it = range.first; it != range.second; ++it) {
for (const auto &vh : values_and_holders(it->second)) {
if (vh.type == type) {
return handle((PyObject *) it->second);
return handle(reinterpret_cast<PyObject *>(it->second));
}
}
}
Expand Down Expand Up @@ -1700,7 +1700,7 @@ inline std::string quote_cpp_type_name(const std::string &cpp_type_name) {

PYBIND11_NOINLINE std::string type_info_description(const std::type_info &ti) {
if (auto *type_data = get_type_info(ti)) {
handle th((PyObject *) type_data->type);
handle th(reinterpret_cast<PyObject *>(type_data->type));
return th.attr("__module__").cast<std::string>() + '.'
+ th.attr("__qualname__").cast<std::string>();
}
Expand Down
6 changes: 4 additions & 2 deletions include/pybind11/detail/value_and_holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ struct value_and_holder {
} else if (v) {
inst->nonsimple.status[index] |= instance::status_holder_constructed;
} else {
inst->nonsimple.status[index] &= (std::uint8_t) ~instance::status_holder_constructed;
inst->nonsimple.status[index]
&= static_cast<std::uint8_t>(~instance::status_holder_constructed);
}
}
bool instance_registered() const {
Expand All @@ -69,7 +70,8 @@ struct value_and_holder {
} else if (v) {
inst->nonsimple.status[index] |= instance::status_instance_registered;
} else {
inst->nonsimple.status[index] &= (std::uint8_t) ~instance::status_instance_registered;
inst->nonsimple.status[index]
&= static_cast<std::uint8_t>(~instance::status_instance_registered);
}
}
};
Expand Down
Loading
Loading