You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.8 KiB

8 years ago
8 years ago
  1. /*
  2. pybind11/exec.h: Support for evaluating Python expressions and statements
  3. from strings and files
  4. Copyright (c) 2016 Klemens Morgenstern <klemens.morgenstern@ed-chemnitz.de> and
  5. Wenzel Jakob <wenzel.jakob@epfl.ch>
  6. All rights reserved. Use of this source code is governed by a
  7. BSD-style license that can be found in the LICENSE file.
  8. */
  9. #pragma once
  10. #include "pybind11.h"
  11. NAMESPACE_BEGIN(pybind11)
  12. enum eval_mode {
  13. /// Evaluate a string containing an isolated expression
  14. eval_expr,
  15. /// Evaluate a string containing a single statement. Returns \c none
  16. eval_single_statement,
  17. /// Evaluate a string containing a sequence of statement. Returns \c none
  18. eval_statements
  19. };
  20. template <eval_mode mode = eval_expr>
  21. object eval(str expr, object global = globals(), object local = object()) {
  22. if (!local)
  23. local = global;
  24. /* PyRun_String does not accept a PyObject / encoding specifier,
  25. this seems to be the only alternative */
  26. std::string buffer = "# -*- coding: utf-8 -*-\n" + (std::string) expr;
  27. int start;
  28. switch (mode) {
  29. case eval_expr: start = Py_eval_input; break;
  30. case eval_single_statement: start = Py_single_input; break;
  31. case eval_statements: start = Py_file_input; break;
  32. default: pybind11_fail("invalid evaluation mode");
  33. }
  34. PyObject *result = PyRun_String(buffer.c_str(), start, global.ptr(), local.ptr());
  35. if (!result)
  36. throw error_already_set();
  37. return reinterpret_steal<object>(result);
  38. }
  39. template <eval_mode mode = eval_expr, size_t N>
  40. object eval(const char (&s)[N], object global = globals(), object local = object()) {
  41. /* Support raw string literals by removing common leading whitespace */
  42. auto expr = (s[0] == '\n') ? str(module::import("textwrap").attr("dedent")(s))
  43. : str(s);
  44. return eval<mode>(expr, global, local);
  45. }
  46. inline void exec(str expr, object global = globals(), object local = object()) {
  47. eval<eval_statements>(expr, global, local);
  48. }
  49. template <size_t N>
  50. void exec(const char (&s)[N], object global = globals(), object local = object()) {
  51. eval<eval_statements>(s, global, local);
  52. }
  53. template <eval_mode mode = eval_statements>
  54. object eval_file(str fname, object global = globals(), object local = object()) {
  55. if (!local)
  56. local = global;
  57. int start;
  58. switch (mode) {
  59. case eval_expr: start = Py_eval_input; break;
  60. case eval_single_statement: start = Py_single_input; break;
  61. case eval_statements: start = Py_file_input; break;
  62. default: pybind11_fail("invalid evaluation mode");
  63. }
  64. int closeFile = 1;
  65. std::string fname_str = (std::string) fname;
  66. #if PY_VERSION_HEX >= 0x03040000
  67. FILE *f = _Py_fopen_obj(fname.ptr(), "r");
  68. #elif PY_VERSION_HEX >= 0x03000000
  69. FILE *f = _Py_fopen(fname.ptr(), "r");
  70. #else
  71. /* No unicode support in open() :( */
  72. auto fobj = reinterpret_steal<object>(PyFile_FromString(
  73. const_cast<char *>(fname_str.c_str()),
  74. const_cast<char*>("r")));
  75. FILE *f = nullptr;
  76. if (fobj)
  77. f = PyFile_AsFile(fobj.ptr());
  78. closeFile = 0;
  79. #endif
  80. if (!f) {
  81. PyErr_Clear();
  82. pybind11_fail("File \"" + fname_str + "\" could not be opened!");
  83. }
  84. #if PY_VERSION_HEX < 0x03000000 && defined(PYPY_VERSION)
  85. PyObject *result = PyRun_File(f, fname_str.c_str(), start, global.ptr(),
  86. local.ptr());
  87. (void) closeFile;
  88. #else
  89. PyObject *result = PyRun_FileEx(f, fname_str.c_str(), start, global.ptr(),
  90. local.ptr(), closeFile);
  91. #endif
  92. if (!result)
  93. throw error_already_set();
  94. return reinterpret_steal<object>(result);
  95. }
  96. NAMESPACE_END(pybind11)