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

Function eval

include/pybind11/eval.h:48–79  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

46
47template <eval_mode mode = eval_expr>
48object eval(const str &expr, object global = globals(), object local = object()) {
49 if (!local) {
50 local = global;
51 }
52
53 detail::ensure_builtins_in_globals(global);
54
55 /* PyRun_String does not accept a PyObject / encoding specifier,
56 this seems to be the only alternative */
57 std::string buffer = "# -*- coding: utf-8 -*-\n" + (std::string) expr;
58
59 int start = 0;
60 switch (mode) {
61 case eval_expr:
62 start = Py_eval_input;
63 break;
64 case eval_single_statement:
65 start = Py_single_input;
66 break;
67 case eval_statements:
68 start = Py_file_input;
69 break;
70 default:
71 pybind11_fail("invalid evaluation mode");
72 }
73
74 PyObject *result = PyRun_String(buffer.c_str(), start, global.ptr(), local.ptr());
75 if (!result) {
76 throw error_already_set();
77 }
78 return reinterpret_steal<object>(result);
79}
80
81template <eval_mode mode = eval_expr, size_t N>
82object eval(const char (&s)[N], object global = globals(), object local = object()) {

Callers 1

TEST_SUBMODULEFunction · 0.85

Calls 8

globalsFunction · 0.85
strClass · 0.85
importFunction · 0.85
moveFunction · 0.85
ptrMethod · 0.80
attrMethod · 0.80
objectFunction · 0.70

Tested by 1

TEST_SUBMODULEFunction · 0.68