Skip to content

[smart_holder] git merge master #4227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
9c04c7b
chore: Delete copy ctor/assign for GIL RAIIs (#4183)
Skylion007 Sep 19, 2022
d02f219
[pre-commit.ci] pre-commit autoupdate (#4189)
pre-commit-ci[bot] Sep 20, 2022
424ac4f
fix: Windows compiler, missing object initializer (#4188)
jiwaszki Sep 20, 2022
95d0e71
test C++14 on MSVC (#4191)
Skylion007 Sep 21, 2022
f743bdf
Avoid local_internals destruction (#4192)
bogdan-lab Sep 21, 2022
5aa0fad
perf: call reserve method in set and map casters (#4194)
Skylion007 Sep 25, 2022
da8c730
[pre-commit.ci] pre-commit autoupdate (#4197)
pre-commit-ci[bot] Sep 27, 2022
c78dfe6
bugfix: Add error checking to list append and insert (#4208)
Skylion007 Oct 3, 2022
600d697
[pre-commit.ci] pre-commit autoupdate (#4210)
pre-commit-ci[bot] Oct 4, 2022
8275b76
ci: update pre-commit schedule (#4212)
henryiii Oct 4, 2022
864ed11
chore: steal arg_v.value from copied arg in unpacking_collector (#4219)
Skylion007 Oct 6, 2022
6cb2147
fix: NVCC 11.4.0 - 11.8.0 host bug workaround (#4220)
ax3l Oct 7, 2022
4a42156
test_eigen.py test_nonunit_stride_to_python bug fix (ASAN failure) (#…
rwgk Oct 7, 2022
7c6f2f8
fix: PyCapsule_GetDestructor is allowed to return a nullptr destructo…
galv Oct 7, 2022
da104a9
Reproducer and fix for issue encountered in smart_holder update. (#4228)
rwgk Oct 10, 2022
67561bc
Merge branch 'master' into smart_holder
rwgk Oct 10, 2022
3818208
Tracking ci.yml changes from master.
rwgk Oct 10, 2022
eb65bfb
Remove unnamed namespace for compatibility with CUDA 11.7 (it does no…
rwgk Oct 8, 2022
0aa8c94
Add pytest.skip() for CUDACC
rwgk Oct 9, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ jobs:
# Testing NVCC; forces sources to behave like .cu files
cuda:
runs-on: ubuntu-latest
name: "🐍 3.8 • CUDA 11.2 • Ubuntu 20.04"
container: nvidia/cuda:11.2.2-devel-ubuntu20.04
name: "🐍 3.10 • CUDA 11.7 • Ubuntu 22.04"
container: nvidia/cuda:11.7.0-devel-ubuntu22.04

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -738,6 +738,9 @@ jobs:
args: -DCMAKE_CXX_STANDARD=20
- python: 3.8
args: -DCMAKE_CXX_STANDARD=17
- python: 3.7
args: -DCMAKE_CXX_STANDARD=14


name: "🐍 ${{ matrix.python }} • MSVC 2019 • x86 ${{ matrix.args }}"
runs-on: windows-2019
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/ci_sh_def.yml
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ jobs:
# Testing NVCC; forces sources to behave like .cu files
cuda:
runs-on: ubuntu-latest
name: "🐍 3.8 • CUDA 11.2 • Ubuntu 20.04"
container: nvidia/cuda:11.2.2-devel-ubuntu20.04
name: "🐍 3.10 • CUDA 11.7 • Ubuntu 22.04"
container: nvidia/cuda:11.7.0-devel-ubuntu22.04

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -761,6 +761,9 @@ jobs:
args: -DCMAKE_CXX_STANDARD=20
- python: 3.8
args: -DCMAKE_CXX_STANDARD=17
- python: 3.7
args: -DCMAKE_CXX_STANDARD=14


name: "🐍 ${{ matrix.python }} • MSVC 2019 • x86 ${{ matrix.args }}"
runs-on: windows-2019
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/ci_sh_def.yml.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- ci.yml 2022-09-15 05:25:58.998556736 -0700
+++ ci_sh_def.yml 2022-09-15 05:36:11.236234132 -0700
--- ci.yml 2022-10-09 21:54:56.682792329 -0700
+++ ci_sh_def.yml 2022-10-09 21:56:30.413425061 -0700
@@ -1,4 +1,16 @@
-name: CI
+# PLEASE KEEP THIS GROUP OF FILES IN SYNC AT ALL TIMES:
Expand Down Expand Up @@ -126,31 +126,31 @@
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")
working-directory: /build-tests

@@ -771,6 +794,7 @@
@@ -774,6 +797,7 @@
-DPYBIND11_WERROR=ON
-DDOWNLOAD_CATCH=ON
-DDOWNLOAD_EIGEN=ON
+ -DCMAKE_CXX_FLAGS="/GR /EHsc /DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
${{ matrix.args }}
- name: Build C++11
run: cmake --build build -j 2
@@ -825,6 +849,7 @@
@@ -828,6 +852,7 @@
-DPYBIND11_WERROR=ON
-DDOWNLOAD_CATCH=ON
-DDOWNLOAD_EIGEN=ON
+ -DCMAKE_CXX_FLAGS="/GR /EHsc /DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
${{ matrix.args }}
- name: Build C++11
run: cmake --build build --config Debug -j 2
@@ -865,6 +890,7 @@
@@ -868,6 +893,7 @@
-DDOWNLOAD_CATCH=ON
-DDOWNLOAD_EIGEN=ON
-DCMAKE_CXX_STANDARD=20
+ -DCMAKE_CXX_FLAGS="/GR /EHsc /DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"

- name: Build C++20
run: cmake --build build -j 2
@@ -912,7 +938,7 @@
@@ -915,7 +941,7 @@
- name: Configure C++11
# LTO leads to many undefined reference like
# `pybind11::detail::function_call::function_call(pybind11::detail::function_call&&)
Expand All @@ -159,7 +159,7 @@

- name: Build C++11
run: cmake --build build -j 2
@@ -930,7 +956,7 @@
@@ -933,7 +959,7 @@
run: git clean -fdx

- name: Configure C++14
Expand All @@ -168,7 +168,7 @@

- name: Build C++14
run: cmake --build build2 -j 2
@@ -948,7 +974,7 @@
@@ -951,7 +977,7 @@
run: git clean -fdx

- name: Configure C++17
Expand Down
12 changes: 9 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
#
# See https://github.com/pre-commit/pre-commit


ci:
autoupdate_commit_msg: "chore(deps): update pre-commit hooks"
autofix_commit_msg: "style: pre-commit fixes"
autoupdate_schedule: monthly

# third-party content
exclude: ^tools/JoinPaths.cmake$

Expand All @@ -36,7 +42,7 @@ repos:

# Upgrade old Python syntax
- repo: https://github.com/asottile/pyupgrade
rev: "v2.37.3"
rev: "v2.38.2"
hooks:
- id: pyupgrade
args: [--py36-plus]
Expand Down Expand Up @@ -113,7 +119,7 @@ repos:

# PyLint has native support - not always usable, but works for us
- repo: https://github.com/PyCQA/pylint
rev: "v2.15.2"
rev: "v2.15.3"
hooks:
- id: pylint
files: ^pybind11
Expand All @@ -129,7 +135,7 @@ repos:

# Check static types with mypy
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v0.971"
rev: "v0.981"
hooks:
- id: mypy
args: []
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1586,7 +1586,7 @@ class unpacking_collector {
throw cast_error_unable_to_convert_call_arg(a.name, a.type);
#endif
}
m_kwargs[a.name] = a.value;
m_kwargs[a.name] = std::move(a.value);
}

void process(list & /*args_list*/, detail::kwargs_proxy kp) {
Expand Down
7 changes: 1 addition & 6 deletions include/pybind11/detail/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -1045,12 +1045,7 @@ PYBIND11_NAMESPACE_END(detail)
/// - regular: static_cast<Return (Class::*)(Arg0, Arg1, Arg2)>(&Class::func)
/// - sweet: overload_cast<Arg0, Arg1, Arg2>(&Class::func)
template <typename... Args>
# if (defined(_MSC_VER) && _MSC_VER < 1920) /* MSVC 2017 */ \
|| (defined(__clang__) && __clang_major__ == 5)
static constexpr detail::overload_cast_impl<Args...> overload_cast = {};
# else
static constexpr detail::overload_cast_impl<Args...> overload_cast;
# endif
static constexpr detail::overload_cast_impl<Args...> overload_cast{};
#endif

/// Const member function selector for overload_cast
Expand Down
11 changes: 9 additions & 2 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ PYBIND11_NOINLINE internals &get_internals() {
// Cannot use py::gil_scoped_acquire here since that constructor calls get_internals.
struct gil_scoped_acquire_local {
gil_scoped_acquire_local() : state(PyGILState_Ensure()) {}
gil_scoped_acquire_local(const gil_scoped_acquire_local &) = delete;
gil_scoped_acquire_local &operator=(const gil_scoped_acquire_local &) = delete;
~gil_scoped_acquire_local() { PyGILState_Release(state); }
const PyGILState_STATE state;
} gil;
Expand Down Expand Up @@ -523,8 +525,13 @@ struct local_internals {

/// Works like `get_internals`, but for things which are locally registered.
inline local_internals &get_local_internals() {
static local_internals locals;
return locals;
// Current static can be created in the interpreter finalization routine. If the later will be
// destroyed in another static variable destructor, creation of this static there will cause
// static deinitialization fiasco. In order to avoid it we avoid destruction of the
// local_internals static. One can read more about the problem and current solution here:
// https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
static auto *locals = new local_internals();
return *locals;
}

/// Constructs a std::string with the given arguments, stores it in `internals`, and returns its
Expand Down
10 changes: 10 additions & 0 deletions include/pybind11/gil.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class gil_scoped_acquire {
inc_ref();
}

gil_scoped_acquire(const gil_scoped_acquire &) = delete;
gil_scoped_acquire &operator=(const gil_scoped_acquire &) = delete;

void inc_ref() { ++tstate->gilstate_counter; }

PYBIND11_NOINLINE void dec_ref() {
Expand Down Expand Up @@ -144,6 +147,9 @@ class gil_scoped_release {
}
}

gil_scoped_release(const gil_scoped_acquire &) = delete;
gil_scoped_release &operator=(const gil_scoped_acquire &) = delete;

/// This method will disable the PyThreadState_DeleteCurrent call and the
/// GIL won't be acquired. This method should be used if the interpreter
/// could be shutting down when this is called, as thread deletion is not
Expand Down Expand Up @@ -178,6 +184,8 @@ class gil_scoped_acquire {

public:
gil_scoped_acquire() { state = PyGILState_Ensure(); }
gil_scoped_acquire(const gil_scoped_acquire &) = delete;
gil_scoped_acquire &operator=(const gil_scoped_acquire &) = delete;
~gil_scoped_acquire() { PyGILState_Release(state); }
void disarm() {}
};
Expand All @@ -187,6 +195,8 @@ class gil_scoped_release {

public:
gil_scoped_release() { state = PyEval_SaveThread(); }
gil_scoped_release(const gil_scoped_release &) = delete;
gil_scoped_release &operator=(const gil_scoped_acquire &) = delete;
~gil_scoped_release() { PyEval_RestoreThread(state); }
void disarm() {}
};
Expand Down
1 change: 1 addition & 0 deletions include/pybind11/operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ struct op_impl {};
/// Operator implementation generator
template <op_id id, op_type ot, typename L, typename R>
struct op_ {
static constexpr bool op_enable_if_hook = true;
template <typename Class, typename... Extra>
void execute(Class &cl, const Extra &...extra) const {
using Base = typename Class::type;
Expand Down
8 changes: 4 additions & 4 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1808,14 +1808,14 @@ class class_ : public detail::generic_type {
return *this;
}

template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
class_ &def(const detail::op_<id, ot, L, R> &op, const Extra &...extra) {
template <typename T, typename... Extra, detail::enable_if_t<T::op_enable_if_hook, int> = 0>
class_ &def(const T &op, const Extra &...extra) {
op.execute(*this, extra...);
return *this;
}

template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
class_ &def_cast(const detail::op_<id, ot, L, R> &op, const Extra &...extra) {
template <typename T, typename... Extra, detail::enable_if_t<T::op_enable_if_hook, int> = 0>
class_ &def_cast(const T &op, const Extra &...extra) {
op.execute_cast(*this, extra...);
return *this;
}
Expand Down
24 changes: 15 additions & 9 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -1829,18 +1829,18 @@ class capsule : public object {
// guard if destructor called while err indicator is set
error_scope error_guard;
auto destructor = reinterpret_cast<void (*)(void *)>(PyCapsule_GetContext(o));
if (destructor == nullptr) {
if (PyErr_Occurred()) {
throw error_already_set();
}
pybind11_fail("Unable to get capsule context");
if (PyErr_Occurred()) {
throw error_already_set();
}
const char *name = get_name_in_error_scope(o);
void *ptr = PyCapsule_GetPointer(o, name);
if (ptr == nullptr) {
throw error_already_set();
}
destructor(ptr);

if (destructor != nullptr) {
destructor(ptr);
}
});

if (!m_ptr || PyCapsule_SetContext(m_ptr, (void *) destructor) != 0) {
Expand Down Expand Up @@ -2022,14 +2022,20 @@ class list : public object {
detail::list_iterator end() const { return {*this, PyList_GET_SIZE(m_ptr)}; }
template <typename T>
void append(T &&val) /* py-non-const */ {
PyList_Append(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr());
if (PyList_Append(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr()) != 0) {
throw error_already_set();
}
}
template <typename IdxType,
typename ValType,
detail::enable_if_t<std::is_integral<IdxType>::value, int> = 0>
void insert(const IdxType &index, ValType &&val) /* py-non-const */ {
PyList_Insert(
m_ptr, ssize_t_cast(index), detail::object_or_cast(std::forward<ValType>(val)).ptr());
if (PyList_Insert(m_ptr,
ssize_t_cast(index),
detail::object_or_cast(std::forward<ValType>(val)).ptr())
!= 0) {
throw error_already_set();
}
}
};

Expand Down
27 changes: 24 additions & 3 deletions include/pybind11/stl.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,31 @@ constexpr forwarded_type<T, U> forward_like(U &&u) {
return std::forward<detail::forwarded_type<T, U>>(std::forward<U>(u));
}

// Checks if a container has a STL style reserve method.
// This will only return true for a `reserve()` with a `void` return.
template <typename C>
using has_reserve_method = std::is_same<decltype(std::declval<C>().reserve(0)), void>;

template <typename Type, typename Key>
struct set_caster {
using type = Type;
using key_conv = make_caster<Key>;

private:
template <typename T = Type, enable_if_t<has_reserve_method<T>::value, int> = 0>
void reserve_maybe(const anyset &s, Type *) {
value.reserve(s.size());
}
void reserve_maybe(const anyset &, void *) {}

public:
bool load(handle src, bool convert) {
if (!isinstance<anyset>(src)) {
return false;
}
auto s = reinterpret_borrow<anyset>(src);
value.clear();
reserve_maybe(s, &value);
for (auto entry : s) {
key_conv conv;
if (!conv.load(entry, convert)) {
Expand Down Expand Up @@ -94,12 +108,21 @@ struct map_caster {
using key_conv = make_caster<Key>;
using value_conv = make_caster<Value>;

private:
template <typename T = Type, enable_if_t<has_reserve_method<T>::value, int> = 0>
void reserve_maybe(const dict &d, Type *) {
value.reserve(d.size());
}
void reserve_maybe(const dict &, void *) {}

public:
bool load(handle src, bool convert) {
if (!isinstance<dict>(src)) {
return false;
}
auto d = reinterpret_borrow<dict>(src);
value.clear();
reserve_maybe(d, &value);
for (auto it : d) {
key_conv kconv;
value_conv vconv;
Expand Down Expand Up @@ -160,9 +183,7 @@ struct list_caster {
}

private:
template <
typename T = Type,
enable_if_t<std::is_same<decltype(std::declval<T>().reserve(0)), void>::value, int> = 0>
template <typename T = Type, enable_if_t<has_reserve_method<T>::value, int> = 0>
void reserve_maybe(const sequence &s, Type *) {
value.reserve(s.size());
}
Expand Down
Loading