eval.h
Go to the documentation of this file.
1 /*
2  pybind11/exec.h: Support for evaluating Python expressions and statements
3  from strings and files
4 
5  Copyright (c) 2016 Klemens Morgenstern <klemens.morgenstern@ed-chemnitz.de> and
6  Wenzel Jakob <wenzel.jakob@epfl.ch>
7 
8  All rights reserved. Use of this source code is governed by a
9  BSD-style license that can be found in the LICENSE file.
10 */
11 
12 #pragma once
13 
14 #include "pybind11.h"
15 
17 
18 enum eval_mode {
21 
24 
27 };
28 
29 template <eval_mode mode = eval_expr>
30 object eval(str expr, object global = globals(), object local = object()) {
31  if (!local)
32  local = global;
33 
34  /* PyRun_String does not accept a PyObject / encoding specifier,
35  this seems to be the only alternative */
36  std::string buffer = "# -*- coding: utf-8 -*-\n" + (std::string) expr;
37 
38  int start;
39  switch (mode) {
40  case eval_expr: start = Py_eval_input; break;
41  case eval_single_statement: start = Py_single_input; break;
42  case eval_statements: start = Py_file_input; break;
43  default: pybind11_fail("invalid evaluation mode");
44  }
45 
46  PyObject *result = PyRun_String(buffer.c_str(), start, global.ptr(), local.ptr());
47  if (!result)
48  throw error_already_set();
49  return reinterpret_steal<object>(result);
50 }
51 
52 template <eval_mode mode = eval_expr, size_t N>
53 object eval(const char (&s)[N], object global = globals(), object local = object()) {
54  /* Support raw string literals by removing common leading whitespace */
55  auto expr = (s[0] == '\n') ? str(module::import("textwrap").attr("dedent")(s))
56  : str(s);
57  return eval<mode>(expr, global, local);
58 }
59 
60 inline void exec(str expr, object global = globals(), object local = object()) {
61  eval<eval_statements>(expr, global, local);
62 }
63 
64 template <size_t N>
65 void exec(const char (&s)[N], object global = globals(), object local = object()) {
66  eval<eval_statements>(s, global, local);
67 }
68 
69 #if defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x3000000
70 template <eval_mode mode = eval_statements>
71 object eval_file(str, object, object) {
72  pybind11_fail("eval_file not supported in PyPy3. Use eval");
73 }
74 template <eval_mode mode = eval_statements>
75 object eval_file(str, object) {
76  pybind11_fail("eval_file not supported in PyPy3. Use eval");
77 }
78 template <eval_mode mode = eval_statements>
79 object eval_file(str) {
80  pybind11_fail("eval_file not supported in PyPy3. Use eval");
81 }
82 #else
83 template <eval_mode mode = eval_statements>
84 object eval_file(str fname, object global = globals(), object local = object()) {
85  if (!local)
86  local = global;
87 
88  int start;
89  switch (mode) {
90  case eval_expr: start = Py_eval_input; break;
91  case eval_single_statement: start = Py_single_input; break;
92  case eval_statements: start = Py_file_input; break;
93  default: pybind11_fail("invalid evaluation mode");
94  }
95 
96  int closeFile = 1;
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");
102 #else
103  /* No unicode support in open() :( */
104  auto fobj = reinterpret_steal<object>(PyFile_FromString(
105  const_cast<char *>(fname_str.c_str()),
106  const_cast<char*>("r")));
107  FILE *f = nullptr;
108  if (fobj)
109  f = PyFile_AsFile(fobj.ptr());
110  closeFile = 0;
111 #endif
112  if (!f) {
113  PyErr_Clear();
114  pybind11_fail("File \"" + fname_str + "\" could not be opened!");
115  }
116 
117 #if PY_VERSION_HEX < 0x03000000 && defined(PYPY_VERSION)
118  PyObject *result = PyRun_File(f, fname_str.c_str(), start, global.ptr(),
119  local.ptr());
120  (void) closeFile;
121 #else
122  PyObject *result = PyRun_FileEx(f, fname_str.c_str(), start, global.ptr(),
123  local.ptr(), closeFile);
124 #endif
125 
126  if (!result)
127  throw error_already_set();
128  return reinterpret_steal<object>(result);
129 }
130 #endif
131 
Evaluate a string containing a single statement. Returns none.
Definition: eval.h:23
object eval(str expr, object global=globals(), object local=object())
Definition: eval.h:30
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:184
object eval_file(str fname, object global=globals(), object local=object())
Definition: eval.h:84
Evaluate a string containing an isolated expression.
Definition: eval.h:20
#define N
Definition: gksort.c:12
PyExc_RuntimeError[[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
Values result
Evaluate a string containing a sequence of statement. Returns none.
Definition: eval.h:26
Definition: pytypes.h:928
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
RealScalar s
eval_mode
Definition: eval.h:18
static module_ import(const char *name)
Import and return a module or throws error_already_set.
Definition: pybind11.h:914
void exec(str expr, object global=globals(), object local=object())
Definition: eval.h:60
dict globals()
Definition: pybind11.h:948
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:02