mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-14 17:43:53 +00:00
b3f3d79f4c
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.
103 lines
2.5 KiB
C++
103 lines
2.5 KiB
C++
/*
|
|
example/example-eval.cpp -- Usage of eval() and eval_file()
|
|
|
|
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/eval.h>
|
|
#include "example.h"
|
|
|
|
void example_eval() {
|
|
py::module main_module = py::module::import("__main__");
|
|
py::object main_namespace = main_module.attr("__dict__");
|
|
|
|
bool ok = false;
|
|
|
|
main_module.def("call_test", [&]() -> int {
|
|
ok = true;
|
|
return 42;
|
|
});
|
|
|
|
cout << "eval_statements test" << endl;
|
|
|
|
auto result = py::eval<py::eval_statements>(
|
|
"print('Hello World!');\n"
|
|
"x = call_test();", main_namespace);
|
|
|
|
if (ok && result == py::none())
|
|
cout << "eval_statements passed" << endl;
|
|
else
|
|
cout << "eval_statements 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;
|
|
|
|
ok = false;
|
|
cout << "eval_single_statement test" << endl;
|
|
|
|
py::eval<py::eval_single_statement>(
|
|
"y = call_test();", main_namespace);
|
|
|
|
if (ok)
|
|
cout << "eval_single_statement passed" << endl;
|
|
else
|
|
cout << "eval_single_statement failed" << endl;
|
|
|
|
cout << "eval_file test" << endl;
|
|
|
|
int val_out;
|
|
main_module.def("call_test2", [&](int value) {val_out = value;});
|
|
|
|
try {
|
|
result = py::eval_file("example-eval_call.py", main_namespace);
|
|
} catch (...) {
|
|
result = py::eval_file("example/example-eval_call.py", main_namespace);
|
|
}
|
|
|
|
if (val_out == 42 && result == py::none())
|
|
cout << "eval_file passed" << endl;
|
|
else
|
|
cout << "eval_file failed" << endl;
|
|
|
|
ok = false;
|
|
cout << "eval failure test" << endl;
|
|
try {
|
|
py::eval("nonsense code ...");
|
|
} catch (py::error_already_set &) {
|
|
PyErr_Clear();
|
|
ok = true;
|
|
}
|
|
|
|
if (ok)
|
|
cout << "eval failure test passed" << endl;
|
|
else
|
|
cout << "eval failure test failed" << endl;
|
|
|
|
ok = false;
|
|
cout << "eval_file failure test" << endl;
|
|
try {
|
|
py::eval_file("nonexisting file");
|
|
} catch (std::exception &) {
|
|
ok = true;
|
|
}
|
|
|
|
if (ok)
|
|
cout << "eval_file failure test passed" << endl;
|
|
else
|
|
cout << "eval_file failure test failed" << endl;
|
|
}
|
|
|
|
void init_ex_eval(py::module & m) {
|
|
m.def("example_eval", &example_eval);
|
|
}
|