Skip to content

Commit bdf22c8

Browse files
committed
Merge branch 'master' into sh_merge_master
2 parents d28c3a5 + a8b3ff3 commit bdf22c8

File tree

9 files changed

+38
-16
lines changed

9 files changed

+38
-16
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ jobs:
316316
# Testing NVCC; forces sources to behave like .cu files
317317
cuda:
318318
runs-on: ubuntu-latest
319-
name: "🐍 3.8 • CUDA 11 • Ubuntu 20.04"
320-
container: nvidia/cuda:11.0-devel-ubuntu20.04
319+
name: "🐍 3.8 • CUDA 11.2 • Ubuntu 20.04"
320+
container: nvidia/cuda:11.2.2-devel-ubuntu20.04
321321

322322
steps:
323323
- uses: actions/checkout@v3

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ repos:
3333

3434
# Upgrade old Python syntax
3535
- repo: https://github.com/asottile/pyupgrade
36-
rev: "v2.32.0"
36+
rev: "v2.32.1"
3737
hooks:
3838
- id: pyupgrade
3939
args: [--py36-plus]
@@ -167,7 +167,7 @@ repos:
167167

168168
# Clang format the codebase automatically
169169
- repo: https://github.com/pre-commit/mirrors-clang-format
170-
rev: "v14.0.1"
170+
rev: "v14.0.3"
171171
hooks:
172172
- id: clang-format
173173
types_or: [c++, c, cuda]

