Merge branch 'master' into sh_merge_master_clean

This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-12-29 13:24:20 -08:00
commit 388c03b18c
29 changed files with 281 additions and 132 deletions

View File

@ -45,6 +45,7 @@ jobs:
python: '3.6'
args: >
-DPYBIND11_FINDPYTHON=ON
-DCMAKE_CXX_FLAGS="-D_=1"
- runs-on: windows-latest
python: '3.6'
args: >
@ -69,7 +70,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)
@ -248,6 +250,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
@ -556,37 +560,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

View File

@ -15,7 +15,7 @@
repos:
# Standard hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.1.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
@ -45,7 +45,7 @@ repos:
# Black, the code formatter, natively supports pre-commit
- repo: https://github.com/psf/black
rev: 21.11b1 # Keep in sync with blacken-docs
rev: 21.12b0 # Keep in sync with blacken-docs
hooks:
- id: black
@ -54,7 +54,7 @@ repos:
hooks:
- id: blacken-docs
additional_dependencies:
- black==21.11b1 # keep in sync with black hook
- black==21.12b0 # keep in sync with black hook
# Changes tabs to spaces
- repo: https://github.com/Lucas-C/pre-commit-hooks
@ -106,7 +106,7 @@ repos:
# Check static types with mypy
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.910-1
rev: v0.930
hooks:
- id: mypy
# Running per-file misbehaves a bit, so just run on all files, it's fast
@ -130,7 +130,7 @@ repos:
args: ["-L", "nd,ot,thist", "--exclude-file", ".codespell-ignorelines"]
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.8.0.1
rev: v0.8.0.3
hooks:
- id: shellcheck

View File

@ -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

View File

