From d0406c747e9eb79bf7db9473a0da1122600d1866 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 Dec 2021 23:11:48 -0500 Subject: [PATCH 1/6] [pre-commit.ci] pre-commit autoupdate (#3563) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v0.910-1 → v0.920](https://github.com/pre-commit/mirrors-mypy/compare/v0.910-1...v0.920) - [github.com/shellcheck-py/shellcheck-py: v0.8.0.1 → v0.8.0.2](https://github.com/shellcheck-py/shellcheck-py/compare/v0.8.0.1...v0.8.0.2) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4afbd53ef..972d2472b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -104,7 +104,7 @@ repos: # Check static types with mypy - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.910-1 + rev: v0.920 hooks: - id: mypy # Running per-file misbehaves a bit, so just run on all files, it's fast @@ -128,7 +128,7 @@ repos: args: ["-L", "nd,ot,thist"] - repo: https://github.com/shellcheck-py/shellcheck-py - rev: v0.8.0.1 + rev: v0.8.0.2 hooks: - id: shellcheck From b3d9c3543dcbebde30185c163208b8a00793ae45 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 21 Dec 2021 11:23:49 -0800 Subject: [PATCH 2/6] vi: replacing currently broken ICC Latest C++17 with C++14. (#3551) * Trivial change. * Trying ICC Latest C++14 instead of C++17. * Trying ICC Latest C++20 instead of C++17. * Settling on ICC Latest C++14 for now. * Undoing trivial change. * ci: try using SETUPTOOLS_USE_DISTUTILS=stdlib Co-authored-by: Henry Schreiner --- .github/workflows/ci.yml | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8e67baf1c..f256f0509 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -247,6 +247,8 @@ jobs: python -m pip install -r tests/requirements.txt - name: Configure + env: + SETUPTOOLS_USE_DISTUTILS: stdlib run: > cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug @@ -555,37 +557,37 @@ jobs: set +e; source /opt/intel/oneapi/setvars.sh; set -e cmake --build build-11 --target test_cmake_build - - name: Configure C++17 + - name: Configure C++14 run: | set +e; source /opt/intel/oneapi/setvars.sh; set -e - cmake -S . -B build-17 \ + cmake -S . -B build-14 \ -DPYBIND11_WERROR=ON \ -DDOWNLOAD_CATCH=ON \ -DDOWNLOAD_EIGEN=OFF \ - -DCMAKE_CXX_STANDARD=17 \ + -DCMAKE_CXX_STANDARD=14 \ -DCMAKE_CXX_COMPILER=$(which icpc) \ -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") - - name: Build C++17 + - name: Build C++14 run: | set +e; source /opt/intel/oneapi/setvars.sh; set -e - cmake --build build-17 -j 2 -v + cmake --build build-14 -j 2 -v - - name: Python tests C++17 + - name: Python tests C++14 run: | set +e; source /opt/intel/oneapi/setvars.sh; set -e sudo service apport stop - cmake --build build-17 --target check + cmake --build build-14 --target check - - name: C++ tests C++17 + - name: C++ tests C++14 run: | set +e; source /opt/intel/oneapi/setvars.sh; set -e - cmake --build build-17 --target cpptest + cmake --build build-14 --target cpptest - - name: Interface test C++17 + - name: Interface test C++14 run: | set +e; source /opt/intel/oneapi/setvars.sh; set -e - cmake --build build-17 --target test_cmake_build + cmake --build build-14 --target test_cmake_build # Testing on CentOS (manylinux uses a centos base, and this is an easy way From 39fbc7992b28188aac6bbcfa7d7814b217f54674 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 21 Dec 2021 14:24:21 -0500 Subject: [PATCH 3/6] fix: avoiding usage of _ if already defined (#3423) * fix: avoid usage of _ * ci: test _ defined * docs: include change in docs * fix: add a test and comment * refactor: const_str -> const_name --- .github/workflows/ci.yml | 4 ++- docs/advanced/cast/custom.rst | 2 +- docs/upgrade.rst | 4 +++ include/pybind11/cast.h | 30 +++++++++++----------- include/pybind11/chrono.h | 4 +-- include/pybind11/complex.h | 2 +- include/pybind11/detail/descr.h | 28 ++++++++++++-------- include/pybind11/detail/init.h | 2 +- include/pybind11/detail/type_caster_base.h | 2 +- include/pybind11/eigen.h | 20 +++++++-------- include/pybind11/functional.h | 4 +-- include/pybind11/numpy.h | 28 ++++++++++---------- include/pybind11/pybind11.h | 2 +- include/pybind11/pytypes.h | 2 +- include/pybind11/stl.h | 12 ++++----- include/pybind11/stl/filesystem.h | 2 +- tests/pybind11_tests.h | 2 +- tests/test_builtin_casters.cpp | 2 +- tests/test_copy_move.cpp | 6 ++--- tests/test_custom_type_casters.cpp | 11 +++++--- 20 files changed, 94 insertions(+), 75 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f256f0509..7176614de 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,6 +44,7 @@ jobs: python: '3.6' args: > -DPYBIND11_FINDPYTHON=ON + -DCMAKE_CXX_FLAGS="-D_=1" - runs-on: windows-latest python: '3.6' args: > @@ -68,7 +69,8 @@ jobs: python-version: ${{ matrix.python }} - name: Setup Boost (Linux) - if: runner.os == 'Linux' + # Can't use boost + define _ + if: runner.os == 'Linux' && matrix.python != '3.6' run: sudo apt-get install libboost-dev - name: Setup Boost (macOS) diff --git a/docs/advanced/cast/custom.rst b/docs/advanced/cast/custom.rst index 19b935347..1df4d3e14 100644 --- a/docs/advanced/cast/custom.rst +++ b/docs/advanced/cast/custom.rst @@ -46,7 +46,7 @@ type is explicitly allowed. * function signatures and declares a local variable * 'value' of type inty */ - PYBIND11_TYPE_CASTER(inty, _("inty")); + PYBIND11_TYPE_CASTER(inty, const_name("inty")); /** * Conversion part 1 (Python->C++): convert a PyObject into a inty diff --git a/docs/upgrade.rst b/docs/upgrade.rst index 69609ca28..d91d51e6f 100644 --- a/docs/upgrade.rst +++ b/docs/upgrade.rst @@ -17,6 +17,10 @@ v2.9 converted to using ``py::module_::import("types").attr("SimpleNamespace")`` instead. +* The use of ``_`` in custom type casters can now be replaced with the more + readable ``const_name`` instead. The old ``_`` shortcut has been retained + unless it is being used as a macro (like for gettext). + .. _upgrade-guide-2.7: diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 01dc5df73..7930fb994 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -208,7 +208,7 @@ public: return PyLong_FromUnsignedLongLong((unsigned long long) src); } - PYBIND11_TYPE_CASTER(T, _::value>("int", "float")); + PYBIND11_TYPE_CASTER(T, const_name::value>("int", "float")); }; template struct void_caster { @@ -221,7 +221,7 @@ public: static handle cast(T, return_value_policy /* policy */, handle /* parent */) { return none().inc_ref(); } - PYBIND11_TYPE_CASTER(T, _("None")); + PYBIND11_TYPE_CASTER(T, const_name("None")); }; template <> class type_caster : public void_caster {}; @@ -264,7 +264,7 @@ public: template using cast_op_type = void*&; explicit operator void *&() { return value; } - static constexpr auto name = _("capsule"); + static constexpr auto name = const_name("capsule"); private: void *value = nullptr; }; @@ -315,7 +315,7 @@ public: static handle cast(bool src, return_value_policy /* policy */, handle /* parent */) { return handle(src ? Py_True : Py_False).inc_ref(); } - PYBIND11_TYPE_CASTER(bool, _("bool")); + PYBIND11_TYPE_CASTER(bool, const_name("bool")); }; // Helper class for UTF-{8,16,32} C++ stl strings: @@ -405,7 +405,7 @@ template struct string_caster { return s; } - PYBIND11_TYPE_CASTER(StringType, _(PYBIND11_STRING_NAME)); + PYBIND11_TYPE_CASTER(StringType, const_name(PYBIND11_STRING_NAME)); private: static handle decode_utfN(const char *buffer, ssize_t nbytes) { @@ -542,7 +542,7 @@ public: return one_char; } - static constexpr auto name = _(PYBIND11_STRING_NAME); + static constexpr auto name = const_name(PYBIND11_STRING_NAME); template using cast_op_type = pybind11::detail::cast_op_type<_T>; }; @@ -579,7 +579,7 @@ public: return cast(*src, policy, parent); } - static constexpr auto name = _("Tuple[") + concat(make_caster::name...) + _("]"); + static constexpr auto name = const_name("Tuple[") + concat(make_caster::name...) + const_name("]"); template using cast_op_type = type; @@ -764,14 +764,14 @@ template struct is_holder_type : template struct is_holder_type> : std::true_type {}; -template struct handle_type_name { static constexpr auto name = _(); }; -template <> struct handle_type_name { static constexpr auto name = _(PYBIND11_BYTES_NAME); }; -template <> struct handle_type_name { static constexpr auto name = _("int"); }; -template <> struct handle_type_name { static constexpr auto name = _("Iterable"); }; -template <> struct handle_type_name { static constexpr auto name = _("Iterator"); }; -template <> struct handle_type_name { static constexpr auto name = _("None"); }; -template <> struct handle_type_name { static constexpr auto name = _("*args"); }; -template <> struct handle_type_name { static constexpr auto name = _("**kwargs"); }; +template struct handle_type_name { static constexpr auto name = const_name(); }; +template <> struct handle_type_name { static constexpr auto name = const_name(PYBIND11_BYTES_NAME); }; +template <> struct handle_type_name { static constexpr auto name = const_name("int"); }; +template <> struct handle_type_name { static constexpr auto name = const_name("Iterable"); }; +template <> struct handle_type_name { static constexpr auto name = const_name("Iterator"); }; +template <> struct handle_type_name { static constexpr auto name = const_name("None"); }; +template <> struct handle_type_name { static constexpr auto name = const_name("*args"); }; +template <> struct handle_type_name { static constexpr auto name = const_name("**kwargs"); }; template struct pyobject_caster { diff --git a/include/pybind11/chrono.h b/include/pybind11/chrono.h index 61bbcbc54..007cc17b4 100644 --- a/include/pybind11/chrono.h +++ b/include/pybind11/chrono.h @@ -97,7 +97,7 @@ public: return PyDelta_FromDSU(dd.count(), ss.count(), us.count()); } - PYBIND11_TYPE_CASTER(type, _("datetime.timedelta")); + PYBIND11_TYPE_CASTER(type, const_name("datetime.timedelta")); }; inline std::tm *localtime_thread_safe(const std::time_t *time, std::tm *buf) { @@ -195,7 +195,7 @@ public: localtime.tm_sec, us.count()); } - PYBIND11_TYPE_CASTER(type, _("datetime.datetime")); + PYBIND11_TYPE_CASTER(type, const_name("datetime.datetime")); }; // Other clocks that are not the system clock are not measured as datetime.datetime objects diff --git a/include/pybind11/complex.h b/include/pybind11/complex.h index f8327eb37..e1ecf4358 100644 --- a/include/pybind11/complex.h +++ b/include/pybind11/complex.h @@ -59,7 +59,7 @@ public: return PyComplex_FromDoubles((double) src.real(), (double) src.imag()); } - PYBIND11_TYPE_CASTER(std::complex, _("complex")); + PYBIND11_TYPE_CASTER(std::complex, const_name("complex")); }; PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/detail/descr.h b/include/pybind11/detail/descr.h index c62e541bd..ee72eccfe 100644 --- a/include/pybind11/detail/descr.h +++ b/include/pybind11/detail/descr.h @@ -53,9 +53,17 @@ constexpr descr operator+(const descr &a, c return plus_impl(a, b, make_index_sequence(), make_index_sequence()); } +template +constexpr descr const_name(char const(&text)[N]) { return descr(text); } +constexpr descr<0> const_name(char const(&)[1]) { return {}; } + +// The "_" might be defined as a macro - don't define it if so. +// Repeating the const_name code to avoid introducing a #define. +#ifndef _ template constexpr descr _(char const(&text)[N]) { return descr(text); } constexpr descr<0> _(char const(&)[1]) { return {}; } +#endif template struct int_to_str : int_to_str { }; template struct int_to_str<0, Digits...> { @@ -64,25 +72,25 @@ template struct int_to_str<0, Digits...> { // Ternary description (like std::conditional) template -constexpr enable_if_t> _(char const(&text1)[N1], char const(&)[N2]) { - return _(text1); +constexpr enable_if_t> const_name(char const(&text1)[N1], char const(&)[N2]) { + return const_name(text1); } template -constexpr enable_if_t> _(char const(&)[N1], char const(&text2)[N2]) { - return _(text2); +constexpr enable_if_t> const_name(char const(&)[N1], char const(&text2)[N2]) { + return const_name(text2); } template -constexpr enable_if_t _(const T1 &d, const T2 &) { return d; } +constexpr enable_if_t const_name(const T1 &d, const T2 &) { return d; } template -constexpr enable_if_t _(const T1 &, const T2 &d) { return d; } +constexpr enable_if_t const_name(const T1 &, const T2 &d) { return d; } template -auto constexpr _() -> remove_cv_t::digits)> { +auto constexpr const_name() -> remove_cv_t::digits)> { return int_to_str::digits; } -template constexpr descr<1, Type> _() { return {'%'}; } +template constexpr descr<1, Type> const_name() { return {'%'}; } constexpr descr<0> concat() { return {}; } @@ -92,12 +100,12 @@ constexpr descr concat(const descr &descr) { return descr; } template constexpr auto concat(const descr &d, const Args &...args) -> decltype(std::declval>() + concat(args...)) { - return d + _(", ") + concat(args...); + return d + const_name(", ") + concat(args...); } template constexpr descr type_descr(const descr &descr) { - return _("{") + descr + _("}"); + return const_name("{") + descr + const_name("}"); } PYBIND11_NAMESPACE_END(detail) diff --git a/include/pybind11/detail/init.h b/include/pybind11/detail/init.h index cace35296..eaaad5a07 100644 --- a/include/pybind11/detail/init.h +++ b/include/pybind11/detail/init.h @@ -24,7 +24,7 @@ public: template using cast_op_type = value_and_holder &; explicit operator value_and_holder &() { return *value; } - static constexpr auto name = _(); + static constexpr auto name = const_name(); private: value_and_holder *value = nullptr; diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 00ce1a7a1..48e218b2f 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -897,7 +897,7 @@ template class type_caster_base : public type_caster_generic { using itype = intrinsic_t; public: - static constexpr auto name = _(); + static constexpr auto name = const_name(); type_caster_base() : type_caster_base(typeid(type)) { } explicit type_caster_base(const std::type_info &info) : type_caster_generic(info) { } diff --git a/include/pybind11/eigen.h b/include/pybind11/eigen.h index 965d66e8a..696099fa6 100644 --- a/include/pybind11/eigen.h +++ b/include/pybind11/eigen.h @@ -192,20 +192,20 @@ template struct EigenProps { static constexpr bool show_f_contiguous = !show_c_contiguous && show_order && requires_col_major; static constexpr auto descriptor = - _("numpy.ndarray[") + npy_format_descriptor::name + - _("[") + _(_<(size_t) rows>(), _("m")) + - _(", ") + _(_<(size_t) cols>(), _("n")) + - _("]") + + const_name("numpy.ndarray[") + npy_format_descriptor::name + + const_name("[") + const_name(const_name<(size_t) rows>(), const_name("m")) + + const_name(", ") + const_name(const_name<(size_t) cols>(), const_name("n")) + + const_name("]") + // For a reference type (e.g. Ref) we have other constraints that might need to be // satisfied: writeable=True (for a mutable reference), and, depending on the map's stride // options, possibly f_contiguous or c_contiguous. We include them in the descriptor output // to provide some hint as to why a TypeError is occurring (otherwise it can be confusing to // see that a function accepts a 'numpy.ndarray[float64[3,2]]' and an error message that you // *gave* a numpy.ndarray of the right type and dimensions. - _(", flags.writeable", "") + - _(", flags.c_contiguous", "") + - _(", flags.f_contiguous", "") + - _("]"); + const_name(", flags.writeable", "") + + const_name(", flags.c_contiguous", "") + + const_name(", flags.f_contiguous", "") + + const_name("]"); }; // Casts an Eigen type to numpy array. If given a base, the numpy array references the src data, @@ -600,8 +600,8 @@ struct type_caster::value>> { ).release(); } - PYBIND11_TYPE_CASTER(Type, _<(Type::IsRowMajor) != 0>("scipy.sparse.csr_matrix[", "scipy.sparse.csc_matrix[") - + npy_format_descriptor::name + _("]")); + PYBIND11_TYPE_CASTER(Type, const_name<(Type::IsRowMajor) != 0>("scipy.sparse.csr_matrix[", "scipy.sparse.csc_matrix[") + + npy_format_descriptor::name + const_name("]")); }; PYBIND11_NAMESPACE_END(detail) diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index ad5608c25..7912aef17 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -113,8 +113,8 @@ public: return cpp_function(std::forward(f_), policy).release(); } - PYBIND11_TYPE_CASTER(type, _("Callable[[") + concat(make_caster::name...) + _("], ") - + make_caster::name + _("]")); + PYBIND11_TYPE_CASTER(type, const_name("Callable[[") + concat(make_caster::name...) + const_name("], ") + + make_caster::name + const_name("]")); }; PYBIND11_NAMESPACE_END(detail) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index b43a77168..8e83b506e 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -39,7 +39,7 @@ class array; // Forward declaration PYBIND11_NAMESPACE_BEGIN(detail) -template <> struct handle_type_name { static constexpr auto name = _("numpy.ndarray"); }; +template <> struct handle_type_name { static constexpr auto name = const_name("numpy.ndarray"); }; template struct npy_format_descriptor; @@ -290,7 +290,7 @@ template struct array_info_scalar { using type = T; static constexpr bool is_array = false; static constexpr bool is_empty = false; - static constexpr auto extents = _(""); + static constexpr auto extents = const_name(""); static void append_extents(list& /* shape */) { } }; // Computes underlying type and a comma-separated list of extents for array @@ -309,8 +309,8 @@ template struct array_info> { array_info::append_extents(shape); } - static constexpr auto extents = _::is_array>( - concat(_(), array_info::extents), _() + static constexpr auto extents = const_name::is_array>( + concat(const_name(), array_info::extents), const_name() ); }; // For numpy we have special handling for arrays of characters, so we don't include @@ -1021,7 +1021,7 @@ template struct format_descriptor::is_array>> { static std::string format() { using namespace detail; - static constexpr auto extents = _("(") + array_info::extents + _(")"); + static constexpr auto extents = const_name("(") + array_info::extents + const_name(")"); return extents.text + format_descriptor>::format(); } }; @@ -1056,28 +1056,28 @@ struct npy_format_descriptor_name; template struct npy_format_descriptor_name::value>> { - static constexpr auto name = _::value>( - _("bool"), _::value>("numpy.int", "numpy.uint") + _() + static constexpr auto name = const_name::value>( + const_name("bool"), const_name::value>("numpy.int", "numpy.uint") + const_name() ); }; template struct npy_format_descriptor_name::value>> { - static constexpr auto name = _::value + static constexpr auto name = const_name::value || std::is_same::value || std::is_same::value || std::is_same::value>( - _("numpy.float") + _(), _("numpy.longdouble") + const_name("numpy.float") + const_name(), const_name("numpy.longdouble") ); }; template struct npy_format_descriptor_name::value>> { - static constexpr auto name = _::value + static constexpr auto name = const_name::value || std::is_same::value || std::is_same::value || std::is_same::value>( - _("numpy.complex") + _(), _("numpy.longcomplex") + const_name("numpy.complex") + const_name(), const_name("numpy.longcomplex") ); }; @@ -1105,7 +1105,7 @@ public: }; #define PYBIND11_DECL_CHAR_FMT \ - static constexpr auto name = _("S") + _(); \ + static constexpr auto name = const_name("S") + const_name(); \ static pybind11::dtype dtype() { return pybind11::dtype(std::string("S") + std::to_string(N)); } template struct npy_format_descriptor { PYBIND11_DECL_CHAR_FMT }; template struct npy_format_descriptor> { PYBIND11_DECL_CHAR_FMT }; @@ -1117,7 +1117,7 @@ private: public: static_assert(!array_info::is_empty, "Zero-sized arrays are not supported"); - static constexpr auto name = _("(") + array_info::extents + _(")") + base_descr::name; + static constexpr auto name = const_name("(") + array_info::extents + const_name(")") + base_descr::name; static pybind11::dtype dtype() { list shape; array_info::append_extents(shape); @@ -1705,7 +1705,7 @@ vectorize_extractor(const Func &f, Return (*) (Args ...)) { } template struct handle_type_name> { - static constexpr auto name = _("numpy.ndarray[") + npy_format_descriptor::name + _("]"); + static constexpr auto name = const_name("numpy.ndarray[") + npy_format_descriptor::name + const_name("]"); }; PYBIND11_NAMESPACE_END(detail) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index cfe03f6db..8c7545ba3 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -262,7 +262,7 @@ protected: } /* Generate a readable signature describing the function's arguments and return value types */ - static constexpr auto signature = _("(") + cast_in::arg_names + _(") -> ") + cast_out::name; + static constexpr auto signature = const_name("(") + cast_in::arg_names + const_name(") -> ") + cast_out::name; PYBIND11_DESCR_CONSTEXPR auto types = decltype(signature)::types(); /* Register the function with Python from generic (non-templated) code */ diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index f803a05ca..902fb1f07 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -435,7 +435,7 @@ inline void raise_from(error_already_set& err, PyObject *type, const char *messa #endif -/** \defgroup python_builtins _ +/** \defgroup python_builtins const_name Unless stated otherwise, the following C++ functions behave the same as their Python counterparts. */ diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 3608d2989..430349482 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -78,7 +78,7 @@ template struct set_caster { return s.release(); } - PYBIND11_TYPE_CASTER(type, _("Set[") + key_conv::name + _("]")); + PYBIND11_TYPE_CASTER(type, const_name("Set[") + key_conv::name + const_name("]")); }; template struct map_caster { @@ -120,7 +120,7 @@ template struct map_caster { return d.release(); } - PYBIND11_TYPE_CASTER(Type, _("Dict[") + key_conv::name + _(", ") + value_conv::name + _("]")); + PYBIND11_TYPE_CASTER(Type, const_name("Dict[") + key_conv::name + const_name(", ") + value_conv::name + const_name("]")); }; template struct list_caster { @@ -166,7 +166,7 @@ public: return l.release(); } - PYBIND11_TYPE_CASTER(Type, _("List[") + value_conv::name + _("]")); + PYBIND11_TYPE_CASTER(Type, const_name("List[") + value_conv::name + const_name("]")); }; template struct type_caster> @@ -223,7 +223,7 @@ public: return l.release(); } - PYBIND11_TYPE_CASTER(ArrayType, _("List[") + value_conv::name + _(_(""), _("[") + _() + _("]")) + _("]")); + PYBIND11_TYPE_CASTER(ArrayType, const_name("List[") + value_conv::name + const_name(const_name(""), const_name("[") + const_name() + const_name("]")) + const_name("]")); }; template struct type_caster> @@ -273,7 +273,7 @@ template struct optio return true; } - PYBIND11_TYPE_CASTER(Type, _("Optional[") + value_conv::name + _("]")); + PYBIND11_TYPE_CASTER(Type, const_name("Optional[") + value_conv::name + const_name("]")); }; #if defined(PYBIND11_HAS_OPTIONAL) @@ -353,7 +353,7 @@ struct variant_caster> { } using Type = V; - PYBIND11_TYPE_CASTER(Type, _("Union[") + detail::concat(make_caster::name...) + _("]")); + PYBIND11_TYPE_CASTER(Type, const_name("Union[") + detail::concat(make_caster::name...) + const_name("]")); }; #if defined(PYBIND11_HAS_VARIANT) diff --git a/include/pybind11/stl/filesystem.h b/include/pybind11/stl/filesystem.h index 431b94b4f..a9a6c8512 100644 --- a/include/pybind11/stl/filesystem.h +++ b/include/pybind11/stl/filesystem.h @@ -92,7 +92,7 @@ public: return true; } - PYBIND11_TYPE_CASTER(T, _("os.PathLike")); + PYBIND11_TYPE_CASTER(T, const_name("os.PathLike")); }; template<> struct type_caster diff --git a/tests/pybind11_tests.h b/tests/pybind11_tests.h index 800ddda48..9b9992323 100644 --- a/tests/pybind11_tests.h +++ b/tests/pybind11_tests.h @@ -65,7 +65,7 @@ PYBIND11_NAMESPACE_BEGIN(pybind11) PYBIND11_NAMESPACE_BEGIN(detail) template<> class type_caster { public: - PYBIND11_TYPE_CASTER(RValueCaster, _("RValueCaster")); + PYBIND11_TYPE_CASTER(RValueCaster, const_name("RValueCaster")); static handle cast(RValueCaster &&, return_value_policy, handle) { return py::str("rvalue").release(); } static handle cast(const RValueCaster &, return_value_policy, handle) { return py::str("lvalue").release(); } }; diff --git a/tests/test_builtin_casters.cpp b/tests/test_builtin_casters.cpp index fe629e7c3..4a9f33837 100644 --- a/tests/test_builtin_casters.cpp +++ b/tests/test_builtin_casters.cpp @@ -19,7 +19,7 @@ PYBIND11_NAMESPACE_BEGIN(detail) template <> class type_caster { public: - static constexpr auto name = _(); + static constexpr auto name = const_name(); // Input is unimportant, a new value will always be constructed based on the // cast operator. diff --git a/tests/test_copy_move.cpp b/tests/test_copy_move.cpp index 5fb0dd810..4711a9482 100644 --- a/tests/test_copy_move.cpp +++ b/tests/test_copy_move.cpp @@ -85,13 +85,13 @@ public: PYBIND11_NAMESPACE_BEGIN(pybind11) PYBIND11_NAMESPACE_BEGIN(detail) template <> struct type_caster { - PYBIND11_TYPE_CASTER(MoveOnlyInt, _("MoveOnlyInt")); + PYBIND11_TYPE_CASTER(MoveOnlyInt, const_name("MoveOnlyInt")); bool load(handle src, bool) { value = MoveOnlyInt(src.cast()); return true; } static handle cast(const MoveOnlyInt &m, return_value_policy r, handle p) { return pybind11::cast(m.value, r, p); } }; template <> struct type_caster { - PYBIND11_TYPE_CASTER(MoveOrCopyInt, _("MoveOrCopyInt")); + PYBIND11_TYPE_CASTER(MoveOrCopyInt, const_name("MoveOrCopyInt")); bool load(handle src, bool) { value = MoveOrCopyInt(src.cast()); return true; } static handle cast(const MoveOrCopyInt &m, return_value_policy r, handle p) { return pybind11::cast(m.value, r, p); } }; @@ -100,7 +100,7 @@ template <> struct type_caster { protected: CopyOnlyInt value; public: - static constexpr auto name = _("CopyOnlyInt"); + static constexpr auto name = const_name("CopyOnlyInt"); bool load(handle src, bool) { value = CopyOnlyInt(src.cast()); return true; } static handle cast(const CopyOnlyInt &m, return_value_policy r, handle p) { return pybind11::cast(m.value, r, p); } static handle cast(const CopyOnlyInt *src, return_value_policy policy, handle parent) { diff --git a/tests/test_custom_type_casters.cpp b/tests/test_custom_type_casters.cpp index 076777d6f..8ad584de6 100644 --- a/tests/test_custom_type_casters.cpp +++ b/tests/test_custom_type_casters.cpp @@ -18,7 +18,12 @@ class ArgAlwaysConverts { }; namespace pybind11 { namespace detail { template <> struct type_caster { public: + // Classic +#ifndef _ PYBIND11_TYPE_CASTER(ArgInspector1, _("ArgInspector1")); +#else + PYBIND11_TYPE_CASTER(ArgInspector1, const_name("ArgInspector1")); +#endif bool load(handle src, bool convert) { value.arg = "loading ArgInspector1 argument " + @@ -33,7 +38,7 @@ public: }; template <> struct type_caster { public: - PYBIND11_TYPE_CASTER(ArgInspector2, _("ArgInspector2")); + PYBIND11_TYPE_CASTER(ArgInspector2, const_name("ArgInspector2")); bool load(handle src, bool convert) { value.arg = "loading ArgInspector2 argument " + @@ -48,7 +53,7 @@ public: }; template <> struct type_caster { public: - PYBIND11_TYPE_CASTER(ArgAlwaysConverts, _("ArgAlwaysConverts")); + PYBIND11_TYPE_CASTER(ArgAlwaysConverts, const_name("ArgAlwaysConverts")); bool load(handle, bool convert) { return convert; @@ -76,7 +81,7 @@ public: }; namespace pybind11 { namespace detail { template <> struct type_caster { - PYBIND11_TYPE_CASTER(DestructionTester, _("DestructionTester")); + PYBIND11_TYPE_CASTER(DestructionTester, const_name("DestructionTester")); bool load(handle, bool) { return true; } static handle cast(const DestructionTester &, return_value_policy, handle) { From e50f841de040a9455b2d76dc395c369869c89cdf Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 21 Dec 2021 15:42:34 -0500 Subject: [PATCH 4/6] fix: do not use LTS on mips64 and ppc64le (#3557) --- tools/pybind11Common.cmake | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index 69ac4b0f6..df2478121 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -318,13 +318,21 @@ function(_pybind11_generate_lto target prefer_thin_lto) set(cxx_append ";-fno-fat-lto-objects") endif() - if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND prefer_thin_lto) + if(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64le" OR CMAKE_SYSTEM_PROCESSOR MATCHES "mips64") + set(NO_FLTO_ARCH TRUE) + else() + set(NO_FLTO_ARCH FALSE) + endif() + + if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" + AND prefer_thin_lto + AND NOT NO_FLTO_ARCH) _pybind11_return_if_cxx_and_linker_flags_work( HAS_FLTO_THIN "-flto=thin${cxx_append}" "-flto=thin${linker_append}" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS) endif() - if(NOT HAS_FLTO_THIN) + if(NOT HAS_FLTO_THIN AND NOT NO_FLTO_ARCH) _pybind11_return_if_cxx_and_linker_flags_work( HAS_FLTO "-flto${cxx_append}" "-flto${linker_append}" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS) From d4b9f3471f465f0cc6d05556a837c26589b08b29 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 21 Dec 2021 16:28:23 -0500 Subject: [PATCH 5/6] docs: update changelog (#3556) * docs: update changelog * Update changelog.rst * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- docs/changelog.rst | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index dff8ec808..87fce2f2b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -10,6 +10,8 @@ Starting with version 1.8.0, pybind11 releases use a `semantic versioning IN DEVELOPMENT -------------- +This is the last version to support Python 2.7 and 3.5. + New Features: * Allow ``py::args`` to be followed by other arguments; the remaining arguments @@ -21,6 +23,16 @@ New Features: defining custom ``__setattr__`` and ``__getattr__`` methods. `#3387 `_ +Changes: + +* Make str/bytes/memoryview more interoperable with ``std::string_view``. + `#3521 `_ + +* Replace ``_`` with ``const_name`` in internals, avoid defining ``pybind::_`` + if ``_`` defined as macro (common gettext usage) + `#3423 `_ + + Bug fixes: * Fix a regression in 2.8.0 that caused undefined behavior (typically @@ -37,7 +49,8 @@ Bug fixes: * Add missing ``std::forward`` calls to some ``cpp_function`` overloads. `#3443 `_ -* Support PyPy 7.3.7 and the PyPy3.8 beta. Test python-3.11 on PRs with the ``python dev`` label. +* Support PyPy 7.3.7 and the PyPy3.8 beta. Test python-3.11 on PRs with the + ``python dev`` label. `#3419 `_ * Fix 2.8.0 regression with MSVC 2017 + C++17 mode + Python 3. @@ -59,6 +72,9 @@ Bug fixes: ``std::optional`` types. `#3376 `_ +* Tweaks to support Microsoft Visual Studio 2022. + `#3497 `_ + Build system improvements: * Nicer CMake printout and IDE organisation for pybind11's own tests. @@ -79,6 +95,12 @@ Build system improvements: overridden by ``.Pybind11Extension``. `#3436 `_ +* Ensure ThreadPool is closed in ``setup_helpers``. + `#3548 `_ + +* Avoid LTS on ``mips64`` and ``ppc64le`` (reported broken). + `#3557 `_ + v2.8.1 (Oct 27, 2021) --------------------- From cb302305a3d2697f8af17a14c5bf78f45359fb56 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 23 Dec 2021 14:50:10 -0500 Subject: [PATCH 6/6] fix: restore full range of _ functions (#3571) --- include/pybind11/detail/descr.h | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/include/pybind11/detail/descr.h b/include/pybind11/detail/descr.h index ee72eccfe..14f9223a5 100644 --- a/include/pybind11/detail/descr.h +++ b/include/pybind11/detail/descr.h @@ -57,14 +57,6 @@ template constexpr descr const_name(char const(&text)[N]) { return descr(text); } constexpr descr<0> const_name(char const(&)[1]) { return {}; } -// The "_" might be defined as a macro - don't define it if so. -// Repeating the const_name code to avoid introducing a #define. -#ifndef _ -template -constexpr descr _(char const(&text)[N]) { return descr(text); } -constexpr descr<0> _(char const(&)[1]) { return {}; } -#endif - template struct int_to_str : int_to_str { }; template struct int_to_str<0, Digits...> { static constexpr auto digits = descr(('0' + Digits)...); @@ -92,6 +84,31 @@ auto constexpr const_name() -> remove_cv_t constexpr descr<1, Type> const_name() { return {'%'}; } +// The "_" might be defined as a macro - don't define it if so. +// Repeating the const_name code to avoid introducing a #define. +#ifndef _ +template +constexpr descr _(char const(&text)[N]) { return const_name(text); } +template +constexpr enable_if_t> _(char const(&text1)[N1], char const(&text2)[N2]) { + return const_name(text1, text2); +} +template +constexpr enable_if_t> _(char const(&text1)[N1], char const(&text2)[N2]) { + return const_name(text1, text2); +} +template +constexpr enable_if_t _(const T1 &d1, const T2 &d2) { return const_name(d1, d2); } +template +constexpr enable_if_t _(const T1 &d1, const T2 &d2) { return const_name(d1, d2); } + +template +auto constexpr _() -> remove_cv_t::digits)> { + return const_name(); +} +template constexpr descr<1, Type> _() { return const_name(); } +#endif + constexpr descr<0> concat() { return {}; } template