mirror of
https://github.com/pybind/pybind11.git
synced 2026-04-20 14:59:27 +00:00
Replace C-style casts to static_cast and reinterpret_cast (#5930)
* Replace C-style casts to static_cast and reinterpret_cast Signed-off-by: cyy <cyyever@outlook.com> * style: pre-commit fixes --------- Signed-off-by: cyy <cyyever@outlook.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -373,7 +373,7 @@ struct type_record {
|
|||||||
+ (base_has_unique_ptr_holder ? "does not" : "does"));
|
+ (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
|
#ifdef PYBIND11_BACKWARD_COMPATIBILITY_TP_DICTOFFSET
|
||||||
dynamic_attr |= base_info->type->tp_dictoffset != 0;
|
dynamic_attr |= base_info->type->tp_dictoffset != 0;
|
||||||
@@ -721,7 +721,9 @@ template <typename... Extra,
|
|||||||
size_t self = constexpr_sum(std::is_same<is_method, Extra>::value...)>
|
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) {
|
constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs) {
|
||||||
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(nargs, has_args, 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)
|
PYBIND11_NAMESPACE_END(detail)
|
||||||
|
|||||||
@@ -66,10 +66,11 @@ struct buffer_info {
|
|||||||
bool readonly = false)
|
bool readonly = false)
|
||||||
: ptr(ptr), itemsize(itemsize), size(1), format(format), ndim(ndim),
|
: ptr(ptr), itemsize(itemsize), size(1), format(format), ndim(ndim),
|
||||||
shape(std::move(shape_in)), strides(std::move(strides_in)), readonly(readonly) {
|
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");
|
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];
|
size *= shape[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -195,7 +196,7 @@ struct compare_buffer_info {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
struct compare_buffer_info<T, detail::enable_if_t<std::is_integral<T>::value>> {
|
struct compare_buffer_info<T, detail::enable_if_t<std::is_integral<T>::value>> {
|
||||||
static bool compare(const buffer_info &b) {
|
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
|
&& (b.format == format_descriptor<T>::value
|
||||||
|| ((sizeof(T) == sizeof(long))
|
|| ((sizeof(T) == sizeof(long))
|
||||||
&& b.format == (std::is_unsigned<T>::value ? "L" : "l"))
|
&& b.format == (std::is_unsigned<T>::value ? "L" : "l"))
|
||||||
|
|||||||
@@ -394,7 +394,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Check if this is a C++ 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
|
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;
|
||||||
@@ -541,7 +542,7 @@ struct string_caster {
|
|||||||
|
|
||||||
const auto *buffer
|
const auto *buffer
|
||||||
= reinterpret_cast<const CharT *>(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr()));
|
= 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
|
// Skip BOM for UTF-16/32
|
||||||
if (UTF_N > 8) {
|
if (UTF_N > 8) {
|
||||||
buffer++;
|
buffer++;
|
||||||
|
|||||||
@@ -231,7 +231,7 @@ public:
|
|||||||
new (&m_repr.hvector) typename repr_type::heap_vector(count, value);
|
new (&m_repr.hvector) typename repr_type::heap_vector(count, value);
|
||||||
} else {
|
} else {
|
||||||
auto &inline_arr = m_repr.iarray;
|
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);
|
inline_arr.size = static_cast<decltype(inline_arr.size)>(count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -273,9 +273,9 @@ public:
|
|||||||
assert(wbi.word < kWords);
|
assert(wbi.word < kWords);
|
||||||
assert(wbi.bit < kBitsPerWord);
|
assert(wbi.bit < kBitsPerWord);
|
||||||
if (b) {
|
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 {
|
} 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);
|
assert(operator[](ha.size - 1) == b);
|
||||||
}
|
}
|
||||||
@@ -300,7 +300,7 @@ private:
|
|||||||
const auto wbi = word_and_bit_index(idx);
|
const auto wbi = word_and_bit_index(idx);
|
||||||
assert(wbi.word < kWords);
|
assert(wbi.word < kWords);
|
||||||
assert(wbi.bit < kBitsPerWord);
|
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) {
|
PYBIND11_NOINLINE void move_to_heap_vector_with_reserved_size(std::size_t reserved_size) {
|
||||||
|
|||||||
@@ -71,7 +71,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 = reinterpret_cast<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!");
|
||||||
}
|
}
|
||||||
@@ -98,7 +98,7 @@ inline PyTypeObject *make_static_property_type() {
|
|||||||
pybind11_fail("make_static_property_type(): failure in PyType_Ready()!");
|
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);
|
PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
|
||||||
|
|
||||||
return type;
|
return type;
|
||||||
@@ -265,7 +265,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 = reinterpret_cast<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!");
|
||||||
}
|
}
|
||||||
@@ -291,7 +291,7 @@ inline PyTypeObject *make_default_metaclass() {
|
|||||||
pybind11_fail("make_default_metaclass(): failure in PyType_Ready()!");
|
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);
|
PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
|
||||||
|
|
||||||
return type;
|
return type;
|
||||||
@@ -306,7 +306,7 @@ inline void traverse_offset_bases(void *valueptr,
|
|||||||
instance *self,
|
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(reinterpret_cast<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);
|
||||||
@@ -530,7 +530,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 = reinterpret_cast<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!");
|
||||||
}
|
}
|
||||||
@@ -557,11 +557,11 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
|
|||||||
pybind11_fail("PyType_Ready failed in make_object_base_type(): " + error_string());
|
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);
|
PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
|
||||||
|
|
||||||
assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
|
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__`.
|
/// dynamic_attr: Allow the garbage collector to traverse the internal instance `__dict__`.
|
||||||
@@ -746,7 +746,7 @@ inline PyObject *make_new_python_type(const type_record &rec) {
|
|||||||
/* Allocate memory for docstring (Python will free this later on) */
|
/* Allocate memory for docstring (Python will free this later on) */
|
||||||
size_t size = std::strlen(rec.doc) + 1;
|
size_t size = std::strlen(rec.doc) + 1;
|
||||||
#if PY_VERSION_HEX >= 0x030D0000
|
#if PY_VERSION_HEX >= 0x030D0000
|
||||||
tp_doc = (char *) PyMem_MALLOC(size);
|
tp_doc = static_cast<char *>(PyMem_MALLOC(size));
|
||||||
#else
|
#else
|
||||||
tp_doc = (char *) PyObject_MALLOC(size);
|
tp_doc = (char *) PyObject_MALLOC(size);
|
||||||
#endif
|
#endif
|
||||||
@@ -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
|
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
|
auto *metaclass = rec.metaclass.ptr() ? reinterpret_cast<PyTypeObject *>(rec.metaclass.ptr())
|
||||||
= rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr() : internals.default_metaclass;
|
: 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) {
|
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!");
|
||||||
}
|
}
|
||||||
@@ -777,7 +777,7 @@ inline PyObject *make_new_python_type(const type_record &rec) {
|
|||||||
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(reinterpret_cast<PyTypeObject *>(base));
|
||||||
type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
|
type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
|
||||||
if (!bases.empty()) {
|
if (!bases.empty()) {
|
||||||
type->tp_bases = bases.release().ptr();
|
type->tp_bases = bases.release().ptr();
|
||||||
@@ -818,18 +818,18 @@ inline PyObject *make_new_python_type(const type_record &rec) {
|
|||||||
|
|
||||||
/* Register type with the parent scope */
|
/* Register type with the parent scope */
|
||||||
if (rec.scope) {
|
if (rec.scope) {
|
||||||
setattr(rec.scope, rec.name, (PyObject *) type);
|
setattr(rec.scope, rec.name, reinterpret_cast<PyObject *>(type));
|
||||||
} else {
|
} else {
|
||||||
Py_INCREF(type); // Keep it alive forever (reference leak)
|
Py_INCREF(type); // Keep it alive forever (reference leak)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (module_) { // Needed by pydoc
|
if (module_) { // Needed by pydoc
|
||||||
setattr((PyObject *) type, "__module__", module_);
|
setattr(reinterpret_cast<PyObject *>(type), "__module__", module_);
|
||||||
}
|
}
|
||||||
|
|
||||||
PYBIND11_SET_OLDPY_QUALNAME(type, qualname);
|
PYBIND11_SET_OLDPY_QUALNAME(type, qualname);
|
||||||
|
|
||||||
return (PyObject *) type;
|
return reinterpret_cast<PyObject *>(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
PYBIND11_NAMESPACE_END(detail)
|
PYBIND11_NAMESPACE_END(detail)
|
||||||
|
|||||||
@@ -21,13 +21,13 @@ inline bool type_is_managed_by_our_internals(PyTypeObject *type_obj) {
|
|||||||
return bool(internals.registered_types_py.find(type_obj)
|
return bool(internals.registered_types_py.find(type_obj)
|
||||||
!= internals.registered_types_py.end());
|
!= internals.registered_types_py.end());
|
||||||
#else
|
#else
|
||||||
return bool(type_obj->tp_new == pybind11_object_new);
|
return (type_obj->tp_new == pybind11_object_new);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool is_instance_method_of_type(PyTypeObject *type_obj, PyObject *attr_name) {
|
inline bool is_instance_method_of_type(PyTypeObject *type_obj, PyObject *attr_name) {
|
||||||
PyObject *descr = _PyType_Lookup(type_obj, 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) {
|
inline object try_get_cpp_conduit_method(PyObject *obj) {
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ inline bool is_function_record_PyObject(PyObject *obj) {
|
|||||||
|
|
||||||
inline function_record *function_record_ptr_from_PyObject(PyObject *obj) {
|
inline function_record *function_record_ptr_from_PyObject(PyObject *obj) {
|
||||||
if (is_function_record_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;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -137,7 +137,7 @@ inline object function_record_PyObject_New() {
|
|||||||
throw error_already_set();
|
throw error_already_set();
|
||||||
}
|
}
|
||||||
py_func_rec->cpp_func_rec = nullptr; // For clarity/purity. Redundant in practice.
|
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)
|
PYBIND11_NAMESPACE_BEGIN(function_record_PyTypeObject_methods)
|
||||||
|
|||||||
@@ -476,9 +476,9 @@ void setstate(value_and_holder &v_h, std::pair<T, O> &&result, bool need_alias)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Our tests never run into an unset dict, but being careful here for now (see #5658)
|
// 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()) {
|
if (dict.is_none()) {
|
||||||
setattr((PyObject *) v_h.inst, "__dict__", d);
|
setattr(reinterpret_cast<PyObject *>(v_h.inst), "__dict__", d);
|
||||||
} else {
|
} else {
|
||||||
// Keep the original object dict and just update it
|
// Keep the original object dict and just update it
|
||||||
if (PyDict_Update(dict.ptr(), d.ptr()) < 0) {
|
if (PyDict_Update(dict.ptr(), d.ptr()) < 0) {
|
||||||
|
|||||||
@@ -606,9 +606,8 @@ public:
|
|||||||
// this could be called without an active interpreter, just use what was cached
|
// this could be called without an active interpreter, just use what was cached
|
||||||
if (!tstate || tstate->interp == last_istate_tls()) {
|
if (!tstate || tstate->interp == last_istate_tls()) {
|
||||||
auto tpp = internals_p_tls();
|
auto tpp = internals_p_tls();
|
||||||
if (tpp) {
|
|
||||||
delete tpp;
|
delete tpp;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
unref();
|
unref();
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_
|
|||||||
assert(bases.empty());
|
assert(bases.empty());
|
||||||
std::vector<PyTypeObject *> check;
|
std::vector<PyTypeObject *> check;
|
||||||
for (handle parent : reinterpret_borrow<tuple>(t->tp_bases)) {
|
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;
|
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++) {
|
||||||
@@ -168,7 +168,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_
|
|||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
for (handle parent : reinterpret_borrow<tuple>(type->tp_bases)) {
|
for (handle parent : reinterpret_borrow<tuple>(type->tp_bases)) {
|
||||||
check.push_back((PyTypeObject *) parent.ptr());
|
check.push_back(reinterpret_cast<PyTypeObject *>(parent.ptr()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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) {
|
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);
|
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) {
|
inline bool try_incref(PyObject *obj) {
|
||||||
@@ -506,7 +506,7 @@ PYBIND11_NOINLINE void instance::allocate_layout() {
|
|||||||
// efficient for small allocations like the one we're doing here;
|
// efficient for small allocations like the one we're doing here;
|
||||||
// for larger allocations they are just wrappers around malloc.
|
// for larger allocations they are just wrappers around malloc.
|
||||||
// TODO: is this still true for pure Python 3.6?
|
// 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) {
|
if (!nonsimple.values_and_holders) {
|
||||||
throw std::bad_alloc();
|
throw std::bad_alloc();
|
||||||
}
|
}
|
||||||
@@ -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 (auto it = range.first; it != range.second; ++it) {
|
||||||
for (const auto &vh : values_and_holders(it->second)) {
|
for (const auto &vh : values_and_holders(it->second)) {
|
||||||
if (vh.type == type) {
|
if (vh.type == type) {
|
||||||
return handle((PyObject *) it->second);
|
return handle(reinterpret_cast<PyObject *>(it->second));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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) {
|
PYBIND11_NOINLINE std::string type_info_description(const std::type_info &ti) {
|
||||||
if (auto *type_data = get_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>() + '.'
|
return th.attr("__module__").cast<std::string>() + '.'
|
||||||
+ th.attr("__qualname__").cast<std::string>();
|
+ th.attr("__qualname__").cast<std::string>();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,7 +54,8 @@ struct value_and_holder {
|
|||||||
} else if (v) {
|
} else if (v) {
|
||||||
inst->nonsimple.status[index] |= instance::status_holder_constructed;
|
inst->nonsimple.status[index] |= instance::status_holder_constructed;
|
||||||
} else {
|
} 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 {
|
bool instance_registered() const {
|
||||||
@@ -69,7 +70,8 @@ struct value_and_holder {
|
|||||||
} else if (v) {
|
} else if (v) {
|
||||||
inst->nonsimple.status[index] |= instance::status_instance_registered;
|
inst->nonsimple.status[index] |= instance::status_instance_registered;
|
||||||
} else {
|
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -159,7 +159,7 @@ inline std::string generate_function_signature(const char *type_caster_name_fiel
|
|||||||
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(reinterpret_cast<PyObject *>(tinfo->type));
|
||||||
signature += th.attr("__module__").cast<std::string>() + "."
|
signature += th.attr("__module__").cast<std::string>() + "."
|
||||||
+ th.attr("__qualname__").cast<std::string>();
|
+ th.attr("__qualname__").cast<std::string>();
|
||||||
} else if (auto th = detail::global_internals_native_enum_type_map_get_item(*t)) {
|
} else if (auto th = detail::global_internals_native_enum_type_map_get_item(*t)) {
|
||||||
@@ -642,7 +642,7 @@ protected:
|
|||||||
|
|
||||||
rec->signature = guarded_strdup(signature.c_str());
|
rec->signature = guarded_strdup(signature.c_str());
|
||||||
rec->args.shrink_to_fit();
|
rec->args.shrink_to_fit();
|
||||||
rec->nargs = (std::uint16_t) args;
|
rec->nargs = static_cast<std::uint16_t>(args);
|
||||||
|
|
||||||
if (rec->sibling && PYBIND11_INSTANCE_METHOD_CHECK(rec->sibling.ptr())) {
|
if (rec->sibling && PYBIND11_INSTANCE_METHOD_CHECK(rec->sibling.ptr())) {
|
||||||
rec->sibling = PYBIND11_INSTANCE_METHOD_GET_FUNCTION(rec->sibling.ptr());
|
rec->sibling = PYBIND11_INSTANCE_METHOD_GET_FUNCTION(rec->sibling.ptr());
|
||||||
@@ -681,7 +681,7 @@ protected:
|
|||||||
rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS;
|
rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS;
|
||||||
|
|
||||||
object py_func_rec = detail::function_record_PyObject_New();
|
object py_func_rec = detail::function_record_PyObject_New();
|
||||||
((detail::function_record_PyObject *) py_func_rec.ptr())->cpp_func_rec
|
(reinterpret_cast<detail::function_record_PyObject *>(py_func_rec.ptr()))->cpp_func_rec
|
||||||
= unique_rec.release();
|
= unique_rec.release();
|
||||||
guarded_strdup.release();
|
guarded_strdup.release();
|
||||||
|
|
||||||
@@ -715,8 +715,8 @@ protected:
|
|||||||
// chain.
|
// chain.
|
||||||
chain_start = rec;
|
chain_start = rec;
|
||||||
rec->next = chain;
|
rec->next = chain;
|
||||||
auto *py_func_rec
|
auto *py_func_rec = reinterpret_cast<detail::function_record_PyObject *>(
|
||||||
= (detail::function_record_PyObject *) PyCFunction_GET_SELF(m_ptr);
|
PyCFunction_GET_SELF(m_ptr));
|
||||||
py_func_rec->cpp_func_rec = unique_rec.release();
|
py_func_rec->cpp_func_rec = unique_rec.release();
|
||||||
guarded_strdup.release();
|
guarded_strdup.release();
|
||||||
} else {
|
} else {
|
||||||
@@ -776,7 +776,7 @@ protected:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto *func = (PyCFunctionObject *) m_ptr;
|
auto *func = reinterpret_cast<PyCFunctionObject *>(m_ptr);
|
||||||
// Install docstring if it's non-empty (when at least one option is enabled)
|
// Install docstring if it's non-empty (when at least one option is enabled)
|
||||||
auto *doc = signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str());
|
auto *doc = signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str());
|
||||||
std::free(const_cast<char *>(PYBIND11_PYCFUNCTION_GET_DOC(func)));
|
std::free(const_cast<char *>(PYBIND11_PYCFUNCTION_GET_DOC(func)));
|
||||||
@@ -851,7 +851,7 @@ protected:
|
|||||||
|
|
||||||
/* Need to know how many arguments + keyword arguments there are to pick the right
|
/* Need to know how many arguments + keyword arguments there are to pick the right
|
||||||
overload */
|
overload */
|
||||||
const auto n_args_in = (size_t) PyTuple_GET_SIZE(args_in);
|
const auto n_args_in = static_cast<size_t>(PyTuple_GET_SIZE(args_in));
|
||||||
|
|
||||||
handle parent = n_args_in > 0 ? PyTuple_GET_ITEM(args_in, 0) : nullptr,
|
handle parent = n_args_in > 0 ? PyTuple_GET_ITEM(args_in, 0) : nullptr,
|
||||||
result = PYBIND11_TRY_NEXT_OVERLOAD;
|
result = PYBIND11_TRY_NEXT_OVERLOAD;
|
||||||
@@ -865,7 +865,8 @@ protected:
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto *const tinfo = get_type_info((PyTypeObject *) overloads->scope.ptr());
|
auto *const tinfo
|
||||||
|
= get_type_info(reinterpret_cast<PyTypeObject *>(overloads->scope.ptr()));
|
||||||
auto *const 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);
|
||||||
|
|
||||||
@@ -1296,7 +1297,7 @@ PYBIND11_NAMESPACE_BEGIN(function_record_PyTypeObject_methods)
|
|||||||
|
|
||||||
// This implementation needs the definition of `class cpp_function`.
|
// This implementation needs the definition of `class cpp_function`.
|
||||||
inline void tp_dealloc_impl(PyObject *self) {
|
inline void tp_dealloc_impl(PyObject *self) {
|
||||||
auto *py_func_rec = (function_record_PyObject *) self;
|
auto *py_func_rec = reinterpret_cast<function_record_PyObject *>(self);
|
||||||
cpp_function::destruct(py_func_rec->cpp_func_rec);
|
cpp_function::destruct(py_func_rec->cpp_func_rec);
|
||||||
py_func_rec->cpp_func_rec = nullptr;
|
py_func_rec->cpp_func_rec = nullptr;
|
||||||
}
|
}
|
||||||
@@ -1669,7 +1670,7 @@ protected:
|
|||||||
|
|
||||||
/* Register supplemental type information in C++ dict */
|
/* Register supplemental type information in C++ dict */
|
||||||
auto *tinfo = new detail::type_info();
|
auto *tinfo = new detail::type_info();
|
||||||
tinfo->type = (PyTypeObject *) m_ptr;
|
tinfo->type = reinterpret_cast<PyTypeObject *>(m_ptr);
|
||||||
tinfo->cpptype = rec.type;
|
tinfo->cpptype = rec.type;
|
||||||
tinfo->type_size = rec.type_size;
|
tinfo->type_size = rec.type_size;
|
||||||
tinfo->type_align = rec.type_align;
|
tinfo->type_align = rec.type_align;
|
||||||
@@ -1704,7 +1705,7 @@ protected:
|
|||||||
PYBIND11_WARNING_DISABLE_GCC("-Warray-bounds")
|
PYBIND11_WARNING_DISABLE_GCC("-Warray-bounds")
|
||||||
PYBIND11_WARNING_DISABLE_GCC("-Wstringop-overread")
|
PYBIND11_WARNING_DISABLE_GCC("-Wstringop-overread")
|
||||||
#endif
|
#endif
|
||||||
internals.registered_types_py[(PyTypeObject *) m_ptr] = {tinfo};
|
internals.registered_types_py[reinterpret_cast<PyTypeObject *>(m_ptr)] = {tinfo};
|
||||||
PYBIND11_WARNING_POP
|
PYBIND11_WARNING_POP
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1712,7 +1713,8 @@ protected:
|
|||||||
mark_parents_nonsimple(tinfo->type);
|
mark_parents_nonsimple(tinfo->type);
|
||||||
tinfo->simple_ancestors = false;
|
tinfo->simple_ancestors = false;
|
||||||
} else if (rec.bases.size() == 1) {
|
} else if (rec.bases.size() == 1) {
|
||||||
auto *parent_tinfo = get_type_info((PyTypeObject *) rec.bases[0].ptr());
|
auto *parent_tinfo
|
||||||
|
= get_type_info(reinterpret_cast<PyTypeObject *>(rec.bases[0].ptr()));
|
||||||
assert(parent_tinfo != nullptr);
|
assert(parent_tinfo != nullptr);
|
||||||
bool parent_simple_ancestors = parent_tinfo->simple_ancestors;
|
bool parent_simple_ancestors = parent_tinfo->simple_ancestors;
|
||||||
tinfo->simple_ancestors = parent_simple_ancestors;
|
tinfo->simple_ancestors = parent_simple_ancestors;
|
||||||
@@ -1731,17 +1733,17 @@ 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(reinterpret_cast<PyTypeObject *>(h.ptr()));
|
||||||
if (tinfo2) {
|
if (tinfo2) {
|
||||||
tinfo2->simple_type = false;
|
tinfo2->simple_type = false;
|
||||||
}
|
}
|
||||||
mark_parents_nonsimple((PyTypeObject *) h.ptr());
|
mark_parents_nonsimple(reinterpret_cast<PyTypeObject *>(h.ptr()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void install_buffer_funcs(buffer_info *(*get_buffer)(PyObject *, void *),
|
void install_buffer_funcs(buffer_info *(*get_buffer)(PyObject *, void *),
|
||||||
void *get_buffer_data) {
|
void *get_buffer_data) {
|
||||||
auto *type = (PyHeapTypeObject *) m_ptr;
|
auto *type = reinterpret_cast<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) {
|
||||||
@@ -1763,8 +1765,8 @@ protected:
|
|||||||
const auto is_static = (rec_func != nullptr) && !(rec_func->is_method && rec_func->scope);
|
const auto is_static = (rec_func != nullptr) && !(rec_func->is_method && rec_func->scope);
|
||||||
const auto has_doc = (rec_func != nullptr) && (rec_func->doc != nullptr)
|
const auto has_doc = (rec_func != nullptr) && (rec_func->doc != nullptr)
|
||||||
&& pybind11::options::show_user_defined_docstrings();
|
&& pybind11::options::show_user_defined_docstrings();
|
||||||
auto property = handle(
|
auto property = handle(reinterpret_cast<PyObject *>(
|
||||||
(PyObject *) (is_static ? get_internals().static_property_type : &PyProperty_Type));
|
is_static ? get_internals().static_property_type : &PyProperty_Type));
|
||||||
attr(name) = property(fget.ptr() ? fget : none(),
|
attr(name) = property(fget.ptr() ? fget : none(),
|
||||||
fset.ptr() ? fset : none(),
|
fset.ptr() ? fset : none(),
|
||||||
/*deleter*/ none(),
|
/*deleter*/ none(),
|
||||||
@@ -2698,8 +2700,9 @@ struct enum_base {
|
|||||||
|
|
||||||
PYBIND11_NOINLINE void init(bool is_arithmetic, bool is_convertible) {
|
PYBIND11_NOINLINE void init(bool is_arithmetic, bool is_convertible) {
|
||||||
m_base.attr("__entries") = dict();
|
m_base.attr("__entries") = dict();
|
||||||
auto property = handle((PyObject *) &PyProperty_Type);
|
auto property = handle(reinterpret_cast<PyObject *>(&PyProperty_Type));
|
||||||
auto static_property = handle((PyObject *) get_internals().static_property_type);
|
auto static_property
|
||||||
|
= handle(reinterpret_cast<PyObject *>(get_internals().static_property_type));
|
||||||
|
|
||||||
m_base.attr("__repr__") = cpp_function(
|
m_base.attr("__repr__") = cpp_function(
|
||||||
[](const object &arg) -> str {
|
[](const object &arg) -> str {
|
||||||
@@ -2730,7 +2733,7 @@ struct enum_base {
|
|||||||
[](handle arg) -> std::string {
|
[](handle arg) -> std::string {
|
||||||
std::string docstring;
|
std::string docstring;
|
||||||
dict entries = arg.attr("__entries");
|
dict entries = arg.attr("__entries");
|
||||||
if (((PyTypeObject *) arg.ptr())->tp_doc) {
|
if ((reinterpret_cast<PyTypeObject *>(arg.ptr()))->tp_doc) {
|
||||||
docstring += std::string(
|
docstring += std::string(
|
||||||
reinterpret_cast<PyTypeObject *>(arg.ptr())->tp_doc);
|
reinterpret_cast<PyTypeObject *>(arg.ptr())->tp_doc);
|
||||||
docstring += "\n\n";
|
docstring += "\n\n";
|
||||||
@@ -2856,7 +2859,7 @@ struct enum_base {
|
|||||||
dict entries = m_base.attr("__entries");
|
dict entries = m_base.attr("__entries");
|
||||||
str name(name_);
|
str name(name_);
|
||||||
if (entries.contains(name)) {
|
if (entries.contains(name)) {
|
||||||
std::string type_name = (std::string) str(m_base.attr("__name__"));
|
std::string type_name = std::string(str(m_base.attr("__name__")));
|
||||||
throw value_error(std::move(type_name) + ": element \"" + std::string(name_)
|
throw value_error(std::move(type_name) + ": element \"" + std::string(name_)
|
||||||
+ "\" already exists!");
|
+ "\" already exists!");
|
||||||
}
|
}
|
||||||
@@ -3051,7 +3054,7 @@ all_type_info_get_cache(PyTypeObject *type) {
|
|||||||
if (res.second) {
|
if (res.second) {
|
||||||
// New cache entry created; set up a weak reference to automatically remove it if the type
|
// New cache entry created; set up a weak reference to automatically remove it if the type
|
||||||
// gets destroyed:
|
// gets destroyed:
|
||||||
weakref((PyObject *) type, cpp_function([type](handle wr) {
|
weakref(reinterpret_cast<PyObject *>(type), cpp_function([type](handle wr) {
|
||||||
with_internals([type](internals &internals) {
|
with_internals([type](internals &internals) {
|
||||||
internals.registered_types_py.erase(type);
|
internals.registered_types_py.erase(type);
|
||||||
|
|
||||||
@@ -3292,7 +3295,7 @@ void implicitly_convertible() {
|
|||||||
}
|
}
|
||||||
tuple args(1);
|
tuple args(1);
|
||||||
args[0] = obj;
|
args[0] = obj;
|
||||||
PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr);
|
PyObject *result = PyObject_Call(reinterpret_cast<PyObject *>(type), args.ptr(), nullptr);
|
||||||
if (result == nullptr) {
|
if (result == nullptr) {
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
}
|
}
|
||||||
@@ -3508,7 +3511,7 @@ get_type_override(const void *this_ptr, const type_info *this_type, const char *
|
|||||||
if (frame != nullptr) {
|
if (frame != nullptr) {
|
||||||
PyCodeObject *f_code = PyFrame_GetCode(frame);
|
PyCodeObject *f_code = PyFrame_GetCode(frame);
|
||||||
// f_code is guaranteed to not be NULL
|
// f_code is guaranteed to not be NULL
|
||||||
if ((std::string) str(f_code->co_name) == name && f_code->co_argcount > 0) {
|
if (std::string(str(f_code->co_name)) == name && f_code->co_argcount > 0) {
|
||||||
# if PY_VERSION_HEX >= 0x030d0000
|
# if PY_VERSION_HEX >= 0x030d0000
|
||||||
PyObject *locals = PyEval_GetFrameLocals();
|
PyObject *locals = PyEval_GetFrameLocals();
|
||||||
# else
|
# else
|
||||||
|
|||||||
@@ -547,7 +547,7 @@ struct error_fetch_and_normalize {
|
|||||||
// The presence of __notes__ is likely due to exception normalization
|
// The presence of __notes__ is likely due to exception normalization
|
||||||
// errors, although that is not necessarily true, therefore insert a
|
// errors, although that is not necessarily true, therefore insert a
|
||||||
// hint only:
|
// hint only:
|
||||||
if (PyObject_HasAttrString(m_value.ptr(), "__notes__")) {
|
if (PyObject_HasAttrString(m_value.ptr(), "__notes__") != 0) {
|
||||||
m_lazy_error_string += "[WITH __notes__]";
|
m_lazy_error_string += "[WITH __notes__]";
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@@ -1563,7 +1563,9 @@ public:
|
|||||||
PYBIND11_OBJECT(type, object, PyType_Check)
|
PYBIND11_OBJECT(type, object, PyType_Check)
|
||||||
|
|
||||||
/// Return a type handle from a handle or an object
|
/// Return a type handle from a handle or an object
|
||||||
static handle handle_of(handle h) { return handle((PyObject *) Py_TYPE(h.ptr())); }
|
static handle handle_of(handle h) {
|
||||||
|
return handle(reinterpret_cast<PyObject *>(Py_TYPE(h.ptr())));
|
||||||
|
}
|
||||||
|
|
||||||
/// Return a type object from a handle or an object
|
/// Return a type object from a handle or an object
|
||||||
static type of(handle h) { return type(type::handle_of(h), borrowed_t{}); }
|
static type of(handle h) { return type(type::handle_of(h), borrowed_t{}); }
|
||||||
@@ -1661,7 +1663,7 @@ public:
|
|||||||
if (PyBytes_AsStringAndSize(temp.ptr(), &buffer, &length) != 0) {
|
if (PyBytes_AsStringAndSize(temp.ptr(), &buffer, &length) != 0) {
|
||||||
throw error_already_set();
|
throw error_already_set();
|
||||||
}
|
}
|
||||||
return std::string(buffer, (size_t) length);
|
return std::string(buffer, static_cast<size_t>(length));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
@@ -1861,10 +1863,12 @@ template <typename Unsigned>
|
|||||||
Unsigned as_unsigned(PyObject *o) {
|
Unsigned as_unsigned(PyObject *o) {
|
||||||
if (sizeof(Unsigned) <= sizeof(unsigned long)) {
|
if (sizeof(Unsigned) <= sizeof(unsigned long)) {
|
||||||
unsigned long v = PyLong_AsUnsignedLong(o);
|
unsigned long v = PyLong_AsUnsignedLong(o);
|
||||||
return v == (unsigned long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v;
|
return v == static_cast<unsigned long>(-1) && PyErr_Occurred() ? (Unsigned) -1
|
||||||
|
: (Unsigned) v;
|
||||||
}
|
}
|
||||||
unsigned long long v = PyLong_AsUnsignedLongLong(o);
|
unsigned long long v = PyLong_AsUnsignedLongLong(o);
|
||||||
return v == (unsigned long long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v;
|
return v == static_cast<unsigned long long>(-1) && PyErr_Occurred() ? (Unsigned) -1
|
||||||
|
: (Unsigned) v;
|
||||||
}
|
}
|
||||||
PYBIND11_NAMESPACE_END(detail)
|
PYBIND11_NAMESPACE_END(detail)
|
||||||
|
|
||||||
@@ -1908,7 +1912,7 @@ public:
|
|||||||
PYBIND11_OBJECT_CVT(float_, object, PyFloat_Check, PyNumber_Float)
|
PYBIND11_OBJECT_CVT(float_, object, PyFloat_Check, PyNumber_Float)
|
||||||
// Allow implicit conversion from float/double:
|
// Allow implicit conversion from float/double:
|
||||||
// NOLINTNEXTLINE(google-explicit-constructor)
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
||||||
float_(float value) : object(PyFloat_FromDouble((double) value), stolen_t{}) {
|
float_(float value) : object(PyFloat_FromDouble(static_cast<double>(value)), stolen_t{}) {
|
||||||
if (!m_ptr) {
|
if (!m_ptr) {
|
||||||
pybind11_fail("Could not allocate float object!");
|
pybind11_fail("Could not allocate float object!");
|
||||||
}
|
}
|
||||||
@@ -1920,7 +1924,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// NOLINTNEXTLINE(google-explicit-constructor)
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
||||||
operator float() const { return (float) PyFloat_AsDouble(m_ptr); }
|
operator float() const { return static_cast<float>(PyFloat_AsDouble(m_ptr)); }
|
||||||
// NOLINTNEXTLINE(google-explicit-constructor)
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
||||||
operator double() const { return PyFloat_AsDouble(m_ptr); }
|
operator double() const { return PyFloat_AsDouble(m_ptr); }
|
||||||
};
|
};
|
||||||
@@ -2122,7 +2126,7 @@ public:
|
|||||||
pybind11_fail("Could not allocate tuple object!");
|
pybind11_fail("Could not allocate tuple object!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
size_t size() const { return (size_t) PyTuple_Size(m_ptr); }
|
size_t size() const { return static_cast<size_t>(PyTuple_Size(m_ptr)); }
|
||||||
bool empty() const { return size() == 0; }
|
bool empty() const { return size() == 0; }
|
||||||
detail::tuple_accessor operator[](size_t index) const { return {*this, index}; }
|
detail::tuple_accessor operator[](size_t index) const { return {*this, index}; }
|
||||||
template <typename T, detail::enable_if_t<detail::is_pyobject<T>::value, int> = 0>
|
template <typename T, detail::enable_if_t<detail::is_pyobject<T>::value, int> = 0>
|
||||||
@@ -2156,7 +2160,7 @@ public:
|
|||||||
typename collector = detail::deferred_t<detail::unpacking_collector<>, Args...>>
|
typename collector = detail::deferred_t<detail::unpacking_collector<>, Args...>>
|
||||||
explicit dict(Args &&...args) : dict(collector(std::forward<Args>(args)...).kwargs()) {}
|
explicit dict(Args &&...args) : dict(collector(std::forward<Args>(args)...).kwargs()) {}
|
||||||
|
|
||||||
size_t size() const { return (size_t) PyDict_Size(m_ptr); }
|
size_t size() const { return static_cast<size_t>(PyDict_Size(m_ptr)); }
|
||||||
bool empty() const { return size() == 0; }
|
bool empty() const { return size() == 0; }
|
||||||
detail::dict_iterator begin() const { return {*this, 0}; }
|
detail::dict_iterator begin() const { return {*this, 0}; }
|
||||||
detail::dict_iterator end() const { return {}; }
|
detail::dict_iterator end() const { return {}; }
|
||||||
@@ -2176,7 +2180,8 @@ private:
|
|||||||
if (PyDict_Check(op)) {
|
if (PyDict_Check(op)) {
|
||||||
return handle(op).inc_ref().ptr();
|
return handle(op).inc_ref().ptr();
|
||||||
}
|
}
|
||||||
return PyObject_CallFunctionObjArgs((PyObject *) &PyDict_Type, op, nullptr);
|
return PyObject_CallFunctionObjArgs(
|
||||||
|
reinterpret_cast<PyObject *>(&PyDict_Type), op, nullptr);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -2188,7 +2193,7 @@ public:
|
|||||||
if (result == -1) {
|
if (result == -1) {
|
||||||
throw error_already_set();
|
throw error_already_set();
|
||||||
}
|
}
|
||||||
return (size_t) result;
|
return static_cast<size_t>(result);
|
||||||
}
|
}
|
||||||
bool empty() const { return size() == 0; }
|
bool empty() const { return size() == 0; }
|
||||||
detail::sequence_accessor operator[](size_t index) const { return {*this, index}; }
|
detail::sequence_accessor operator[](size_t index) const { return {*this, index}; }
|
||||||
@@ -2211,7 +2216,7 @@ public:
|
|||||||
pybind11_fail("Could not allocate list object!");
|
pybind11_fail("Could not allocate list object!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
size_t size() const { return (size_t) PyList_Size(m_ptr); }
|
size_t size() const { return static_cast<size_t>(PyList_Size(m_ptr)); }
|
||||||
bool empty() const { return size() == 0; }
|
bool empty() const { return size() == 0; }
|
||||||
detail::list_accessor operator[](size_t index) const { return {*this, index}; }
|
detail::list_accessor operator[](size_t index) const { return {*this, index}; }
|
||||||
template <typename T, detail::enable_if_t<detail::is_pyobject<T>::value, int> = 0>
|
template <typename T, detail::enable_if_t<detail::is_pyobject<T>::value, int> = 0>
|
||||||
@@ -2497,7 +2502,7 @@ inline size_t len(handle h) {
|
|||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
throw error_already_set();
|
throw error_already_set();
|
||||||
}
|
}
|
||||||
return (size_t) result;
|
return static_cast<size_t>(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the length hint of a Python object.
|
/// Get the length hint of a Python object.
|
||||||
@@ -2510,7 +2515,7 @@ inline size_t len_hint(handle h) {
|
|||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return (size_t) result;
|
return static_cast<size_t>(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline str repr(handle h) {
|
inline str repr(handle h) {
|
||||||
|
|||||||
Reference in New Issue
Block a user