Skip to content

Commit 04a8521

Browse files
committed
pybind11: fix and generalize example
1 parent decf7f9 commit 04a8521

File tree

5 files changed

+28
-26
lines changed

5 files changed

+28
-26
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ __pycache__
2525
# Accidents.
2626
/core
2727
/m5out
28+
29+
# In-tree userland builds.
2830
*.o
2931
*.out
32+
*.so
3033

3134
# Kernel modules.
3235
*.ko

Diff for: userland/libs/pybind11/class.cpp

-24
This file was deleted.

Diff for: userland/libs/pybind11/class_test.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <string>
2+
3+
#include <pybind11/pybind11.h>
4+
5+
struct ClassTest {
6+
ClassTest(const std::string &name) : name(name) { }
7+
void setName(const std::string &name_) { name = name_; }
8+
const std::string &getName() const { return name; }
9+
std::string name;
10+
};
11+
12+
namespace py = pybind11;
13+
14+
PYBIND11_PLUGIN(class_test) {
15+
py::module m("my_module", "pybind11 example plugin");
16+
py::class_<ClassTest>(m, "ClassTest")
17+
.def(py::init<const std::string &>())
18+
.def("setName", &ClassTest::setName)
19+
.def("getName", &ClassTest::getName)
20+
.def_readwrite("name", &ClassTest::name);
21+
return m.ptr();
22+
}

Diff for: userland/libs/pybind11/class_test_main.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
print(my_class_test.getName())
77
my_class_test.setName("012")
88
print(my_class_test.getName())
9+
assert(my_class_test.getName() == my_class_test.name)

Diff for: userland/libs/pybind11/test.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env bash
2-
set -eu
3-
g++ -O3 -Wall -shared -std=c++11 -fPIC class_test.cpp -o class_test`python3-config --extension-suffix` -I /usr/include/python3.6m
2+
set -eux
3+
g++ `python3-config --cflags` -shared -std=c++11 -fPIC class_test.cpp -o class_test`python3-config --extension-suffix` `python3-config --libs`
44
./class_test_main.py

0 commit comments

Comments
 (0)