File: test_eval.cpp

package info (click to toggle)
nanobind 2.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,060 kB
  • sloc: cpp: 11,838; python: 5,862; ansic: 4,820; makefile: 22; sh: 15
file content (87 lines) | stat: -rw-r--r-- 2,342 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <nanobind/nanobind.h>
#include <nanobind/eval.h>
#include <nanobind/stl/pair.h>

namespace nb = nanobind;

NB_MODULE(test_eval_ext, m) {
    auto global = nb::dict(nb::module_::import_("__main__").attr("__dict__"));

    m.def("test_eval_statements", [global]() {
        auto local = nb::dict();
        local["call_test"] = nb::cpp_function([&]() -> int { return 42; });

        // Regular string literal
        nb::exec("message = 'Hello World!'\n"
                 "x = call_test()",
                 global,
                 local);

        // Multi-line raw string literal
        nb::exec(R"(
            if x == 42:
                print(message)
            else:
                raise RuntimeError
            )",
                 global,
                 local);
        auto x = nb::cast<int>(local["x"]);
        return x == 42;
    });

    m.def("test_eval", [global]() {
        auto local = nb::dict();
        local["x"] = nb::int_(42);
        auto x = nb::eval("x", global, local);
        return nb::cast<int>(x) == 42;
    });

    m.def("test_eval_single_statement", []() {
        auto local = nb::dict();
        local["call_test"] = nb::cpp_function([&]() -> int { return 42; });

        auto result = nb::eval<nb::eval_single_statement>("x = call_test()", nb::dict(), local);
        auto x = nb::cast<int>(local["x"]);
        return result.is_none() && x == 42;
    });

    m.def("test_eval_failure", []() {
        try {
            nb::eval("nonsense code ...");
        } catch (nb::python_error &) {
            return true;
        }
        return false;
    });

    // test_eval_closure
    m.def("test_eval_closure", []() {
        nb::dict global;
        global["closure_value"] = 42;
        nb::dict local;
        local["closure_value"] = 0;
        nb::exec(R"(
            local_value = closure_value

            def func_global():
                return closure_value

            def func_local():
                return local_value
            )",
                 global,
                 local);
        return std::make_pair(global, local);
    });

    m.def("globals_contains_a", []() {
        return nb::globals().contains("a");
    });

    m.def("globals_add_b", []() {
        auto globals = nb::globals();
        globals["b"] = 123;
        return globals;
    });
}