@ -7,8 +7,10 @@ Starting with version 1.8.0, pybind11 releases use a `semantic versioning
<http://semver.org>`_ policy.
IN DEVELOPMENT
--------------
Version 2.9.0 (Dec 28, 2021)
----------------------------
This is the last version to support Python 2.7 and 3.5.
New Features:
@ -17,17 +19,18 @@ New Features:
used.
`#3402 <https://github.com/pybind/pybind11/pull/3402>`_
* Add C++ Exception type to throw and catch ``AttributeError``. Useful for
defining custom ``__setattr__`` and ``__getattr__`` methods.
`#3387 <https://github.com/pybind/pybind11/pull/3387>`_
Changes:
* Make str/bytes/memoryview more interoperable with ``std::string_view``.
`#3521 <https://github.com/pybind/pybind11/pull/3521>`_
* Replace ``_`` with ``const_name`` in internals, avoid defining ``pybind::_``
if ``_`` defined as macro (common gettext usage)
`#3423 <https://github.com/pybind/pybind11/pull/3423>`_
Bug fixes:
* Fix a regression in 2.8.0 that caused undefined behavior (typically
segfaults) in ``make_key_iterator``/``make_value_iterator`` if dereferencing
the iterator returned a temporary value instead of a reference.
`#3348 <https://github.com/pybind/pybind11/pull/3348>`_
* Fix a rare warning about extra copy in an Eigen constructor.
`#3486 <https://github.com/pybind/pybind11/pull/3486>`_
@ -37,27 +40,16 @@ Bug fixes:
* Add missing ``std::forward`` calls to some ``cpp_function`` overloads.
`#3443 <https://github.com/pybind/pybind11/pull/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 <https://github.com/pybind/pybind11/pull/3419>`_
* Fix 2.8.0 regression with MSVC 2017 + C++17 mode + Python 3.
`#3407 <https://github.com/pybind/pybind11/pull/3407>`_
* Modernize usage of ``PyCodeObject`` on Python 3.9 (toward supporting Python
3.11a1)
`#3368 <https://github.com/pybind/pybind11/pull/3368>`_
* A long-standing bug in eigen.h was fixed (originally PR #3343). The bug was
unmasked by newly added ``static_assert``'s in the Eigen 3.4.0 release.
`#3352 <https://github.com/pybind/pybind11/pull/3352>`_
* Replace usage of deprecated ``Eigen::MappedSparseMatrix`` with
``Eigen::Map<Eigen::SparseMatrix<...>>`` for Eigen 3.3+.
`#3499 <https://github.com/pybind/pybind11/pull/3499>`_
* Fixed the potential for dangling references when using properties with
``std::optional`` types.
`#3376 <https://github.com/pybind/pybind11/pull/3376>`_
* Tweaks to support Microsoft Visual Studio 2022.
`#3497 <https://github.com/pybind/pybind11/pull/3497>`_
Build system improvements:
@ -68,17 +60,16 @@ Build system improvements:
space in the package status message.
`#3472 <https://github.com/pybind/pybind11/pull/3472>`_
* Support multiple raw inclusion of CMake helper files (Conan.io does this for
multi-config generators).
`#3420 <https://github.com/pybind/pybind11/pull/3420>`_
* Fix harmless warning on CMake 3.22.
`#3368 <https://github.com/pybind/pybind11/pull/3368>`_
* Flags starting with ``-g`` in ``$CFLAGS`` and ``$CPPFLAGS`` are no longer
overridden by ``.Pybind11Extension``.
`#3436 <https://github.com/pybind/pybind11/pull/3436>`_
* Ensure ThreadPool is closed in ``setup_helpers``.
`#3548 <https://github.com/pybind/pybind11/pull/3548>`_
* Avoid LTS on ``mips64`` and ``ppc64le`` (reported broken).
`#3557 <https://github.com/pybind/pybind11/pull/3557>`_
v2.8.1 (Oct 27, 2021)
---------------------

View File

@ -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:

View File

@ -230,7 +230,7 @@ public:
return PyLong_FromUnsignedLongLong((unsigned long long) src);
}
PYBIND11_TYPE_CASTER(T, _<std::is_integral<T>::value>("int", "float"));
PYBIND11_TYPE_CASTER(T, const_name<std::is_integral<T>::value>("int", "float"));
};
template<typename T> struct void_caster {
@ -243,7 +243,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<void_type> : public void_caster<void_type> {};
@ -286,7 +286,7 @@ public:
template <typename T> 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;
};
@ -337,7 +337,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:
@ -427,7 +427,7 @@ template <typename StringType, bool IsView = false> 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) {
@ -564,7 +564,7 @@ public:
return one_char;
}
static constexpr auto name = _(PYBIND11_STRING_NAME);
static constexpr auto name = const_name(PYBIND11_STRING_NAME);
template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
};
@ -601,7 +601,7 @@ public:
return cast(*src, policy, parent);
}
static constexpr auto name = _("Tuple[") + concat(make_caster<Ts>::name...) + _("]");
static constexpr auto name = const_name("Tuple[") + concat(make_caster<Ts>::name...) + const_name("]");
template <typename T> using cast_op_type = type;
@ -790,14 +790,14 @@ template <typename base, typename holder> struct is_holder_type :
template <typename base, typename deleter> struct is_holder_type<base, std::unique_ptr<base, deleter>> :
std::true_type {};
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"); };
template <typename T> struct handle_type_name { static constexpr auto name = const_name<T>(); };
template <> struct handle_type_name<bytes> { static constexpr auto name = const_name(PYBIND11_BYTES_NAME); };
template <> struct handle_type_name<int_> { static constexpr auto name = const_name("int"); };
template <> struct handle_type_name<iterable> { static constexpr auto name = const_name("Iterable"); };
template <> struct handle_type_name<iterator> { static constexpr auto name = const_name("Iterator"); };
template <> struct handle_type_name<none> { static constexpr auto name = const_name("None"); };
template <> struct handle_type_name<args> { static constexpr auto name = const_name("*args"); };
template <> struct handle_type_name<kwargs> { static constexpr auto name = const_name("**kwargs"); };
template <typename type>
struct pyobject_caster {

View File

@ -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

View File

@ -59,7 +59,7 @@ public:
return PyComplex_FromDoubles((double) src.real(), (double) src.imag());
}
PYBIND11_TYPE_CASTER(std::complex<T>, _("complex"));
PYBIND11_TYPE_CASTER(std::complex<T>, const_name("complex"));
};
PYBIND11_NAMESPACE_END(detail)
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

