|
1 | 1 | /*
|
2 |
| - example/example18.cpp -- Usage of exec, eval etc. |
| 2 | + example/example18.cpp -- Usage of eval() and eval_file() |
3 | 3 |
|
4 | 4 | Copyright (c) 2016 Klemens D. Morgenstern
|
5 | 5 |
|
|
8 | 8 | */
|
9 | 9 |
|
10 | 10 |
|
11 |
| -#include <pybind11/exec.h> |
| 11 | +#include <pybind11/eval.h> |
12 | 12 | #include "example.h"
|
13 | 13 |
|
14 | 14 | void example18() {
|
15 | 15 | py::module main_module = py::module::import("__main__");
|
16 | 16 | py::object main_namespace = main_module.attr("__dict__");
|
17 | 17 |
|
18 |
| - bool executed = false; |
| 18 | + bool ok = false; |
19 | 19 |
|
20 |
| - main_module.def("call_test", [&]()-> int {executed = true; return 42;}); |
| 20 | + main_module.def("call_test", [&]() -> int { |
| 21 | + ok = true; |
| 22 | + return 42; |
| 23 | + }); |
21 | 24 |
|
22 |
| - cout << "exec test" << endl; |
| 25 | + cout << "eval_statements test" << endl; |
23 | 26 |
|
24 |
| - py::exec( |
| 27 | + auto result = py::eval<py::eval_statements>( |
25 | 28 | "print('Hello World!');\n"
|
26 |
| - "x = call_test();", |
27 |
| - main_namespace); |
| 29 | + "x = call_test();", main_namespace); |
28 | 30 |
|
29 |
| - if (executed) |
30 |
| - cout << "exec passed" << endl; |
31 |
| - else { |
32 |
| - cout << "exec failed" << endl; |
33 |
| - } |
| 31 | + if (ok && result == py::none()) |
| 32 | + cout << "eval_statements passed" << endl; |
| 33 | + else |
| 34 | + cout << "eval_statements failed" << endl; |
34 | 35 |
|
35 | 36 | cout << "eval test" << endl;
|
36 | 37 |
|
37 | 38 | py::object val = py::eval("x", main_namespace);
|
38 | 39 |
|
39 | 40 | if (val.cast<int>() == 42)
|
40 | 41 | cout << "eval passed" << endl;
|
41 |
| - else { |
| 42 | + else |
42 | 43 | cout << "eval failed" << endl;
|
43 |
| - } |
44 |
| - |
45 | 44 |
|
46 |
| - executed = false; |
47 |
| - cout << "exec_statement test" << endl; |
| 45 | + ok = false; |
| 46 | + cout << "eval_single_statement test" << endl; |
48 | 47 |
|
49 |
| - py::exec_statement("y = call_test();", main_namespace); |
| 48 | + py::eval<py::eval_single_statement>( |
| 49 | + "y = call_test();", main_namespace); |
50 | 50 |
|
| 51 | + if (ok) |
| 52 | + cout << "eval_single_statement passed" << endl; |
| 53 | + else |
| 54 | + cout << "eval_single_statement failed" << endl; |
51 | 55 |
|
52 |
| - if (executed) |
53 |
| - cout << "exec_statement passed" << endl; |
54 |
| - else { |
55 |
| - cout << "exec_statement failed" << endl; |
56 |
| - } |
57 |
| - |
58 |
| - cout << "exec_file test" << endl; |
| 56 | + cout << "eval_file test" << endl; |
59 | 57 |
|
60 | 58 | int val_out;
|
61 | 59 | main_module.def("call_test2", [&](int value) {val_out = value;});
|
62 | 60 |
|
63 |
| - |
64 |
| - py::exec_file("example18_call.py", main_namespace); |
65 |
| - |
66 |
| - if (val_out == 42) |
67 |
| - cout << "exec_file passed" << endl; |
68 |
| - else { |
69 |
| - cout << "exec_file failed" << endl; |
70 |
| - } |
71 |
| - |
72 |
| - executed = false; |
73 |
| - cout << "exec failure test" << endl; |
74 | 61 | try {
|
75 |
| - py::exec("non-sense code ..."); |
76 |
| - } |
77 |
| - catch (py::error_already_set & err) { |
78 |
| - executed = true; |
79 |
| - } |
80 |
| - if (executed) |
81 |
| - cout << "exec failure test passed" << endl; |
82 |
| - else { |
83 |
| - cout << "exec failure test failed" << endl; |
| 62 | + result = py::eval_file("example18_call.py", main_namespace); |
| 63 | + } catch (...) { |
| 64 | + result = py::eval_file("example/example18_call.py", main_namespace); |
84 | 65 | }
|
85 | 66 |
|
| 67 | + if (val_out == 42 && result == py::none()) |
| 68 | + cout << "eval_file passed" << endl; |
| 69 | + else |
| 70 | + cout << "eval_file failed" << endl; |
86 | 71 |
|
87 |
| - executed = false; |
88 |
| - cout << "exec_file failure test" << endl; |
89 |
| - try { |
90 |
| - py::exec_file("none-existing file"); |
91 |
| - } |
92 |
| - catch (std::invalid_argument & err) { |
93 |
| - executed = true; |
94 |
| - } |
95 |
| - if (executed) |
96 |
| - cout << "exec_file failure test passed" << endl; |
97 |
| - else { |
98 |
| - cout << "exec_file failure test failed" << endl; |
99 |
| - } |
100 |
| - |
101 |
| - executed = false; |
| 72 | + ok = false; |
102 | 73 | cout << "eval failure test" << endl;
|
103 | 74 | try {
|
104 |
| - py::eval("print('dummy')"); |
105 |
| - } |
106 |
| - catch (py::error_already_set & err) { |
107 |
| - executed = true; |
| 75 | + py::eval("nonsense code ..."); |
| 76 | + } catch (py::error_already_set &) { |
| 77 | + PyErr_Clear(); |
| 78 | + ok = true; |
108 | 79 | }
|
109 |
| - if (executed) |
| 80 | + |
| 81 | + if (ok) |
110 | 82 | cout << "eval failure test passed" << endl;
|
111 |
| - else { |
| 83 | + else |
112 | 84 | cout << "eval failure test failed" << endl;
|
| 85 | + |
| 86 | + ok = false; |
| 87 | + cout << "eval_file failure test" << endl; |
| 88 | + try { |
| 89 | + py::eval_file("nonexisting file"); |
| 90 | + } catch (std::exception &) { |
| 91 | + ok = true; |
113 | 92 | }
|
| 93 | + |
| 94 | + if (ok) |
| 95 | + cout << "eval_file failure test passed" << endl; |
| 96 | + else |
| 97 | + cout << "eval_file failure test failed" << endl; |
114 | 98 | }
|
115 | 99 |
|
116 | 100 | void init_ex18(py::module & m) {
|
117 |
| - m.def("example18", &example18); |
| 101 | + m.def("example18", &example18); |
118 | 102 | }
|
119 |
| - |
120 |
| - |
0 commit comments