Skip to content

Commit

Permalink
failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
rhaschke committed Dec 23, 2019
1 parent fe755dc commit 98ea003
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function(pybind11_enable_warnings target_name)
endif()
endfunction()

set(test_targets pybind11_tests)
set(test_targets pybind11_tests test_move_arg)

# Build pybind11_cross_module_tests if any test_whatever.py are being built that require it
foreach(t ${PYBIND11_CROSS_MODULE_TESTS})
Expand Down
31 changes: 31 additions & 0 deletions tests/test_move_arg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "test_move_arg.h"
#include <pybind11/pybind11.h>
#include <pybind11/iostream.h>
#include <sstream>

namespace py = pybind11;

PYBIND11_MODULE(test_move_arg, m) {
py::class_<Item>(m, "Item")
.def(py::init<int>(), py::call_guard<py::scoped_ostream_redirect>())
.def("__repr__", [](const Item& item) {
std::stringstream ss;
ss << "py " << item;
return ss.str();
}, py::call_guard<py::scoped_ostream_redirect>());

m.def("access", [](const Item& item) {
std::cout << "access " << item << "\n";
}, py::call_guard<py::scoped_ostream_redirect>());

#if 0 // rvalue arguments fail during compilation
m.def("consume", [](Item&& item) {
std::cout << "consume " << item << "\n ";
Item sink(std::move(item));
std::cout << " old: " << item << "\n new: " << sink << "\n";
}, py::call_guard<py::scoped_ostream_redirect>());
#endif

m.def("working", [](int&& a) { std::cout << a << "\n"; },
py::call_guard<py::scoped_ostream_redirect>());
}
34 changes: 34 additions & 0 deletions tests/test_move_arg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once
#include <memory>
#include <vector>
#include <iostream>

class Item;
std::ostream& operator<<(std::ostream& os, const Item& item);

/** Item class requires unique instances, i.e. only supports move construction */
class Item
{
public:
Item(int value) : value_(std::make_unique<int>(value)) {
std::cout<< "new " << *this << "\n";
}
~Item() {
std::cout << "destroy " << *this << "\n";
}
Item(const Item&) = delete;
Item(Item&& other) {
std::cout << "move " << other << " -> ";
value_ = std::move(other.value_);
std::cout << *this << "\n";
}

std::unique_ptr<int> value_;
};

std::ostream& operator<<(std::ostream& os, const Item& item) {
os << "item " << &item << "(";
if (item.value_) os << *item.value_;
os << ")";
return os;
}
14 changes: 14 additions & 0 deletions tests/test_move_arg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest
from test_move_arg import Item, access


def test():
item = Item(42)
other = item
access(item)
print(item)
del item
print(other)

if __name__ == "__main__":
test()

0 comments on commit 98ea003

Please sign in to comment.