include/pybind11/cast.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,8 @@ struct return_value_policy_override<
10371037
// Basic python -> C++ casting; throws if casting fails
10381038
template <typename T, typename SFINAE>
10391039
type_caster<T, SFINAE> &load_type(type_caster<T, SFINAE> &conv, const handle &handle) {
1040+
static_assert(!detail::is_pyobject<T>::value,
1041+
"Internal error: type_caster should only be used for C++ types");
10401042
if (!conv.load(handle, true)) {
10411043
#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
10421044
throw cast_error("Unable to cast Python instance to C++ type (#define "
@@ -1127,21 +1129,30 @@ detail::enable_if_t<!detail::move_never<T>::value, T> move(object &&obj) {
11271129
// - If both movable and copyable, check ref count: if 1, move; otherwise copy
11281130
// - Otherwise (not movable), copy.
11291131
template <typename T>
1130-
detail::enable_if_t<detail::move_always<T>::value, T> cast(object &&object) {
1132+
detail::enable_if_t<!detail::is_pyobject<T>::value && detail::move_always<T>::value, T>
1133+
cast(object &&object) {
11311134
return move<T>(std::move(object));
11321135
}
11331136
template <typename T>
1134-
detail::enable_if_t<detail::move_if_unreferenced<T>::value, T> cast(object &&object) {
1137+
detail::enable_if_t<!detail::is_pyobject<T>::value && detail::move_if_unreferenced<T>::value, T>
1138+
cast(object &&object) {
11351139
if (object.ref_count() > 1) {
11361140
return cast<T>(object);
11371141
}
11381142
return move<T>(std::move(object));
11391143
}
11401144
template <typename T>
1141-
detail::enable_if_t<detail::move_never<T>::value, T> cast(object &&object) {
1145+
detail::enable_if_t<!detail::is_pyobject<T>::value && detail::move_never<T>::value, T>
1146+
cast(object &&object) {
11421147
return cast<T>(object);
11431148
}
11441149

1150+
// pytype rvalue -> pytype (calls converting constructor)
1151+
template <typename T>
1152+
detail::enable_if_t<detail::is_pyobject<T>::value, T> cast(object &&object) {
1153+
return T(std::move(object));
1154+
}
1155+
11451156
template <typename T>
11461157
T object::cast() const & {
11471158
return pybind11::cast<T>(*this);

include/pybind11/pybind11.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,7 +2181,8 @@ struct enum_base {
21812181
[](const object &arg) -> str {
21822182
handle type = type::handle_of(arg);
21832183
object type_name = type.attr("__name__");
2184-
return pybind11::str("<{}.{}: {}>").format(type_name, enum_name(arg), int_(arg));
2184+
return pybind11::str("<{}.{}: {}>")
2185+
.format(std::move(type_name), enum_name(arg), int_(arg));
21852186
},
21862187
name("__repr__"),
21872188
is_method(m_base));
@@ -2191,7 +2192,7 @@ struct enum_base {
21912192
m_base.attr("__str__") = cpp_function(
21922193
[](handle arg) -> str {
21932194
object type_name = type::handle_of(arg).attr("__name__");
2194-
return pybind11::str("{}.{}").format(type_name, enum_name(arg));
2195+
return pybind11::str("{}.{}").format(std::move(type_name), enum_name(arg));
21952196
},
21962197
name("name"),
21972198
is_method(m_base));
@@ -2831,8 +2832,8 @@ PYBIND11_NOINLINE void print(const tuple &args, const dict &kwargs) {
28312832
for (size_t i = 0; i < args.size(); ++i) {
28322833
strings[i] = str(args[i]);
28332834
}
2834-
auto sep = kwargs.contains("sep") ? kwargs["sep"] : cast(" ");
2835-
auto line = sep.attr("join")(strings);
2835+
auto sep = kwargs.contains("sep") ? kwargs["sep"] : str(" ");
2836+
auto line = sep.attr("join")(std::move(strings));
28362837

28372838
object file;
28382839
if (kwargs.contains("file")) {
@@ -2851,7 +2852,7 @@ PYBIND11_NOINLINE void print(const tuple &args, const dict &kwargs) {
28512852

28522853
auto write = file.attr("write");
28532854
write(line);
2854-
write(kwargs.contains("end") ? kwargs["end"] : cast("\n"));
2855+
write(kwargs.contains("end") ? kwargs["end"] : str("\n"));
28552856

28562857
if (kwargs.contains("flush") && kwargs["flush"].cast<bool>()) {
28572858
file.attr("flush")();
@@ -2894,7 +2895,7 @@ get_type_override(const void *this_ptr, const type_info *this_type, const char *
28942895

28952896
function override = getattr(self, name, function());
28962897
if (override.is_cpp_function()) {
2897-
cache.insert(key);
2898+
cache.insert(std::move(key));
28982899
return function();
28992900
}
29002901

include/pybind11/stl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
#include "detail/common.h"
1414

1515
#include <deque>
16-
#include <iostream>
1716
#include <list>
1817
#include <map>
18+
#include <ostream>
1919
#include <set>
2020
#include <unordered_map>
2121
#include <unordered_set>

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ endif()
370370
# Compile with compiler warnings turned on
371371
function(pybind11_enable_warnings target_name)
372372
if(MSVC)
373-
target_compile_options(${target_name} PRIVATE /W4)
373+
target_compile_options(${target_name} PRIVATE /W4 /wd4189)
374374
elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang)" AND NOT PYBIND11_CUDA_TESTS)
375375
target_compile_options(
376376
${target_name}

tests/test_copy_move.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,7 @@ TEST_SUBMODULE(copy_move_policies, m) {
289289
py::return_value_policy::move);
290290
m.def(
291291
"get_moveissue2", [](int i) { return MoveIssue2(i); }, py::return_value_policy::move);
292+
293+
// Make sure that cast from pytype rvalue to other pytype works
294+
m.def("get_pytype_rvalue_castissue", [](double i) { return py::float_(i).cast<py::int_>(); });
292295
}

tests/test_copy_move.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,10 @@ def test_move_fallback():
123123
assert m1.value == 1
124124
m2 = m.get_moveissue2(2)
125125
assert m2.value == 2
126+
127+
128+
def test_pytype_rvalue_cast():
129+
"""Make sure that cast from pytype rvalue to other pytype works"""
130+
131+
value = m.get_pytype_rvalue_castissue(1.0)
132+
assert value == 1

tools/pybind11Common.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ if(MSVC) # That's also clang-cl
9696
set_property(
9797
TARGET pybind11::windows_extras
9898
APPEND
99-
PROPERTY INTERFACE_COMPILE_OPTIONS /bigobj)
99+
PROPERTY INTERFACE_COMPILE_OPTIONS $<$<COMPILE_LANGUAGE:CXX>:/bigobj>)
100100

101101
# /MP enables multithreaded builds (relevant when there are many files) for MSVC
102102
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") # no Clang no Intel

0 commit comments

Comments
 (0)