View File

@ -11,11 +11,11 @@
#define PYBIND11_VERSION_MAJOR 2
#define PYBIND11_VERSION_MINOR 9
#define PYBIND11_VERSION_PATCH 0.dev1
#define PYBIND11_VERSION_PATCH 0
// Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html
// Additional convention: 0xD = dev
#define PYBIND11_VERSION_HEX 0x020900D1
#define PYBIND11_VERSION_HEX 0x02090000
#define PYBIND11_NAMESPACE_BEGIN(name) namespace name {
#define PYBIND11_NAMESPACE_END(name) }

View File

@ -54,35 +54,64 @@ constexpr descr<N1 + N2, Ts1..., Ts2...> operator+(const descr<N1, Ts1...> &a, c
}
template <size_t N>
constexpr descr<N - 1> _(char const(&text)[N]) { return descr<N - 1>(text); }
constexpr descr<0> _(char const(&)[1]) { return {}; }
constexpr descr<N - 1> const_name(char const(&text)[N]) { return descr<N - 1>(text); }
constexpr descr<0> const_name(char const(&)[1]) { return {}; }
template <size_t Rem, size_t... Digits> struct int_to_str : int_to_str<Rem/10, Rem%10, Digits...> { };
template <size_t...Digits> struct int_to_str<0, Digits...> {
// WARNING: This only works with C++17 or higher.
static constexpr auto digits = descr<sizeof...(Digits)>(('0' + Digits)...);
};
// Ternary description (like std::conditional)
template <bool B, size_t N1, size_t N2>
constexpr enable_if_t<B, descr<N1 - 1>> _(char const(&text1)[N1], char const(&)[N2]) {
return _(text1);
constexpr enable_if_t<B, descr<N1 - 1>> const_name(char const(&text1)[N1], char const(&)[N2]) {
return const_name(text1);
}
template <bool B, size_t N1, size_t N2>
constexpr enable_if_t<!B, descr<N2 - 1>> _(char const(&)[N1], char const(&text2)[N2]) {
return _(text2);
constexpr enable_if_t<!B, descr<N2 - 1>> const_name(char const(&)[N1], char const(&text2)[N2]) {
return const_name(text2);
}
template <bool B, typename T1, typename T2>
constexpr enable_if_t<B, T1> _(const T1 &d, const T2 &) { return d; }
constexpr enable_if_t<B, T1> const_name(const T1 &d, const T2 &) { return d; }
template <bool B, typename T1, typename T2>
constexpr enable_if_t<!B, T2> _(const T1 &, const T2 &d) { return d; }
constexpr enable_if_t<!B, T2> const_name(const T1 &, const T2 &d) { return d; }
template <size_t Size>
auto constexpr _() -> remove_cv_t<decltype(int_to_str<Size / 10, Size % 10>::digits)> {
auto constexpr const_name() -> remove_cv_t<decltype(int_to_str<Size / 10, Size % 10>::digits)> {
return int_to_str<Size / 10, Size % 10>::digits;
}
template <typename Type> constexpr descr<1, Type> _() { return {'%'}; }
template <typename Type> constexpr descr<1, Type> const_name() { return {'%'}; }
// If "_" is defined as a macro, py::detail::_ cannot be provided.
// It is therefore best to use py::detail::const_name universally.
// This block is for backward compatibility only.
// (The const_name code is repeated to avoid introducing a "_" #define ourselves.)
#ifndef _
#define PYBIND11_DETAIL_UNDERSCORE_BACKWARD_COMPATIBILITY
template <size_t N>
constexpr descr<N-1> _(char const(&text)[N]) { return const_name<N>(text); }
template <bool B, size_t N1, size_t N2>
constexpr enable_if_t<B, descr<N1 - 1>> _(char const(&text1)[N1], char const(&text2)[N2]) {
return const_name<B,N1,N2>(text1, text2);
}
template <bool B, size_t N1, size_t N2>
constexpr enable_if_t<!B, descr<N2 - 1>> _(char const(&text1)[N1], char const(&text2)[N2]) {
return const_name<B,N1,N2>(text1, text2);
}
template <bool B, typename T1, typename T2>
constexpr enable_if_t<B, T1> _(const T1 &d1, const T2 &d2) { return const_name<B,T1,T2>(d1, d2); }
template <bool B, typename T1, typename T2>
constexpr enable_if_t<!B, T2> _(const T1 &d1, const T2 &d2) { return const_name<B,T1,T2>(d1, d2); }
template <size_t Size>
auto constexpr _() -> remove_cv_t<decltype(int_to_str<Size / 10, Size % 10>::digits)> {
return const_name<Size>();
}
template <typename Type> constexpr descr<1, Type> _() { return const_name<Type>(); }
#endif // #ifndef _
constexpr descr<0> concat() { return {}; }
@ -92,12 +121,12 @@ constexpr descr<N, Ts...> concat(const descr<N, Ts...> &descr) { return descr; }
template <size_t N, typename... Ts, typename... Args>
constexpr auto concat(const descr<N, Ts...> &d, const Args &...args)
-> decltype(std::declval<descr<N + 2, Ts...>>() + concat(args...)) {
return d + _(", ") + concat(args...);
return d + const_name(", ") + concat(args...);
}
template <size_t N, typename... Ts>
constexpr descr<N + 2, Ts...> type_descr(const descr<N, Ts...> &descr) {
return _("{") + descr + _("}");
return const_name("{") + descr + const_name("}");
}
PYBIND11_NAMESPACE_END(detail)

View File

@ -26,7 +26,7 @@ public:
template <typename> using cast_op_type = value_and_holder &;
explicit operator value_and_holder &() { return *value; }
static constexpr auto name = _<value_and_holder>();
static constexpr auto name = const_name<value_and_holder>();
private:
value_and_holder *value = nullptr;

View File

@ -897,7 +897,7 @@ template <typename type> class type_caster_base : public type_caster_generic {
using itype = intrinsic_t<type>;
public:
static constexpr auto name = _<type>();
static constexpr auto name = const_name<type>();
type_caster_base() : type_caster_base(typeid(type)) { }
explicit type_caster_base(const std::type_info &info) : type_caster_generic(info) { }

View File

@ -192,20 +192,20 @@ template <typename Type_> 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<Scalar>::name +
_("[") + _<fixed_rows>(_<(size_t) rows>(), _("m")) +
_(", ") + _<fixed_cols>(_<(size_t) cols>(), _("n")) +
_("]") +
const_name("numpy.ndarray[") + npy_format_descriptor<Scalar>::name +
const_name("[") + const_name<fixed_rows>(const_name<(size_t) rows>(), const_name("m")) +
const_name(", ") + const_name<fixed_cols>(const_name<(size_t) cols>(), const_name("n")) +
const_name("]") +
// For a reference type (e.g. Ref<MatrixXd>) 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.
_<show_writeable>(", flags.writeable", "") +
_<show_c_contiguous>(", flags.c_contiguous", "") +
_<show_f_contiguous>(", flags.f_contiguous", "") +
_("]");
const_name<show_writeable>(", flags.writeable", "") +
const_name<show_c_contiguous>(", flags.c_contiguous", "") +
const_name<show_f_contiguous>(", 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<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
).release();
}
PYBIND11_TYPE_CASTER(Type, _<(Type::IsRowMajor) != 0>("scipy.sparse.csr_matrix[", "scipy.sparse.csc_matrix[")
+ npy_format_descriptor<Scalar>::name + _("]"));
PYBIND11_TYPE_CASTER(Type, const_name<(Type::IsRowMajor) != 0>("scipy.sparse.csr_matrix[", "scipy.sparse.csc_matrix[")
+ npy_format_descriptor<Scalar>::name + const_name("]"));
};
PYBIND11_NAMESPACE_END(detail)

View File

@ -113,8 +113,8 @@ public:
return cpp_function(std::forward<Func>(f_), policy).release();
}
PYBIND11_TYPE_CASTER(type, _("Callable[[") + concat(make_caster<Args>::name...) + _("], ")
+ make_caster<retval_type>::name + _("]"));
PYBIND11_TYPE_CASTER(type, const_name("Callable[[") + concat(make_caster<Args>::name...) + const_name("], ")
+ make_caster<retval_type>::name + const_name("]"));
};
PYBIND11_NAMESPACE_END(detail)

