blob: 610d0e2e570e695d7a381488f6e80d42212f64d4 [file] [log] [blame]
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +02001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_eval.cpp -- Usage of eval() and eval_file()
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +02003
4 Copyright (c) 2016 Klemens D. Morgenstern
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/
9
10
Wenzel Jakob0d3fc352016-07-08 10:52:10 +020011#include <pybind11/eval.h>
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020012#include "pybind11_tests.h"
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020013
Jason Rhinelander52f4be82016-09-03 14:54:22 -040014test_initializer eval([](py::module &m) {
Dean Moldovan3b44dae2016-08-18 16:55:26 +020015 auto global = py::dict(py::module::import("__main__").attr("__dict__"));
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020016
Dean Moldovan3b44dae2016-08-18 16:55:26 +020017 m.def("test_eval_statements", [global]() {
18 auto local = py::dict();
19 local["call_test"] = py::cpp_function([&]() -> int {
20 return 42;
21 });
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020022
Dean Moldovan194d8b92017-03-29 00:27:56 +020023 // Regular string literal
Dean Moldovan076c7382017-04-30 01:53:06 +020024 py::exec(
Dean Moldovan194d8b92017-03-29 00:27:56 +020025 "message = 'Hello World!'\n"
26 "x = call_test()",
Dean Moldovan3b44dae2016-08-18 16:55:26 +020027 global, local
28 );
Dean Moldovan194d8b92017-03-29 00:27:56 +020029
30 // Multi-line raw string literal
Dean Moldovan076c7382017-04-30 01:53:06 +020031 py::exec(R"(
Dean Moldovan194d8b92017-03-29 00:27:56 +020032 if x == 42:
33 print(message)
34 else:
35 raise RuntimeError
36 )", global, local
37 );
Dean Moldovan3b44dae2016-08-18 16:55:26 +020038 auto x = local["x"].cast<int>();
39
Dean Moldovan076c7382017-04-30 01:53:06 +020040 return x == 42;
Wenzel Jakob0d3fc352016-07-08 10:52:10 +020041 });
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020042
Dean Moldovan3b44dae2016-08-18 16:55:26 +020043 m.def("test_eval", [global]() {
44 auto local = py::dict();
45 local["x"] = py::int_(42);
46 auto x = py::eval("x", global, local);
47 return x.cast<int>() == 42;
48 });
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020049
Dean Moldovan3b44dae2016-08-18 16:55:26 +020050 m.def("test_eval_single_statement", []() {
51 auto local = py::dict();
52 local["call_test"] = py::cpp_function([&]() -> int {
53 return 42;
54 });
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020055
Dean Moldovan3b44dae2016-08-18 16:55:26 +020056 auto result = py::eval<py::eval_single_statement>("x = call_test()", py::dict(), local);
57 auto x = local["x"].cast<int>();
Dean Moldovan36f0a152017-02-08 01:01:56 +010058 return result.is_none() && x == 42;
Dean Moldovan3b44dae2016-08-18 16:55:26 +020059 });
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020060
Dean Moldovan3b44dae2016-08-18 16:55:26 +020061 m.def("test_eval_file", [global](py::str filename) {
62 auto local = py::dict();
63 local["y"] = py::int_(43);
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020064
Dean Moldovan3b44dae2016-08-18 16:55:26 +020065 int val_out;
66 local["call_test2"] = py::cpp_function([&](int value) { val_out = value; });
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020067
Dean Moldovan3b44dae2016-08-18 16:55:26 +020068 auto result = py::eval_file(filename, global, local);
Dean Moldovan36f0a152017-02-08 01:01:56 +010069 return val_out == 43 && result.is_none();
Dean Moldovan3b44dae2016-08-18 16:55:26 +020070 });
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020071
Dean Moldovan3b44dae2016-08-18 16:55:26 +020072 m.def("test_eval_failure", []() {
73 try {
74 py::eval("nonsense code ...");
75 } catch (py::error_already_set &) {
Dean Moldovan3b44dae2016-08-18 16:55:26 +020076 return true;
77 }
78 return false;
79 });
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020080
Dean Moldovan3b44dae2016-08-18 16:55:26 +020081 m.def("test_eval_file_failure", []() {
82 try {
83 py::eval_file("non-existing file");
84 } catch (std::exception &) {
85 return true;
86 }
87 return false;
88 });
Jason Rhinelander52f4be82016-09-03 14:54:22 -040089});