MCPcopy Create free account
hub / github.com/pybind/pybind11 / TEST_SUBMODULE

Function TEST_SUBMODULE

tests/test_eval.cpp:16–118  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14#include <utility>
15
16TEST_SUBMODULE(eval_, m) {
17 // test_evals
18
19 auto global = py::dict(py::module_::import("__main__").attr("__dict__"));
20
21 m.def("test_eval_statements", [global]() {
22 auto local = py::dict();
23 local["call_test"] = py::cpp_function([&]() -> int { return 42; });
24
25 // Regular string literal
26 py::exec("message = 'Hello World!'\n"
27 "x = call_test()",
28 global,
29 local);
30
31 // Multi-line raw string literal
32 py::exec(R"(
33 if x == 42:
34 print(message)
35 else:
36 raise RuntimeError
37 )",
38 global,
39 local);
40 auto x = local["x"].cast<int>();
41
42 return x == 42;
43 });
44
45 m.def("test_eval", [global]() {
46 auto local = py::dict();
47 local["x"] = py::int_(42);
48 auto x = py::eval("x", global, local);
49 return x.cast<int>() == 42;
50 });
51
52 m.def("test_eval_single_statement", []() {
53 auto local = py::dict();
54 local["call_test"] = py::cpp_function([&]() -> int { return 42; });
55
56 auto result = py::eval<py::eval_single_statement>("x = call_test()", py::dict(), local);
57 auto x = local["x"].cast<int>();
58 return result.is_none() && x == 42;
59 });
60
61 m.def("test_eval_file", [global](py::str filename) {
62 auto local = py::dict();
63 local["y"] = py::int_(43);
64
65 int val_out = 0;
66 local["call_test2"] = py::cpp_function([&](int value) { val_out = value; });
67
68 auto result = py::eval_file(std::move(filename), global, local);
69 return val_out == 43 && result.is_none();
70 });
71
72 m.def("test_eval_failure", []() {
73 try {

Callers

nothing calls this directly

Calls 9

dictClass · 0.85
importFunction · 0.85
cpp_functionClass · 0.85
execFunction · 0.85
int_Class · 0.85
evalFunction · 0.85
eval_fileFunction · 0.85
moveFunction · 0.85
attrMethod · 0.80

Tested by

no test coverage detected