View File

@ -39,7 +39,7 @@ class array; // Forward declaration
PYBIND11_NAMESPACE_BEGIN(detail)
template <> struct handle_type_name<array> { static constexpr auto name = _("numpy.ndarray"); };
template <> struct handle_type_name<array> { static constexpr auto name = const_name("numpy.ndarray"); };
template <typename type, typename SFINAE = void> struct npy_format_descriptor;
@ -290,7 +290,7 @@ template <typename T> 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 <typename T, size_t N> struct array_info<std::array<T, N>> {
array_info<T>::append_extents(shape);
}
static constexpr auto extents = _<array_info<T>::is_array>(
concat(_<N>(), array_info<T>::extents), _<N>()
static constexpr auto extents = const_name<array_info<T>::is_array>(
concat(const_name<N>(), array_info<T>::extents), const_name<N>()
);
};
// For numpy we have special handling for arrays of characters, so we don't include
@ -1021,7 +1021,7 @@ template <typename T>
struct format_descriptor<T, detail::enable_if_t<detail::array_info<T>::is_array>> {
static std::string format() {
using namespace detail;
static constexpr auto extents = _("(") + array_info<T>::extents + _(")");
static constexpr auto extents = const_name("(") + array_info<T>::extents + const_name(")");
return extents.text + format_descriptor<remove_all_extents_t<T>>::format();
}
};
@ -1056,28 +1056,28 @@ 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>("numpy.int", "numpy.uint") + _<sizeof(T)*8>()
static constexpr auto name = const_name<std::is_same<T, bool>::value>(
const_name("bool"), const_name<std::is_signed<T>::value>("numpy.int", "numpy.uint") + const_name<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
static constexpr auto name = const_name<std::is_same<T, float>::value
|| std::is_same<T, const float>::value
|| std::is_same<T, double>::value
|| std::is_same<T, const double>::value>(
_("numpy.float") + _<sizeof(T)*8>(), _("numpy.longdouble")
const_name("numpy.float") + const_name<sizeof(T)*8>(), const_name("numpy.longdouble")
);
};
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
static constexpr auto name = const_name<std::is_same<typename T::value_type, float>::value
|| std::is_same<typename T::value_type, const float>::value
|| std::is_same<typename T::value_type, double>::value
|| std::is_same<typename T::value_type, const double>::value>(
_("numpy.complex") + _<sizeof(typename T::value_type)*16>(), _("numpy.longcomplex")
const_name("numpy.complex") + const_name<sizeof(typename T::value_type)*16>(), const_name("numpy.longcomplex")
);
};
@ -1105,7 +1105,7 @@ public:
};
#define PYBIND11_DECL_CHAR_FMT \
static constexpr auto name = _("S") + _<N>(); \
static constexpr auto name = const_name("S") + const_name<N>(); \
static pybind11::dtype dtype() { return pybind11::dtype(std::string("S") + std::to_string(N)); }
template <size_t N> struct npy_format_descriptor<char[N]> { PYBIND11_DECL_CHAR_FMT };
template <size_t N> struct npy_format_descriptor<std::array<char, N>> { PYBIND11_DECL_CHAR_FMT };
@ -1117,7 +1117,7 @@ private:
public:
static_assert(!array_info<T>::is_empty, "Zero-sized arrays are not supported");
static constexpr auto name = _("(") + array_info<T>::extents + _(")") + base_descr::name;
static constexpr auto name = const_name("(") + array_info<T>::extents + const_name(")") + base_descr::name;
static pybind11::dtype dtype() {
list shape;
array_info<T>::append_extents(shape);
@ -1705,7 +1705,7 @@ vectorize_extractor(const Func &f, Return (*) (Args ...)) {
}
template <typename T, int Flags> struct handle_type_name<array_t<T, Flags>> {
static constexpr auto name = _("numpy.ndarray[") + npy_format_descriptor<T>::name + _("]");
static constexpr auto name = const_name("numpy.ndarray[") + npy_format_descriptor<T>::name + const_name("]");
};
PYBIND11_NAMESPACE_END(detail)

View File

@ -264,7 +264,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 */

View File

@ -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.
*/

View File

@ -78,7 +78,7 @@ template <typename Type, typename Key> 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 <typename Type, typename Key, typename Value> struct map_caster {
@ -120,7 +120,7 @@ template <typename Type, typename Key, typename Value> 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 <typename Type, typename Value> 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 <typename Type, typename Alloc> struct type_caster<std::vector<Type, Alloc>>
@ -223,7 +223,7 @@ public:
return l.release();
}
PYBIND11_TYPE_CASTER(ArrayType, _("List[") + value_conv::name + _<Resizable>(_(""), _("[") + _<Size>() + _("]")) + _("]"));
PYBIND11_TYPE_CASTER(ArrayType, const_name("List[") + value_conv::name + const_name<Resizable>(const_name(""), const_name("[") + const_name<Size>() + const_name("]")) + const_name("]"));
};
template <typename Type, size_t Size> struct type_caster<std::array<Type, Size>>
@ -273,7 +273,7 @@ template<typename Type, typename Value = typename Type::value_type> 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<V<Ts...>> {
}
using Type = V<Ts...>;
PYBIND11_TYPE_CASTER(Type, _("Union[") + detail::concat(make_caster<Ts>::name...) + _("]"));
PYBIND11_TYPE_CASTER(Type, const_name("Union[") + detail::concat(make_caster<Ts>::name...) + const_name("]"));
};
#if defined(PYBIND11_HAS_VARIANT)

