22 #if defined(PYPY_VERSION) || PY_VERSION_HEX < 0x03080000 27 if (!global.contains(
"__builtins__"))
47 template <eval_mode mode = eval_expr>
48 object eval(
const str &expr,
object global =
globals(),
object local =
object()) {
57 std::string
buffer =
"# -*- coding: utf-8 -*-\n" + (std::string) expr;
62 start = Py_eval_input;
65 start = Py_single_input;
68 start = Py_file_input;
74 PyObject *
result = PyRun_String(buffer.c_str(), start, global.ptr(), local.ptr());
78 return reinterpret_steal<object>(
result);
81 template <eval_mode mode = eval_expr,
size_t N>
82 object eval(
const char (&
s)[
N],
object global =
globals(),
object local =
object()) {
85 return eval<mode>(expr, std::move(global), std::move(local));
88 inline void exec(
const str &expr,
object global =
globals(),
object local =
object()) {
89 eval<eval_statements>(expr, std::move(global), std::move(local));
93 void exec(
const char (&
s)[N],
object global =
globals(),
object local =
object()) {
94 eval<eval_statements>(
s, std::move(global), std::move(local));
97 #if defined(PYPY_VERSION) 98 template <eval_mode mode = eval_statements>
102 template <eval_mode mode = eval_statements>
106 template <eval_mode mode = eval_statements>
111 template <eval_mode mode = eval_statements>
122 start = Py_eval_input;
125 start = Py_single_input;
128 start = Py_file_input;
135 std::string fname_str = (std::string) fname;
136 FILE *
f = _Py_fopen_obj(fname.
ptr(),
"r");
139 pybind11_fail(
"File \"" + fname_str +
"\" could not be opened!");
142 if (!global.contains(
"__file__")) {
143 global[
"__file__"] = std::move(fname);
147 = PyRun_FileEx(f, fname_str.c_str(), start, global.ptr(), local.ptr(), closeFile);
152 return reinterpret_steal<object>(
result);
Evaluate a string containing a single statement. Returns none.
#define PYBIND11_BUILTINS_MODULE
object eval(const str &expr, object global=globals(), object local=object())
object eval_file(str fname, object global=globals(), object local=object())
Evaluate a string containing an isolated expression.
#define PYBIND11_NAMESPACE
void exec(const str &expr, object global=globals(), object local=object())
PyExc_RuntimeError [[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
Evaluate a string containing a sequence of statement. Returns none.
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
static module_ import(const char *name)
Import and return a module or throws error_already_set.
void ensure_builtins_in_globals(object &global)
static const DiscreteKey mode(modeKey, 2)
PyObject * ptr() const
Return the underlying PyObject * pointer.
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)