diff options
26 files changed, 1743 insertions, 751 deletions
diff --git a/host/lib/deps/pybind11/README.md b/host/lib/deps/pybind11/README.md index 16cc54ebb..6622895b2 100644 --- a/host/lib/deps/pybind11/README.md +++ b/host/lib/deps/pybind11/README.md @@ -1,6 +1,6 @@ # PyBind11: Third-Party Dependency for UHD -Version: 25abf7ef (post-2.2.4) +Version: 2.6.1 (git hash: f1abf5d) PyBind11 is a replacement for Boost.Python. Unlike Boost.Python, however, we ship it with the UHD repository instead of relying on it to be there external to @@ -9,6 +9,9 @@ most distributions at this time. It also allows us to lock down the version of PyBind11 used; with Boost, we often have to apply hacks to make sure UHD works across all of the Boost versions that we support. +Note that the UHD CMake allows for using a different version of PyBind11 (e.g., +the one installed via package manager). + ## License for PyBind11 As a seperate, third-party project, PyBind11 has a different license from UHD. diff --git a/host/lib/deps/pybind11/include/pybind11/attr.h b/host/lib/deps/pybind11/include/pybind11/attr.h index 1bd25b236..ac75a4531 100644 --- a/host/lib/deps/pybind11/include/pybind11/attr.h +++ b/host/lib/deps/pybind11/include/pybind11/attr.h @@ -6,13 +6,11 @@ */ - - #pragma once #include "cast.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) @@ -24,6 +22,9 @@ struct is_method { handle class_; is_method(const handle &c) : class_(c) { } }; struct is_operator { }; +struct is_final { }; + + struct scope { handle value; scope(const handle &s) : value(s) { } }; @@ -37,6 +38,7 @@ struct sibling { handle value; sibling(const handle &value) : value(value.ptr()) template <typename T> struct base { + PYBIND11_DEPRECATED("base<T>() was deprecated in favor of specifying 'T' as a template argument to class_") base() { } }; @@ -58,7 +60,7 @@ struct metaclass { handle value; PYBIND11_DEPRECATED("py::metaclass() is no longer required. It's turned on by default now.") - metaclass() {} + metaclass() { } explicit metaclass(handle value) : value(value) { } @@ -71,6 +73,9 @@ struct module_local { const bool value; constexpr module_local(bool v = true) : struct arithmetic { }; +struct prepend { }; + + template <typename... Ts> struct call_guard; template <> struct call_guard<> { using type = detail::void_type; }; @@ -93,7 +98,7 @@ struct call_guard<T, Ts...> { -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) enum op_id : int; enum op_type : int; @@ -117,7 +122,8 @@ struct argument_record { struct function_record { function_record() : is_constructor(false), is_new_style_constructor(false), is_stateless(false), - is_operator(false), has_args(false), has_kwargs(false), is_method(false) { } + is_operator(false), is_method(false), has_args(false), + has_kwargs(false), has_kw_only_args(false), prepend(false) { } char *name = nullptr; @@ -156,18 +162,30 @@ struct function_record { bool is_operator : 1; + bool is_method : 1; + + bool has_args : 1; bool has_kwargs : 1; - bool is_method : 1; + bool has_kw_only_args : 1; + + + bool prepend : 1; std::uint16_t nargs; + std::uint16_t nargs_kw_only = 0; + + + std::uint16_t nargs_pos_only = 0; + + PyMethodDef *def = nullptr; @@ -183,7 +201,8 @@ struct function_record { struct type_record { PYBIND11_NOINLINE type_record() - : multiple_inheritance(false), dynamic_attr(false), buffer_protocol(false), module_local(false) { } + : multiple_inheritance(false), dynamic_attr(false), buffer_protocol(false), + default_holder(true), module_local(false), is_final(false) { } handle scope; @@ -236,6 +255,9 @@ struct type_record { bool module_local : 1; + + bool is_final : 1; + PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *)) { auto base_info = detail::get_type_info(base, false); if (!base_info) { @@ -330,12 +352,20 @@ template <> struct process_attribute<is_new_style_constructor> : process_attribu static void init(const is_new_style_constructor &, function_record *r) { r->is_new_style_constructor = true; } }; +inline void process_kw_only_arg(const arg &a, function_record *r) { + if (!a.name || strlen(a.name) == 0) + pybind11_fail("arg(): cannot specify an unnamed argument after an kw_only() annotation"); + ++r->nargs_kw_only; +} + template <> struct process_attribute<arg> : process_attribute_default<arg> { static void init(const arg &a, function_record *r) { if (r->is_method && r->args.empty()) r->args.emplace_back("self", nullptr, handle(), true , false ); r->args.emplace_back(a.name, nullptr, handle(), !a.flag_noconvert, a.flag_none); + + if (r->has_kw_only_args) process_kw_only_arg(a, r); } }; @@ -367,6 +397,22 @@ template <> struct process_attribute<arg_v> : process_attribute_default<arg_v> { #endif } r->args.emplace_back(a.name, a.descr, a.value.inc_ref(), !a.flag_noconvert, a.flag_none); + + if (r->has_kw_only_args) process_kw_only_arg(a, r); + } +}; + + +template <> struct process_attribute<kw_only> : process_attribute_default<kw_only> { + static void init(const kw_only &, function_record *r) { + r->has_kw_only_args = true; + } +}; + + +template <> struct process_attribute<pos_only> : process_attribute_default<pos_only> { + static void init(const pos_only &, function_record *r) { + r->nargs_pos_only = static_cast<std::uint16_t>(r->args.size()); } }; @@ -394,6 +440,11 @@ struct process_attribute<dynamic_attr> : process_attribute_default<dynamic_attr> }; template <> +struct process_attribute<is_final> : process_attribute_default<is_final> { + static void init(const is_final &, type_record *r) { r->is_final = true; } +}; + +template <> struct process_attribute<buffer_protocol> : process_attribute_default<buffer_protocol> { static void init(const buffer_protocol &, type_record *r) { r->buffer_protocol = true; } }; @@ -410,6 +461,12 @@ struct process_attribute<module_local> : process_attribute_default<module_local> template <> +struct process_attribute<prepend> : process_attribute_default<prepend> { + static void init(const prepend &, function_record *r) { r->prepend = true; } +}; + + +template <> struct process_attribute<arithmetic> : process_attribute_default<arithmetic> {}; template <typename... Ts> @@ -462,5 +519,5 @@ constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs) { return named == 0 || (self + named + has_args + has_kwargs) == nargs; } -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/include/pybind11/buffer_info.h b/host/lib/deps/pybind11/include/pybind11/buffer_info.h index 9ca97607d..1f24e16ad 100644 --- a/host/lib/deps/pybind11/include/pybind11/buffer_info.h +++ b/host/lib/deps/pybind11/include/pybind11/buffer_info.h @@ -6,13 +6,34 @@ */ - - #pragma once #include "detail/common.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) + +PYBIND11_NAMESPACE_BEGIN(detail) + + +inline std::vector<ssize_t> c_strides(const std::vector<ssize_t> &shape, ssize_t itemsize) { + auto ndim = shape.size(); + std::vector<ssize_t> strides(ndim, itemsize); + if (ndim > 0) + for (size_t i = ndim - 1; i > 0; --i) + strides[i - 1] = strides[i] * shape[i]; + return strides; +} + + +inline std::vector<ssize_t> f_strides(const std::vector<ssize_t> &shape, ssize_t itemsize) { + auto ndim = shape.size(); + std::vector<ssize_t> strides(ndim, itemsize); + for (size_t i = 1; i < ndim; ++i) + strides[i] = strides[i - 1] * shape[i - 1]; + return strides; +} + +PYBIND11_NAMESPACE_END(detail) struct buffer_info { @@ -23,13 +44,14 @@ struct buffer_info { ssize_t ndim = 0; std::vector<ssize_t> shape; std::vector<ssize_t> strides; + bool readonly = false; - buffer_info() { } + buffer_info() = default; buffer_info(void *ptr, ssize_t itemsize, const std::string &format, ssize_t ndim, - detail::any_container<ssize_t> shape_in, detail::any_container<ssize_t> strides_in) + detail::any_container<ssize_t> shape_in, detail::any_container<ssize_t> strides_in, bool readonly=false) : ptr(ptr), itemsize(itemsize), size(1), format(format), ndim(ndim), - shape(std::move(shape_in)), strides(std::move(strides_in)) { + shape(std::move(shape_in)), strides(std::move(strides_in)), readonly(readonly) { if (ndim != (ssize_t) shape.size() || ndim != (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) @@ -37,20 +59,29 @@ struct buffer_info { } template <typename T> - buffer_info(T *ptr, detail::any_container<ssize_t> shape_in, detail::any_container<ssize_t> strides_in) - : buffer_info(private_ctr_tag(), ptr, sizeof(T), format_descriptor<T>::format(), static_cast<ssize_t>(shape_in->size()), std::move(shape_in), std::move(strides_in)) { } + buffer_info(T *ptr, detail::any_container<ssize_t> shape_in, detail::any_container<ssize_t> strides_in, bool readonly=false) + : buffer_info(private_ctr_tag(), ptr, sizeof(T), format_descriptor<T>::format(), static_cast<ssize_t>(shape_in->size()), std::move(shape_in), std::move(strides_in), readonly) { } - buffer_info(void *ptr, ssize_t itemsize, const std::string &format, ssize_t size) - : buffer_info(ptr, itemsize, format, 1, {size}, {itemsize}) { } + buffer_info(void *ptr, ssize_t itemsize, const std::string &format, ssize_t size, bool readonly=false) + : buffer_info(ptr, itemsize, format, 1, {size}, {itemsize}, readonly) { } template <typename T> - buffer_info(T *ptr, ssize_t size) - : buffer_info(ptr, sizeof(T), format_descriptor<T>::format(), size) { } + buffer_info(T *ptr, ssize_t size, bool readonly=false) + : buffer_info(ptr, sizeof(T), format_descriptor<T>::format(), size, readonly) { } + + template <typename T> + buffer_info(const T *ptr, ssize_t size, bool readonly=true) + : buffer_info(const_cast<T*>(ptr), sizeof(T), format_descriptor<T>::format(), size, readonly) { } explicit buffer_info(Py_buffer *view, bool ownview = true) : buffer_info(view->buf, view->itemsize, view->format, view->ndim, - {view->shape, view->shape + view->ndim}, {view->strides, view->strides + view->ndim}) { - this->view = view; + {view->shape, view->shape + view->ndim}, + + view->strides + ? std::vector<ssize_t>(view->strides, view->strides + view->ndim) + : detail::c_strides({view->shape, view->shape + view->ndim}, view->itemsize), + view->readonly) { + this->m_view = view; this->ownview = ownview; } @@ -69,27 +100,30 @@ struct buffer_info { ndim = rhs.ndim; shape = std::move(rhs.shape); strides = std::move(rhs.strides); - std::swap(view, rhs.view); + std::swap(m_view, rhs.m_view); std::swap(ownview, rhs.ownview); + readonly = rhs.readonly; return *this; } ~buffer_info() { - if (view && ownview) { PyBuffer_Release(view); delete view; } + if (m_view && ownview) { PyBuffer_Release(m_view); delete m_view; } } + Py_buffer *view() const { return m_view; } + Py_buffer *&view() { return m_view; } private: struct private_ctr_tag { }; buffer_info(private_ctr_tag, void *ptr, ssize_t itemsize, const std::string &format, ssize_t ndim, - detail::any_container<ssize_t> &&shape_in, detail::any_container<ssize_t> &&strides_in) - : buffer_info(ptr, itemsize, format, ndim, std::move(shape_in), std::move(strides_in)) { } + detail::any_container<ssize_t> &&shape_in, detail::any_container<ssize_t> &&strides_in, bool readonly) + : buffer_info(ptr, itemsize, format, ndim, std::move(shape_in), std::move(strides_in), readonly) { } - Py_buffer *view = nullptr; + Py_buffer *m_view = nullptr; bool ownview = false; }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) template <typename T, typename SFINAE = void> struct compare_buffer_info { static bool compare(const buffer_info& b) { @@ -105,5 +139,5 @@ template <typename T> struct compare_buffer_info<T, detail::enable_if_t<std::is_ } }; -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/include/pybind11/cast.h b/host/lib/deps/pybind11/include/pybind11/cast.h index eeb0ac11c..2682a8c3d 100644 --- a/host/lib/deps/pybind11/include/pybind11/cast.h +++ b/host/lib/deps/pybind11/include/pybind11/cast.h @@ -6,8 +6,6 @@ */ - - #pragma once #include "pytypes.h" @@ -32,8 +30,12 @@ #include <string_view> #endif -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +#if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L +# define PYBIND11_HAS_U8STRING +#endif + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) @@ -55,7 +57,7 @@ public: Py_CLEAR(ptr); - if (stack.capacity() > 16 && stack.size() != 0 && stack.capacity() / stack.size() > 2) + if (stack.capacity() > 16 && !stack.empty() && stack.capacity() / stack.size() > 2) stack.shrink_to_fit(); } @@ -146,7 +148,7 @@ inline const std::vector<detail::type_info *> &all_type_info(PyTypeObject *type) PYBIND11_NOINLINE inline detail::type_info* get_type_info(PyTypeObject *type) { auto &bases = all_type_info(type); - if (bases.size() == 0) + if (bases.empty()) return nullptr; if (bases.size() > 1) pybind11_fail("pybind11::detail::get_type_info: type has multiple pybind11-registered bases"); @@ -191,10 +193,10 @@ PYBIND11_NOINLINE inline handle get_type_handle(const std::type_info &tp, bool t } struct value_and_holder { - instance *inst; - size_t index; - const detail::type_info *type; - void **vh; + instance *inst = nullptr; + size_t index = 0u; + const detail::type_info *type = nullptr; + void **vh = nullptr; value_and_holder(instance *i, const detail::type_info *type, size_t vpos, size_t index) : @@ -203,7 +205,7 @@ struct value_and_holder { {} - value_and_holder() : inst{nullptr} {} + value_and_holder() = default; value_and_holder(size_t index) : index{index} {} @@ -257,8 +259,8 @@ public: struct iterator { private: - instance *inst; - const type_vec *types; + instance *inst = nullptr; + const type_vec *types = nullptr; value_and_holder curr; friend struct values_and_holders; iterator(instance *inst, const type_vec *tinfo) @@ -271,8 +273,8 @@ public: iterator(size_t end) : curr(end) {} public: - bool operator==(const iterator &other) { return curr.index == other.curr.index; } - bool operator!=(const iterator &other) { return curr.index != other.curr.index; } + bool operator==(const iterator &other) const { return curr.index == other.curr.index; } + bool operator!=(const iterator &other) const { return curr.index != other.curr.index; } iterator &operator++() { if (!inst->simple_layout) curr.vh += 1 + (*types)[curr.index]->holder_size_in_ptrs; @@ -316,8 +318,8 @@ PYBIND11_NOINLINE inline value_and_holder instance::get_value_and_holder(const t "(compile in debug mode for type details)"); #else pybind11_fail("pybind11::detail::instance::get_value_and_holder: `" + - std::string(find_type->type->tp_name) + "' is not a pybind11 base of the given `" + - std::string(Py_TYPE(this)->tp_name) + "' instance"); + get_fully_qualified_tp_name(find_type->type) + "' is not a pybind11 base of the given `" + + get_fully_qualified_tp_name(Py_TYPE(this)) + "' instance"); #endif } @@ -406,7 +408,7 @@ PYBIND11_NOINLINE inline std::string error_string() { #if !defined(PYPY_VERSION) if (scope.trace) { - PyTracebackObject *trace = (PyTracebackObject *) scope.trace; + auto *trace = (PyTracebackObject *) scope.trace; while (trace->tb_next) @@ -432,7 +434,7 @@ PYBIND11_NOINLINE inline handle get_object_handle(const void *ptr, const detail: auto &instances = get_internals().registered_instances; auto range = instances.equal_range(ptr); for (auto it = range.first; it != range.second; ++it) { - for (auto vh : values_and_holders(it->second)) { + for (const auto &vh : values_and_holders(it->second)) { if (vh.type == type) return handle((PyObject *) it->second); } @@ -511,9 +513,17 @@ public: case return_value_policy::copy: if (copy_constructor) valueptr = copy_constructor(src); - else - throw cast_error("return_value_policy = copy, but the " - "object is non-copyable!"); + else { +#if defined(NDEBUG) + throw cast_error("return_value_policy = copy, but type is " + "non-copyable! (compile in debug mode for details)"); +#else + std::string type_name(tinfo->cpptype->name()); + detail::clean_type_id(type_name); + throw cast_error("return_value_policy = copy, but type " + + type_name + " is non-copyable!"); +#endif + } wrapper->owned = true; break; @@ -522,9 +532,18 @@ public: valueptr = move_constructor(src); else if (copy_constructor) valueptr = copy_constructor(src); - else - throw cast_error("return_value_policy = move, but the " - "object is neither movable nor copyable!"); + else { +#if defined(NDEBUG) + throw cast_error("return_value_policy = move, but type is neither " + "movable nor copyable! " + "(compile in debug mode for details)"); +#else + std::string type_name(tinfo->cpptype->name()); + detail::clean_type_id(type_name); + throw cast_error("return_value_policy = move, but type " + + type_name + " is neither movable nor copyable!"); +#endif + } wrapper->owned = true; break; @@ -552,10 +571,10 @@ public: if (type->operator_new) { vptr = type->operator_new(type->type_size); } else { - #if defined(PYBIND11_CPP17) + #if defined(__cpp_aligned_new) && (!defined(_MSC_VER) || _MSC_VER >= 1912) if (type->type_align > __STDCPP_DEFAULT_NEW_ALIGNMENT__) vptr = ::operator new(type->type_size, - (std::align_val_t) type->type_align); + std::align_val_t(type->type_align)); else #endif vptr = ::operator new(type->type_size); @@ -593,7 +612,7 @@ public: PYBIND11_NOINLINE bool try_load_foreign_module_local(handle src) { constexpr auto *local_key = PYBIND11_MODULE_LOCAL_ID; - const auto pytype = src.get_type(); + const auto pytype = type::handle_of(src); if (!hasattr(pytype, local_key)) return false; @@ -741,17 +760,31 @@ template <typename T, typename SFINAE = void> struct is_copy_constructible : std template <typename Container> struct is_copy_constructible<Container, enable_if_t<all_of< std::is_copy_constructible<Container>, - std::is_same<typename Container::value_type &, typename Container::reference> + std::is_same<typename Container::value_type &, typename Container::reference>, + + negation<std::is_same<Container, typename Container::value_type>> >::value>> : is_copy_constructible<typename Container::value_type> {}; -#if !defined(PYBIND11_CPP17) + template <typename T1, typename T2> struct is_copy_constructible<std::pair<T1, T2>> : all_of<is_copy_constructible<T1>, is_copy_constructible<T2>> {}; -#endif -NAMESPACE_END(detail) + +template <typename T, typename SFINAE = void> struct is_copy_assignable : std::is_copy_assignable<T> {}; +template <typename Container> struct is_copy_assignable<Container, enable_if_t<all_of< + std::is_copy_assignable<Container>, + std::is_same<typename Container::value_type &, typename Container::reference> + >::value>> : is_copy_assignable<typename Container::value_type> {}; +template <typename T1, typename T2> struct is_copy_assignable<std::pair<T1, T2>> + : all_of<is_copy_assignable<T1>, is_copy_assignable<T2>> {}; + +PYBIND11_NAMESPACE_END(detail) + + + + @@ -771,20 +804,22 @@ NAMESPACE_END(detail) template <typename itype, typename SFINAE = void> -struct polymorphic_type_hook +struct polymorphic_type_hook_base { static const void *get(const itype *src, const std::type_info*&) { return src; } }; template <typename itype> -struct polymorphic_type_hook<itype, detail::enable_if_t<std::is_polymorphic<itype>::value>> +struct polymorphic_type_hook_base<itype, detail::enable_if_t<std::is_polymorphic<itype>::value>> { static const void *get(const itype *src, const std::type_info*& type) { type = src ? &typeid(*src) : nullptr; return dynamic_cast<const void*>(src); } }; +template <typename itype, typename SFINAE = void> +struct polymorphic_type_hook : public polymorphic_type_hook_base<itype> {}; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) template <typename type> class type_caster_base : public type_caster_generic { @@ -926,11 +961,15 @@ public: template <typename CharT> using is_std_char_type = any_of< std::is_same<CharT, char>, +#if defined(PYBIND11_HAS_U8STRING) + std::is_same<CharT, char8_t>, +#endif std::is_same<CharT, char16_t>, std::is_same<CharT, char32_t>, std::is_same<CharT, wchar_t> >; + template <typename T> struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_type<T>::value>> { using _py_type_0 = conditional_t<sizeof(T) <= sizeof(long), long, long long>; @@ -959,10 +998,12 @@ public: : (py_type) PYBIND11_LONG_AS_LONGLONG(src.ptr()); } + bool py_err = py_value == (py_type) -1 && PyErr_Occurred(); - if (py_err || (std::is_integral<T>::value && sizeof(py_type) != sizeof(T) && - (py_value < (py_type) std::numeric_limits<T>::min() || - py_value > (py_type) std::numeric_limits<T>::max()))) { + + + + if (py_err || (std::is_integral<T>::value && sizeof(py_type) != sizeof(T) && py_value != (py_type) (T) py_value)) { bool type_error = py_err && PyErr_ExceptionMatches( #if PY_VERSION_HEX < 0x03000000 && !defined(PYPY_VERSION) PyExc_SystemError @@ -1052,7 +1093,7 @@ public: } - auto &bases = all_type_info((PyTypeObject *) h.get_type().ptr()); + auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr()); if (bases.size() == 1) { value = values_and_holders(reinterpret_cast<instance *>(h.ptr())).begin()->value_ptr(); return true; @@ -1108,6 +1149,8 @@ public: if (res == 0 || res == 1) { value = (bool) res; return true; + } else { + PyErr_Clear(); } } return false; @@ -1125,6 +1168,9 @@ template <typename StringType, bool IsView = false> struct string_caster { static_assert(!std::is_same<CharT, char>::value || sizeof(CharT) == 1, "Unsupported char size != 1"); +#if defined(PYBIND11_HAS_U8STRING) + static_assert(!std::is_same<CharT, char8_t>::value || sizeof(CharT) == 1, "Unsupported char8_t size != 1"); +#endif static_assert(!std::is_same<CharT, char16_t>::value || sizeof(CharT) == 2, "Unsupported char16_t size != 2"); static_assert(!std::is_same<CharT, char32_t>::value || sizeof(CharT) == 4, "Unsupported char32_t size != 4"); @@ -1143,7 +1189,7 @@ template <typename StringType, bool IsView = false> struct string_caster { #if PY_MAJOR_VERSION >= 3 return load_bytes(load_src); #else - if (sizeof(CharT) == 1) { + if (std::is_same<CharT, char>::value) { return load_bytes(load_src); } @@ -1157,11 +1203,11 @@ template <typename StringType, bool IsView = false> struct string_caster { #endif } - object utfNbytes = reinterpret_steal<object>(PyUnicode_AsEncodedString( + auto utfNbytes = reinterpret_steal<object>(PyUnicode_AsEncodedString( load_src.ptr(), UTF_N == 8 ? "utf-8" : UTF_N == 16 ? "utf-16" : "utf-32", nullptr)); if (!utfNbytes) { PyErr_Clear(); return false; } - const CharT *buffer = reinterpret_cast<const CharT *>(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr())); + 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); if (UTF_N > 8) { buffer++; length--; } value = StringType(buffer, length); @@ -1175,7 +1221,7 @@ template <typename StringType, bool IsView = false> struct string_caster { static handle cast(const StringType &src, return_value_policy , handle ) { const char *buffer = reinterpret_cast<const char *>(src.data()); - ssize_t nbytes = ssize_t(src.size() * sizeof(CharT)); + auto nbytes = ssize_t(src.size() * sizeof(CharT)); handle s = decode_utfN(buffer, nbytes); if (!s) throw error_already_set(); return s; @@ -1193,8 +1239,6 @@ private: #else - - return PyUnicode_Decode(buffer, nbytes, UTF_N == 8 ? "utf-8" : UTF_N == 16 ? "utf-16" : "utf-32", nullptr); #endif } @@ -1203,7 +1247,7 @@ private: template <typename C = CharT> - bool load_bytes(enable_if_t<sizeof(C) == 1, handle> src) { + bool load_bytes(enable_if_t<std::is_same<C, char>::value, handle> src) { if (PYBIND11_BYTES_CHECK(src.ptr())) { @@ -1218,7 +1262,7 @@ private: } template <typename C = CharT> - bool load_bytes(enable_if_t<sizeof(C) != 1, handle>) { return false; } + bool load_bytes(enable_if_t<!std::is_same<C, char>::value, handle>) { return false; } }; template <typename CharT, class Traits, class Allocator> @@ -1281,7 +1325,7 @@ public: if (StringCaster::UTF_N == 8 && str_len > 1 && str_len <= 4) { - unsigned char v0 = static_cast<unsigned char>(value[0]); + auto v0 = static_cast<unsigned char>(value[0]); size_t char0_bytes = !(v0 & 0x80) ? 1 : (v0 & 0xE0) == 0xC0 ? 2 : (v0 & 0xF0) == 0xE0 ? 3 : @@ -1339,6 +1383,17 @@ public: return cast_impl(std::forward<T>(src), policy, parent, indices{}); } + + template <typename T> + static handle cast(T *src, return_value_policy policy, handle parent) { + if (!src) return none().release(); + if (policy == return_value_policy::take_ownership) { + auto h = cast(std::move(*src), policy, parent); delete src; return h; + } else { + return cast(*src, policy, parent); + } + } + static constexpr auto name = _("Tuple[") + concat(make_caster<Ts>::name...) + _("]"); template <typename T> using cast_op_type = type; @@ -1356,9 +1411,14 @@ protected: template <size_t... Is> bool load_impl(const sequence &seq, bool convert, index_sequence<Is...>) { +#ifdef __cpp_fold_expressions + if ((... || !std::get<Is>(subcasters).load(seq[Is], convert))) + return false; +#else for (bool r : {std::get<Is>(subcasters).load(seq[Is], convert)...}) if (!r) return false; +#endif return true; } @@ -1411,16 +1471,11 @@ public: } explicit operator type*() { return this->value; } - explicit operator type&() { return *(this->value); } - explicit operator holder_type*() { return std::addressof(holder); } - - #if defined(__ICC) || defined(__INTEL_COMPILER) - operator holder_type&() { return holder; } - #else + explicit operator type&() { return *(static_cast<type *>(this->value)); } + explicit operator holder_type*() { return std::addressof(holder); } explicit operator holder_type&() { return holder; } - #endif static handle cast(const holder_type &src, return_value_policy, handle) { const auto *ptr = holder_helper<holder_type>::get(src); @@ -1517,6 +1572,10 @@ template <typename base, typename deleter> struct is_holder_type<base, std::uniq template <typename T> struct handle_type_name { static constexpr auto name = _<T>(); }; template <> struct handle_type_name<bytes> { static constexpr auto name = _(PYBIND11_BYTES_NAME); }; +template <> struct handle_type_name<int_> { static constexpr auto name = _("int"); }; +template <> struct handle_type_name<iterable> { static constexpr auto name = _("Iterable"); }; +template <> struct handle_type_name<iterator> { static constexpr auto name = _("Iterator"); }; +template <> struct handle_type_name<none> { static constexpr auto name = _("None"); }; template <> struct handle_type_name<args> { static constexpr auto name = _("*args"); }; template <> struct handle_type_name<kwargs> { static constexpr auto name = _("**kwargs"); }; @@ -1603,7 +1662,7 @@ template <typename T, typename SFINAE> type_caster<T, SFINAE> &load_type(type_ca throw cast_error("Unable to cast Python instance to C++ type (compile in debug mode for details)"); #else throw cast_error("Unable to cast Python instance of type " + - (std::string) str(handle.get_type()) + " to C++ type '" + type_id<T>() + "'"); + (std::string) str(type::handle_of(handle)) + " to C++ type '" + type_id<T>() + "'"); #endif } return conv; @@ -1615,7 +1674,7 @@ template <typename T> make_caster<T> load_type(const handle &handle) { return conv; } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) template <typename T, detail::enable_if_t<!detail::is_pyobject<T>::value, int> = 0> @@ -1632,13 +1691,16 @@ T cast(const handle &handle) { return T(reinterpret_borrow<object>(handle)); } template <typename T, detail::enable_if_t<!detail::is_pyobject<T>::value, int> = 0> -object cast(const T &value, return_value_policy policy = return_value_policy::automatic_reference, +object cast(T &&value, return_value_policy policy = return_value_policy::automatic_reference, handle parent = handle()) { + using no_ref_T = typename std::remove_reference<T>::type; if (policy == return_value_policy::automatic) - policy = std::is_pointer<T>::value ? return_value_policy::take_ownership : return_value_policy::copy; + policy = std::is_pointer<no_ref_T>::value ? return_value_policy::take_ownership : + std::is_lvalue_reference<T>::value ? return_value_policy::copy : return_value_policy::move; else if (policy == return_value_policy::automatic_reference) - policy = std::is_pointer<T>::value ? return_value_policy::reference : return_value_policy::copy; - return reinterpret_steal<object>(detail::make_caster<T>::cast(value, policy, parent)); + policy = std::is_pointer<no_ref_T>::value ? return_value_policy::reference : + std::is_lvalue_reference<T>::value ? return_value_policy::copy : return_value_policy::move; + return reinterpret_steal<object>(detail::make_caster<T>::cast(std::forward<T>(value), policy, parent)); } template <typename T> T handle::cast() const { return pybind11::cast<T>(*this); } @@ -1651,7 +1713,7 @@ detail::enable_if_t<!detail::move_never<T>::value, T> move(object &&obj) { throw cast_error("Unable to cast Python instance to C++ rvalue: instance has multiple references" " (compile in debug mode for details)"); #else - throw cast_error("Unable to move from Python " + (std::string) str(obj.get_type()) + + throw cast_error("Unable to move from Python " + (std::string) str(type::handle_of(obj)) + " instance to C++ " + type_id<T>() + " instance: instance has multiple references"); #endif @@ -1683,22 +1745,22 @@ template <typename T> T object::cast() && { return pybind11::cast<T>(std::move(* template <> inline void object::cast() const & { return; } template <> inline void object::cast() && { return; } -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) template <typename T, enable_if_t<!is_pyobject<T>::value, int>> object object_or_cast(T &&o) { return pybind11::cast(std::forward<T>(o)); } -struct overload_unused {}; -template <typename ret_type> using overload_caster_t = conditional_t< - cast_is_temporary_value_reference<ret_type>::value, make_caster<ret_type>, overload_unused>; +struct override_unused {}; +template <typename ret_type> using override_caster_t = conditional_t< + cast_is_temporary_value_reference<ret_type>::value, make_caster<ret_type>, override_unused>; template <typename T> enable_if_t<cast_is_temporary_value_reference<T>::value, T> cast_ref(object &&o, make_caster<T> &caster) { return cast_op<T>(load_type(caster, o)); } -template <typename T> enable_if_t<!cast_is_temporary_value_reference<T>::value, T> cast_ref(object &&, overload_unused &) { +template <typename T> enable_if_t<!cast_is_temporary_value_reference<T>::value, T> cast_ref(object &&, override_unused &) { pybind11_fail("Internal error: cast_ref fallback invoked"); } @@ -1710,7 +1772,7 @@ template <typename T> enable_if_t<cast_is_temporary_value_reference<T>::value, T pybind11_fail("Internal error: cast_safe fallback invoked"); } template <> inline void cast_safe<void>(object &&) {} -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) template <return_value_policy policy = return_value_policy::automatic_reference> tuple make_tuple() { return tuple(0); } @@ -1800,6 +1862,16 @@ public: #endif }; + + + +struct kw_only {}; + + + + +struct pos_only {}; + template <typename T> arg_v arg::operator=(T &&value) const { return {std::move(*this), std::forward<T>(value)}; } @@ -1811,7 +1883,7 @@ inline namespace literals { constexpr arg operator"" _a(const char *name, size_t) { return arg(name); } } -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) struct function_record; @@ -1883,14 +1955,19 @@ private: template <size_t... Is> bool load_impl_sequence(function_call &call, index_sequence<Is...>) { +#ifdef __cpp_fold_expressions + if ((... || !std::get<Is>(argcasters).load(call.args[Is], call.args_convert[Is]))) + return false; +#else for (bool r : {std::get<Is>(argcasters).load(call.args[Is], call.args_convert[Is])...}) if (!r) return false; +#endif return true; } template <typename Return, typename Func, size_t... Is, typename Guard> - Return call_impl(Func &&f, index_sequence<Is...>, Guard &&) { + Return call_impl(Func &&f, index_sequence<Is...>, Guard &&) && { return std::forward<Func>(f)(cast_op<Args>(std::move(std::get<Is>(argcasters)))...); } @@ -1967,7 +2044,7 @@ private: } void process(list &args_list, detail::args_proxy ap) { - for (const auto &a : ap) + for (auto a : ap) args_list.append(a); } @@ -1999,7 +2076,7 @@ private: void process(list & , detail::kwargs_proxy kp) { if (!kp) return; - for (const auto &k : reinterpret_borrow<dict>(kp)) { + for (auto k : reinterpret_borrow<dict>(kp)) { if (m_kwargs.contains(k.first)) { #if defined(NDEBUG) multiple_values_error(); @@ -2077,7 +2154,19 @@ object object_api<Derived>::call(Args &&...args) const { return operator()<policy>(std::forward<Args>(args)...); } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) + + +template<typename T> +handle type::handle_of() { + static_assert( + std::is_base_of<detail::type_caster_generic, detail::make_caster<T>>::value, + "py::type::of<T> only supports the case where T is a registered C++ types." + ); + + return detail::get_type_handle(typeid(T), true); +} + #define PYBIND11_MAKE_OPAQUE(...) \ namespace pybind11 { namespace detail { \ @@ -2088,4 +2177,4 @@ NAMESPACE_END(detail) #define PYBIND11_TYPE(...) __VA_ARGS__ -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/include/pybind11/chrono.h b/host/lib/deps/pybind11/include/pybind11/chrono.h index 71fd6e7e6..60fa8200f 100644 --- a/host/lib/deps/pybind11/include/pybind11/chrono.h +++ b/host/lib/deps/pybind11/include/pybind11/chrono.h @@ -6,8 +6,6 @@ */ - - #pragma once #include "pybind11.h" @@ -27,15 +25,15 @@ #define PyDateTime_DELTA_GET_MICROSECONDS(o) (((PyDateTime_Delta*)o)->microseconds) #endif -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) template <typename type> class duration_caster { public: - typedef typename type::rep rep; - typedef typename type::period period; + using rep = typename type::rep; + using period = typename type::period; - typedef std::chrono::duration<uint_fast32_t, std::ratio<86400>> days; + using days = std::chrono::duration<uint_fast32_t, std::ratio<86400>>; bool load(handle src, bool) { using namespace std::chrono; @@ -98,7 +96,7 @@ public: template <typename Duration> class type_caster<std::chrono::time_point<std::chrono::system_clock, Duration>> { public: - typedef std::chrono::time_point<std::chrono::system_clock, Duration> type; + using type = std::chrono::time_point<std::chrono::system_clock, Duration>; bool load(handle src, bool) { using namespace std::chrono; @@ -106,8 +104,11 @@ public: if (!PyDateTimeAPI) { PyDateTime_IMPORT; } if (!src) return false; + + std::tm cal; + microseconds msecs; + if (PyDateTime_Check(src.ptr())) { - std::tm cal; cal.tm_sec = PyDateTime_DATE_GET_SECOND(src.ptr()); cal.tm_min = PyDateTime_DATE_GET_MINUTE(src.ptr()); cal.tm_hour = PyDateTime_DATE_GET_HOUR(src.ptr()); @@ -115,11 +116,30 @@ public: cal.tm_mon = PyDateTime_GET_MONTH(src.ptr()) - 1; cal.tm_year = PyDateTime_GET_YEAR(src.ptr()) - 1900; cal.tm_isdst = -1; - - value = system_clock::from_time_t(std::mktime(&cal)) + microseconds(PyDateTime_DATE_GET_MICROSECOND(src.ptr())); - return true; + msecs = microseconds(PyDateTime_DATE_GET_MICROSECOND(src.ptr())); + } else if (PyDate_Check(src.ptr())) { + cal.tm_sec = 0; + cal.tm_min = 0; + cal.tm_hour = 0; + cal.tm_mday = PyDateTime_GET_DAY(src.ptr()); + cal.tm_mon = PyDateTime_GET_MONTH(src.ptr()) - 1; + cal.tm_year = PyDateTime_GET_YEAR(src.ptr()) - 1900; + cal.tm_isdst = -1; + msecs = microseconds(0); + } else if (PyTime_Check(src.ptr())) { + cal.tm_sec = PyDateTime_TIME_GET_SECOND(src.ptr()); + cal.tm_min = PyDateTime_TIME_GET_MINUTE(src.ptr()); + cal.tm_hour = PyDateTime_TIME_GET_HOUR(src.ptr()); + cal.tm_mday = 1; + cal.tm_mon = 0; + cal.tm_year = 70; + cal.tm_isdst = -1; + msecs = microseconds(PyDateTime_TIME_GET_MICROSECOND(src.ptr())); } else return false; + + value = time_point_cast<Duration>(system_clock::from_time_t(std::mktime(&cal)) + msecs); + return true; } static handle cast(const std::chrono::time_point<std::chrono::system_clock, Duration> &src, return_value_policy , handle ) { @@ -128,13 +148,20 @@ public: if (!PyDateTimeAPI) { PyDateTime_IMPORT; } - std::time_t tt = system_clock::to_time_t(src); - std::tm localtime = *std::localtime(&tt); + using us_t = duration<int, std::micro>; + auto us = duration_cast<us_t>(src.time_since_epoch() % seconds(1)); + if (us.count() < 0) + us += seconds(1); - using us_t = duration<int, std::micro>; + + + std::time_t tt = system_clock::to_time_t(time_point_cast<system_clock::duration>(src - us)); + + + std::tm localtime = *std::localtime(&tt); return PyDateTime_FromDateAndTime(localtime.tm_year + 1900, localtime.tm_mon + 1, @@ -142,7 +169,7 @@ public: localtime.tm_hour, localtime.tm_min, localtime.tm_sec, - (duration_cast<us_t>(src.time_since_epoch() % seconds(1))).count()); + us.count()); } PYBIND11_TYPE_CASTER(type, _("datetime.datetime")); }; @@ -158,5 +185,5 @@ template <typename Rep, typename Period> class type_caster<std::chrono::duration : public duration_caster<std::chrono::duration<Rep, Period>> { }; -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/include/pybind11/common.h b/host/lib/deps/pybind11/include/pybind11/common.h index f99e1c4aa..f936812d2 100644 --- a/host/lib/deps/pybind11/include/pybind11/common.h +++ b/host/lib/deps/pybind11/include/pybind11/common.h @@ -4,7 +4,5 @@ All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. */ - - #include "detail/common.h" #warning "Including 'common.h' is deprecated. It will be removed in v3.0. Use 'pybind11.h'." diff --git a/host/lib/deps/pybind11/include/pybind11/complex.h b/host/lib/deps/pybind11/include/pybind11/complex.h index d1079728a..d772f2586 100644 --- a/host/lib/deps/pybind11/include/pybind11/complex.h +++ b/host/lib/deps/pybind11/include/pybind11/complex.h @@ -6,8 +6,6 @@ */ - - #pragma once #include "pybind11.h" @@ -18,7 +16,7 @@ # undef I #endif -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) template <typename T> struct format_descriptor<std::complex<T>, detail::enable_if_t<std::is_floating_point<T>::value>> { static constexpr const char c = format_descriptor<T>::c; @@ -33,7 +31,7 @@ template <typename T> constexpr const char format_descriptor< #endif -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) template <typename T> struct is_fmt_numeric<std::complex<T>, detail::enable_if_t<std::is_floating_point<T>::value>> { static constexpr bool value = true; @@ -62,5 +60,5 @@ public: PYBIND11_TYPE_CASTER(std::complex<T>, _("complex")); }; -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/include/pybind11/detail/class.h b/host/lib/deps/pybind11/include/pybind11/detail/class.h index f185bb071..fcfc231ac 100644 --- a/host/lib/deps/pybind11/include/pybind11/detail/class.h +++ b/host/lib/deps/pybind11/include/pybind11/detail/class.h @@ -6,16 +6,15 @@ */ - - #pragma once #include "../attr.h" +#include "../options.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) -#if PY_VERSION_HEX >= 0x03030000 +#if PY_VERSION_HEX >= 0x03030000 && !defined(PYPY_VERSION) # define PYBIND11_BUILTIN_QUALNAME # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj) #else @@ -24,6 +23,18 @@ NAMESPACE_BEGIN(detail) # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj) setattr((PyObject *) obj, "__qualname__", nameobj) #endif +inline std::string get_fully_qualified_tp_name(PyTypeObject *type) { +#if !defined(PYPY_VERSION) + return type->tp_name; +#else + auto module_name = handle((PyObject *) type).attr("__module__").cast<std::string>(); + if (module_name == PYBIND11_BUILTINS_MODULE) + return type->tp_name; + else + return std::move(module_name) + "." + type->tp_name; +#endif +} + inline PyTypeObject *type_incref(PyTypeObject *type) { Py_INCREF(type); return type; @@ -107,7 +118,7 @@ extern "C" inline int pybind11_meta_setattro(PyObject* obj, PyObject* name, PyOb const auto static_prop = (PyObject *) get_internals().static_property_type; - const auto call_descr_set = descr && PyObject_IsInstance(descr, static_prop) + const auto call_descr_set = descr && value && PyObject_IsInstance(descr, static_prop) && !PyObject_IsInstance(value, static_prop); if (call_descr_set) { @@ -142,6 +153,69 @@ extern "C" inline PyObject *pybind11_meta_getattro(PyObject *obj, PyObject *name #endif +extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, PyObject *kwargs) { + + + PyObject *self = PyType_Type.tp_call(type, args, kwargs); + if (self == nullptr) { + return nullptr; + } + + + auto instance = reinterpret_cast<detail::instance *>(self); + + + for (const auto &vh : values_and_holders(instance)) { + if (!vh.holder_constructed()) { + PyErr_Format(PyExc_TypeError, "%.200s.__init__() must be called when overriding __init__", + get_fully_qualified_tp_name(vh.type->type).c_str()); + Py_DECREF(self); + return nullptr; + } + } + + return self; +} + + +extern "C" inline void pybind11_meta_dealloc(PyObject *obj) { + auto *type = (PyTypeObject *) obj; + auto &internals = get_internals(); + + + + + auto found_type = internals.registered_types_py.find(type); + if (found_type != internals.registered_types_py.end() && + found_type->second.size() == 1 && + found_type->second[0]->type == type) { + + auto *tinfo = found_type->second[0]; + auto tindex = std::type_index(*tinfo->cpptype); + internals.direct_conversions.erase(tindex); + + if (tinfo->module_local) + registered_local_types_cpp().erase(tindex); + else + internals.registered_types_cpp.erase(tindex); + internals.registered_types_py.erase(tinfo->type); + + + auto &cache = internals.inactive_override_cache; + for (auto it = cache.begin(), last = cache.end(); it != last; ) { + if (it->first == (PyObject *) tinfo->type) + it = cache.erase(it); + else + ++it; + } + + delete tinfo; + } + + PyType_Type.tp_dealloc(obj); +} + + inline PyTypeObject* make_default_metaclass() { constexpr auto *name = "pybind11_type"; auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name)); @@ -161,11 +235,15 @@ inline PyTypeObject* make_default_metaclass() { type->tp_base = type_incref(&PyType_Type); type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; + type->tp_call = pybind11_meta_call; + type->tp_setattro = pybind11_meta_setattro; #if PY_MAJOR_VERSION >= 3 type->tp_getattro = pybind11_meta_getattro; #endif + type->tp_dealloc = pybind11_meta_dealloc; + if (PyType_Ready(type) < 0) pybind11_fail("make_default_metaclass(): failure in PyType_Ready()!"); @@ -203,7 +281,7 @@ inline bool deregister_instance_impl(void *ptr, instance *self) { auto ®istered_instances = get_internals().registered_instances; auto range = registered_instances.equal_range(ptr); for (auto it = range.first; it != range.second; ++it) { - if (Py_TYPE(self) == Py_TYPE(it->second)) { + if (self == it->second) { registered_instances.erase(it); return true; } @@ -257,12 +335,7 @@ extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, extern "C" inline int pybind11_object_init(PyObject *self, PyObject *, PyObject *) { PyTypeObject *type = Py_TYPE(self); - std::string msg; -#if defined(PYPY_VERSION) - msg += handle((PyObject *) type).attr("__module__").cast<std::string>() + "."; -#endif - msg += type->tp_name; - msg += ": No constructor defined!"; + std::string msg = get_fully_qualified_tp_name(type) + ": No constructor defined!"; PyErr_SetString(PyExc_TypeError, msg.c_str()); return -1; } @@ -330,6 +403,7 @@ extern "C" inline void pybind11_object_dealloc(PyObject *self) { auto type = Py_TYPE(self); type->tp_free(self); +#if PY_VERSION_HEX < 0x03080000 @@ -337,6 +411,11 @@ extern "C" inline void pybind11_object_dealloc(PyObject *self) { auto pybind11_object_type = (PyTypeObject *) get_internals().instance_base; if (type->tp_dealloc == pybind11_object_type->tp_dealloc) Py_DECREF(type); +#else + + + Py_DECREF(type); +#endif } @@ -390,7 +469,7 @@ extern "C" inline PyObject *pybind11_get_dict(PyObject *self, void *) { extern "C" inline int pybind11_set_dict(PyObject *self, PyObject *new_dict, void *) { if (!PyDict_Check(new_dict)) { PyErr_Format(PyExc_TypeError, "__dict__ must be set to a dictionary, not a '%.200s'", - Py_TYPE(new_dict)->tp_name); + get_fully_qualified_tp_name(Py_TYPE(new_dict)).c_str()); return -1; } PyObject *&dict = *_PyObject_GetDictPtr(self); @@ -417,11 +496,6 @@ extern "C" inline int pybind11_clear(PyObject *self) { inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) { auto type = &heap_type->ht_type; -#if defined(PYPY_VERSION) - pybind11_fail(std::string(type->tp_name) + ": dynamic attributes are " - "currently not supported in " - "conjunction with PyPy!"); -#endif type->tp_flags |= Py_TPFLAGS_HAVE_GC; type->tp_dictoffset = type->tp_basicsize; type->tp_basicsize += (ssize_t)sizeof(PyObject *); @@ -444,7 +518,7 @@ extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int fla if (tinfo && tinfo->get_buffer) break; } - if (view == nullptr || obj == nullptr || !tinfo || !tinfo->get_buffer) { + if (view == nullptr || !tinfo || !tinfo->get_buffer) { if (view) view->obj = nullptr; PyErr_SetString(PyExc_BufferError, "pybind11_getbuffer(): Internal error"); @@ -460,6 +534,13 @@ extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int fla view->len = view->itemsize; for (auto s : info->shape) view->len *= s; + view->readonly = info->readonly; + if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE && info->readonly) { + if (view) + view->obj = nullptr; + PyErr_SetString(PyExc_BufferError, "Writable buffer requested for readonly storage"); + return -1; + } if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) view->format = const_cast<char *>(info->format.c_str()); if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) { @@ -501,17 +582,17 @@ inline PyObject* make_new_python_type(const type_record &rec) { #endif } - object module; + object module_; if (rec.scope) { if (hasattr(rec.scope, "__module__")) - module = rec.scope.attr("__module__"); + module_ = rec.scope.attr("__module__"); else if (hasattr(rec.scope, "__name__")) - module = rec.scope.attr("__name__"); + module_ = rec.scope.attr("__name__"); } auto full_name = c_str( #if !defined(PYPY_VERSION) - module ? str(module).cast<std::string>() + "." + rec.name : + module_ ? str(module_).cast<std::string>() + "." + rec.name : #endif rec.name); @@ -525,7 +606,7 @@ inline PyObject* make_new_python_type(const type_record &rec) { auto &internals = get_internals(); auto bases = tuple(rec.bases); - auto base = (bases.size() == 0) ? internals.instance_base + auto base = (bases.empty()) ? internals.instance_base : bases[0].ptr(); @@ -546,7 +627,7 @@ inline PyObject* make_new_python_type(const type_record &rec) { type->tp_doc = tp_doc; type->tp_base = type_incref((PyTypeObject *)base); type->tp_basicsize = static_cast<ssize_t>(sizeof(instance)); - if (bases.size() > 0) + if (!bases.empty()) type->tp_bases = bases.release().ptr(); @@ -556,12 +637,17 @@ inline PyObject* make_new_python_type(const type_record &rec) { type->tp_as_number = &heap_type->as_number; type->tp_as_sequence = &heap_type->as_sequence; type->tp_as_mapping = &heap_type->as_mapping; +#if PY_VERSION_HEX >= 0x03050000 + type->tp_as_async = &heap_type->as_async; +#endif - type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; + type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE; #if PY_MAJOR_VERSION < 3 type->tp_flags |= Py_TPFLAGS_CHECKTYPES; #endif + if (!rec.is_final) + type->tp_flags |= Py_TPFLAGS_BASETYPE; if (rec.dynamic_attr) enable_dynamic_attributes(heap_type); @@ -581,13 +667,13 @@ inline PyObject* make_new_python_type(const type_record &rec) { else Py_INCREF(type); - if (module) - setattr((PyObject *) type, "__module__", module); + if (module_) + setattr((PyObject *) type, "__module__", module_); PYBIND11_SET_OLDPY_QUALNAME(type, qualname); return (PyObject *) type; } -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/include/pybind11/detail/common.h b/host/lib/deps/pybind11/include/pybind11/detail/common.h index 750174879..d4ad96c89 100644 --- a/host/lib/deps/pybind11/include/pybind11/detail/common.h +++ b/host/lib/deps/pybind11/include/pybind11/detail/common.h @@ -6,16 +6,14 @@ */ - - #pragma once -#if !defined(NAMESPACE_BEGIN) -# define NAMESPACE_BEGIN(name) namespace name { -#endif -#if !defined(NAMESPACE_END) -# define NAMESPACE_END(name) } -#endif +#define PYBIND11_VERSION_MAJOR 2 +#define PYBIND11_VERSION_MINOR 6 +#define PYBIND11_VERSION_PATCH 1 + +#define PYBIND11_NAMESPACE_BEGIN(name) namespace name { +#define PYBIND11_NAMESPACE_END(name) } @@ -48,8 +46,8 @@ #if defined(__INTEL_COMPILER) -# if __INTEL_COMPILER < 1700 -# error pybind11 requires Intel C++ compiler v17 or newer +# if __INTEL_COMPILER < 1800 +# error pybind11 requires Intel C++ compiler v18 or newer # endif #elif defined(__clang__) && !defined(__apple_build_version__) # if __clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ < 3) @@ -93,9 +91,18 @@ # define PYBIND11_DEPRECATED(reason) __attribute__((deprecated(reason))) #endif -#define PYBIND11_VERSION_MAJOR 2 -#define PYBIND11_VERSION_MINOR 3 -#define PYBIND11_VERSION_PATCH dev0 +#if defined(PYBIND11_CPP17) +# define PYBIND11_MAYBE_UNUSED [[maybe_unused]] +#elif defined(_MSC_VER) && !defined(__clang__) +# define PYBIND11_MAYBE_UNUSED +#else +# define PYBIND11_MAYBE_UNUSED __attribute__ ((__unused__)) +#endif + + +#if defined(_MSC_VER) && _MSC_VER >= 1900 +# define HAVE_SNPRINTF 1 +#endif #if defined(_MSC_VER) @@ -104,7 +111,7 @@ # endif # pragma warning(push) # pragma warning(disable: 4510 4610 4512 4005) -# if defined(_DEBUG) +# if defined(_DEBUG) && !defined(Py_DEBUG) # define PYBIND11_DEBUG_MARKER # undef _DEBUG # endif @@ -114,9 +121,6 @@ #include <frameobject.h> #include <pythread.h> -#if defined(_WIN32) && (defined(min) || defined(max)) -# error Macro clash with min and max -- define NOMINMAX when compiling your program on Windows -#endif #if defined(isalnum) # undef isalnum @@ -128,6 +132,10 @@ # undef toupper #endif +#if defined(copysign) +# undef copysign +#endif + #if defined(_MSC_VER) # if defined(PYBIND11_DEBUG_MARKER) # define _DEBUG @@ -142,6 +150,7 @@ #include <vector> #include <string> #include <stdexcept> +#include <exception> #include <unordered_set> #include <unordered_map> #include <memory> @@ -169,7 +178,11 @@ #define PYBIND11_STR_TYPE ::pybind11::str #define PYBIND11_BOOL_ATTR "__bool__" #define PYBIND11_NB_BOOL(ptr) ((ptr)->nb_bool) +#define PYBIND11_BUILTINS_MODULE "builtins" + + #define PYBIND11_PLUGIN_IMPL(name) \ + extern "C" PYBIND11_MAYBE_UNUSED PYBIND11_EXPORT PyObject *PyInit_##name(); \ extern "C" PYBIND11_EXPORT PyObject *PyInit_##name() #else @@ -193,11 +206,15 @@ #define PYBIND11_STR_TYPE ::pybind11::bytes #define PYBIND11_BOOL_ATTR "__nonzero__" #define PYBIND11_NB_BOOL(ptr) ((ptr)->nb_nonzero) +#define PYBIND11_BUILTINS_MODULE "__builtin__" + + #define PYBIND11_PLUGIN_IMPL(name) \ - static PyObject *pybind11_init_wrapper(); \ - extern "C" PYBIND11_EXPORT void init##name() { \ - (void)pybind11_init_wrapper(); \ - } \ + static PyObject *pybind11_init_wrapper(); \ + extern "C" PYBIND11_MAYBE_UNUSED PYBIND11_EXPORT void init##name(); \ + extern "C" PYBIND11_EXPORT void init##name() { \ + (void)pybind11_init_wrapper(); \ + } \ PyObject *pybind11_init_wrapper() #endif @@ -212,6 +229,8 @@ extern "C" { #define PYBIND11_STRINGIFY(x) #x #define PYBIND11_TOSTRING(x) PYBIND11_STRINGIFY(x) #define PYBIND11_CONCAT(first, second) first##second +#define PYBIND11_ENSURE_INTERNALS_READY \ + pybind11::detail::get_internals(); #define PYBIND11_CHECK_PYTHON_VERSION \ { \ @@ -244,6 +263,7 @@ extern "C" { static PyObject *pybind11_init(); \ PYBIND11_PLUGIN_IMPL(name) { \ PYBIND11_CHECK_PYTHON_VERSION \ + PYBIND11_ENSURE_INTERNALS_READY \ try { \ return pybind11_init(); \ } PYBIND11_CATCH_INIT_EXCEPTIONS \ @@ -252,19 +272,25 @@ extern "C" { #define PYBIND11_MODULE(name, variable) \ - static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &); \ + static ::pybind11::module_::module_def \ + PYBIND11_CONCAT(pybind11_module_def_, name) PYBIND11_MAYBE_UNUSED; \ + PYBIND11_MAYBE_UNUSED \ + static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \ PYBIND11_PLUGIN_IMPL(name) { \ PYBIND11_CHECK_PYTHON_VERSION \ - auto m = pybind11::module(PYBIND11_TOSTRING(name)); \ + PYBIND11_ENSURE_INTERNALS_READY \ + auto m = ::pybind11::module_::create_extension_module( \ + PYBIND11_TOSTRING(name), nullptr, \ + &PYBIND11_CONCAT(pybind11_module_def_, name)); \ try { \ PYBIND11_CONCAT(pybind11_init_, name)(m); \ return m.ptr(); \ } PYBIND11_CATCH_INIT_EXCEPTIONS \ } \ - void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &variable) + void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &variable) -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) using ssize_t = Py_ssize_t; using size_t = std::size_t; @@ -293,7 +319,7 @@ enum class return_value_policy : uint8_t { reference_internal }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) inline static constexpr int log2(size_t n, int k = 0) { return (n <= 1) ? k : log2(n >> 1, k + 1); } @@ -375,7 +401,7 @@ using std::make_index_sequence; #else template<size_t ...> struct index_sequence { }; template<size_t N, size_t ...S> struct make_index_sequence_impl : make_index_sequence_impl <N - 1, N - 1, S...> { }; -template<size_t ...S> struct make_index_sequence_impl <0, S...> { typedef index_sequence<S...> type; }; +template<size_t ...S> struct make_index_sequence_impl <0, S...> { using type = index_sequence<S...>; }; template<size_t N> using make_index_sequence = typename make_index_sequence_impl<N>::type; #endif @@ -389,8 +415,16 @@ template <bool... Bs> using select_indices = typename select_indices_impl<index_ template <bool B> using bool_constant = std::integral_constant<bool, B>; template <typename T> struct negation : bool_constant<!T::value> { }; + + + +#if defined(__PGIC__) || defined(__INTEL_COMPILER) +template<typename... > using void_t = void; +#else template <typename...> struct void_t_impl { using type = void; }; template <typename... Ts> using void_t = typename void_t_impl<Ts...>::type; +#endif + #if defined(__cpp_fold_expressions) && !(defined(_MSC_VER) && (_MSC_VER < 1916)) @@ -416,17 +450,17 @@ template <class T, template<class> class... Predicates> using satisfies_none_of template <typename T> struct remove_class { }; -template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...)> { typedef R type(A...); }; -template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...) const> { typedef R type(A...); }; +template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...)> { using type = R (A...); }; +template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...) const> { using type = R (A...); }; -template <typename T> struct intrinsic_type { typedef T type; }; -template <typename T> struct intrinsic_type<const T> { typedef typename intrinsic_type<T>::type type; }; -template <typename T> struct intrinsic_type<T*> { typedef typename intrinsic_type<T>::type type; }; -template <typename T> struct intrinsic_type<T&> { typedef typename intrinsic_type<T>::type type; }; -template <typename T> struct intrinsic_type<T&&> { typedef typename intrinsic_type<T>::type type; }; -template <typename T, size_t N> struct intrinsic_type<const T[N]> { typedef typename intrinsic_type<T>::type type; }; -template <typename T, size_t N> struct intrinsic_type<T[N]> { typedef typename intrinsic_type<T>::type type; }; +template <typename T> struct intrinsic_type { using type = T; }; +template <typename T> struct intrinsic_type<const T> { using type = typename intrinsic_type<T>::type; }; +template <typename T> struct intrinsic_type<T*> { using type = typename intrinsic_type<T>::type; }; +template <typename T> struct intrinsic_type<T&> { using type = typename intrinsic_type<T>::type; }; +template <typename T> struct intrinsic_type<T&&> { using type = typename intrinsic_type<T>::type; }; +template <typename T, size_t N> struct intrinsic_type<const T[N]> { using type = typename intrinsic_type<T>::type; }; +template <typename T, size_t N> struct intrinsic_type<T[N]> { using type = typename intrinsic_type<T>::type; }; template <typename T> using intrinsic_t = typename intrinsic_type<T>::type; @@ -444,7 +478,7 @@ template <typename T, typename... Ts> constexpr size_t constexpr_sum(T n, Ts... ns) { return size_t{n} + constexpr_sum(ns...); } #endif -NAMESPACE_BEGIN(constexpr_impl) +PYBIND11_NAMESPACE_BEGIN(constexpr_impl) constexpr int first(int i) { return i; } template <typename T, typename... Ts> @@ -453,7 +487,7 @@ constexpr int first(int i, T v, Ts... vs) { return v ? i : first(i + 1, vs...); constexpr int last(int , int result) { return result; } template <typename T, typename... Ts> constexpr int last(int i, int result, T v, Ts... vs) { return last(i + 1, v ? i : result, vs...); } -NAMESPACE_END(constexpr_impl) +PYBIND11_NAMESPACE_END(constexpr_impl) @@ -497,8 +531,9 @@ template <typename Base, typename Derived> using is_strict_base_of = bool_consta + template <typename Base, typename Derived> using is_accessible_base_of = bool_constant< - std::is_base_of<Base, Derived>::value && std::is_convertible<Derived *, Base *>::value>; + (std::is_same<Base, Derived>::value || std::is_base_of<Base, Derived>::value) && std::is_convertible<Derived *, Base *>::value>; template <template<typename...> class Base> struct is_template_base_of_impl { @@ -564,10 +599,10 @@ inline void ignore_unused(const int *) { } #define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN) (((PATTERN), void()), ...) #else using expand_side_effects = bool[]; -#define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN) pybind11::detail::expand_side_effects{ ((PATTERN), void(), false)..., false } +#define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN) (void)pybind11::detail::expand_side_effects{ ((PATTERN), void(), false)..., false } #endif -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) class builtin_exception : public std::runtime_error { @@ -589,6 +624,8 @@ PYBIND11_RUNTIME_EXCEPTION(index_error, PyExc_IndexError) PYBIND11_RUNTIME_EXCEPTION(key_error, PyExc_KeyError) PYBIND11_RUNTIME_EXCEPTION(value_error, PyExc_ValueError) PYBIND11_RUNTIME_EXCEPTION(type_error, PyExc_TypeError) +PYBIND11_RUNTIME_EXCEPTION(buffer_error, PyExc_BufferError) +PYBIND11_RUNTIME_EXCEPTION(import_error, PyExc_ImportError) PYBIND11_RUNTIME_EXCEPTION(cast_error, PyExc_RuntimeError) PYBIND11_RUNTIME_EXCEPTION(reference_cast_error, PyExc_RuntimeError) @@ -597,7 +634,7 @@ PYBIND11_RUNTIME_EXCEPTION(reference_cast_error, PyExc_RuntimeError) template <typename T, typename SFINAE = void> struct format_descriptor { }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) @@ -610,7 +647,7 @@ template <typename T> struct is_fmt_numeric<T, enable_if_t<std::is_arithmetic<T> std::is_integral<T>::value ? detail::log2(sizeof(T))*2 + std::is_unsigned<T>::value : 8 + ( std::is_same<T, double>::value ? 1 : std::is_same<T, long double>::value ? 2 : 0)); }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) template <typename T> struct format_descriptor<T, detail::enable_if_t<std::is_arithmetic<T>::value>> { static constexpr const char c = "?bBhHiIqQfdg"[detail::is_fmt_numeric<T>::index]; @@ -635,14 +672,10 @@ struct error_scope { struct nodelete { template <typename T> void operator()(T*) { } }; - -#if defined(PYBIND11_CPP14) -#define PYBIND11_OVERLOAD_CAST 1 - -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) template <typename... Args> struct overload_cast_impl { - constexpr overload_cast_impl() {} + constexpr overload_cast_impl() {}; template <typename Return> constexpr auto operator()(Return (*pf)(Args...)) const noexcept @@ -656,28 +689,32 @@ struct overload_cast_impl { constexpr auto operator()(Return (Class::*pmf)(Args...) const, std::true_type) const noexcept -> decltype(pmf) { return pmf; } }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) +#if defined(PYBIND11_CPP14) +#define PYBIND11_OVERLOAD_CAST 1 + template <typename... Args> static constexpr detail::overload_cast_impl<Args...> overload_cast = {}; +#endif static constexpr auto const_ = std::true_type{}; -#else +#if !defined(PYBIND11_CPP14) template <typename... Args> struct overload_cast { static_assert(detail::deferred_t<std::false_type, Args...>::value, "pybind11::overload_cast<...> requires compiling in C++14 mode"); }; #endif -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) @@ -716,8 +753,8 @@ public: const std::vector<T> *operator->() const { return &v; } }; -NAMESPACE_END(detail) - +std::string get_fully_qualified_tp_name(PyTypeObject*); -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/include/pybind11/detail/descr.h b/host/lib/deps/pybind11/include/pybind11/detail/descr.h index 2b3931283..cfb860eb4 100644 --- a/host/lib/deps/pybind11/include/pybind11/detail/descr.h +++ b/host/lib/deps/pybind11/include/pybind11/detail/descr.h @@ -6,14 +6,12 @@ */ - - #pragma once #include "common.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) #if !defined(_MSC_VER) # define PYBIND11_DESCR_CONSTEXPR static constexpr @@ -97,5 +95,5 @@ constexpr descr<N + 2, Ts...> type_descr(const descr<N, Ts...> &descr) { return _("{") + descr + _("}"); } -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/include/pybind11/detail/init.h b/host/lib/deps/pybind11/include/pybind11/detail/init.h index 8119558a6..74c36040a 100644 --- a/host/lib/deps/pybind11/include/pybind11/detail/init.h +++ b/host/lib/deps/pybind11/include/pybind11/detail/init.h @@ -6,14 +6,12 @@ */ - - #pragma once #include "class.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) template <> class type_caster<value_and_holder> { @@ -31,7 +29,7 @@ private: value_and_holder *value = nullptr; }; -NAMESPACE_BEGIN(initimpl) +PYBIND11_NAMESPACE_BEGIN(initimpl) inline void no_nullptr(void *ptr) { if (!ptr) throw type_error("pybind11::init(): factory function returned nullptr"); @@ -133,6 +131,7 @@ void construct(value_and_holder &v_h, Alias<Class> *alias_ptr, bool) { template <typename Class> void construct(value_and_holder &v_h, Holder<Class> holder, bool need_alias) { auto *ptr = holder_helper<Holder<Class>>::get(holder); + no_nullptr(ptr); if (Class::has_alias && need_alias && !is_alias<Class>(ptr)) throw type_error("pybind11::init(): construction failed: returned holder-wrapped instance " @@ -331,6 +330,6 @@ struct pickle_factory<Get, Set, RetState(Self), NewInstance(ArgState)> { } }; -NAMESPACE_END(initimpl) -NAMESPACE_END(detail) -NAMESPACE_END(pybind11) +PYBIND11_NAMESPACE_END(initimpl) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(pybind11) diff --git a/host/lib/deps/pybind11/include/pybind11/detail/internals.h b/host/lib/deps/pybind11/include/pybind11/detail/internals.h index 7586edbb8..896ec2393 100644 --- a/host/lib/deps/pybind11/include/pybind11/detail/internals.h +++ b/host/lib/deps/pybind11/include/pybind11/detail/internals.h @@ -6,14 +6,12 @@ */ - - #pragma once #include "../pytypes.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) inline PyTypeObject *make_static_property_type(); inline PyTypeObject *make_default_metaclass(); @@ -24,8 +22,9 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass); #if PY_VERSION_HEX >= 0x03070000 # define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr # define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key)) -# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (tstate)) +# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value)) # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr) +# define PYBIND11_TLS_FREE(key) PyThread_tss_free(key) #else # define PYBIND11_TLS_KEY_INIT(var) decltype(PyThread_create_key()) var = 0 @@ -44,6 +43,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass); # define PYBIND11_TLS_REPLACE_VALUE(key, value) \ PyThread_set_key_value((key), (value)) # endif +# define PYBIND11_TLS_FREE(key) (void)key #endif @@ -81,10 +81,10 @@ struct type_equal_to { template <typename value_type> using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>; -struct overload_hash { +struct override_hash { inline size_t operator()(const std::pair<const PyObject *, const char *>& v) const { size_t value = std::hash<const void *>()(v.first); - value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value<<6) + (value>>2); + value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value<<6) + (value>>2); return value; } }; @@ -96,7 +96,7 @@ struct internals { type_map<type_info *> registered_types_cpp; std::unordered_map<PyTypeObject *, std::vector<type_info *>> registered_types_py; std::unordered_multimap<const void *, instance*> registered_instances; - std::unordered_set<std::pair<const PyObject *, const char *>, overload_hash> inactive_overload_cache; + std::unordered_set<std::pair<const PyObject *, const char *>, override_hash> inactive_override_cache; type_map<std::vector<bool (*)(PyObject *, void *&)>> direct_conversions; std::unordered_map<const PyObject *, std::vector<PyObject *>> patients; std::forward_list<void (*) (std::exception_ptr)> registered_exception_translators; @@ -109,6 +109,16 @@ struct internals { #if defined(WITH_THREAD) PYBIND11_TLS_KEY_INIT(tstate); PyInterpreterState *istate = nullptr; + ~internals() { + + + + + + + + PYBIND11_TLS_FREE(tstate); + } #endif }; @@ -138,25 +148,71 @@ struct type_info { }; -#define PYBIND11_INTERNALS_VERSION 3 +#define PYBIND11_INTERNALS_VERSION 4 -#if defined(_DEBUG) -# define PYBIND11_BUILD_TYPE "_debug" + +#if defined(_MSC_VER) && defined(_DEBUG) +# define PYBIND11_BUILD_TYPE "_debug" #else -# define PYBIND11_BUILD_TYPE "" +# define PYBIND11_BUILD_TYPE "" #endif -#if defined(WITH_THREAD) -# define PYBIND11_INTERNALS_KIND "" -#else -# define PYBIND11_INTERNALS_KIND "_without_thread" + + + +#ifndef PYBIND11_COMPILER_TYPE +# if defined(_MSC_VER) +# define PYBIND11_COMPILER_TYPE "_msvc" +# elif defined(__INTEL_COMPILER) +# define PYBIND11_COMPILER_TYPE "_icc" +# elif defined(__clang__) +# define PYBIND11_COMPILER_TYPE "_clang" +# elif defined(__PGI) +# define PYBIND11_COMPILER_TYPE "_pgi" +# elif defined(__MINGW32__) +# define PYBIND11_COMPILER_TYPE "_mingw" +# elif defined(__CYGWIN__) +# define PYBIND11_COMPILER_TYPE "_gcc_cygwin" +# elif defined(__GNUC__) +# define PYBIND11_COMPILER_TYPE "_gcc" +# else +# define PYBIND11_COMPILER_TYPE "_unknown" +# endif +#endif + + +#ifndef PYBIND11_STDLIB +# if defined(_LIBCPP_VERSION) +# define PYBIND11_STDLIB "_libcpp" +# elif defined(__GLIBCXX__) || defined(__GLIBCPP__) +# define PYBIND11_STDLIB "_libstdcpp" +# else +# define PYBIND11_STDLIB "" +# endif +#endif + + +#ifndef PYBIND11_BUILD_ABI +# if defined(__GXX_ABI_VERSION) +# define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION) +# else +# define PYBIND11_BUILD_ABI "" +# endif +#endif + +#ifndef PYBIND11_INTERNALS_KIND +# if defined(WITH_THREAD) +# define PYBIND11_INTERNALS_KIND "" +# else +# define PYBIND11_INTERNALS_KIND "_without_thread" +# endif #endif #define PYBIND11_INTERNALS_ID "__pybind11_internals_v" \ - PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND PYBIND11_BUILD_TYPE "__" + PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE "__" #define PYBIND11_MODULE_LOCAL_ID "__pybind11_module_local_v" \ - PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND PYBIND11_BUILD_TYPE "__" + PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE "__" @@ -165,12 +221,49 @@ inline internals **&get_internals_pp() { return internals_pp; } +inline void translate_exception(std::exception_ptr p) { + try { + if (p) std::rethrow_exception(p); + } catch (error_already_set &e) { e.restore(); return; + } catch (const builtin_exception &e) { e.set_error(); return; + } catch (const std::bad_alloc &e) { PyErr_SetString(PyExc_MemoryError, e.what()); return; + } catch (const std::domain_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; + } catch (const std::invalid_argument &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; + } catch (const std::length_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; + } catch (const std::out_of_range &e) { PyErr_SetString(PyExc_IndexError, e.what()); return; + } catch (const std::range_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; + } catch (const std::overflow_error &e) { PyErr_SetString(PyExc_OverflowError, e.what()); return; + } catch (const std::exception &e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return; + } catch (...) { + PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!"); + return; + } +} + +#if !defined(__GLIBCXX__) +inline void translate_local_exception(std::exception_ptr p) { + try { + if (p) std::rethrow_exception(p); + } catch (error_already_set &e) { e.restore(); return; + } catch (const builtin_exception &e) { e.set_error(); return; + } +} +#endif + PYBIND11_NOINLINE inline internals &get_internals() { auto **&internals_pp = get_internals_pp(); if (internals_pp && *internals_pp) return **internals_pp; + + + struct gil_scoped_acquire_local { + gil_scoped_acquire_local() : state (PyGILState_Ensure()) {} + ~gil_scoped_acquire_local() { PyGILState_Release(state); } + const PyGILState_STATE state; + } gil; + constexpr auto *id = PYBIND11_INTERNALS_ID; auto builtins = handle(PyEval_GetBuiltins()); if (builtins.contains(id) && isinstance<capsule>(builtins[id])) { @@ -182,22 +275,17 @@ PYBIND11_NOINLINE inline internals &get_internals() { #if !defined(__GLIBCXX__) - (*internals_pp)->registered_exception_translators.push_front( - [](std::exception_ptr p) -> void { - try { - if (p) std::rethrow_exception(p); - } catch (error_already_set &e) { e.restore(); return; - } catch (const builtin_exception &e) { e.set_error(); return; - } - } - ); + (*internals_pp)->registered_exception_translators.push_front(&translate_local_exception); #endif } else { if (!internals_pp) internals_pp = new internals*(); auto *&internals_ptr = *internals_pp; internals_ptr = new internals(); #if defined(WITH_THREAD) - PyEval_InitThreads(); + + #if PY_VERSION_HEX < 0x03090000 + PyEval_InitThreads(); + #endif PyThreadState *tstate = PyThreadState_Get(); #if PY_VERSION_HEX >= 0x03070000 internals_ptr->tstate = PyThread_tss_alloc(); @@ -213,25 +301,7 @@ PYBIND11_NOINLINE inline internals &get_internals() { internals_ptr->istate = tstate->interp; #endif builtins[id] = capsule(internals_pp); - internals_ptr->registered_exception_translators.push_front( - [](std::exception_ptr p) -> void { - try { - if (p) std::rethrow_exception(p); - } catch (error_already_set &e) { e.restore(); return; - } catch (const builtin_exception &e) { e.set_error(); return; - } catch (const std::bad_alloc &e) { PyErr_SetString(PyExc_MemoryError, e.what()); return; - } catch (const std::domain_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; - } catch (const std::invalid_argument &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; - } catch (const std::length_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; - } catch (const std::out_of_range &e) { PyErr_SetString(PyExc_IndexError, e.what()); return; - } catch (const std::range_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; - } catch (const std::exception &e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return; - } catch (...) { - PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!"); - return; - } - } - ); + internals_ptr->registered_exception_translators.push_front(&translate_exception); internals_ptr->static_property_type = make_static_property_type(); internals_ptr->default_metaclass = make_default_metaclass(); internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass); @@ -256,7 +326,7 @@ const char *c_str(Args &&...args) { return strings.front().c_str(); } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) @@ -288,4 +358,4 @@ T &get_or_create_shared_data(const std::string &name) { return *ptr; } -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/include/pybind11/detail/typeid.h b/host/lib/deps/pybind11/include/pybind11/detail/typeid.h index 116b4ae36..237a0b4d7 100644 --- a/host/lib/deps/pybind11/include/pybind11/detail/typeid.h +++ b/host/lib/deps/pybind11/include/pybind11/detail/typeid.h @@ -6,8 +6,6 @@ */ - - #pragma once #include <cstdio> @@ -17,8 +15,10 @@ #include <cxxabi.h> #endif -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +#include "common.h" + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) inline void erase_all(std::string &string, const std::string &search) { for (size_t pos = 0;;) { @@ -42,7 +42,7 @@ PYBIND11_NOINLINE inline void clean_type_id(std::string &name) { #endif detail::erase_all(name, "pybind11::"); } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) template <typename T> static std::string type_id() { @@ -51,4 +51,4 @@ template <typename T> static std::string type_id() { return name; } -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/include/pybind11/eigen.h b/host/lib/deps/pybind11/include/pybind11/eigen.h index e71fa7f8f..8a4983eb5 100644 --- a/host/lib/deps/pybind11/include/pybind11/eigen.h +++ b/host/lib/deps/pybind11/include/pybind11/eigen.h @@ -6,8 +6,6 @@ */ - - #pragma once #include "numpy.h" @@ -42,14 +40,14 @@ static_assert(EIGEN_VERSION_AT_LEAST(3,2,7), "Eigen support in pybind11 requires Eigen >= 3.2.7"); -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) using EigenDStride = Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>; template <typename MatrixType> using EigenDRef = Eigen::Ref<MatrixType, 0, EigenDStride>; template <typename MatrixType> using EigenDMap = Eigen::Map<MatrixType, 0, EigenDStride>; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) #if EIGEN_VERSION_AT_LEAST(3,3,0) using EigenIndex = Eigen::Index; @@ -433,7 +431,7 @@ public: if (!need_copy) { - Array aref = reinterpret_borrow<Array>(src); + auto aref = reinterpret_borrow<Array>(src); if (aref && (!need_writeable || aref.writeable())) { fits = props::conformable(aref); @@ -540,9 +538,9 @@ public: template<typename Type> struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> { - typedef typename Type::Scalar Scalar; - typedef remove_reference_t<decltype(*std::declval<Type>().outerIndexPtr())> StorageIndex; - typedef typename Type::Index Index; + using Scalar = typename Type::Scalar; + using StorageIndex = remove_reference_t<decltype(*std::declval<Type>().outerIndexPtr())>; + using Index = typename Type::Index; static constexpr bool rowMajor = Type::IsRowMajor; bool load(handle src, bool) { @@ -550,11 +548,11 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> { return false; auto obj = reinterpret_borrow<object>(src); - object sparse_module = module::import("scipy.sparse"); + object sparse_module = module_::import("scipy.sparse"); object matrix_type = sparse_module.attr( rowMajor ? "csr_matrix" : "csc_matrix"); - if (!obj.get_type().is(matrix_type)) { + if (!type::handle_of(obj).is(matrix_type)) { try { obj = matrix_type(obj); } catch (const error_already_set &) { @@ -581,7 +579,7 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> { static handle cast(const Type &src, return_value_policy , handle ) { const_cast<Type&>(src).makeCompressed(); - object matrix_type = module::import("scipy.sparse").attr( + object matrix_type = module_::import("scipy.sparse").attr( rowMajor ? "csr_matrix" : "csc_matrix"); array data(src.nonZeros(), src.valuePtr()); @@ -598,8 +596,8 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> { + npy_format_descriptor<Scalar>::name + _("]")); }; -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) #if defined(__GNUG__) || defined(__clang__) # pragma GCC diagnostic pop diff --git a/host/lib/deps/pybind11/include/pybind11/embed.h b/host/lib/deps/pybind11/include/pybind11/embed.h index d28c543e1..0d7bca32b 100644 --- a/host/lib/deps/pybind11/include/pybind11/embed.h +++ b/host/lib/deps/pybind11/include/pybind11/embed.h @@ -6,8 +6,6 @@ */ - - #pragma once #include "pybind11.h" @@ -19,40 +17,41 @@ #if PY_MAJOR_VERSION >= 3 # define PYBIND11_EMBEDDED_MODULE_IMPL(name) \ + extern "C" PyObject *pybind11_init_impl_##name(); \ extern "C" PyObject *pybind11_init_impl_##name() { \ return pybind11_init_wrapper_##name(); \ } #else # define PYBIND11_EMBEDDED_MODULE_IMPL(name) \ + extern "C" void pybind11_init_impl_##name(); \ extern "C" void pybind11_init_impl_##name() { \ pybind11_init_wrapper_##name(); \ } #endif -#define PYBIND11_EMBEDDED_MODULE(name, variable) \ - static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &); \ - static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \ - auto m = pybind11::module(PYBIND11_TOSTRING(name)); \ - try { \ - PYBIND11_CONCAT(pybind11_init_, name)(m); \ - return m.ptr(); \ - } catch (pybind11::error_already_set &e) { \ - PyErr_SetString(PyExc_ImportError, e.what()); \ - return nullptr; \ - } catch (const std::exception &e) { \ - PyErr_SetString(PyExc_ImportError, e.what()); \ - return nullptr; \ - } \ - } \ - PYBIND11_EMBEDDED_MODULE_IMPL(name) \ - pybind11::detail::embedded_module name(PYBIND11_TOSTRING(name), \ - PYBIND11_CONCAT(pybind11_init_impl_, name)); \ - void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &variable) +#define PYBIND11_EMBEDDED_MODULE(name, variable) \ + static ::pybind11::module_::module_def \ + PYBIND11_CONCAT(pybind11_module_def_, name); \ + static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \ + static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \ + auto m = ::pybind11::module_::create_extension_module( \ + PYBIND11_TOSTRING(name), nullptr, \ + &PYBIND11_CONCAT(pybind11_module_def_, name)); \ + try { \ + PYBIND11_CONCAT(pybind11_init_, name)(m); \ + return m.ptr(); \ + } PYBIND11_CATCH_INIT_EXCEPTIONS \ + } \ + PYBIND11_EMBEDDED_MODULE_IMPL(name) \ + ::pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name) \ + (PYBIND11_TOSTRING(name), \ + PYBIND11_CONCAT(pybind11_init_impl_, name)); \ + void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &variable) -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) struct embedded_module { @@ -71,7 +70,7 @@ struct embedded_module { } }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) inline void initialize_interpreter(bool init_signal_handlers = true) { @@ -81,7 +80,7 @@ inline void initialize_interpreter(bool init_signal_handlers = true) { Py_InitializeEx(init_signal_handlers ? 1 : 0); - module::import("sys").attr("path").cast<list>().append("."); + module_::import("sys").attr("path").cast<list>().append("."); } @@ -126,4 +125,4 @@ private: bool is_valid = true; }; -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/include/pybind11/eval.h b/host/lib/deps/pybind11/include/pybind11/eval.h index 15e61293d..9bbad4e9b 100644 --- a/host/lib/deps/pybind11/include/pybind11/eval.h +++ b/host/lib/deps/pybind11/include/pybind11/eval.h @@ -6,13 +6,27 @@ */ - - #pragma once #include "pybind11.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +inline void ensure_builtins_in_globals(object &global) { + #if PY_VERSION_HEX < 0x03080000 + + + + + if (!global.contains("__builtins__")) + global["__builtins__"] = module_::import(PYBIND11_BUILTINS_MODULE); + #else + (void) global; + #endif +} + +PYBIND11_NAMESPACE_END(detail) enum eval_mode { @@ -30,6 +44,8 @@ object eval(str expr, object global = globals(), object local = object()) { if (!local) local = global; + detail::ensure_builtins_in_globals(global); + std::string buffer = "# -*- coding: utf-8 -*-\n" + (std::string) expr; @@ -50,7 +66,7 @@ object eval(str expr, object global = globals(), object local = object()) { template <eval_mode mode = eval_expr, size_t N> object eval(const char (&s)[N], object global = globals(), object local = object()) { - auto expr = (s[0] == '\n') ? str(module::import("textwrap").attr("dedent")(s)) + auto expr = (s[0] == '\n') ? str(module_::import("textwrap").attr("dedent")(s)) : str(s); return eval<mode>(expr, global, local); } @@ -64,11 +80,27 @@ void exec(const char (&s)[N], object global = globals(), object local = object() eval<eval_statements>(s, global, local); } +#if defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x03000000 +template <eval_mode mode = eval_statements> +object eval_file(str, object, object) { + pybind11_fail("eval_file not supported in PyPy3. Use eval"); +} +template <eval_mode mode = eval_statements> +object eval_file(str, object) { + pybind11_fail("eval_file not supported in PyPy3. Use eval"); +} +template <eval_mode mode = eval_statements> +object eval_file(str) { + pybind11_fail("eval_file not supported in PyPy3. Use eval"); +} +#else template <eval_mode mode = eval_statements> object eval_file(str fname, object global = globals(), object local = object()) { if (!local) local = global; + detail::ensure_builtins_in_globals(global); + int start; switch (mode) { case eval_expr: start = Py_eval_input; break; @@ -111,5 +143,6 @@ object eval_file(str fname, object global = globals(), object local = object()) throw error_already_set(); return reinterpret_steal<object>(result); } +#endif -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/include/pybind11/functional.h b/host/lib/deps/pybind11/include/pybind11/functional.h index 5a6aa0121..8f19eddf4 100644 --- a/host/lib/deps/pybind11/include/pybind11/functional.h +++ b/host/lib/deps/pybind11/include/pybind11/functional.h @@ -6,15 +6,13 @@ */ - - #pragma once #include "pybind11.h" #include <functional> -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) template <typename Return, typename... Args> struct type_caster<std::function<Return(Args...)>> { @@ -48,12 +46,33 @@ public: } } - value = [func](Args... args) -> Return { - gil_scoped_acquire acq; - object retval(func(std::forward<Args>(args)...)); - return (retval.template cast<Return>()); + struct func_handle { + function f; + func_handle(function&& f_) : f(std::move(f_)) {} + func_handle(const func_handle& f_) { + gil_scoped_acquire acq; + f = f_.f; + } + ~func_handle() { + gil_scoped_acquire acq; + function kill_f(std::move(f)); + } + }; + + + struct func_wrapper { + func_handle hfunc; + func_wrapper(func_handle&& hf): hfunc(std::move(hf)) {} + Return operator()(Args... args) const { + gil_scoped_acquire acq; + object retval(hfunc.f(std::forward<Args>(args)...)); + + return (retval.template cast<Return>()); + } }; + + value = func_wrapper(func_handle(std::move(func))); return true; } @@ -73,5 +92,5 @@ public: + make_caster<retval_type>::name + _("]")); }; -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/include/pybind11/iostream.h b/host/lib/deps/pybind11/include/pybind11/iostream.h index 361875e74..71e0c68c5 100644 --- a/host/lib/deps/pybind11/include/pybind11/iostream.h +++ b/host/lib/deps/pybind11/include/pybind11/iostream.h @@ -6,8 +6,6 @@ */ - - #pragma once #include "pybind11.h" @@ -18,19 +16,20 @@ #include <memory> #include <iostream> -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) class pythonbuf : public std::streambuf { private: using traits_type = std::streambuf::traits_type; - char d_buffer[1024]; + const size_t buf_size; + std::unique_ptr<char[]> d_buffer; object pywrite; object pyflush; - int overflow(int c) { + int overflow(int c) override { if (!traits_type::eq_int_type(c, traits_type::eof())) { *pptr() = traits_type::to_char_type(c); pbump(1); @@ -38,33 +37,48 @@ private: return sync() == 0 ? traits_type::not_eof(c) : traits_type::eof(); } - int sync() { + + + + int _sync() { if (pbase() != pptr()) { str line(pbase(), static_cast<size_t>(pptr() - pbase())); - pywrite(line); - pyflush(); + { + gil_scoped_acquire tmp; + pywrite(line); + pyflush(); + } setp(pbase(), epptr()); } return 0; } + int sync() override { + return _sync(); + } + public: - pythonbuf(object pyostream) - : pywrite(pyostream.attr("write")), + + pythonbuf(object pyostream, size_t buffer_size = 1024) + : buf_size(buffer_size), + d_buffer(new char[buf_size]), + pywrite(pyostream.attr("write")), pyflush(pyostream.attr("flush")) { - setp(d_buffer, d_buffer + sizeof(d_buffer) - 1); + setp(d_buffer.get(), d_buffer.get() + buf_size - 1); } + pythonbuf(pythonbuf&&) = default; + - ~pythonbuf() { - sync(); + ~pythonbuf() override { + _sync(); } }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) @@ -77,7 +91,7 @@ protected: public: scoped_ostream_redirect( std::ostream &costream = std::cout, - object pyostream = module::import("sys").attr("stdout")) + object pyostream = module_::import("sys").attr("stdout")) : costream(costream), buffer(pyostream) { old = costream.rdbuf(&buffer); } @@ -98,12 +112,12 @@ class scoped_estream_redirect : public scoped_ostream_redirect { public: scoped_estream_redirect( std::ostream &costream = std::cerr, - object pyostream = module::import("sys").attr("stderr")) + object pyostream = module_::import("sys").attr("stderr")) : scoped_ostream_redirect(costream,pyostream) {} }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) class OstreamRedirect { @@ -129,14 +143,14 @@ public: } }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) -inline class_<detail::OstreamRedirect> add_ostream_redirect(module m, std::string name = "ostream_redirect") { +inline class_<detail::OstreamRedirect> add_ostream_redirect(module_ m, std::string name = "ostream_redirect") { return class_<detail::OstreamRedirect>(m, name.c_str(), module_local()) .def(init<bool,bool>(), arg("stdout")=true, arg("stderr")=true) .def("__enter__", &detail::OstreamRedirect::enter) .def("__exit__", [](detail::OstreamRedirect &self_, args) { self_.exit(); }); } -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/include/pybind11/numpy.h b/host/lib/deps/pybind11/include/pybind11/numpy.h index de086c26d..635062ef4 100644 --- a/host/lib/deps/pybind11/include/pybind11/numpy.h +++ b/host/lib/deps/pybind11/include/pybind11/numpy.h @@ -6,8 +6,6 @@ */ - - #pragma once #include "pybind11.h" @@ -15,11 +13,13 @@ #include <numeric> #include <algorithm> #include <array> +#include <cstdint> #include <cstdlib> #include <cstring> #include <sstream> #include <string> #include <functional> +#include <type_traits> #include <utility> #include <vector> #include <typeindex> @@ -30,13 +30,18 @@ #endif -static_assert(sizeof(ssize_t) == sizeof(Py_intptr_t), "ssize_t != Py_intptr_t"); +static_assert(sizeof(::pybind11::ssize_t) == sizeof(Py_intptr_t), "ssize_t != Py_intptr_t"); +static_assert(std::is_signed<Py_intptr_t>::value, "Py_intptr_t must be signed"); + -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) class array; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) + +template <> struct handle_type_name<array> { static constexpr auto name = _("numpy.ndarray"); }; + template <typename type, typename SFINAE = void> struct npy_format_descriptor; struct PyArrayDescr_Proxy { @@ -106,6 +111,18 @@ inline numpy_internals& get_numpy_internals() { return *ptr; } +template <typename T> struct same_size { + template <typename U> using as = bool_constant<sizeof(T) == sizeof(U)>; +}; + +template <typename Concrete> constexpr int platform_lookup() { return -1; } + + +template <typename Concrete, typename T, typename... Ts, typename... Ints> +constexpr int platform_lookup(int I, Ints... Is) { + return sizeof(Concrete) == sizeof(T) ? I : platform_lookup<Concrete, Ts...>(Is...); +} + struct npy_api { enum constants { NPY_ARRAY_C_CONTIGUOUS_ = 0x0001, @@ -124,7 +141,23 @@ struct npy_api { NPY_FLOAT_, NPY_DOUBLE_, NPY_LONGDOUBLE_, NPY_CFLOAT_, NPY_CDOUBLE_, NPY_CLONGDOUBLE_, NPY_OBJECT_ = 17, - NPY_STRING_, NPY_UNICODE_, NPY_VOID_ + NPY_STRING_, NPY_UNICODE_, NPY_VOID_, + + NPY_INT8_ = NPY_BYTE_, + NPY_UINT8_ = NPY_UBYTE_, + NPY_INT16_ = NPY_SHORT_, + NPY_UINT16_ = NPY_USHORT_, + + + + NPY_INT32_ = platform_lookup<std::int32_t, long, int, short>( + NPY_LONG_, NPY_INT_, NPY_SHORT_), + NPY_UINT32_ = platform_lookup<std::uint32_t, unsigned long, unsigned int, unsigned short>( + NPY_ULONG_, NPY_UINT_, NPY_USHORT_), + NPY_INT64_ = platform_lookup<std::int64_t, long, long long, int>( + NPY_LONG_, NPY_LONGLONG_, NPY_INT_), + NPY_UINT64_ = platform_lookup<std::uint64_t, unsigned long, unsigned long long, unsigned int>( + NPY_ULONG_, NPY_ULONGLONG_, NPY_UINT_), }; typedef struct { @@ -147,8 +180,9 @@ struct npy_api { unsigned int (*PyArray_GetNDArrayCFeatureVersion_)(); PyObject *(*PyArray_DescrFromType_)(int); PyObject *(*PyArray_NewFromDescr_) - (PyTypeObject *, PyObject *, int, Py_intptr_t *, - Py_intptr_t *, void *, int, PyObject *); + (PyTypeObject *, PyObject *, int, Py_intptr_t const *, + Py_intptr_t const *, void *, int, PyObject *); + PyObject *(*PyArray_DescrNewFromType_)(int); int (*PyArray_CopyInto_)(PyObject *, PyObject *); PyObject *(*PyArray_NewCopy_)(PyObject *, int); @@ -159,9 +193,10 @@ struct npy_api { PyObject *(*PyArray_FromAny_) (PyObject *, PyObject *, int, int, int, PyObject *); int (*PyArray_DescrConverter_) (PyObject *, PyObject **); bool (*PyArray_EquivTypes_) (PyObject *, PyObject *); - int (*PyArray_GetArrayParamsFromObject_)(PyObject *, PyObject *, char, PyObject **, int *, - Py_ssize_t *, PyObject **, PyObject *); + int (*PyArray_GetArrayParamsFromObject_)(PyObject *, PyObject *, unsigned char, PyObject **, int *, + Py_intptr_t *, PyObject **, PyObject *); PyObject *(*PyArray_Squeeze_)(PyObject *); + int (*PyArray_SetBaseObject_)(PyObject *, PyObject *); PyObject* (*PyArray_Resize_)(PyObject*, PyArray_Dims*, int, int); private: @@ -177,7 +212,7 @@ private: API_PyArray_CopyInto = 82, API_PyArray_NewCopy = 85, API_PyArray_NewFromDescr = 94, - API_PyArray_DescrNewFromType = 9, + API_PyArray_DescrNewFromType = 96, API_PyArray_DescrConverter = 174, API_PyArray_EquivTypes = 182, API_PyArray_GetArrayParamsFromObject = 278, @@ -186,7 +221,7 @@ private: }; static npy_api lookup() { - module m = module::import("numpy.core.multiarray"); + module_ m = module_::import("numpy.core.multiarray"); auto c = m.attr("_ARRAY_API"); #if PY_MAJOR_VERSION >= 3 void **api_ptr = (void **) PyCapsule_GetPointer(c.ptr(), NULL); @@ -245,7 +280,7 @@ template <typename T> struct is_complex : std::false_type { }; template <typename T> struct is_complex<std::complex<T>> : std::true_type { }; template <typename T> struct array_info_scalar { - typedef T type; + using type = T; static constexpr bool is_array = false; static constexpr bool is_empty = false; static constexpr auto extents = _(""); @@ -292,6 +327,12 @@ template <typename T> using is_pod_struct = all_of< satisfies_none_of<T, std::is_reference, std::is_array, is_std_array, std::is_arithmetic, is_complex, std::is_enum> >; + +template <typename T> using is_pod = all_of< + std::is_standard_layout<T>, + std::is_trivial<T> +>; + template <ssize_t Dim = 0, typename Strides> ssize_t byte_offset_unsafe(const Strides &) { return 0; } template <ssize_t Dim = 0, typename Strides, typename... Ix> ssize_t byte_offset_unsafe(const Strides &strides, ssize_t i, Ix... index) { @@ -373,6 +414,10 @@ class unchecked_mutable_reference : public unchecked_reference<T, Dims> { using ConstBase::Dynamic; public: + using ConstBase::operator(); + using ConstBase::operator[]; + + template <typename... Ix> T& operator()(Ix... index) { static_assert(ssize_t{sizeof...(Ix)} == Dims || Dynamic, "Invalid number of indices for unchecked array reference"); @@ -393,7 +438,7 @@ struct type_caster<unchecked_reference<T, Dim>> { template <typename T, ssize_t Dim> struct type_caster<unchecked_mutable_reference<T, Dim>> : type_caster<unchecked_reference<T, Dim>> {}; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) class dtype : public object { public: @@ -450,7 +495,7 @@ public: private: static object _dtype_from_pep3118() { - static PyObject *obj = module::import("numpy.core._internal") + static PyObject *obj = module_::import("numpy.core._internal") .attr("_dtype_from_pep3118").cast<object>().release().ptr(); return reinterpret_borrow<object>(obj); } @@ -499,7 +544,7 @@ public: forcecast = detail::npy_api::NPY_ARRAY_FORCECAST_ }; - array() : array({{0}}, static_cast<const double *>(nullptr)) {} + array() : array(0, static_cast<const double *>(nullptr)) {} using ShapeContainer = detail::any_container<ssize_t>; using StridesContainer = detail::any_container<ssize_t>; @@ -509,7 +554,7 @@ public: const void *ptr = nullptr, handle base = handle()) { if (strides->empty()) - *strides = c_strides(*shape, dt.itemsize()); + *strides = detail::c_strides(*shape, dt.itemsize()); auto ndim = shape->size(); if (ndim != strides->size()) @@ -528,7 +573,10 @@ public: auto &api = detail::npy_api::get(); auto tmp = reinterpret_steal<object>(api.PyArray_NewFromDescr_( - api.PyArray_Type_, descr.release().ptr(), (int) ndim, shape->data(), strides->data(), + api.PyArray_Type_, descr.release().ptr(), (int) ndim, + + reinterpret_cast<Py_intptr_t*>(shape->data()), + reinterpret_cast<Py_intptr_t*>(strides->data()), const_cast<void *>(ptr), flags, nullptr)); if (!tmp) throw error_already_set(); @@ -560,8 +608,8 @@ public: template <typename T> explicit array(ssize_t count, const T *ptr, handle base = handle()) : array({count}, {}, ptr, base) { } - explicit array(const buffer_info &info) - : array(pybind11::dtype(info), info.shape, info.strides, info.ptr) { } + explicit array(const buffer_info &info, handle base = handle()) + : array(pybind11::dtype(info), info.shape, info.strides, info.ptr, base) { } pybind11::dtype dtype() const { @@ -689,10 +737,12 @@ public: void resize(ShapeContainer new_shape, bool refcheck = true) { detail::npy_api::PyArray_Dims d = { - new_shape->data(), int(new_shape->size()) + + reinterpret_cast<Py_intptr_t*>(new_shape->data()), + int(new_shape->size()) }; - object new_array = reinterpret_steal<object>( + auto new_array = reinterpret_steal<object>( detail::npy_api::get().PyArray_Resize_(m_ptr, &d, int(refcheck), -1) ); if (!new_array) throw error_already_set(); @@ -726,25 +776,6 @@ protected: throw std::domain_error("array is not writeable"); } - - static std::vector<ssize_t> c_strides(const std::vector<ssize_t> &shape, ssize_t itemsize) { - auto ndim = shape.size(); - std::vector<ssize_t> strides(ndim, itemsize); - if (ndim > 0) - for (size_t i = ndim - 1; i > 0; --i) - strides[i - 1] = strides[i] * shape[i]; - return strides; - } - - - static std::vector<ssize_t> f_strides(const std::vector<ssize_t> &shape, ssize_t itemsize) { - auto ndim = shape.size(); - std::vector<ssize_t> strides(ndim, itemsize); - for (size_t i = 1; i < ndim; ++i) - strides[i] = strides[i - 1] * shape[i - 1]; - return strides; - } - template<typename... Ix> void check_dimensions(Ix... index) const { check_dimensions_impl(ssize_t(0), shape(), ssize_t(index)...); } @@ -796,17 +827,19 @@ public: if (!m_ptr) throw error_already_set(); } - explicit array_t(const buffer_info& info) : array(info) { } + explicit array_t(const buffer_info& info, handle base = handle()) : array(info, base) { } array_t(ShapeContainer shape, StridesContainer strides, const T *ptr = nullptr, handle base = handle()) : array(std::move(shape), std::move(strides), ptr, base) { } explicit array_t(ShapeContainer shape, const T *ptr = nullptr, handle base = handle()) : array_t(private_ctor{}, std::move(shape), - ExtraFlags & f_style ? f_strides(*shape, itemsize()) : c_strides(*shape, itemsize()), + ExtraFlags & f_style + ? detail::f_strides(*shape, itemsize()) + : detail::c_strides(*shape, itemsize()), ptr, base) { } - explicit array_t(size_t count, const T *ptr = nullptr, handle base = handle()) + explicit array_t(ssize_t count, const T *ptr = nullptr, handle base = handle()) : array({count}, {}, ptr, base) { } constexpr ssize_t itemsize() const { @@ -827,14 +860,14 @@ public: template<typename... Ix> const T& at(Ix... index) const { - if (sizeof...(index) != ndim()) + if ((ssize_t) sizeof...(index) != ndim()) fail_dim_check(sizeof...(index), "index dimension mismatch"); return *(static_cast<const T*>(array::data()) + byte_offset(ssize_t(index)...) / itemsize()); } template<typename... Ix> T& mutable_at(Ix... index) { - if (sizeof...(index) != ndim()) + if ((ssize_t) sizeof...(index) != ndim()) fail_dim_check(sizeof...(index), "index dimension mismatch"); return *(static_cast<T*>(array::mutable_data()) + byte_offset(ssize_t(index)...) / itemsize()); } @@ -861,7 +894,8 @@ public: static bool check_(handle h) { const auto &api = detail::npy_api::get(); return api.PyArray_Check_(h.ptr()) - && api.PyArray_EquivTypes_(detail::array_proxy(h.ptr())->descr, dtype::of<T>().ptr()); + && api.PyArray_EquivTypes_(detail::array_proxy(h.ptr())->descr, dtype::of<T>().ptr()) + && detail::check_flags(h.ptr(), ExtraFlags & (array::c_style | array::f_style)); } protected: @@ -908,7 +942,7 @@ struct format_descriptor<T, detail::enable_if_t<detail::array_info<T>::is_array> } }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) template <typename T, int ExtraFlags> struct pyobject_caster<array_t<T, ExtraFlags>> { using type = array_t<T, ExtraFlags>; @@ -939,14 +973,14 @@ struct npy_format_descriptor_name; template <typename T> struct npy_format_descriptor_name<T, enable_if_t<std::is_integral<T>::value>> { static constexpr auto name = _<std::is_same<T, bool>::value>( - _("bool"), _<std::is_signed<T>::value>("int", "uint") + _<sizeof(T)*8>() + _("bool"), _<std::is_signed<T>::value>("numpy.int", "numpy.uint") + _<sizeof(T)*8>() ); }; template <typename T> struct npy_format_descriptor_name<T, enable_if_t<std::is_floating_point<T>::value>> { static constexpr auto name = _<std::is_same<T, float>::value || std::is_same<T, double>::value>( - _("float") + _<sizeof(T)*8>(), _("longdouble") + _("numpy.float") + _<sizeof(T)*8>(), _("numpy.longdouble") ); }; @@ -954,7 +988,7 @@ template <typename T> struct npy_format_descriptor_name<T, enable_if_t<is_complex<T>::value>> { static constexpr auto name = _<std::is_same<typename T::value_type, float>::value || std::is_same<typename T::value_type, double>::value>( - _("complex") + _<sizeof(typename T::value_type)*16>(), _("longcomplex") + _("numpy.complex") + _<sizeof(typename T::value_type)*16>(), _("numpy.longcomplex") ); }; @@ -965,8 +999,8 @@ private: constexpr static const int values[15] = { npy_api::NPY_BOOL_, - npy_api::NPY_BYTE_, npy_api::NPY_UBYTE_, npy_api::NPY_SHORT_, npy_api::NPY_USHORT_, - npy_api::NPY_INT_, npy_api::NPY_UINT_, npy_api::NPY_LONGLONG_, npy_api::NPY_ULONGLONG_, + npy_api::NPY_BYTE_, npy_api::NPY_UBYTE_, npy_api::NPY_INT16_, npy_api::NPY_UINT16_, + npy_api::NPY_INT32_, npy_api::NPY_UINT32_, npy_api::NPY_INT64_, npy_api::NPY_UINT64_, npy_api::NPY_FLOAT_, npy_api::NPY_DOUBLE_, npy_api::NPY_LONGDOUBLE_, npy_api::NPY_CFLOAT_, npy_api::NPY_CDOUBLE_, npy_api::NPY_CLONGDOUBLE_ }; @@ -976,7 +1010,7 @@ public: static pybind11::dtype dtype() { if (auto ptr = npy_api::get().PyArray_DescrFromType_(value)) - return reinterpret_borrow<pybind11::dtype>(ptr); + return reinterpret_steal<pybind11::dtype>(ptr); pybind11_fail("Unsupported buffer format!"); } }; @@ -1027,8 +1061,14 @@ inline PYBIND11_NOINLINE void register_structured_dtype( if (numpy_internals.get_type_info(tinfo, false)) pybind11_fail("NumPy: dtype is already registered"); + + + std::vector<field_descriptor> ordered_fields(std::move(fields)); + std::sort(ordered_fields.begin(), ordered_fields.end(), + [](const field_descriptor &a, const field_descriptor &b) { return a.offset < b.offset; }); + list names, formats, offsets; - for (auto field : *fields) { + for (auto& field : ordered_fields) { if (!field.descr) pybind11_fail(std::string("NumPy: unsupported field dtype: `") + field.name + "` @ " + tinfo.name()); @@ -1045,9 +1085,6 @@ inline PYBIND11_NOINLINE void register_structured_dtype( - std::vector<field_descriptor> ordered_fields(std::move(fields)); - std::sort(ordered_fields.begin(), ordered_fields.end(), - [](const field_descriptor &a, const field_descriptor &b) { return a.offset < b.offset; }); ssize_t offset = 0; std::ostringstream oss; @@ -1147,7 +1184,7 @@ private: #define PYBIND11_MAP_NEXT0(test, next, ...) next PYBIND11_MAP_OUT #define PYBIND11_MAP_NEXT1(test, next) PYBIND11_MAP_NEXT0 (test, next, 0) #define PYBIND11_MAP_NEXT(test, next) PYBIND11_MAP_NEXT1 (PYBIND11_MAP_GET_END test, next) -#ifdef _MSC_VER +#if defined(_MSC_VER) && !defined(__clang__) #define PYBIND11_MAP_LIST_NEXT1(test, next) \ PYBIND11_EVAL0 (PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0)) #else @@ -1169,7 +1206,7 @@ private: (::std::vector<::pybind11::detail::field_descriptor> \ {PYBIND11_MAP_LIST (PYBIND11_FIELD_DESCRIPTOR, Type, __VA_ARGS__)}) -#ifdef _MSC_VER +#if defined(_MSC_VER) && !defined(__clang__) #define PYBIND11_MAP2_LIST_NEXT1(test, next) \ PYBIND11_EVAL0 (PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0)) #else @@ -1193,19 +1230,6 @@ private: #endif -template <class T> -using array_iterator = typename std::add_pointer<T>::type; - -template <class T> -array_iterator<T> array_begin(const buffer_info& buffer) { - return array_iterator<T>(reinterpret_cast<T*>(buffer.ptr)); -} - -template <class T> -array_iterator<T> array_end(const buffer_info& buffer) { - return array_iterator<T>(reinterpret_cast<T*>(buffer.ptr) + buffer.size); -} - class common_iterator { public: using container_type = std::vector<ssize_t>; @@ -1219,7 +1243,7 @@ public: m_strides.back() = static_cast<value_type>(strides.back()); for (size_type i = m_strides.size() - 1; i != 0; --i) { size_type j = i - 1; - value_type s = static_cast<value_type>(shape[i]); + auto s = static_cast<value_type>(shape[i]); m_strides[j] = strides[j] + m_strides[i] - strides[i] * s; } } @@ -1397,7 +1421,7 @@ struct vectorize_arg { using call_type = remove_reference_t<T>; static constexpr bool vectorize = - satisfies_any_of<call_type, std::is_arithmetic, is_complex, std::is_pod>::value && + satisfies_any_of<call_type, std::is_arithmetic, is_complex, is_pod>::value && satisfies_none_of<call_type, std::is_pointer, std::is_array, is_std_array, std::is_enum>::value && (!std::is_reference<T>::value || (std::is_lvalue_reference<T>::value && std::is_const<call_type>::value)); @@ -1405,9 +1429,66 @@ struct vectorize_arg { using type = conditional_t<vectorize, array_t<remove_cv_t<call_type>, array::forcecast>, T>; }; + + +template <typename Func, typename Return, typename... Args> +struct vectorize_returned_array { + using Type = array_t<Return>; + + static Type create(broadcast_trivial trivial, const std::vector<ssize_t> &shape) { + if (trivial == broadcast_trivial::f_trivial) + return array_t<Return, array::f_style>(shape); + else + return array_t<Return>(shape); + } + + static Return *mutable_data(Type &array) { + return array.mutable_data(); + } + + static Return call(Func &f, Args &... args) { + return f(args...); + } + + static void call(Return *out, size_t i, Func &f, Args &... args) { + out[i] = f(args...); + } +}; + + +template <typename Func, typename... Args> +struct vectorize_returned_array<Func, void, Args...> { + using Type = none; + + static Type create(broadcast_trivial, const std::vector<ssize_t> &) { + return none(); + } + + static void *mutable_data(Type &) { + return nullptr; + } + + static detail::void_type call(Func &f, Args &... args) { + f(args...); + return {}; + } + + static void call(void *, size_t, Func &f, Args &... args) { + f(args...); + } +}; + + template <typename Func, typename Return, typename... Args> struct vectorize_helper { + + +#ifdef __CUDACC__ +public: +#else private: +#endif + static constexpr size_t N = sizeof...(Args); static constexpr size_t NVectorized = constexpr_sum(vectorize_arg<Args>::vectorize...); static_assert(NVectorized >= 1, @@ -1432,6 +1513,8 @@ private: using arg_call_types = std::tuple<typename vectorize_arg<Args>::call_type...>; template <size_t Index> using param_n_t = typename std::tuple_element<Index, arg_call_types>::type; + using returned_array = vectorize_returned_array<Func, Return, Args...>; + @@ -1455,7 +1538,7 @@ private: ssize_t nd = 0; std::vector<ssize_t> shape(0); auto trivial = broadcast(buffers, nd, shape); - size_t ndim = (size_t) nd; + auto ndim = (size_t) nd; size_t size = std::accumulate(shape.begin(), shape.end(), (size_t) 1, std::multiplies<size_t>()); @@ -1463,22 +1546,21 @@ private: if (size == 1 && ndim == 0) { PYBIND11_EXPAND_SIDE_EFFECTS(params[VIndex] = buffers[BIndex].ptr); - return cast(f(*reinterpret_cast<param_n_t<Index> *>(params[Index])...)); + return cast(returned_array::call(f, *reinterpret_cast<param_n_t<Index> *>(params[Index])...)); } - array_t<Return> result; - if (trivial == broadcast_trivial::f_trivial) result = array_t<Return, array::f_style>(shape); - else result = array_t<Return>(shape); + auto result = returned_array::create(trivial, shape); - if (size == 0) return result; + if (size == 0) return std::move(result); + auto mutable_data = returned_array::mutable_data(result); if (trivial == broadcast_trivial::non_trivial) - apply_broadcast(buffers, params, result, i_seq, vi_seq, bi_seq); + apply_broadcast(buffers, params, mutable_data, size, shape, i_seq, vi_seq, bi_seq); else - apply_trivial(buffers, params, result.mutable_data(), size, i_seq, vi_seq, bi_seq); + apply_trivial(buffers, params, mutable_data, size, i_seq, vi_seq, bi_seq); - return result; + return std::move(result); } template <size_t... Index, size_t... VIndex, size_t... BIndex> @@ -1499,7 +1581,7 @@ private: }}; for (size_t i = 0; i < size; ++i) { - out[i] = f(*reinterpret_cast<param_n_t<Index> *>(params[Index])...); + returned_array::call(out, i, f, *reinterpret_cast<param_n_t<Index> *>(params[Index])...); for (auto &x : vecparams) x.first += x.second; } } @@ -1507,19 +1589,18 @@ private: template <size_t... Index, size_t... VIndex, size_t... BIndex> void apply_broadcast(std::array<buffer_info, NVectorized> &buffers, std::array<void *, N> ¶ms, - array_t<Return> &output_array, + Return *out, + size_t size, + const std::vector<ssize_t> &output_shape, index_sequence<Index...>, index_sequence<VIndex...>, index_sequence<BIndex...>) { - buffer_info output = output_array.request(); - multi_array_iterator<NVectorized> input_iter(buffers, output.shape); + multi_array_iterator<NVectorized> input_iter(buffers, output_shape); - for (array_iterator<Return> iter = array_begin<Return>(output), end = array_end<Return>(output); - iter != end; - ++iter, ++input_iter) { + for (size_t i = 0; i < size; ++i, ++input_iter) { PYBIND11_EXPAND_SIDE_EFFECTS(( params[VIndex] = input_iter.template data<BIndex>() )); - *iter = f(*reinterpret_cast<param_n_t<Index> *>(std::get<Index>(params))...); + returned_array::call(out, i, f, *reinterpret_cast<param_n_t<Index> *>(std::get<Index>(params))...); } } }; @@ -1534,7 +1615,7 @@ template <typename T, int Flags> struct handle_type_name<array_t<T, Flags>> { static constexpr auto name = _("numpy.ndarray[") + npy_format_descriptor<T>::name + _("]"); }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) template <typename Return, typename... Args> @@ -1564,7 +1645,7 @@ Helper vectorize(Return (Class::*f)(Args...) const) { return Helper(std::mem_fn(f)); } -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) #if defined(_MSC_VER) #pragma warning(pop) diff --git a/host/lib/deps/pybind11/include/pybind11/operators.h b/host/lib/deps/pybind11/include/pybind11/operators.h index 61ff246ac..4bbba61c3 100644 --- a/host/lib/deps/pybind11/include/pybind11/operators.h +++ b/host/lib/deps/pybind11/include/pybind11/operators.h @@ -6,8 +6,6 @@ */ - - #pragma once #include "pybind11.h" @@ -19,8 +17,8 @@ # pragma warning(disable: 4127) #endif -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) enum op_id : int { @@ -148,6 +146,9 @@ PYBIND11_INPLACE_OPERATOR(ixor, operator^=, l ^= r) PYBIND11_INPLACE_OPERATOR(ior, operator|=, l |= r) PYBIND11_UNARY_OPERATOR(neg, operator-, -l) PYBIND11_UNARY_OPERATOR(pos, operator+, +l) + + + PYBIND11_UNARY_OPERATOR(abs, abs, std::abs(l)) PYBIND11_UNARY_OPERATOR(hash, hash, std::hash<L>()(l)) PYBIND11_UNARY_OPERATOR(invert, operator~, (~l)) @@ -158,11 +159,13 @@ PYBIND11_UNARY_OPERATOR(float, float_, (double) l) #undef PYBIND11_BINARY_OPERATOR #undef PYBIND11_INPLACE_OPERATOR #undef PYBIND11_UNARY_OPERATOR -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) using detail::self; -NAMESPACE_END(PYBIND11_NAMESPACE) +using detail::hash; + +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) #if defined(_MSC_VER) # pragma warning(pop) diff --git a/host/lib/deps/pybind11/include/pybind11/options.h b/host/lib/deps/pybind11/include/pybind11/options.h index 625c28bc1..4acffb9de 100644 --- a/host/lib/deps/pybind11/include/pybind11/options.h +++ b/host/lib/deps/pybind11/include/pybind11/options.h @@ -6,13 +6,11 @@ */ - - #pragma once #include "detail/common.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) class options { public: @@ -63,4 +61,4 @@ private: state previous_state; }; -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/include/pybind11/pybind11.h b/host/lib/deps/pybind11/include/pybind11/pybind11.h index 2ce5fcbcd..6a6b8aa3f 100644 --- a/host/lib/deps/pybind11/include/pybind11/pybind11.h +++ b/host/lib/deps/pybind11/include/pybind11/pybind11.h @@ -6,8 +6,6 @@ */ - - #pragma once #if defined(__INTEL_COMPILER) @@ -29,6 +27,7 @@ # pragma warning(disable: 4996) # pragma warning(disable: 4702) # pragma warning(disable: 4522) +# pragma warning(disable: 4505) #elif defined(__GNUG__) && !defined(__clang__) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wunused-but-set-parameter" @@ -39,9 +38,6 @@ # if __GNUC__ >= 7 # pragma GCC diagnostic ignored "-Wnoexcept-type" # endif -#elif defined(__clang__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wunused-value" #endif #include "attr.h" @@ -49,12 +45,21 @@ #include "detail/class.h" #include "detail/init.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +#include <memory> +#include <vector> +#include <string> +#include <utility> + +#if defined(__GNUG__) && !defined(__clang__) +# include <cxxabi.h> +#endif + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) class cpp_function : public function { public: - cpp_function() { } + cpp_function() = default; cpp_function(std::nullptr_t) { } @@ -74,6 +79,15 @@ public: template <typename Return, typename Class, typename... Arg, typename... Extra> cpp_function(Return (Class::*f)(Arg...), const Extra&... extra) { + initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(std::forward<Arg>(args)...); }, + (Return (*) (Class *, Arg...)) nullptr, extra...); + } + + + + + template <typename Return, typename Class, typename... Arg, typename... Extra> + cpp_function(Return (Class::*f)(Arg...)&, const Extra&... extra) { initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); }, (Return (*) (Class *, Arg...)) nullptr, extra...); } @@ -81,6 +95,15 @@ public: template <typename Return, typename Class, typename... Arg, typename... Extra> cpp_function(Return (Class::*f)(Arg...) const, const Extra&... extra) { + initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(std::forward<Arg>(args)...); }, + (Return (*)(const Class *, Arg ...)) nullptr, extra...); + } + + + + + template <typename Return, typename Class, typename... Arg, typename... Extra> + cpp_function(Return (Class::*f)(Arg...) const&, const Extra&... extra) { initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); }, (Return (*)(const Class *, Arg ...)) nullptr, extra...); } @@ -144,7 +167,7 @@ protected: auto data = (sizeof(capture) <= sizeof(call.func.data) ? &call.func.data : call.func.data[0]); - capture *cap = const_cast<capture *>(reinterpret_cast<const capture *>(data)); + auto *cap = const_cast<capture *>(reinterpret_cast<const capture *>(data)); return_value_policy policy = return_value_policy_override<Return>::policy(call.func.policy); @@ -165,6 +188,16 @@ protected: process_attributes<Extra...>::init(extra..., rec); + { + constexpr bool has_kw_only_args = any_of<std::is_same<kw_only, Extra>...>::value, + has_pos_only_args = any_of<std::is_same<pos_only, Extra>...>::value, + has_args = any_of<std::is_same<args, Args>...>::value, + has_arg_annotations = any_of<is_keyword<Extra>...>::value; + static_assert(has_arg_annotations || !has_kw_only_args, "py::kw_only requires the use of argument annotations"); + static_assert(has_arg_annotations || !has_pos_only_args, "py::pos_only requires the use of argument annotations (for docstrings and aligning the annotations to the argument)"); + static_assert(!(has_args && has_kw_only_args), "py::kw_only cannot be combined with a py::args argument"); + } + static constexpr auto signature = _("(") + cast_in::arg_names + _(") -> ") + cast_out::name; PYBIND11_DESCR_CONSTEXPR auto types = decltype(signature)::types(); @@ -199,14 +232,14 @@ protected: if (a.descr) a.descr = strdup(a.descr); else if (a.value) - a.descr = strdup(a.value.attr("__repr__")().cast<std::string>().c_str()); + a.descr = strdup(repr(a.value).cast<std::string>().c_str()); } rec->is_constructor = !strcmp(rec->name, "__init__") || !strcmp(rec->name, "__setstate__"); #if !defined(NDEBUG) && !defined(PYBIND11_DISABLE_NEW_STYLE_INIT_WARNING) if (rec->is_constructor && !rec->is_new_style_constructor) { - const auto class_name = std::string(((PyTypeObject *) rec->scope.ptr())->tp_name); + const auto class_name = detail::get_fully_qualified_tp_name((PyTypeObject *) rec->scope.ptr()); const auto func_name = std::string(rec->name); PyErr_WarnEx( PyExc_FutureWarning, @@ -229,6 +262,9 @@ protected: if (*(pc + 1) == '*') continue; + + if (rec->nargs_kw_only > 0 && arg_index + rec->nargs_kw_only == args) + signature += "*, "; if (arg_index < rec->args.size() && rec->args[arg_index].name) { signature += rec->args[arg_index].name; } else if (arg_index == 0 && rec->is_method) { @@ -243,6 +279,10 @@ protected: signature += " = "; signature += rec->args[arg_index].descr; } + + + if (rec->nargs_pos_only > 0 && (arg_index + 1) == rec->nargs_pos_only) + signature += ", /"; arg_index++; } else if (c == '%') { const std::type_info *t = types[type_index++]; @@ -268,6 +308,7 @@ protected: signature += c; } } + if (arg_index != args || types[type_index] != nullptr) pybind11_fail("Internal error while parsing type signature (2)"); @@ -330,7 +371,6 @@ protected: m_ptr = rec->sibling.ptr(); inc_ref(); - chain_start = chain; if (chain->is_method != rec->is_method) pybind11_fail("overloading a method with both static and instance methods is not supported; " #if defined(NDEBUG) @@ -340,9 +380,22 @@ protected: std::string(pybind11::str(rec->scope.attr("__name__"))) + "." + std::string(rec->name) + signature #endif ); - while (chain->next) - chain = chain->next; - chain->next = rec; + + if (rec->prepend) { + + + + chain_start = rec; + rec->next = chain; + auto rec_capsule = reinterpret_borrow<capsule>(((PyCFunctionObject *) m_ptr)->m_self); + rec_capsule.set_pointer(rec); + } else { + + chain_start = chain; + while (chain->next) + chain = chain->next; + chain->next = rec; + } } std::string signatures; @@ -379,7 +432,7 @@ protected: } - PyCFunctionObject *func = (PyCFunctionObject *) m_ptr; + auto *func = (PyCFunctionObject *) m_ptr; if (func->m_ml->ml_doc) std::free(const_cast<char *>(func->m_ml->ml_doc)); func->m_ml->ml_doc = strdup(signatures.c_str()); @@ -394,6 +447,12 @@ protected: static void destruct(detail::function_record *rec) { + + + #if !defined(PYPY_VERSION) && PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 9 + static bool is_zero = Py_GetVersion()[4] == '0'; + #endif + while (rec) { detail::function_record *next = rec->next; if (rec->free_data) @@ -408,7 +467,15 @@ protected: } if (rec->def) { std::free(const_cast<char *>(rec->def->ml_doc)); - delete rec->def; + + + + #if !defined(PYPY_VERSION) && PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 9 + if (!is_zero) + delete rec->def; + #else + delete rec->def; + #endif } delete rec; rec = next; @@ -424,7 +491,7 @@ protected: *it = overloads; - const size_t n_args_in = (size_t) PyTuple_GET_SIZE(args_in); + const auto n_args_in = (size_t) PyTuple_GET_SIZE(args_in); handle parent = n_args_in > 0 ? PyTuple_GET_ITEM(args_in, 0) : nullptr, result = PYBIND11_TRY_NEXT_OVERLOAD; @@ -461,9 +528,10 @@ protected: const function_record &func = *it; - size_t pos_args = func.nargs; - if (func.has_args) --pos_args; - if (func.has_kwargs) --pos_args; + size_t num_args = func.nargs; + if (func.has_args) --num_args; + if (func.has_kwargs) --num_args; + size_t pos_args = num_args - func.nargs_kw_only; if (!func.has_args && n_args_in > pos_args) continue; @@ -473,7 +541,7 @@ protected: function_call call(func, parent); - size_t args_to_copy = std::min(pos_args, n_args_in); + size_t args_to_copy = (std::min)(pos_args, n_args_in); size_t args_copied = 0; @@ -484,7 +552,7 @@ protected: self_value_and_holder.type->dealloc(self_value_and_holder); call.init_self = PyTuple_GET_ITEM(args_in, 0); - call.args.push_back(reinterpret_cast<PyObject *>(&self_value_and_holder)); + call.args.emplace_back(reinterpret_cast<PyObject *>(&self_value_and_holder)); call.args_convert.push_back(false); ++args_copied; } @@ -513,15 +581,35 @@ protected: dict kwargs = reinterpret_borrow<dict>(kwargs_in); - if (args_copied < pos_args) { + if (args_copied < func.nargs_pos_only) { + for (; args_copied < func.nargs_pos_only; ++args_copied) { + const auto &arg_rec = func.args[args_copied]; + handle value; + + if (arg_rec.value) { + value = arg_rec.value; + } + if (value) { + call.args.push_back(value); + call.args_convert.push_back(arg_rec.convert); + } else + break; + } + + if (args_copied < func.nargs_pos_only) + continue; + } + + + if (args_copied < num_args) { bool copied_kwargs = false; - for (; args_copied < pos_args; ++args_copied) { - const auto &arg = func.args[args_copied]; + for (; args_copied < num_args; ++args_copied) { + const auto &arg_rec = func.args[args_copied]; handle value; - if (kwargs_in && arg.name) - value = PyDict_GetItemString(kwargs.ptr(), arg.name); + if (kwargs_in && arg_rec.name) + value = PyDict_GetItemString(kwargs.ptr(), arg_rec.name); if (value) { @@ -529,25 +617,29 @@ protected: kwargs = reinterpret_steal<dict>(PyDict_Copy(kwargs.ptr())); copied_kwargs = true; } - PyDict_DelItemString(kwargs.ptr(), arg.name); - } else if (arg.value) { - value = arg.value; + PyDict_DelItemString(kwargs.ptr(), arg_rec.name); + } else if (arg_rec.value) { + value = arg_rec.value; + } + + if (!arg_rec.none && value.is_none()) { + break; } if (value) { call.args.push_back(value); - call.args_convert.push_back(arg.convert); + call.args_convert.push_back(arg_rec.convert); } else break; } - if (args_copied < pos_args) + if (args_copied < num_args) continue; } - if (kwargs && kwargs.size() > 0 && !func.has_kwargs) + if (kwargs && !kwargs.empty() && !func.has_kwargs) continue; @@ -645,6 +737,10 @@ protected: } catch (error_already_set &e) { e.restore(); return nullptr; +#ifdef __GLIBCXX__ + } catch ( abi::__forced_unwind& ) { + throw; +#endif } catch (...) { @@ -714,18 +810,27 @@ protected: for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) { if (!some_args) some_args = true; else msg += ", "; - msg += pybind11::repr(args_[ti]); + try { + msg += pybind11::repr(args_[ti]); + } catch (const error_already_set&) { + msg += "<repr raised Error>"; + } } if (kwargs_in) { auto kwargs = reinterpret_borrow<dict>(kwargs_in); - if (kwargs.size() > 0) { + if (!kwargs.empty()) { if (some_args) msg += "; "; msg += "kwargs: "; bool first = true; for (auto kwarg : kwargs) { if (first) first = false; else msg += ", "; - msg += pybind11::str("{}={!r}").format(kwarg.first, kwarg.second); + msg += pybind11::str("{}=").format(kwarg.first); + try { + msg += pybind11::repr(kwarg.second); + } catch (const error_already_set&) { + msg += "<repr raised Error>"; + } } } } @@ -751,32 +856,23 @@ protected: }; -class module : public object { +class module_ : public object { public: - PYBIND11_OBJECT_DEFAULT(module, object, PyModule_Check) + PYBIND11_OBJECT_DEFAULT(module_, object, PyModule_Check) - explicit module(const char *name, const char *doc = nullptr) { - if (!options::show_user_defined_docstrings()) doc = nullptr; + PYBIND11_DEPRECATED("Use PYBIND11_MODULE or module_::create_extension_module instead") + explicit module_(const char *name, const char *doc = nullptr) { #if PY_MAJOR_VERSION >= 3 - PyModuleDef *def = new PyModuleDef(); - std::memset(def, 0, sizeof(PyModuleDef)); - def->m_name = name; - def->m_doc = doc; - def->m_size = -1; - Py_INCREF(def); - m_ptr = PyModule_Create(def); + *this = create_extension_module(name, doc, new PyModuleDef()); #else - m_ptr = Py_InitModule3(name, nullptr, doc); + *this = create_extension_module(name, doc, nullptr); #endif - if (m_ptr == nullptr) - pybind11_fail("Internal error in module::module()"); - inc_ref(); } template <typename Func, typename... Extra> - module &def(const char *name_, Func &&f, const Extra& ... extra) { + module_ &def(const char *name_, Func &&f, const Extra& ... extra) { cpp_function func(std::forward<Func>(f), name(name_), scope(*this), sibling(getattr(*this, name_, none())), extra...); @@ -786,10 +882,10 @@ public: } - module def_submodule(const char *name, const char *doc = nullptr) { + module_ def_submodule(const char *name, const char *doc = nullptr) { std::string full_name = std::string(PyModule_GetName(m_ptr)) + std::string(".") + std::string(name); - auto result = reinterpret_borrow<module>(PyImport_AddModule(full_name.c_str())); + auto result = reinterpret_borrow<module_>(PyImport_AddModule(full_name.c_str())); if (doc && options::show_user_defined_docstrings()) result.attr("__doc__") = pybind11::str(doc); attr(name) = result; @@ -797,11 +893,11 @@ public: } - static module import(const char *name) { + static module_ import(const char *name) { PyObject *obj = PyImport_ImportModule(name); if (!obj) throw error_already_set(); - return reinterpret_steal<module>(obj); + return reinterpret_steal<module_>(obj); } @@ -809,14 +905,10 @@ public: PyObject *obj = PyImport_ReloadModule(ptr()); if (!obj) throw error_already_set(); - *this = reinterpret_steal<module>(obj); + *this = reinterpret_steal<module_>(obj); } - - - - PYBIND11_NOINLINE void add_object(const char *name, handle obj, bool overwrite = false) { if (!overwrite && hasattr(*this, name)) pybind11_fail("Error during initialization: multiple incompatible definitions with name \"" + @@ -824,25 +916,66 @@ public: PyModule_AddObject(ptr(), name, obj.inc_ref().ptr() ); } + +#if PY_MAJOR_VERSION >= 3 + using module_def = PyModuleDef; +#else + struct module_def {}; +#endif + + + static module_ create_extension_module(const char *name, const char *doc, module_def *def) { +#if PY_MAJOR_VERSION >= 3 + + def = new (def) PyModuleDef { + PyModuleDef_HEAD_INIT, + name, + options::show_user_defined_docstrings() ? doc : nullptr, + -1, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr + }; + auto m = PyModule_Create(def); +#else + + (void) def; + auto m = Py_InitModule3(name, nullptr, options::show_user_defined_docstrings() ? doc : nullptr); +#endif + if (m == nullptr) { + if (PyErr_Occurred()) + throw error_already_set(); + pybind11_fail("Internal error in module_::create_extension_module()"); + } + + + return reinterpret_borrow<module_>(m); + } }; +using module = module_; + + + + inline dict globals() { PyObject *p = PyEval_GetGlobals(); - return reinterpret_borrow<dict>(p ? p : module::import("__main__").attr("__dict__").ptr()); + return reinterpret_borrow<dict>(p ? p : module_::import("__main__").attr("__dict__").ptr()); } -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) class generic_type : public object { - template <typename...> friend class class_; public: PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check) protected: void initialize(const type_record &rec) { - if (rec.scope && hasattr(rec.scope, rec.name)) + if (rec.scope && hasattr(rec.scope, "__dict__") && rec.scope.attr("__dict__").contains(rec.name)) pybind11_fail("generic_type: cannot initialize type \"" + std::string(rec.name) + "\": an object with that name is already defined"); @@ -906,13 +1039,13 @@ protected: void install_buffer_funcs( buffer_info *(*get_buffer)(PyObject *, void *), void *get_buffer_data) { - PyHeapTypeObject *type = (PyHeapTypeObject*) m_ptr; + auto *type = (PyHeapTypeObject*) m_ptr; auto tinfo = detail::get_type_info(&type->ht_type); if (!type->ht_type.tp_as_buffer) pybind11_fail( "To be able to register buffer protocol support for the type '" + - std::string(tinfo->type->tp_name) + + get_fully_qualified_tp_name(tinfo->type) + "' the associated class<>(..) invocation must " "include the pybind11::buffer_protocol() annotation!"); @@ -955,17 +1088,31 @@ void call_operator_delete(T *p, size_t s, size_t) { T::operator delete(p, s); } inline void call_operator_delete(void *p, size_t s, size_t a) { (void)s; (void)a; -#if defined(PYBIND11_CPP17) - if (a > __STDCPP_DEFAULT_NEW_ALIGNMENT__) - ::operator delete(p, s, std::align_val_t(a)); - else + #if defined(__cpp_aligned_new) && (!defined(_MSC_VER) || _MSC_VER >= 1912) + if (a > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { + #ifdef __cpp_sized_deallocation + ::operator delete(p, s, std::align_val_t(a)); + #else + ::operator delete(p, std::align_val_t(a)); + #endif + return; + } + #endif + #ifdef __cpp_sized_deallocation ::operator delete(p, s); -#else - ::operator delete(p); -#endif + #else + ::operator delete(p); + #endif +} + +inline void add_class_method(object& cls, const char *name_, const cpp_function &cf) { + cls.attr(cf.name()) = cf; + if (strcmp(name_, "__eq__") == 0 && !cls.attr("__dict__").contains("__hash__")) { + cls.attr("__hash__") = none(); + } } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) @@ -1062,7 +1209,7 @@ public: class_ &def(const char *name_, Func&& f, const Extra&... extra) { cpp_function cf(method_adaptor<type>(std::forward<Func>(f)), name(name_), is_method(*this), sibling(getattr(*this, name_, none())), extra...); - attr(cf.name()) = cf; + add_class_method(*this, name_, cf); return *this; } @@ -1072,7 +1219,7 @@ public: "def_static(...) called with a non-static member function pointer"); cpp_function cf(std::forward<Func>(f), name(name_), scope(*this), sibling(getattr(*this, name_, none())), extra...); - attr(cf.name()) = cf; + attr(cf.name()) = staticmethod(cf); return *this; } @@ -1112,15 +1259,20 @@ public: return *this; } - template <typename Func> class_& def_buffer(Func &&func) { + template <typename Func> + class_& def_buffer(Func &&func) { struct capture { Func func; }; - capture *ptr = new capture { std::forward<Func>(func) }; + auto *ptr = new capture { std::forward<Func>(func) }; install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* { detail::make_caster<type> caster; if (!caster.load(obj, false)) return nullptr; return new buffer_info(((capture *) ptr)->func(caster)); }, ptr); + weakref(m_ptr, cpp_function([ptr](handle wr) { + delete ptr; + wr.dec_ref(); + })).release(); return *this; } @@ -1136,7 +1288,7 @@ public: template <typename C, typename D, typename... Extra> class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) { - static_assert(std::is_base_of<C, type>::value, "def_readwrite() requires a class member (or base class member)"); + static_assert(std::is_same<C, type>::value || std::is_base_of<C, type>::value, "def_readwrite() requires a class member (or base class member)"); cpp_function fget([pm](const type &c) -> const D &{ return c.*pm; }, is_method(*this)), fset([pm](type &c, const D &value) { c.*pm = value; }, is_method(*this)); def_property(name, fget, fset, return_value_policy::reference_internal, extra...); @@ -1145,7 +1297,7 @@ public: template <typename C, typename D, typename... Extra> class_ &def_readonly(const char *name, const D C::*pm, const Extra& ...extra) { - static_assert(std::is_base_of<C, type>::value, "def_readonly() requires a class member (or base class member)"); + static_assert(std::is_same<C, type>::value || std::is_base_of<C, type>::value, "def_readonly() requires a class member (or base class member)"); cpp_function fget([pm](const type &c) -> const D &{ return c.*pm; }, is_method(*this)); def_property_readonly(name, fget, return_value_policy::reference_internal, extra...); return *this; @@ -1217,6 +1369,8 @@ public: template <typename... Extra> class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) { + static_assert( 0 == detail::constexpr_sum(std::is_base_of<arg, Extra>::value...), + "Argument annotations are not allowed for properties"); auto rec_fget = get_function_record(fget), rec_fset = get_function_record(fset); auto *rec_active = rec_fget; if (rec_fget) { @@ -1297,6 +1451,13 @@ private: static void dealloc(detail::value_and_holder &v_h) { + + + + + + + error_scope scope; if (v_h.holder_constructed()) { v_h.holder<holder_type>().~holder_type(); v_h.set_holder_constructed(false); @@ -1341,7 +1502,17 @@ detail::initimpl::pickle_factory<GetState, SetState> pickle(GetState &&g, SetSta return {std::forward<GetState>(g), std::forward<SetState>(s)}; } -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) + +inline str enum_name(handle arg) { + dict entries = arg.get_type().attr("__entries"); + for (auto kv : entries) { + if (handle(kv.second[int_(0)]).equal(arg)) + return pybind11::str(kv.first); + } + return "???"; +} + struct enum_base { enum_base(handle base, handle parent) : m_base(base), m_parent(parent) { } @@ -1351,29 +1522,21 @@ struct enum_base { auto static_property = handle((PyObject *) get_internals().static_property_type); m_base.attr("__repr__") = cpp_function( - [](handle arg) -> str { - handle type = arg.get_type(); + [](object arg) -> str { + handle type = type::handle_of(arg); object type_name = type.attr("__name__"); - dict entries = type.attr("__entries"); - for (const auto &kv : entries) { - object other = kv.second[int_(0)]; - if (other.equal(arg)) - return pybind11::str("{}.{}").format(type_name, kv.first); - } - return pybind11::str("{}.???").format(type_name); - }, is_method(m_base) + return pybind11::str("<{}.{}: {}>").format(type_name, enum_name(arg), int_(arg)); + }, name("__repr__"), is_method(m_base) ); - m_base.attr("name") = property(cpp_function( + m_base.attr("name") = property(cpp_function(&enum_name, name("name"), is_method(m_base))); + + m_base.attr("__str__") = cpp_function( [](handle arg) -> str { - dict entries = arg.get_type().attr("__entries"); - for (const auto &kv : entries) { - if (handle(kv.second[int_(0)]).equal(arg)) - return pybind11::str(kv.first); - } - return "???"; - }, is_method(m_base) - )); + object type_name = type::handle_of(arg).attr("__name__"); + return pybind11::str("{}.{}").format(type_name, enum_name(arg)); + }, name("name"), is_method(m_base) + ); m_base.attr("__doc__") = static_property(cpp_function( [](handle arg) -> std::string { @@ -1382,7 +1545,7 @@ struct enum_base { if (((PyTypeObject *) arg.ptr())->tp_doc) docstring += std::string(((PyTypeObject *) arg.ptr())->tp_doc) + "\n\n"; docstring += "Members:"; - for (const auto &kv : entries) { + for (auto kv : entries) { auto key = std::string(pybind11::str(kv.first)); auto comment = kv.second[int_(1)]; docstring += "\n\n " + key; @@ -1390,26 +1553,26 @@ struct enum_base { docstring += " : " + (std::string) pybind11::str(comment); } return docstring; - } + }, name("__doc__") ), none(), none(), ""); m_base.attr("__members__") = static_property(cpp_function( [](handle arg) -> dict { dict entries = arg.attr("__entries"), m; - for (const auto &kv : entries) + for (auto kv : entries) m[kv.first] = kv.second[int_(0)]; return m; - }), none(), none(), "" + }, name("__members__")), none(), none(), "" ); #define PYBIND11_ENUM_OP_STRICT(op, expr, strict_behavior) \ m_base.attr(op) = cpp_function( \ [](object a, object b) { \ - if (!a.get_type().is(b.get_type())) \ + if (!type::handle_of(a).is(type::handle_of(b))) \ strict_behavior; \ return expr; \ }, \ - is_method(m_base)) + name(op), is_method(m_base), arg("other")) #define PYBIND11_ENUM_OP_CONV(op, expr) \ m_base.attr(op) = cpp_function( \ @@ -1417,11 +1580,19 @@ struct enum_base { int_ a(a_), b(b_); \ return expr; \ }, \ - is_method(m_base)) + name(op), is_method(m_base), arg("other")) + + #define PYBIND11_ENUM_OP_CONV_LHS(op, expr) \ + m_base.attr(op) = cpp_function( \ + [](object a_, object b) { \ + int_ a(a_); \ + return expr; \ + }, \ + name(op), is_method(m_base), arg("other")) if (is_convertible) { - PYBIND11_ENUM_OP_CONV("__eq__", !b.is_none() && a.equal(b)); - PYBIND11_ENUM_OP_CONV("__ne__", b.is_none() || !a.equal(b)); + PYBIND11_ENUM_OP_CONV_LHS("__eq__", !b.is_none() && a.equal(b)); + PYBIND11_ENUM_OP_CONV_LHS("__ne__", b.is_none() || !a.equal(b)); if (is_arithmetic) { PYBIND11_ENUM_OP_CONV("__lt__", a < b); @@ -1434,6 +1605,8 @@ struct enum_base { PYBIND11_ENUM_OP_CONV("__ror__", a | b); PYBIND11_ENUM_OP_CONV("__xor__", a ^ b); PYBIND11_ENUM_OP_CONV("__rxor__", a ^ b); + m_base.attr("__invert__") = cpp_function( + [](object arg) { return ~(int_(arg)); }, name("__invert__"), is_method(m_base)); } } else { PYBIND11_ENUM_OP_STRICT("__eq__", int_(a).equal(int_(b)), return false); @@ -1449,14 +1622,15 @@ struct enum_base { } } + #undef PYBIND11_ENUM_OP_CONV_LHS #undef PYBIND11_ENUM_OP_CONV #undef PYBIND11_ENUM_OP_STRICT - object getstate = cpp_function( - [](object arg) { return int_(arg); }, is_method(m_base)); + m_base.attr("__getstate__") = cpp_function( + [](object arg) { return int_(arg); }, name("__getstate__"), is_method(m_base)); - m_base.attr("__getstate__") = getstate; - m_base.attr("__hash__") = getstate; + m_base.attr("__hash__") = cpp_function( + [](object arg) { return int_(arg); }, name("__hash__"), is_method(m_base)); } PYBIND11_NOINLINE void value(char const* name_, object value, const char *doc = nullptr) { @@ -1473,7 +1647,7 @@ struct enum_base { PYBIND11_NOINLINE void export_values() { dict entries = m_base.attr("__entries"); - for (const auto &kv : entries) + for (auto kv : entries) m_parent.attr(kv.first) = kv.second[int_(0)]; } @@ -1481,7 +1655,7 @@ struct enum_base { handle m_parent; }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) template <typename Type> class enum_ : public class_<Type> { @@ -1500,15 +1674,21 @@ public: constexpr bool is_convertible = std::is_convertible<Type, Scalar>::value; m_base.init(is_arithmetic, is_convertible); - def(init([](Scalar i) { return static_cast<Type>(i); })); + def(init([](Scalar i) { return static_cast<Type>(i); }), arg("value")); def("__int__", [](Type value) { return (Scalar) value; }); #if PY_MAJOR_VERSION < 3 def("__long__", [](Type value) { return (Scalar) value; }); #endif - cpp_function setstate( - [](Type &value, Scalar arg) { value = static_cast<Type>(arg); }, - is_method(*this)); - attr("__setstate__") = setstate; + #if PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8) + def("__index__", [](Type value) { return (Scalar) value; }); + #endif + + attr("__setstate__") = cpp_function( + [](detail::value_and_holder &v_h, Scalar arg) { + detail::initimpl::setstate<Base>(v_h, static_cast<Type>(arg), + Py_TYPE(v_h.inst) != v_h.type->type); }, + detail::is_new_style_constructor(), + pybind11::name("__setstate__"), is_method(*this), arg("state")); } @@ -1527,7 +1707,7 @@ private: detail::enum_base m_base; }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) inline void keep_alive_impl(handle nurse, handle patient) { @@ -1594,7 +1774,7 @@ struct iterator_state { bool first_or_done; }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) template <return_value_policy Policy = return_value_policy::reference_internal, @@ -1603,7 +1783,7 @@ template <return_value_policy Policy = return_value_policy::reference_internal, typename ValueType = decltype(*std::declval<Iterator>()), typename... Extra> iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) { - typedef detail::iterator_state<Iterator, Sentinel, false, Policy> state; + using state = detail::iterator_state<Iterator, Sentinel, false, Policy>; if (!detail::get_type_info(typeid(state), false)) { class_<state>(handle(), "iterator", pybind11::module_local()) @@ -1632,7 +1812,7 @@ template <return_value_policy Policy = return_value_policy::reference_internal, typename KeyType = decltype((*std::declval<Iterator>()).first), typename... Extra> iterator make_key_iterator(Iterator first, Sentinel last, Extra &&... extra) { - typedef detail::iterator_state<Iterator, Sentinel, true, Policy> state; + using state = detail::iterator_state<Iterator, Sentinel, true, Policy>; if (!detail::get_type_info(typeid(state), false)) { class_<state>(handle(), "iterator", pybind11::module_local()) @@ -1705,11 +1885,11 @@ template <typename type> class exception : public object { public: exception() = default; - exception(handle scope, const char *name, PyObject *base = PyExc_Exception) { + exception(handle scope, const char *name, handle base = PyExc_Exception) { std::string full_name = scope.attr("__name__").cast<std::string>() + std::string(".") + name; - m_ptr = PyErr_NewException(const_cast<char *>(full_name.c_str()), base, NULL); - if (hasattr(scope, name)) + m_ptr = PyErr_NewException(const_cast<char *>(full_name.c_str()), base.ptr(), NULL); + if (hasattr(scope, "__dict__") && scope.attr("__dict__").contains(name)) pybind11_fail("Error during initialization: multiple incompatible " "definitions with name \"" + std::string(name) + "\""); scope.attr(name) = *this; @@ -1721,19 +1901,19 @@ public: } }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) template <typename CppException> exception<CppException> &get_exception_object() { static exception<CppException> ex; return ex; } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) template <typename CppException> exception<CppException> ®ister_exception(handle scope, const char *name, - PyObject *base = PyExc_Exception) { + handle base = PyExc_Exception) { auto &ex = detail::get_exception_object<CppException>(); if (!ex) ex = exception<CppException>(scope, name, base); @@ -1748,7 +1928,7 @@ exception<CppException> ®ister_exception(handle scope, return ex; } -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) PYBIND11_NOINLINE inline void print(tuple args, dict kwargs) { auto strings = tuple(args.size()); for (size_t i = 0; i < args.size(); ++i) { @@ -1762,7 +1942,7 @@ PYBIND11_NOINLINE inline void print(tuple args, dict kwargs) { file = kwargs["file"].cast<object>(); } else { try { - file = module::import("sys").attr("stdout"); + file = module_::import("sys").attr("stdout"); } catch (const error_already_set &) { return; @@ -1776,7 +1956,7 @@ PYBIND11_NOINLINE inline void print(tuple args, dict kwargs) { if (kwargs.contains("flush") && kwargs["flush"].cast<bool>()) file.attr("flush")(); } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args> void print(Args &&...args) { @@ -1906,29 +2086,30 @@ class gil_scoped_release { }; #endif error_already_set::~error_already_set() { - if (type) { - error_scope scope; + if (m_type) { gil_scoped_acquire gil; - type.release().dec_ref(); - value.release().dec_ref(); - trace.release().dec_ref(); + error_scope scope; + m_type.release().dec_ref(); + m_value.release().dec_ref(); + m_trace.release().dec_ref(); } } -inline function get_type_overload(const void *this_ptr, const detail::type_info *this_type, const char *name) { - handle self = detail::get_object_handle(this_ptr, this_type); +PYBIND11_NAMESPACE_BEGIN(detail) +inline function get_type_override(const void *this_ptr, const type_info *this_type, const char *name) { + handle self = get_object_handle(this_ptr, this_type); if (!self) return function(); - handle type = self.get_type(); + handle type = type::handle_of(self); auto key = std::make_pair(type.ptr(), name); - auto &cache = detail::get_internals().inactive_overload_cache; + auto &cache = get_internals().inactive_override_cache; if (cache.find(key) != cache.end()) return function(); - function overload = getattr(self, name, function()); - if (overload.is_cpp_function()) { + function override = getattr(self, name, function()); + if (override.is_cpp_function()) { cache.insert(key); return function(); } @@ -1965,45 +2146,80 @@ inline function get_type_overload(const void *this_ptr, const detail::type_info Py_DECREF(result); #endif - return overload; + return override; } +PYBIND11_NAMESPACE_END(detail) -template <class T> function get_overload(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)); - return tinfo ? get_type_overload(this_ptr, tinfo, name) : function(); + return tinfo ? detail::get_type_override(this_ptr, tinfo, name) : function(); } -#define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) { \ +#define PYBIND11_OVERRIDE_IMPL(ret_type, cname, name, ...) \ + do { \ pybind11::gil_scoped_acquire gil; \ - pybind11::function overload = pybind11::get_overload(static_cast<const cname *>(this), name); \ - if (overload) { \ - auto o = overload(__VA_ARGS__); \ + pybind11::function override = pybind11::get_override(static_cast<const cname *>(this), name); \ + if (override) { \ + auto o = override(__VA_ARGS__); \ if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) { \ - static pybind11::detail::overload_caster_t<ret_type> caster; \ + static pybind11::detail::override_caster_t<ret_type> caster; \ return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \ } \ else return pybind11::detail::cast_safe<ret_type>(std::move(o)); \ } \ - } + } while (false) + + +#define PYBIND11_OVERRIDE_NAME(ret_type, cname, name, fn, ...) \ + do { \ + PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \ + return cname::fn(__VA_ARGS__); \ + } while (false) + + +#define PYBIND11_OVERRIDE_PURE_NAME(ret_type, cname, name, fn, ...) \ + do { \ + PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \ + pybind11::pybind11_fail("Tried to call pure virtual function \"" PYBIND11_STRINGIFY(cname) "::" name "\""); \ + } while (false) + + +#define PYBIND11_OVERRIDE(ret_type, cname, fn, ...) \ + PYBIND11_OVERRIDE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__) + + +#define PYBIND11_OVERRIDE_PURE(ret_type, cname, fn, ...) \ + PYBIND11_OVERRIDE_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__) -#define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...) \ - PYBIND11_OVERLOAD_INT(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__) \ - return cname::fn(__VA_ARGS__) -#define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \ - PYBIND11_OVERLOAD_INT(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__) \ - pybind11::pybind11_fail("Tried to call pure virtual function \"" PYBIND11_STRINGIFY(cname) "::" name "\""); -#define PYBIND11_OVERLOAD(ret_type, cname, fn, ...) \ - PYBIND11_OVERLOAD_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__) +PYBIND11_DEPRECATED("get_type_overload has been deprecated") +inline function get_type_overload(const void *this_ptr, const detail::type_info *this_type, const char *name) { + return detail::get_type_override(this_ptr, this_type, name); +} + +template <class T> +inline function get_overload(const T *this_ptr, const char *name) { + return get_override(this_ptr, name); +} + +#define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) \ + PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__) +#define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...) \ + PYBIND11_OVERRIDE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, fn, __VA_ARGS__) +#define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \ + PYBIND11_OVERRIDE_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, fn, __VA_ARGS__); +#define PYBIND11_OVERLOAD(ret_type, cname, fn, ...) \ + PYBIND11_OVERRIDE(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), fn, __VA_ARGS__) #define PYBIND11_OVERLOAD_PURE(ret_type, cname, fn, ...) \ - PYBIND11_OVERLOAD_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__) + PYBIND11_OVERRIDE_PURE(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), fn, __VA_ARGS__); -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) # pragma warning(pop) -#elif defined(__GNUG__) +#elif defined(__GNUG__) && !defined(__clang__) # pragma GCC diagnostic pop #endif diff --git a/host/lib/deps/pybind11/include/pybind11/pytypes.h b/host/lib/deps/pybind11/include/pybind11/pytypes.h index f0633e6ba..42bac225e 100644 --- a/host/lib/deps/pybind11/include/pybind11/pytypes.h +++ b/host/lib/deps/pybind11/include/pybind11/pytypes.h @@ -6,8 +6,6 @@ */ - - #pragma once #include "detail/common.h" @@ -15,14 +13,15 @@ #include <utility> #include <type_traits> -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) class handle; class object; class str; class iterator; +class type; struct arg; struct arg_v; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) class args_proxy; inline bool isinstance_generic(handle obj, const std::type_info &tp); @@ -123,13 +122,14 @@ public: int ref_count() const { return static_cast<int>(Py_REFCNT(derived().ptr())); } + handle get_type() const; private: bool rich_compare(object_api const &other, int value) const; }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) class handle : public detail::object_api<handle> { @@ -226,9 +226,9 @@ template <typename T> T reinterpret_borrow(handle h) { return {h, object::borrow template <typename T> T reinterpret_steal(handle h) { return {h, object::stolen_t{}}; } -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) inline std::string error_string(); -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) @@ -239,18 +239,32 @@ public: error_already_set() : std::runtime_error(detail::error_string()) { - PyErr_Fetch(&type.ptr(), &value.ptr(), &trace.ptr()); + PyErr_Fetch(&m_type.ptr(), &m_value.ptr(), &m_trace.ptr()); } error_already_set(const error_already_set &) = default; error_already_set(error_already_set &&) = default; - inline ~error_already_set(); + inline ~error_already_set() override; + + + + + void restore() { PyErr_Restore(m_type.release().ptr(), m_value.release().ptr(), m_trace.release().ptr()); } - void restore() { PyErr_Restore(type.release().ptr(), value.release().ptr(), trace.release().ptr()); } + + + + void discard_as_unraisable(object err_context) { + restore(); + PyErr_WriteUnraisable(err_context.ptr()); + } + void discard_as_unraisable(const char *err_context) { + discard_as_unraisable(reinterpret_steal<object>(PYBIND11_FROM_STRING(err_context))); + } PYBIND11_DEPRECATED("Use of error_already_set.clear() is deprecated") @@ -259,10 +273,14 @@ public: - bool matches(handle ex) const { return PyErr_GivenExceptionMatches(ex.ptr(), type.ptr()); } + bool matches(handle exc) const { return PyErr_GivenExceptionMatches(m_type.ptr(), exc.ptr()); } + + const object& type() const { return m_type; } + const object& value() const { return m_value; } + const object& trace() const { return m_trace; } private: - object type, value, trace; + object m_type, m_value, m_trace; }; @@ -274,7 +292,7 @@ bool isinstance(handle obj) { return T::check_(obj); } template <typename T, detail::enable_if_t<!std::is_base_of<object, T>::value, int> = 0> bool isinstance(handle obj) { return detail::isinstance_generic(obj, typeid(T)); } -template <> inline bool isinstance<handle>(handle obj) = delete; +template <> inline bool isinstance<handle>(handle) = delete; template <> inline bool isinstance<object>(handle obj) { return obj.ptr() != nullptr; } @@ -350,7 +368,7 @@ inline ssize_t hash(handle obj) { -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) inline handle get_function(handle value) { if (value) { #if PY_MAJOR_VERSION >= 3 @@ -424,7 +442,7 @@ private: mutable object cache; }; -NAMESPACE_BEGIN(accessor_policies) +PYBIND11_NAMESPACE_BEGIN(accessor_policies) struct obj_attr { using key_type = object; static object get(handle obj, handle key) { return getattr(obj, key); } @@ -501,7 +519,7 @@ struct tuple_item { } } }; -NAMESPACE_END(accessor_policies) +PYBIND11_NAMESPACE_END(accessor_policies) template <typename Policy> @@ -542,7 +560,7 @@ public: friend bool operator<=(const It &a, const It &b) { return !(a > b); } }; -NAMESPACE_BEGIN(iterator_policies) +PYBIND11_NAMESPACE_BEGIN(iterator_policies) template <typename T> struct arrow_proxy { @@ -612,10 +630,10 @@ protected: private: handle obj; - PyObject *key, *value; + PyObject *key = nullptr, *value = nullptr; ssize_t pos = -1; }; -NAMESPACE_END(iterator_policies) +PYBIND11_NAMESPACE_END(iterator_policies) #if !defined(PYPY_VERSION) using tuple_iterator = generic_iterator<iterator_policies::sequence_fast_readonly>; @@ -640,12 +658,12 @@ inline bool PyIterable_Check(PyObject *obj) { } inline bool PyNone_Check(PyObject *o) { return o == Py_None; } -#if PY_MAJOR_VERSION >= 3 inline bool PyEllipsis_Check(PyObject *o) { return o == Py_Ellipsis; } -#endif inline bool PyUnicode_Check_Permissive(PyObject *o) { return PyUnicode_Check(o) || PYBIND11_BYTES_CHECK(o); } +inline bool PyStaticMethod_Check(PyObject *o) { return o->ob_type == &PyStaticMethod_Type; } + class kwargs_proxy : public handle { public: explicit kwargs_proxy(handle h) : handle(h) { } @@ -672,7 +690,7 @@ class simple_collector; template <return_value_policy policy = return_value_policy::automatic_reference> class unpacking_collector; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) @@ -686,7 +704,9 @@ NAMESPACE_END(detail) Name(handle h, stolen_t) : Parent(h, stolen_t{}) { } \ PYBIND11_DEPRECATED("Use py::isinstance<py::python_type>(obj) instead") \ bool check() const { return m_ptr != nullptr && (bool) CheckFun(m_ptr); } \ - static bool check_(handle h) { return h.ptr() != nullptr && CheckFun(h.ptr()); } + static bool check_(handle h) { return h.ptr() != nullptr && CheckFun(h.ptr()); } \ + template <typename Policy_> \ + Name(const ::pybind11::detail::accessor<Policy_> &a) : Name(object(a)) { } #define PYBIND11_OBJECT_CVT(Name, Parent, CheckFun, ConvertFun) \ PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \ @@ -696,15 +716,20 @@ NAMESPACE_END(detail) { if (!m_ptr) throw error_already_set(); } \ Name(object &&o) \ : Parent(check_(o) ? o.release().ptr() : ConvertFun(o.ptr()), stolen_t{}) \ - { if (!m_ptr) throw error_already_set(); } \ - template <typename Policy_> \ - Name(const ::pybind11::detail::accessor<Policy_> &a) : Name(object(a)) { } + { if (!m_ptr) throw error_already_set(); } + +#define PYBIND11_OBJECT_CHECK_FAILED(Name, o) \ + ::pybind11::type_error("Object of type '" + \ + ::pybind11::detail::get_fully_qualified_tp_name(Py_TYPE(o.ptr())) + \ + "' is not an instance of '" #Name "'") #define PYBIND11_OBJECT(Name, Parent, CheckFun) \ PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \ \ - Name(const object &o) : Parent(o) { } \ - Name(object &&o) : Parent(std::move(o)) { } + Name(const object &o) : Parent(o) \ + { if (o && !check_(o)) throw PYBIND11_OBJECT_CHECK_FAILED(Name, o); } \ + Name(object &&o) : Parent(std::move(o)) \ + { if (o && !check_(o)) throw PYBIND11_OBJECT_CHECK_FAILED(Name, o); } #define PYBIND11_OBJECT_DEFAULT(Name, Parent, CheckFun) \ PYBIND11_OBJECT(Name, Parent, CheckFun) \ @@ -761,6 +786,32 @@ private: object value = {}; }; + + +class type : public object { +public: + PYBIND11_OBJECT(type, object, PyType_Check) + + + static handle handle_of(handle h) { return handle((PyObject*) Py_TYPE(h.ptr())); } + + + static type of(handle h) { return type(type::handle_of(h), borrowed_t{}); } + + + + + + template<typename T> + static handle handle_of(); + + + + + template<typename T> + static type of() {return type(type::handle_of<T>(), borrowed_t{}); } +}; + class iterable : public object { public: PYBIND11_OBJECT_DEFAULT(iterable, object, detail::PyIterable_Check) @@ -788,7 +839,7 @@ public: explicit str(const bytes &b); - explicit str(handle h) : object(raw_str(h.ptr()), stolen_t{}) { } + explicit str(handle h) : object(raw_str(h.ptr()), stolen_t{}) { if (!m_ptr) throw error_already_set(); } operator std::string() const { object temp = *this; @@ -859,6 +910,9 @@ public: } }; + + + inline bytes::bytes(const pybind11::str &s) { object temp = s; if (PyUnicode_Check(s.ptr())) { @@ -887,19 +941,19 @@ inline str::str(const bytes& b) { m_ptr = obj.release().ptr(); } + + class none : public object { public: PYBIND11_OBJECT(none, object, detail::PyNone_Check) none() : object(Py_None, borrowed_t{}) { } }; -#if PY_MAJOR_VERSION >= 3 class ellipsis : public object { public: PYBIND11_OBJECT(ellipsis, object, detail::PyEllipsis_Check) ellipsis() : object(Py_Ellipsis, borrowed_t{}) { } }; -#endif class bool_ : public object { public: @@ -918,7 +972,7 @@ private: } }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) @@ -938,7 +992,7 @@ Unsigned as_unsigned(PyObject *o) { return v == (unsigned long long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v; } } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) class int_ : public object { public: @@ -1011,6 +1065,13 @@ public: (ssize_t *) stop, (ssize_t *) step, (ssize_t *) slicelength) == 0; } + bool compute(ssize_t length, ssize_t *start, ssize_t *stop, ssize_t *step, + ssize_t *slicelength) const { + return PySlice_GetIndicesEx((PYBIND11_SLICE_OBJECT *) m_ptr, + length, start, + stop, step, + slicelength) == 0; + } }; class capsule : public object { @@ -1057,12 +1118,24 @@ public: } template <typename T> operator T *() const { + return get_pointer<T>(); + } + + + template<typename T = void> + T* get_pointer() 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) pybind11_fail("Unable to extract capsule contents!"); return result; } + + void set_pointer(const void *value) { + if (PyCapsule_SetPointer(m_ptr, const_cast<void *>(value)) != 0) + pybind11_fail("Could not set capsule pointer"); + } + const char *name() const { return PyCapsule_GetName(m_ptr); } }; @@ -1073,6 +1146,7 @@ public: if (!m_ptr) pybind11_fail("Could not allocate tuple object!"); } size_t size() const { return (size_t) PyTuple_Size(m_ptr); } + bool empty() const { return size() == 0; } detail::tuple_accessor operator[](size_t index) const { return {*this, index}; } detail::item_accessor operator[](handle h) const { return object::operator[](h); } detail::tuple_iterator begin() const { return {*this, 0}; } @@ -1092,11 +1166,13 @@ public: explicit dict(Args &&...args) : dict(collector(std::forward<Args>(args)...).kwargs()) { } size_t size() const { return (size_t) PyDict_Size(m_ptr); } + bool empty() const { return size() == 0; } detail::dict_iterator begin() const { return {*this, 0}; } detail::dict_iterator end() const { return {}; } void clear() const { PyDict_Clear(ptr()); } - bool contains(handle key) const { return PyDict_Contains(ptr(), key.ptr()) == 1; } - bool contains(const char *key) const { return PyDict_Contains(ptr(), pybind11::str(key).ptr()) == 1; } + template <typename T> bool contains(T &&key) const { + return PyDict_Contains(m_ptr, detail::object_or_cast(std::forward<T>(key)).ptr()) == 1; + } private: @@ -1110,7 +1186,13 @@ private: class sequence : public object { public: PYBIND11_OBJECT_DEFAULT(sequence, object, PySequence_Check) - size_t size() const { return (size_t) PySequence_Size(m_ptr); } + size_t size() const { + ssize_t result = PySequence_Size(m_ptr); + if (result == -1) + throw error_already_set(); + return (size_t) result; + } + bool empty() const { return size() == 0; } detail::sequence_accessor operator[](size_t index) const { return {*this, index}; } detail::item_accessor operator[](handle h) const { return object::operator[](h); } detail::sequence_iterator begin() const { return {*this, 0}; } @@ -1124,6 +1206,7 @@ public: if (!m_ptr) pybind11_fail("Could not allocate list object!"); } size_t size() const { return (size_t) PyList_Size(m_ptr); } + bool empty() const { return size() == 0; } detail::list_accessor operator[](size_t index) const { return {*this, index}; } detail::item_accessor operator[](handle h) const { return object::operator[](h); } detail::list_iterator begin() const { return {*this, 0}; } @@ -1131,6 +1214,10 @@ public: template <typename T> void append(T &&val) const { PyList_Append(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr()); } + template <typename T> void insert(size_t index, T &&val) const { + PyList_Insert(m_ptr, static_cast<ssize_t>(index), + detail::object_or_cast(std::forward<T>(val)).ptr()); + } }; class args : public tuple { PYBIND11_OBJECT_DEFAULT(args, tuple, PyTuple_Check) }; @@ -1143,10 +1230,14 @@ public: if (!m_ptr) pybind11_fail("Could not allocate set object!"); } size_t size() const { return (size_t) PySet_Size(m_ptr); } + bool empty() const { return size() == 0; } template <typename T> bool add(T &&val) const { return PySet_Add(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr()) == 0; } void clear() const { PySet_Clear(m_ptr); } + template <typename T> bool contains(T &&val) const { + return PySet_Contains(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr()) == 1; + } }; class function : public object { @@ -1161,14 +1252,19 @@ public: bool is_cpp_function() const { return (bool) cpp_function(); } }; +class staticmethod : public object { +public: + PYBIND11_OBJECT_CVT(staticmethod, object, detail::PyStaticMethod_Check, PyStaticMethod_New) +}; + class buffer : public object { public: PYBIND11_OBJECT_DEFAULT(buffer, object, PyObject_CheckBuffer) - buffer_info request(bool writable = false) { + buffer_info request(bool writable = false) const { int flags = PyBUF_STRIDES | PyBUF_FORMAT; if (writable) flags |= PyBUF_WRITABLE; - Py_buffer *view = new Py_buffer(); + auto *view = new Py_buffer(); if (PyObject_GetBuffer(m_ptr, view, flags) != 0) { delete view; throw error_already_set(); @@ -1179,43 +1275,124 @@ public: class memoryview : public object { public: + PYBIND11_OBJECT_CVT(memoryview, object, PyMemoryView_Check, PyMemoryView_FromObject) + + explicit memoryview(const buffer_info& info) { - static Py_buffer buf { }; - - static std::vector<Py_ssize_t> py_strides { }; - static std::vector<Py_ssize_t> py_shape { }; - buf.buf = info.ptr; - buf.itemsize = info.itemsize; - buf.format = const_cast<char *>(info.format.c_str()); - buf.ndim = (int) info.ndim; - buf.len = info.size; - py_strides.clear(); - py_shape.clear(); - for (size_t i = 0; i < (size_t) info.ndim; ++i) { - py_strides.push_back(info.strides[i]); - py_shape.push_back(info.shape[i]); - } - buf.strides = py_strides.data(); - buf.shape = py_shape.data(); - buf.suboffsets = nullptr; - buf.readonly = false; - buf.internal = nullptr; + if (!info.view()) + pybind11_fail("Prohibited to create memoryview without Py_buffer"); - m_ptr = PyMemoryView_FromBuffer(&buf); + m_ptr = (info.view()->obj) ? + PyMemoryView_FromObject(info.view()->obj) : + PyMemoryView_FromBuffer(info.view()); if (!m_ptr) pybind11_fail("Unable to create memoryview from buffer descriptor"); } - PYBIND11_OBJECT_CVT(memoryview, object, PyMemoryView_Check, PyMemoryView_FromObject) + + static memoryview from_buffer( + void *ptr, ssize_t itemsize, const char *format, + detail::any_container<ssize_t> shape, + detail::any_container<ssize_t> strides, bool readonly = false); + + static memoryview from_buffer( + const void *ptr, ssize_t itemsize, const char *format, + detail::any_container<ssize_t> shape, + detail::any_container<ssize_t> strides) { + return memoryview::from_buffer( + const_cast<void*>(ptr), itemsize, format, shape, strides, true); + } + + template<typename T> + static memoryview from_buffer( + T *ptr, detail::any_container<ssize_t> shape, + detail::any_container<ssize_t> strides, bool readonly = false) { + return memoryview::from_buffer( + reinterpret_cast<void*>(ptr), sizeof(T), + format_descriptor<T>::value, shape, strides, readonly); + } + + template<typename T> + static memoryview from_buffer( + const T *ptr, detail::any_container<ssize_t> shape, + detail::any_container<ssize_t> strides) { + return memoryview::from_buffer( + const_cast<T*>(ptr), shape, strides, true); + } + +#if PY_MAJOR_VERSION >= 3 + + static memoryview from_memory(void *mem, ssize_t size, bool readonly = false) { + PyObject* ptr = PyMemoryView_FromMemory( + reinterpret_cast<char*>(mem), size, + (readonly) ? PyBUF_READ : PyBUF_WRITE); + if (!ptr) + pybind11_fail("Could not allocate memoryview object!"); + return memoryview(object(ptr, stolen_t{})); + } + + static memoryview from_memory(const void *mem, ssize_t size) { + return memoryview::from_memory(const_cast<void*>(mem), size, true); + } +#endif }; +#ifndef DOXYGEN_SHOULD_SKIP_THIS +inline memoryview memoryview::from_buffer( + void *ptr, ssize_t itemsize, const char* format, + detail::any_container<ssize_t> shape, + detail::any_container<ssize_t> strides, bool readonly) { + size_t ndim = shape->size(); + if (ndim != strides->size()) + pybind11_fail("memoryview: shape length doesn't match strides length"); + ssize_t size = ndim ? 1 : 0; + for (size_t i = 0; i < ndim; ++i) + size *= (*shape)[i]; + Py_buffer view; + view.buf = ptr; + view.obj = nullptr; + view.len = size * itemsize; + view.readonly = static_cast<int>(readonly); + view.itemsize = itemsize; + view.format = const_cast<char*>(format); + view.ndim = static_cast<int>(ndim); + view.shape = shape->data(); + view.strides = strides->data(); + view.suboffsets = nullptr; + view.internal = nullptr; + PyObject* obj = PyMemoryView_FromBuffer(&view); + if (!obj) + throw error_already_set(); + return memoryview(object(obj, stolen_t{})); +} +#endif + + + inline size_t len(handle h) { ssize_t result = PyObject_Length(h.ptr()); if (result < 0) - pybind11_fail("Unable to compute length of object"); + throw error_already_set(); + return (size_t) result; +} + + + +inline size_t len_hint(handle h) { +#if PY_VERSION_HEX >= 0x03040000 + ssize_t result = PyObject_LengthHint(h.ptr(), 0); +#else + ssize_t result = PyObject_Length(h.ptr()); +#endif + if (result < 0) { + + + PyErr_Clear(); + return 0; + } return (size_t) result; } @@ -1237,7 +1414,7 @@ inline iterator iter(handle obj) { } -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) template <typename D> iterator object_api<D>::begin() const { return iter(derived()); } template <typename D> iterator object_api<D>::end() const { return iterator::sentinel(); } template <typename D> item_accessor object_api<D>::operator[](handle key) const { @@ -1266,7 +1443,7 @@ template <typename D> str_attr_accessor object_api<D>::doc() const { return attr("__doc__"); } template <typename D> -handle object_api<D>::get_type() const { return (PyObject *) Py_TYPE(derived().ptr()); } +handle object_api<D>::get_type() const { return type::handle_of(derived()); } template <typename D> bool object_api<D>::rich_compare(object_api const &other, int value) const { @@ -1318,5 +1495,5 @@ PYBIND11_MATH_OPERATOR_BINARY(operator>>=, PyNumber_InPlaceRshift) #undef PYBIND11_MATH_OPERATOR_UNARY #undef PYBIND11_MATH_OPERATOR_BINARY -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/include/pybind11/stl.h b/host/lib/deps/pybind11/include/pybind11/stl.h index b38060eea..41e72a15a 100644 --- a/host/lib/deps/pybind11/include/pybind11/stl.h +++ b/host/lib/deps/pybind11/include/pybind11/stl.h @@ -6,8 +6,6 @@ */ - - #pragma once #include "pybind11.h" @@ -49,8 +47,8 @@ # define PYBIND11_HAS_VARIANT 1 #endif -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) @@ -267,7 +265,9 @@ template<typename T> struct optional_caster { static handle cast(T_ &&src, return_value_policy policy, handle parent) { if (!src) return none().inc_ref(); - policy = return_value_policy_override<typename T::value_type>::policy(policy); + if (!std::is_lvalue_reference<T>::value) { + policy = return_value_policy_override<T>::policy(policy); + } return value_conv::cast(*std::forward<T_>(src), policy, parent); } @@ -288,7 +288,7 @@ template<typename T> struct optional_caster { PYBIND11_TYPE_CASTER(T, _("Optional[") + value_conv::name + _("]")); }; -#if PYBIND11_HAS_OPTIONAL +#if defined(PYBIND11_HAS_OPTIONAL) template<typename T> struct type_caster<std::optional<T>> : public optional_caster<std::optional<T>> {}; @@ -296,7 +296,7 @@ template<> struct type_caster<std::nullopt_t> : public void_caster<std::nullopt_t> {}; #endif -#if PYBIND11_HAS_EXP_OPTIONAL +#if defined(PYBIND11_HAS_EXP_OPTIONAL) template<typename T> struct type_caster<std::experimental::optional<T>> : public optional_caster<std::experimental::optional<T>> {}; @@ -368,19 +368,19 @@ struct variant_caster<V<Ts...>> { PYBIND11_TYPE_CASTER(Type, _("Union[") + detail::concat(make_caster<Ts>::name...) + _("]")); }; -#if PYBIND11_HAS_VARIANT +#if defined(PYBIND11_HAS_VARIANT) template <typename... Ts> struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> { }; #endif -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) inline std::ostream &operator<<(std::ostream &os, const handle &obj) { os << (std::string) str(obj); return os; } -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) #if defined(_MSC_VER) #pragma warning(pop) diff --git a/host/lib/deps/pybind11/include/pybind11/stl_bind.h b/host/lib/deps/pybind11/include/pybind11/stl_bind.h index 919f91c6e..07e3df284 100644 --- a/host/lib/deps/pybind11/include/pybind11/stl_bind.h +++ b/host/lib/deps/pybind11/include/pybind11/stl_bind.h @@ -6,8 +6,6 @@ */ - - #pragma once #include "detail/common.h" @@ -16,8 +14,8 @@ #include <algorithm> #include <sstream> -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) template <typename T> struct container_traits { @@ -116,6 +114,14 @@ void vector_modifiers(enable_if_t<is_copy_constructible<typename Vector::value_t using SizeType = typename Vector::size_type; using DiffType = typename Vector::difference_type; + auto wrap_i = [](DiffType i, SizeType n) { + if (i < 0) + i += n; + if (i < 0 || (SizeType)i >= n) + throw index_error(); + return i; + }; + cl.def("append", [](Vector &v, const T &value) { v.push_back(value); }, arg("x"), @@ -123,12 +129,19 @@ void vector_modifiers(enable_if_t<is_copy_constructible<typename Vector::value_t cl.def(init([](iterable it) { auto v = std::unique_ptr<Vector>(new Vector()); - v->reserve(len(it)); + v->reserve(len_hint(it)); for (handle h : it) v->push_back(h.cast<T>()); return v.release(); })); + cl.def("clear", + [](Vector &v) { + v.clear(); + }, + "Clear the contents" + ); + cl.def("extend", [](Vector &v, const Vector &src) { v.insert(v.end(), src.begin(), src.end()); @@ -137,11 +150,36 @@ void vector_modifiers(enable_if_t<is_copy_constructible<typename Vector::value_t "Extend the list by appending all the items in the given list" ); + cl.def("extend", + [](Vector &v, iterable it) { + const size_t old_size = v.size(); + v.reserve(old_size + len_hint(it)); + try { + for (handle h : it) { + v.push_back(h.cast<T>()); + } + } catch (const cast_error &) { + v.erase(v.begin() + static_cast<typename Vector::difference_type>(old_size), v.end()); + try { + v.shrink_to_fit(); + } catch (const std::exception &) { + + } + throw; + } + }, + arg("L"), + "Extend the list by appending all the items in the given list" + ); + cl.def("insert", - [](Vector &v, SizeType i, const T &x) { - if (i > v.size()) + [](Vector &v, DiffType i, const T &x) { + + if (i < 0) + i += v.size(); + if (i < 0 || (SizeType)i > v.size()) throw index_error(); - v.insert(v.begin() + (DiffType) i, x); + v.insert(v.begin() + i, x); }, arg("i") , arg("x"), "Insert an item at a given position." @@ -159,11 +197,10 @@ void vector_modifiers(enable_if_t<is_copy_constructible<typename Vector::value_t ); cl.def("pop", - [](Vector &v, SizeType i) { - if (i >= v.size()) - throw index_error(); - T t = v[i]; - v.erase(v.begin() + (DiffType) i); + [wrap_i](Vector &v, DiffType i) { + i = wrap_i(i, v.size()); + T t = v[(SizeType) i]; + v.erase(v.begin() + i); return t; }, arg("i"), @@ -171,10 +208,9 @@ void vector_modifiers(enable_if_t<is_copy_constructible<typename Vector::value_t ); cl.def("__setitem__", - [](Vector &v, SizeType i, const T &t) { - if (i >= v.size()) - throw index_error(); - v[i] = t; + [wrap_i](Vector &v, DiffType i, const T &t) { + i = wrap_i(i, v.size()); + v[(SizeType)i] = t; } ); @@ -186,7 +222,7 @@ void vector_modifiers(enable_if_t<is_copy_constructible<typename Vector::value_t if (!slice.compute(v.size(), &start, &stop, &step, &slicelength)) throw error_already_set(); - Vector *seq = new Vector(); + auto *seq = new Vector(); seq->reserve((size_t) slicelength); for (size_t i=0; i<slicelength; ++i) { @@ -217,10 +253,9 @@ void vector_modifiers(enable_if_t<is_copy_constructible<typename Vector::value_t ); cl.def("__delitem__", - [](Vector &v, SizeType i) { - if (i >= v.size()) - throw index_error(); - v.erase(v.begin() + DiffType(i)); + [wrap_i](Vector &v, DiffType i) { + i = wrap_i(i, v.size()); + v.erase(v.begin() + i); }, "Delete the list elements at index ``i``" ); @@ -256,13 +291,21 @@ template <typename Vector, typename Class_> void vector_accessor(enable_if_t<!vector_needs_copy<Vector>::value, Class_> &cl) { using T = typename Vector::value_type; using SizeType = typename Vector::size_type; + using DiffType = typename Vector::difference_type; using ItType = typename Vector::iterator; + auto wrap_i = [](DiffType i, SizeType n) { + if (i < 0) + i += n; + if (i < 0 || (SizeType)i >= n) + throw index_error(); + return i; + }; + cl.def("__getitem__", - [](Vector &v, SizeType i) -> T & { - if (i >= v.size()) - throw index_error(); - return v[i]; + [wrap_i](Vector &v, DiffType i) -> T & { + i = wrap_i(i, v.size()); + return v[(SizeType)i]; }, return_value_policy::reference_internal ); @@ -282,12 +325,15 @@ template <typename Vector, typename Class_> void vector_accessor(enable_if_t<vector_needs_copy<Vector>::value, Class_> &cl) { using T = typename Vector::value_type; using SizeType = typename Vector::size_type; + using DiffType = typename Vector::difference_type; using ItType = typename Vector::iterator; cl.def("__getitem__", - [](const Vector &v, SizeType i) -> T { - if (i >= v.size()) + [](const Vector &v, DiffType i) -> T { + if (i < 0 && (i += v.size()) < 0) throw index_error(); - return v[i]; + if ((SizeType)i >= v.size()) + throw index_error(); + return v[(SizeType)i]; } ); @@ -350,14 +396,19 @@ vector_buffer(Class_& cl) { if (!detail::compare_buffer_info<T>::compare(info) || (ssize_t) sizeof(T) != info.itemsize) throw type_error("Format mismatch (Python: " + info.format + " C++: " + format_descriptor<T>::format() + ")"); - auto vec = std::unique_ptr<Vector>(new Vector()); - vec->reserve((size_t) info.shape[0]); T *p = static_cast<T*>(info.ptr); ssize_t step = info.strides[0] / static_cast<ssize_t>(sizeof(T)); T *end = p + info.shape[0] * step; - for (; p != end; p += step) - vec->push_back(*p); - return vec.release(); + if (step == 1) { + return Vector(p, end); + } + else { + Vector vec; + vec.reserve((size_t) info.shape[0]); + for (; p != end; p += step) + vec.push_back(*p); + return vec; + } })); return; @@ -366,7 +417,7 @@ vector_buffer(Class_& cl) { template <typename Vector, typename Class_, typename... Args> enable_if_t<!detail::any_of<std::is_same<Args, buffer_protocol>...>::value> vector_buffer(Class_&) {} -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) @@ -464,7 +515,7 @@ class_<Vector, holder_type> bind_vector(handle scope, std::string const &name, A -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) template <typename, typename, typename... Args> void map_if_insertion_operator(const Args &...) { } @@ -472,7 +523,7 @@ template <typename, typename, typename... Args> void map_assignment(const Args & template <typename Map, typename Class_> -void map_assignment(enable_if_t<std::is_copy_assignable<typename Map::mapped_type>::value, Class_> &cl) { +void map_assignment(enable_if_t<is_copy_assignable<typename Map::mapped_type>::value, Class_> &cl) { using KeyType = typename Map::key_type; using MappedType = typename Map::mapped_type; @@ -488,7 +539,7 @@ void map_assignment(enable_if_t<std::is_copy_assignable<typename Map::mapped_typ template<typename Map, typename Class_> void map_assignment(enable_if_t< - !std::is_copy_assignable<typename Map::mapped_type>::value && + !is_copy_assignable<typename Map::mapped_type>::value && is_copy_constructible<typename Map::mapped_type>::value, Class_> &cl) { using KeyType = typename Map::key_type; @@ -530,7 +581,7 @@ template <typename Map, typename Class_> auto map_if_insertion_operator(Class_ & } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) template <typename Map, typename holder_type = std::unique_ptr<Map>, typename... Args> class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args&&... args) { @@ -580,6 +631,15 @@ class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args&&. return_value_policy::reference_internal ); + cl.def("__contains__", + [](Map &m, const KeyType &k) -> bool { + auto it = m.find(k); + if (it == m.end()) + return false; + return true; + } + ); + detail::map_assignment<Map, Class_>(cl); @@ -597,4 +657,4 @@ class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args&&. return cl; } -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/host/lib/deps/pybind11/remove_comments.py b/host/lib/deps/pybind11/remove_comments.py index 987c41e9d..1cc60ef35 100644 --- a/host/lib/deps/pybind11/remove_comments.py +++ b/host/lib/deps/pybind11/remove_comments.py @@ -34,9 +34,7 @@ def comment_remover(text): def remove_trailing_whitespace(text): """ Remove, uh, trailing whitespace. """ - pattern = re.compile(r' +$', - re.DOTALL | re.MULTILINE - ) + pattern = re.compile(r' +$', re.DOTALL | re.MULTILINE) return re.sub(pattern, '', text) |