View File

@ -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<std::filesystem::path>

View File

@ -8,5 +8,5 @@ def _to_int(s):
return s
__version__ = "2.9.0.dev1"
__version__ = "2.9.0"
version_info = tuple(_to_int(s) for s in __version__.split("."))

View File

@ -466,8 +466,14 @@ class ParallelCompile(object):
threads = 1
if threads > 1:
for _ in ThreadPool(threads).imap_unordered(_single_compile, objects):
pass
pool = ThreadPool(threads)
# In Python 2, ThreadPool can't be used as a context manager.
# Once we are no longer supporting it, this can be 'with pool:'
try:
for _ in pool.imap_unordered(_single_compile, objects):
pass
finally:
pool.terminate()
else:
for ob in objects:
_single_compile(ob)

View File

@ -115,6 +115,7 @@ set(PYBIND11_TEST_FILES
test_class_sh_unique_ptr_member.cpp
test_class_sh_virtual_py_cpp_mix.cpp
test_classh_mock.cpp
test_const_name.cpp
test_constants_and_functions.cpp
test_copy_move.cpp
test_custom_type_casters.cpp

View File

@ -65,7 +65,7 @@ PYBIND11_NAMESPACE_BEGIN(pybind11)
PYBIND11_NAMESPACE_BEGIN(detail)
template<> class type_caster<RValueCaster> {
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(); }
};

