From 04a85219057a5e5609a0855989f8c02b155bb662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciro=20Santilli=20=E5=85=AD=E5=9B=9B=E4=BA=8B=E4=BB=B6=20?= =?UTF-8?q?=E6=B3=95=E8=BD=AE=E5=8A=9F?= Date: Fri, 21 Feb 2020 00:00:02 +0000 Subject: [PATCH] pybind11: fix and generalize example --- .gitignore | 3 +++ userland/libs/pybind11/class.cpp | 24 ----------------------- userland/libs/pybind11/class_test.cpp | 22 +++++++++++++++++++++ userland/libs/pybind11/class_test_main.py | 1 + userland/libs/pybind11/test.sh | 4 ++-- 5 files changed, 28 insertions(+), 26 deletions(-) delete mode 100644 userland/libs/pybind11/class.cpp create mode 100644 userland/libs/pybind11/class_test.cpp diff --git a/.gitignore b/.gitignore index 970a209..04677c6 100644 --- a/.gitignore +++ b/.gitignore @@ -25,8 +25,11 @@ __pycache__ # Accidents. /core /m5out + +# In-tree userland builds. *.o *.out +*.so # Kernel modules. *.ko diff --git a/userland/libs/pybind11/class.cpp b/userland/libs/pybind11/class.cpp deleted file mode 100644 index e45677a..0000000 --- a/userland/libs/pybind11/class.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include - -#include - -struct Pet { - Pet(const std::string &name) : name(name) { } - void setName(const std::string &name_) { name = name_; } - const std::string &getName() const { return name; } - - std::string name; -}; - -namespace py = pybind11; - -PYBIND11_PLUGIN(example) { - py::module m("example", "pybind11 example plugin"); - - py::class_(m, "Pet") - .def(py::init()) - .def("setName", &Pet::setName) - .def("getName", &Pet::getName); - - return m.ptr(); -} diff --git a/userland/libs/pybind11/class_test.cpp b/userland/libs/pybind11/class_test.cpp new file mode 100644 index 0000000..98717ea --- /dev/null +++ b/userland/libs/pybind11/class_test.cpp @@ -0,0 +1,22 @@ +#include + +#include + +struct ClassTest { + ClassTest(const std::string &name) : name(name) { } + void setName(const std::string &name_) { name = name_; } + const std::string &getName() const { return name; } + std::string name; +}; + +namespace py = pybind11; + +PYBIND11_PLUGIN(class_test) { + py::module m("my_module", "pybind11 example plugin"); + py::class_(m, "ClassTest") + .def(py::init()) + .def("setName", &ClassTest::setName) + .def("getName", &ClassTest::getName) + .def_readwrite("name", &ClassTest::name); + return m.ptr(); +} diff --git a/userland/libs/pybind11/class_test_main.py b/userland/libs/pybind11/class_test_main.py index 742ec10..adba727 100755 --- a/userland/libs/pybind11/class_test_main.py +++ b/userland/libs/pybind11/class_test_main.py @@ -6,3 +6,4 @@ my_class_test = class_test.ClassTest("abc"); print(my_class_test.getName()) my_class_test.setName("012") print(my_class_test.getName()) +assert(my_class_test.getName() == my_class_test.name) diff --git a/userland/libs/pybind11/test.sh b/userland/libs/pybind11/test.sh index fc37915..7f5d276 100755 --- a/userland/libs/pybind11/test.sh +++ b/userland/libs/pybind11/test.sh @@ -1,4 +1,4 @@ #!/usr/bin/env bash -set -eu -g++ -O3 -Wall -shared -std=c++11 -fPIC class_test.cpp -o class_test`python3-config --extension-suffix` -I /usr/include/python3.6m +set -eux +g++ `python3-config --cflags` -shared -std=c++11 -fPIC class_test.cpp -o class_test`python3-config --extension-suffix` `python3-config --libs` ./class_test_main.py