]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/python/src/exec.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / python / src / exec.cpp
1 // Copyright Stefan Seefeld 2005.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5
6 #include <boost/python/exec.hpp>
7 #include <boost/python/borrowed.hpp>
8 #include <boost/python/dict.hpp>
9 #include <boost/python/extract.hpp>
10 #include <boost/python/handle.hpp>
11
12 namespace boost
13 {
14 namespace python
15 {
16
17 object BOOST_PYTHON_DECL eval(str string, object global, object local)
18 {
19 // Set suitable default values for global and local dicts.
20 if (global.is_none())
21 {
22 if (PyObject *g = PyEval_GetGlobals())
23 global = object(detail::borrowed_reference(g));
24 else
25 global = dict();
26 }
27 if (local.is_none()) local = global;
28 // should be 'char const *' but older python versions don't use 'const' yet.
29 char *s = python::extract<char *>(string);
30 PyObject* result = PyRun_String(s, Py_eval_input, global.ptr(), local.ptr());
31 if (!result) throw_error_already_set();
32 return object(detail::new_reference(result));
33 }
34
35 object BOOST_PYTHON_DECL exec(str string, object global, object local)
36 {
37 // Set suitable default values for global and local dicts.
38 if (global.is_none())
39 {
40 if (PyObject *g = PyEval_GetGlobals())
41 global = object(detail::borrowed_reference(g));
42 else
43 global = dict();
44 }
45 if (local.is_none()) local = global;
46 // should be 'char const *' but older python versions don't use 'const' yet.
47 char *s = python::extract<char *>(string);
48 PyObject* result = PyRun_String(s, Py_file_input, global.ptr(), local.ptr());
49 if (!result) throw_error_already_set();
50 return object(detail::new_reference(result));
51 }
52
53 object BOOST_PYTHON_DECL exec_statement(str string, object global, object local)
54 {
55 // Set suitable default values for global and local dicts.
56 if (global.is_none())
57 {
58 if (PyObject *g = PyEval_GetGlobals())
59 global = object(detail::borrowed_reference(g));
60 else
61 global = dict();
62 }
63 if (local.is_none()) local = global;
64 // should be 'char const *' but older python versions don't use 'const' yet.
65 char *s = python::extract<char *>(string);
66 PyObject* result = PyRun_String(s, Py_single_input, global.ptr(), local.ptr());
67 if (!result) throw_error_already_set();
68 return object(detail::new_reference(result));
69 }
70
71 // Execute python source code from file filename.
72 // global and local are the global and local scopes respectively,
73 // used during execution.
74 object BOOST_PYTHON_DECL exec_file(str filename, object global, object local)
75 {
76 // Set suitable default values for global and local dicts.
77 if (global.is_none())
78 {
79 if (PyObject *g = PyEval_GetGlobals())
80 global = object(detail::borrowed_reference(g));
81 else
82 global = dict();
83 }
84 if (local.is_none()) local = global;
85 // should be 'char const *' but older python versions don't use 'const' yet.
86 char *f = python::extract<char *>(filename);
87 // Let python open the file to avoid potential binary incompatibilities.
88 #if PY_VERSION_HEX >= 0x03040000
89 FILE *fs = _Py_fopen(f, "r");
90 #elif PY_VERSION_HEX >= 0x03000000
91 PyObject *fo = Py_BuildValue("s", f);
92 FILE *fs = _Py_fopen(fo, "r");
93 Py_DECREF(fo);
94 #else
95 PyObject *pyfile = PyFile_FromString(f, const_cast<char*>("r"));
96 if (!pyfile) throw std::invalid_argument(std::string(f) + " : no such file");
97 python::handle<> file(pyfile);
98 FILE *fs = PyFile_AsFile(file.get());
99 #endif
100 PyObject* result = PyRun_File(fs,
101 f,
102 Py_file_input,
103 global.ptr(), local.ptr());
104 if (!result) throw_error_already_set();
105 return object(detail::new_reference(result));
106 }
107
108 } // namespace boost::python
109 } // namespace boost