example18.cpp 2.61 KB
Newer Older
Klemens Morgenstern's avatar
Klemens Morgenstern committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
    example/example18.cpp -- Usage of exec, eval etc.

    Copyright (c) 2016 Klemens D. Morgenstern

    All rights reserved. Use of this source code is governed by a
    BSD-style license that can be found in the LICENSE file.
*/


#include <pybind11/exec.h>
#include "example.h"

void example18() {
    py::module main_module = py::module::import("__main__");
    py::object main_namespace = main_module.attr("__dict__");

    bool executed = false;

    main_module.def("call_test", [&]()-> int {executed = true; return 42;});

    cout << "exec test" << endl;

    py::exec(
            "print('Hello World!');\n"
            "x = call_test();",
                          main_namespace);

    if (executed)
        cout << "exec passed" << endl;
    else {
        cout << "exec failed" << endl;
    }

    cout << "eval test" << endl;

    py::object val = py::eval("x", main_namespace);

    if (val.cast<int>() == 42)
        cout << "eval passed" << endl;
    else {
        cout << "eval failed" << endl;
    }


    executed = false;
    cout << "exec_statement test" << endl;

    py::exec_statement("y = call_test();", main_namespace);


    if (executed)
        cout << "exec_statement passed" << endl;
    else {
        cout << "exec_statement failed" << endl;
    }

    cout << "exec_file test" << endl;

    int val_out;
    main_module.def("call_test2", [&](int value) {val_out = value;});


    py::exec_file("example18_call.py", main_namespace);

    if (val_out == 42)
        cout << "exec_file passed" << endl;
    else {
        cout << "exec_file failed" << endl;
    }

    executed = false;
    cout << "exec failure test" << endl;
    try {
    	py::exec("non-sense code ...");
    }
    catch (py::error_already_set & err) {
    	executed = true;
    }
    if (executed)
        cout << "exec failure test passed" << endl;
    else {
        cout << "exec failure test failed" << endl;
    }


    executed = false;
    cout << "exec_file failure test" << endl;
    try {
    	py::exec_file("none-existing file");
    }
    catch (std::invalid_argument & err) {
    	executed = true;
    }
    if (executed)
        cout << "exec_file failure test passed" << endl;
    else {
        cout << "exec_file failure test failed" << endl;
    }

    executed = false;
    cout << "eval failure test" << endl;
    try {
    	py::eval("print('dummy')");
    }
    catch (py::error_already_set & err) {
    	executed = true;
    }
    if (executed)
        cout << "eval failure test passed" << endl;
    else {
        cout << "eval failure test failed" << endl;
    }
}

void init_ex18(py::module & m) {
	m.def("example18", &example18);
}