29 template <eval_mode mode = eval_expr>
36 std::string
buffer =
"# -*- coding: utf-8 -*-\n" + (std::string) expr;
40 case eval_expr: start = Py_eval_input;
break;
46 PyObject *
result = PyRun_String(buffer.c_str(), start, global.ptr(), local.ptr());
49 return reinterpret_steal<object>(
result);
52 template <eval_mode mode = eval_expr,
size_t N>
53 object eval(
const char (&
s)[
N],
object global =
globals(),
object local =
object()) {
57 return eval<mode>(expr, global, local);
60 inline void exec(
str expr,
object global =
globals(),
object local =
object()) {
61 eval<eval_statements>(expr, global, local);
65 void exec(
const char (&
s)[N],
object global =
globals(),
object local =
object()) {
66 eval<eval_statements>(
s, global, local);
69 #if defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x3000000 70 template <eval_mode mode = eval_statements>
74 template <eval_mode mode = eval_statements>
78 template <eval_mode mode = eval_statements>
83 template <eval_mode mode = eval_statements>
90 case eval_expr: start = Py_eval_input;
break;
97 std::string fname_str = (std::string) fname;
98 #if PY_VERSION_HEX >= 0x03040000 99 FILE *
f = _Py_fopen_obj(fname.
ptr(),
"r");
100 #elif PY_VERSION_HEX >= 0x03000000 101 FILE *f = _Py_fopen(fname.
ptr(),
"r");
104 auto fobj = reinterpret_steal<object>(PyFile_FromString(
105 const_cast<char *>(fname_str.c_str()),
106 const_cast<char*>(
"r")));
109 f = PyFile_AsFile(fobj.ptr());
114 pybind11_fail(
"File \"" + fname_str +
"\" could not be opened!");
117 #if PY_VERSION_HEX < 0x03000000 && defined(PYPY_VERSION) 118 PyObject *
result = PyRun_File(f, fname_str.c_str(), start, global.ptr(),
122 PyObject *result = PyRun_FileEx(f, fname_str.c_str(), start, global.ptr(),
123 local.ptr(), closeFile);
128 return reinterpret_steal<object>(
result);
Evaluate a string containing a single statement. Returns none.
object eval(str expr, object global=globals(), object local=object())
PyObject * ptr() const
Return the underlying PyObject * pointer.
object eval_file(str fname, object global=globals(), object local=object())
Evaluate a string containing an isolated expression.
#define PYBIND11_NAMESPACE
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 exec(str expr, object global=globals(), object local=object())
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)