cppyabm
1.0.17
An agent-based library to integrate C++ and Python
examples
Cppy
build
_deps
pybind11-src
tests
test_cmake_build
embed.cpp
Go to the documentation of this file.
1
#include <
pybind11/embed.h
>
2
namespace
py
=
pybind11
;
3
4
PYBIND11_EMBEDDED_MODULE
(test_cmake_build,
m
) {
5
m
.def(
"add"
, [](
int
i
,
int
j) {
return
i
+ j; });
6
}
7
8
int
main
(
int
argc,
char
*argv[]) {
9
if
(argc != 2)
10
throw
std::runtime_error(
"Expected test.py file as the first argument"
);
11
auto
test_py_file = argv[1];
12
13
py::scoped_interpreter guard{};
14
15
auto
m
= py::module_::import(
"test_cmake_build"
);
16
if
(
m
.attr(
"add"
)(1, 2).cast<
int
>() != 3)
17
throw
std::runtime_error(
"embed.cpp failed"
);
18
19
py::module_::import(
"sys"
).attr(
"argv"
) =
py::make_tuple
(
"test.py"
,
"embed.cpp"
);
20
py::eval_file
(test_py_file,
py::globals
());
21
}
test_multiple_inheritance.i
i
Definition:
test_multiple_inheritance.py:22
main
int main(int argc, char *argv[])
Definition:
embed.cpp:8
make_tuple
tuple make_tuple()
Definition:
cast.h:1866
embed.h
PYBIND11_EMBEDDED_MODULE
PYBIND11_EMBEDDED_MODULE(test_cmake_build, m)
Definition:
embed.cpp:4
eval_file
object eval_file(str fname, object global=globals(), object local=object())
Definition:
eval.h:102
pybind11
Definition:
__init__.py:1
globals
dict globals()
Definition:
pybind11.h:1072
test_async.m
m
Definition:
test_async.py:5
Generated by
1.8.19