View File

@ -19,7 +19,7 @@ PYBIND11_NAMESPACE_BEGIN(detail)
template <>
class type_caster<ConstRefCasted> {
public:
static constexpr auto name = _<ConstRefCasted>();
static constexpr auto name = const_name<ConstRefCasted>();
// Input is unimportant, a new value will always be constructed based on the
// cast operator.

70
tests/test_const_name.cpp Normal file
View File

@ -0,0 +1,70 @@
// Copyright (c) 2021 The Pybind Development Team.
// All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "pybind11_tests.h"
#if defined(_MSC_VER) && _MSC_VER < 1910
// MSVC 2015 fails in bizarre ways.
# define PYBIND11_SKIP_TEST_CONST_NAME
#else // Only test with MSVC 2017 or newer.
// IUT = Implementation Under Test
# define CONST_NAME_TESTS(TEST_FUNC, IUT) \
std::string TEST_FUNC(int selector) { \
switch (selector) { \
case 0: \
return IUT("").text; \
case 1: \
return IUT("A").text; \
case 2: \
return IUT("Bd").text; \
case 3: \
return IUT("Cef").text; \
case 4: \
return IUT<int>().text; /*NOLINT(bugprone-macro-parentheses)*/ \
case 5: \
return IUT<std::string>().text; /*NOLINT(bugprone-macro-parentheses)*/ \
case 6: \
return IUT<true>("T1", "T2").text; /*NOLINT(bugprone-macro-parentheses)*/ \
case 7: \
return IUT<false>("U1", "U2").text; /*NOLINT(bugprone-macro-parentheses)*/ \
case 8: \
/*NOLINTNEXTLINE(bugprone-macro-parentheses)*/ \
return IUT<true>(IUT("D1"), IUT("D2")).text; \
case 9: \
/*NOLINTNEXTLINE(bugprone-macro-parentheses)*/ \
return IUT<false>(IUT("E1"), IUT("E2")).text; \
case 10: \
return IUT("KeepAtEnd").text; \
default: \
break; \
} \
throw std::runtime_error("Invalid selector value."); \
}
CONST_NAME_TESTS(const_name_tests, py::detail::const_name)
# ifdef PYBIND11_DETAIL_UNDERSCORE_BACKWARD_COMPATIBILITY
CONST_NAME_TESTS(underscore_tests, py::detail::_)
# endif
#endif // MSVC >= 2017
TEST_SUBMODULE(const_name, m) {
#ifdef PYBIND11_SKIP_TEST_CONST_NAME
m.attr("const_name_tests") = "PYBIND11_SKIP_TEST_CONST_NAME";
#else
m.def("const_name_tests", const_name_tests);
#endif
#ifdef PYBIND11_SKIP_TEST_CONST_NAME
m.attr("underscore_tests") = "PYBIND11_SKIP_TEST_CONST_NAME";
#elif defined(PYBIND11_DETAIL_UNDERSCORE_BACKWARD_COMPATIBILITY)
m.def("underscore_tests", underscore_tests);
#else
m.attr("underscore_tests") = "PYBIND11_DETAIL_UNDERSCORE_BACKWARD_COMPATIBILITY not defined.";
#endif
}

31
tests/test_const_name.py Normal file
View File

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
import pytest
import env
from pybind11_tests import const_name as m
@pytest.mark.parametrize("func", (m.const_name_tests, m.underscore_tests))
@pytest.mark.parametrize(
"selector, expected",
enumerate(
(
"",
"A",
"Bd",
"Cef",
"%",
"%",
"T1",
"U2",
"D1",
"E2",
"KeepAtEnd",
)
),
)
def test_const_name(func, selector, expected):
if isinstance(func, type(u"") if env.PY2 else str):
pytest.skip(func)
text = func(selector)
assert text == expected

View File

@ -85,13 +85,13 @@ public:
PYBIND11_NAMESPACE_BEGIN(pybind11)
PYBIND11_NAMESPACE_BEGIN(detail)
template <> struct type_caster<MoveOnlyInt> {
PYBIND11_TYPE_CASTER(MoveOnlyInt, _("MoveOnlyInt"));
PYBIND11_TYPE_CASTER(MoveOnlyInt, const_name("MoveOnlyInt"));
bool load(handle src, bool) { value = MoveOnlyInt(src.cast<int>()); 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<MoveOrCopyInt> {
PYBIND11_TYPE_CASTER(MoveOrCopyInt, _("MoveOrCopyInt"));
PYBIND11_TYPE_CASTER(MoveOrCopyInt, const_name("MoveOrCopyInt"));
bool load(handle src, bool) { value = MoveOrCopyInt(src.cast<int>()); 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<CopyOnlyInt> {
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<int>()); 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) {

View File

@ -18,7 +18,12 @@ class ArgAlwaysConverts { };
namespace pybind11 { namespace detail {
template <> struct type_caster<ArgInspector1> {
public:
// Classic
#ifdef PYBIND11_DETAIL_UNDERSCORE_BACKWARD_COMPATIBILITY
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<ArgInspector2> {
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<ArgAlwaysConverts> {
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<DestructionTester> {
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) {

View File

@ -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)