Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ jobs:
# Testing NVCC; forces sources to behave like .cu files
cuda:
runs-on: ubuntu-latest
name: "🐍 3.8 • CUDA 11 • Ubuntu 20.04"
container: nvidia/cuda:11.0-devel-ubuntu20.04
name: "🐍 3.8 • CUDA 11.2 • Ubuntu 20.04"
container: nvidia/cuda:11.2.2-devel-ubuntu20.04

steps:
- uses: actions/checkout@v3
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci_sh_def.yml
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ jobs:
# Testing NVCC; forces sources to behave like .cu files
cuda:
runs-on: ubuntu-latest
name: "🐍 3.8 • CUDA 11 • Ubuntu 20.04"
container: nvidia/cuda:11.0-devel-ubuntu20.04
name: "🐍 3.8 • CUDA 11.2 • Ubuntu 20.04"
container: nvidia/cuda:11.2.2-devel-ubuntu20.04

steps:
- uses: actions/checkout@v3
Expand Down
4 changes: 2 additions & 2 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-05-05 16:12:58.585658042 -0700
+++ ci_sh_def.yml 2022-05-05 16:13:29.421627917 -0700
--- ci.yml 2022-05-17 16:37:59.918807241 -0700
+++ ci_sh_def.yml 2022-05-17 16:38:42.057673676 -0700
@@ -1,4 +1,16 @@
-name: CI
+# PLEASE KEEP THIS GROUP OF FILES IN SYNC AT ALL TIMES:
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ repos:

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

# Clang format the codebase automatically
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: "v14.0.1"
rev: "v14.0.3"
hooks:
- id: clang-format
types_or: [c++, c, cuda]
17 changes: 14 additions & 3 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,8 @@ struct return_value_policy_override<
// Basic python -> C++ casting; throws if casting fails
template <typename T, typename SFINAE>
type_caster<T, SFINAE> &load_type(type_caster<T, SFINAE> &conv, const handle &handle) {
static_assert(!detail::is_pyobject<T>::value,
"Internal error: type_caster should only be used for C++ types");
if (!conv.load(handle, true)) {
#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
throw cast_error("Unable to cast Python instance to C++ type (#define "
Expand Down Expand Up @@ -1127,21 +1129,30 @@ detail::enable_if_t<!detail::move_never<T>::value, T> move(object &&obj) {
// - If both movable and copyable, check ref count: if 1, move; otherwise copy
// - Otherwise (not movable), copy.
template <typename T>
detail::enable_if_t<detail::move_always<T>::value, T> cast(object &&object) {
detail::enable_if_t<!detail::is_pyobject<T>::value && detail::move_always<T>::value, T>
cast(object &&object) {
return move<T>(std::move(object));
}
template <typename T>
detail::enable_if_t<detail::move_if_unreferenced<T>::value, T> cast(object &&object) {
detail::enable_if_t<!detail::is_pyobject<T>::value && detail::move_if_unreferenced<T>::value, T>
cast(object &&object) {
if (object.ref_count() > 1) {
return cast<T>(object);
}
return move<T>(std::move(object));
}
template <typename T>
detail::enable_if_t<detail::move_never<T>::value, T> cast(object &&object) {
detail::enable_if_t<!detail::is_pyobject<T>::value && detail::move_never<T>::value, T>
cast(object &&object) {
return cast<T>(object);
}

// pytype rvalue -> pytype (calls converting constructor)
template <typename T>
detail::enable_if_t<detail::is_pyobject<T>::value, T> cast(object &&object) {
return T(std::move(object));
}

template <typename T>
T object::cast() const & {
return pybind11::cast<T>(*this);
Expand Down
13 changes: 7 additions & 6 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -2181,7 +2181,8 @@ struct enum_base {
[](const object &arg) -> str {
handle type = type::handle_of(arg);
object type_name = type.attr("__name__");
return pybind11::str("<{}.{}: {}>").format(type_name, enum_name(arg), int_(arg));
return pybind11::str("<{}.{}: {}>")
.format(std::move(type_name), enum_name(arg), int_(arg));
},
name("__repr__"),
is_method(m_base));
Expand All @@ -2191,7 +2192,7 @@ struct enum_base {
m_base.attr("__str__") = cpp_function(
[](handle arg) -> str {
object type_name = type::handle_of(arg).attr("__name__");
return pybind11::str("{}.{}").format(type_name, enum_name(arg));
return pybind11::str("{}.{}").format(std::move(type_name), enum_name(arg));
},
name("name"),
is_method(m_base));
Expand Down Expand Up @@ -2831,8 +2832,8 @@ PYBIND11_NOINLINE void print(const tuple &args, const dict &kwargs) {
for (size_t i = 0; i < args.size(); ++i) {
strings[i] = str(args[i]);
}
auto sep = kwargs.contains("sep") ? kwargs["sep"] : cast(" ");
auto line = sep.attr("join")(strings);
auto sep = kwargs.contains("sep") ? kwargs["sep"] : str(" ");
auto line = sep.attr("join")(std::move(strings));

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

auto write = file.attr("write");
write(line);
write(kwargs.contains("end") ? kwargs["end"] : cast("\n"));
write(kwargs.contains("end") ? kwargs["end"] : str("\n"));

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

function override = getattr(self, name, function());
if (override.is_cpp_function()) {
cache.insert(key);
cache.insert(std::move(key));
return function();
}

Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/stl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#include "detail/common.h"

#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <ostream>
#include <set>
#include <unordered_map>
#include <unordered_set>
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ endif()
# Compile with compiler warnings turned on
function(pybind11_enable_warnings target_name)
if(MSVC)
target_compile_options(${target_name} PRIVATE /W4)
target_compile_options(${target_name} PRIVATE /W4 /wd4189)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang)" AND NOT PYBIND11_CUDA_TESTS)
target_compile_options(
${target_name}
Expand Down
3 changes: 3 additions & 0 deletions tests/test_copy_move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,7 @@ TEST_SUBMODULE(copy_move_policies, m) {
py::return_value_policy::move);
m.def(
"get_moveissue2", [](int i) { return MoveIssue2(i); }, py::return_value_policy::move);

// Make sure that cast from pytype rvalue to other pytype works
m.def("get_pytype_rvalue_castissue", [](double i) { return py::float_(i).cast<py::int_>(); });
}
7 changes: 7 additions & 0 deletions tests/test_copy_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,10 @@ def test_move_fallback():
assert m1.value == 1
m2 = m.get_moveissue2(2)
assert m2.value == 2


def test_pytype_rvalue_cast():
"""Make sure that cast from pytype rvalue to other pytype works"""

value = m.get_pytype_rvalue_castissue(1.0)
assert value == 1
2 changes: 1 addition & 1 deletion tools/pybind11Common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ if(MSVC) # That's also clang-cl
set_property(
TARGET pybind11::windows_extras
APPEND
PROPERTY INTERFACE_COMPILE_OPTIONS /bigobj)
PROPERTY INTERFACE_COMPILE_OPTIONS $<$<COMPILE_LANGUAGE:CXX>:/bigobj>)

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