-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "test_move_arg.h" | ||
#include <boost/python.hpp> | ||
#include <sstream> | ||
|
||
namespace py = boost::python; | ||
|
||
std::string item_repr(const Item& item) { | ||
std::stringstream ss; | ||
ss << "py " << item; | ||
return ss.str(); | ||
} | ||
|
||
void item_access(const Item& item) { | ||
std::cout << "access " << item << "\n"; | ||
} | ||
|
||
void item_access_ptr(const std::auto_ptr<Item>& item) { | ||
std::cout << "access ptr " << item.get() << " "; | ||
if (item.get()) std::cout << *item; | ||
std::cout << "\n"; | ||
} | ||
|
||
void item_consume(std::auto_ptr<Item>& item) { | ||
std::cout << "consume " << *item << "\n "; | ||
Item sink(std::move(*item.release())); | ||
std::cout << " old: " << item.get() << "\n new: " << sink << "\n"; | ||
} | ||
|
||
BOOST_PYTHON_MODULE(test_move_arg_bp) { | ||
py::class_<Item, std::auto_ptr<Item>, boost::noncopyable>("Item", py::init<int>()) | ||
.def("__repr__", &item_repr); | ||
|
||
py::def("access", item_access); | ||
py::def("access_ptr", item_access_ptr); | ||
|
||
py::def("consume", item_consume); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import pytest | ||
from test_move_arg_bp import Item, access, access_ptr, consume | ||
|
||
|
||
def test(): | ||
item = Item(42) | ||
other = item | ||
print(item) | ||
access(item) | ||
consume(item) | ||
print("back in python") | ||
|
||
try: | ||
access_ptr(item) | ||
access(item) | ||
except Exception as e: | ||
print(e) | ||
|
||
del item | ||
|
||
try: | ||
print(other) | ||
except Exception as e: | ||
print(e) | ||
|
||
if __name__ == "__main__": | ||
test() |