2016-06-09 14:10:26 +00:00
|
|
|
/*
|
Rename examples files, as per #288
This renames example files from `exampleN` to `example-description`.
Specifically, the following renaming is applied:
example1 -> example-methods-and-attributes
example2 -> example-python-types
example3 -> example-operator-overloading
example4 -> example-constants-and-functions
example5 -> example-callbacks (*)
example6 -> example-sequence-and-iterators
example7 -> example-buffers
example8 -> example-custom-ref-counting
example9 -> example-modules
example10 -> example-numpy-vectorize
example11 -> example-arg-keywords-and-defaults
example12 -> example-virtual-functions
example13 -> example-keep-alive
example14 -> example-opaque-types
example15 -> example-pickling
example16 -> example-inheritance
example17 -> example-stl-binders
example18 -> example-eval
example19 -> example-custom-exceptions
* the inheritance parts of example5 are moved into example-inheritance
(previously example16), and the remainder is left as example-callbacks.
This commit also renames the internal variables ("Example1",
"Example2", "Example4", etc.) into non-numeric names ("ExampleMandA",
"ExamplePythonTypes", "ExampleWithEnum", etc.) to correspond to the
file renaming.
The order of tests is preserved, but this can easily be changed if
there is some more natural ordering by updating the list in
examples/CMakeLists.txt.
2016-07-18 20:43:18 +00:00
|
|
|
example/example-eval.cpp -- Usage of eval() and eval_file()
|
2016-06-09 14:10:26 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-07-08 08:52:10 +00:00
|
|
|
#include <pybind11/eval.h>
|
2016-06-09 14:10:26 +00:00
|
|
|
#include "example.h"
|
|
|
|
|
Rename examples files, as per #288
This renames example files from `exampleN` to `example-description`.
Specifically, the following renaming is applied:
example1 -> example-methods-and-attributes
example2 -> example-python-types
example3 -> example-operator-overloading
example4 -> example-constants-and-functions
example5 -> example-callbacks (*)
example6 -> example-sequence-and-iterators
example7 -> example-buffers
example8 -> example-custom-ref-counting
example9 -> example-modules
example10 -> example-numpy-vectorize
example11 -> example-arg-keywords-and-defaults
example12 -> example-virtual-functions
example13 -> example-keep-alive
example14 -> example-opaque-types
example15 -> example-pickling
example16 -> example-inheritance
example17 -> example-stl-binders
example18 -> example-eval
example19 -> example-custom-exceptions
* the inheritance parts of example5 are moved into example-inheritance
(previously example16), and the remainder is left as example-callbacks.
This commit also renames the internal variables ("Example1",
"Example2", "Example4", etc.) into non-numeric names ("ExampleMandA",
"ExamplePythonTypes", "ExampleWithEnum", etc.) to correspond to the
file renaming.
The order of tests is preserved, but this can easily be changed if
there is some more natural ordering by updating the list in
examples/CMakeLists.txt.
2016-07-18 20:43:18 +00:00
|
|
|
void example_eval() {
|
2016-06-09 14:10:26 +00:00
|
|
|
py::module main_module = py::module::import("__main__");
|
|
|
|
py::object main_namespace = main_module.attr("__dict__");
|
|
|
|
|
2016-07-08 08:52:10 +00:00
|
|
|
bool ok = false;
|
2016-06-09 14:10:26 +00:00
|
|
|
|
2016-07-08 08:52:10 +00:00
|
|
|
main_module.def("call_test", [&]() -> int {
|
|
|
|
ok = true;
|
|
|
|
return 42;
|
|
|
|
});
|
2016-06-09 14:10:26 +00:00
|
|
|
|
2016-07-08 08:52:10 +00:00
|
|
|
cout << "eval_statements test" << endl;
|
2016-06-09 14:10:26 +00:00
|
|
|
|
2016-07-08 08:52:10 +00:00
|
|
|
auto result = py::eval<py::eval_statements>(
|
2016-06-09 14:10:26 +00:00
|
|
|
"print('Hello World!');\n"
|
2016-07-08 08:52:10 +00:00
|
|
|
"x = call_test();", main_namespace);
|
2016-06-09 14:10:26 +00:00
|
|
|
|
2016-07-08 08:52:10 +00:00
|
|
|
if (ok && result == py::none())
|
|
|
|
cout << "eval_statements passed" << endl;
|
|
|
|
else
|
|
|
|
cout << "eval_statements failed" << endl;
|
2016-06-09 14:10:26 +00:00
|
|
|
|
|
|
|
cout << "eval test" << endl;
|
|
|
|
|
|
|
|
py::object val = py::eval("x", main_namespace);
|
|
|
|
|
|
|
|
if (val.cast<int>() == 42)
|
|
|
|
cout << "eval passed" << endl;
|
2016-07-08 08:52:10 +00:00
|
|
|
else
|
2016-06-09 14:10:26 +00:00
|
|
|
cout << "eval failed" << endl;
|
|
|
|
|
2016-07-08 08:52:10 +00:00
|
|
|
ok = false;
|
|
|
|
cout << "eval_single_statement test" << endl;
|
2016-06-09 14:10:26 +00:00
|
|
|
|
2016-07-08 08:52:10 +00:00
|
|
|
py::eval<py::eval_single_statement>(
|
|
|
|
"y = call_test();", main_namespace);
|
2016-06-09 14:10:26 +00:00
|
|
|
|
2016-07-08 08:52:10 +00:00
|
|
|
if (ok)
|
|
|
|
cout << "eval_single_statement passed" << endl;
|
|
|
|
else
|
|
|
|
cout << "eval_single_statement failed" << endl;
|
2016-06-09 14:10:26 +00:00
|
|
|
|
2016-07-08 08:52:10 +00:00
|
|
|
cout << "eval_file test" << endl;
|
2016-06-09 14:10:26 +00:00
|
|
|
|
|
|
|
int val_out;
|
|
|
|
main_module.def("call_test2", [&](int value) {val_out = value;});
|
|
|
|
|
|
|
|
try {
|
Rename examples files, as per #288
This renames example files from `exampleN` to `example-description`.
Specifically, the following renaming is applied:
example1 -> example-methods-and-attributes
example2 -> example-python-types
example3 -> example-operator-overloading
example4 -> example-constants-and-functions
example5 -> example-callbacks (*)
example6 -> example-sequence-and-iterators
example7 -> example-buffers
example8 -> example-custom-ref-counting
example9 -> example-modules
example10 -> example-numpy-vectorize
example11 -> example-arg-keywords-and-defaults
example12 -> example-virtual-functions
example13 -> example-keep-alive
example14 -> example-opaque-types
example15 -> example-pickling
example16 -> example-inheritance
example17 -> example-stl-binders
example18 -> example-eval
example19 -> example-custom-exceptions
* the inheritance parts of example5 are moved into example-inheritance
(previously example16), and the remainder is left as example-callbacks.
This commit also renames the internal variables ("Example1",
"Example2", "Example4", etc.) into non-numeric names ("ExampleMandA",
"ExamplePythonTypes", "ExampleWithEnum", etc.) to correspond to the
file renaming.
The order of tests is preserved, but this can easily be changed if
there is some more natural ordering by updating the list in
examples/CMakeLists.txt.
2016-07-18 20:43:18 +00:00
|
|
|
result = py::eval_file("example-eval_call.py", main_namespace);
|
2016-07-08 08:52:10 +00:00
|
|
|
} catch (...) {
|
Rename examples files, as per #288
This renames example files from `exampleN` to `example-description`.
Specifically, the following renaming is applied:
example1 -> example-methods-and-attributes
example2 -> example-python-types
example3 -> example-operator-overloading
example4 -> example-constants-and-functions
example5 -> example-callbacks (*)
example6 -> example-sequence-and-iterators
example7 -> example-buffers
example8 -> example-custom-ref-counting
example9 -> example-modules
example10 -> example-numpy-vectorize
example11 -> example-arg-keywords-and-defaults
example12 -> example-virtual-functions
example13 -> example-keep-alive
example14 -> example-opaque-types
example15 -> example-pickling
example16 -> example-inheritance
example17 -> example-stl-binders
example18 -> example-eval
example19 -> example-custom-exceptions
* the inheritance parts of example5 are moved into example-inheritance
(previously example16), and the remainder is left as example-callbacks.
This commit also renames the internal variables ("Example1",
"Example2", "Example4", etc.) into non-numeric names ("ExampleMandA",
"ExamplePythonTypes", "ExampleWithEnum", etc.) to correspond to the
file renaming.
The order of tests is preserved, but this can easily be changed if
there is some more natural ordering by updating the list in
examples/CMakeLists.txt.
2016-07-18 20:43:18 +00:00
|
|
|
result = py::eval_file("example/example-eval_call.py", main_namespace);
|
2016-06-09 14:10:26 +00:00
|
|
|
}
|
|
|
|
|
2016-07-08 08:52:10 +00:00
|
|
|
if (val_out == 42 && result == py::none())
|
|
|
|
cout << "eval_file passed" << endl;
|
|
|
|
else
|
|
|
|
cout << "eval_file failed" << endl;
|
2016-06-09 14:10:26 +00:00
|
|
|
|
2016-07-08 08:52:10 +00:00
|
|
|
ok = false;
|
2016-06-09 14:10:26 +00:00
|
|
|
cout << "eval failure test" << endl;
|
|
|
|
try {
|
2016-07-08 08:52:10 +00:00
|
|
|
py::eval("nonsense code ...");
|
|
|
|
} catch (py::error_already_set &) {
|
|
|
|
PyErr_Clear();
|
|
|
|
ok = true;
|
2016-06-09 14:10:26 +00:00
|
|
|
}
|
2016-07-08 08:52:10 +00:00
|
|
|
|
|
|
|
if (ok)
|
2016-06-09 14:10:26 +00:00
|
|
|
cout << "eval failure test passed" << endl;
|
2016-07-08 08:52:10 +00:00
|
|
|
else
|
2016-06-09 14:10:26 +00:00
|
|
|
cout << "eval failure test failed" << endl;
|
2016-07-08 08:52:10 +00:00
|
|
|
|
|
|
|
ok = false;
|
|
|
|
cout << "eval_file failure test" << endl;
|
|
|
|
try {
|
|
|
|
py::eval_file("nonexisting file");
|
|
|
|
} catch (std::exception &) {
|
|
|
|
ok = true;
|
2016-06-09 14:10:26 +00:00
|
|
|
}
|
2016-07-08 08:52:10 +00:00
|
|
|
|
|
|
|
if (ok)
|
|
|
|
cout << "eval_file failure test passed" << endl;
|
|
|
|
else
|
|
|
|
cout << "eval_file failure test failed" << endl;
|
2016-06-09 14:10:26 +00:00
|
|
|
}
|
|
|
|
|
Rename examples files, as per #288
This renames example files from `exampleN` to `example-description`.
Specifically, the following renaming is applied:
example1 -> example-methods-and-attributes
example2 -> example-python-types
example3 -> example-operator-overloading
example4 -> example-constants-and-functions
example5 -> example-callbacks (*)
example6 -> example-sequence-and-iterators
example7 -> example-buffers
example8 -> example-custom-ref-counting
example9 -> example-modules
example10 -> example-numpy-vectorize
example11 -> example-arg-keywords-and-defaults
example12 -> example-virtual-functions
example13 -> example-keep-alive
example14 -> example-opaque-types
example15 -> example-pickling
example16 -> example-inheritance
example17 -> example-stl-binders
example18 -> example-eval
example19 -> example-custom-exceptions
* the inheritance parts of example5 are moved into example-inheritance
(previously example16), and the remainder is left as example-callbacks.
This commit also renames the internal variables ("Example1",
"Example2", "Example4", etc.) into non-numeric names ("ExampleMandA",
"ExamplePythonTypes", "ExampleWithEnum", etc.) to correspond to the
file renaming.
The order of tests is preserved, but this can easily be changed if
there is some more natural ordering by updating the list in
examples/CMakeLists.txt.
2016-07-18 20:43:18 +00:00
|
|
|
void init_ex_eval(py::module & m) {
|
|
|
|
m.def("example_eval", &example_eval);
|
2016-06-09 14:10:26 +00:00
|
|
|
}
|