Skip to content

Commit dc1036d

Browse files
committed
embdding added
1 parent b049a41 commit dc1036d

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

Examples/embedding/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# embdding
2+
3+
###### python3 embdding
4+
5+
make
6+
./embdding
7+
8+
I am called from cpp
9+
sys.version: 3.5.2 (default, Sep 14 2017, 22:51:06)
10+
[GCC 5.4.0 20160609]
11+
In cpp, method 1, five_squared: 25
12+
In cpp, method 2, five_squared: 25
13+
catch ZeroDivisionError in cpp
14+
Traceback (most recent call last):
15+
File "<string>", line 1, in <module>
16+
ZeroDivisionError: division by zero
17+
In python, going to sleep for 20 seconds
18+

Examples/embedding/embdding.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <boost/python.hpp>
4+
using namespace boost::python;
5+
6+
void running_python_code(const char *code)
7+
{
8+
object main_module = import("__main__");
9+
object main_namespace = main_module.attr("__dict__");
10+
object result = exec(code, main_namespace);
11+
}
12+
13+
void get_result()
14+
{
15+
int five_squared;
16+
17+
object main_module = import("__main__");
18+
object main_namespace = main_module.attr("__dict__");
19+
// method 1
20+
object ignored = exec("result = 5 ** 2", main_namespace);
21+
five_squared = extract<int>(main_namespace["result"]);
22+
std::cout << "In cpp, method 1, five_squared: " << five_squared << std::endl;
23+
// method 2
24+
object result = eval("5 ** 2");
25+
five_squared = extract<int>(result);
26+
std::cout << "In cpp, method 2, five_squared: " << five_squared << std::endl;
27+
}
28+
29+
void exception_handling()
30+
{
31+
try
32+
{
33+
object result = eval("5/0");
34+
std::cout << "I will never be printed" << std::endl;
35+
}
36+
catch (error_already_set const &)
37+
{
38+
if (PyErr_ExceptionMatches(PyExc_ZeroDivisionError))
39+
{
40+
std::cout << "catch ZeroDivisionError in cpp" << std::endl;
41+
PyErr_Print();
42+
}
43+
else
44+
PyErr_Print();
45+
}
46+
}
47+
48+
void sleep_for_20_seconds()
49+
{
50+
object main_module = import("__main__");
51+
object main_namespace = main_module.attr("__dict__");
52+
exec("print(\"In python, going to sleep for 20 seconds\")\n"
53+
"import time\n"
54+
"time.sleep(20)", main_namespace);
55+
56+
}
57+
58+
int main()
59+
{
60+
Py_Initialize();
61+
running_python_code("import sys\n"
62+
"print(\"I am called from cpp\")\n"
63+
"print(\"sys.version: \", sys.version)");
64+
get_result();
65+
exception_handling();
66+
sleep_for_20_seconds();
67+
return 0;
68+
}

Examples/embedding/makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
PYTHON_VERSION = 3.5
2+
TARGET = embdding
3+
4+
CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result
5+
6+
PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION)
7+
8+
# Only need if install from source
9+
10+
BOOST_INC = /usr/local/include/boost
11+
BOOST_LIB = /usr/local/lib
12+
13+
14+
ifeq ($(PYTHON_VERSION), 3.5)
15+
BOOST = lboost_python3
16+
PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m
17+
else
18+
BOOST = lboost_python
19+
PYTHON_VERSION_FINAL = $(PYTHON_VERSION)
20+
endif
21+
22+
23+
$(TARGET): $(TARGET).cpp
24+
g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -L/usr/lib/python$(PYTHON_VERSION)/config $(TARGET).cpp -o $(TARGET) -lpython$(PYTHON_VERSION_FINAL) -$(BOOST) -lboost_system

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
* [enums](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/enums)
3232
* [reference_count](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/reference_count)
3333

34+
* [embedding](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/embedding)
35+
3436
##### Boost Installation with python3 support
3537

3638
* OS Ubuntu 16.10

0 commit comments

Comments
 (0)