20 #if PY_VERSION_HEX < 0x03080000
25 if (!global.contains(
"__builtins__"))
45 template <eval_mode mode = eval_expr>
54 std::string
buffer =
"# -*- coding: utf-8 -*-\n" + (std::string) expr;
58 case eval_expr: start = Py_eval_input;
break;
64 PyObject *result = PyRun_String(
buffer.c_str(), start, global.
ptr(), local.ptr());
67 return reinterpret_steal<object>(result);
70 template <eval_mode mode = eval_expr,
size_t N>
71 object eval(
const char (&s)[N],
object global =
globals(),
object local =
object()) {
75 return eval<mode>(expr, global, local);
78 inline void exec(
str expr,
object global =
globals(),
object local =
object()) {
79 eval<eval_statements>(expr, global, local);
83 void exec(
const char (&s)[N],
object global =
globals(),
object local =
object()) {
84 eval<eval_statements>(s, global, local);
87 #if defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x03000000
88 template <eval_mode mode = eval_statements>
92 template <eval_mode mode = eval_statements>
96 template <eval_mode mode = eval_statements>
101 template <eval_mode mode = eval_statements>
110 case eval_expr: start = Py_eval_input;
break;
117 std::string fname_str = (std::string) fname;
118 #if PY_VERSION_HEX >= 0x03040000
119 FILE *f = _Py_fopen_obj(fname.
ptr(),
"r");
120 #elif PY_VERSION_HEX >= 0x03000000
121 FILE *f = _Py_fopen(fname.
ptr(),
"r");
124 auto fobj = reinterpret_steal<object>(PyFile_FromString(
125 const_cast<char *
>(fname_str.c_str()),
126 const_cast<char*
>(
"r")));
129 f = PyFile_AsFile(fobj.ptr());
134 pybind11_fail(
"File \"" + fname_str +
"\" could not be opened!");
137 #if PY_VERSION_HEX < 0x03000000 && defined(PYPY_VERSION)
138 PyObject *result = PyRun_File(f, fname_str.c_str(), start, global.ptr(),
142 PyObject *result = PyRun_FileEx(f, fname_str.c_str(), start, global.ptr(),
143 local.ptr(), closeFile);
148 return reinterpret_steal<object>(result);