]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/python/src/exec.cpp
update sources to v12.2.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 return eval(python::extract<char const *>(string), global, local);
20 }
21
22 object BOOST_PYTHON_DECL eval(char const *string, object global, object local)
23 {
24 // Set suitable default values for global and local dicts.
25 if (global.is_none())
26 {
27 if (PyObject *g = PyEval_GetGlobals())
28 global = object(detail::borrowed_reference(g));
29 else
30 global = dict();
31 }
32 if (local.is_none()) local = global;
33 // should be 'char const *' but older python versions don't use 'const' yet.
34 char *s = const_cast<char *>(string);
35 PyObject* result = PyRun_String(s, Py_eval_input, global.ptr(), local.ptr());
36 if (!result) throw_error_already_set();
37 return object(detail::new_reference(result));
38 }
39
40 object BOOST_PYTHON_DECL exec(str string, object global, object local)
41 {
42 return exec(python::extract<char const *>(string), global, local);
43 }
44
45 object BOOST_PYTHON_DECL exec(char const *string, object global, object local)
46 {
47 // Set suitable default values for global and local dicts.
48 if (global.is_none())
49 {
50 if (PyObject *g = PyEval_GetGlobals())
51 global = object(detail::borrowed_reference(g));
52 else
53 global = dict();
54 }
55 if (local.is_none()) local = global;
56 // should be 'char const *' but older python versions don't use 'const' yet.
57 char *s = const_cast<char *>(string);
58 PyObject* result = PyRun_String(s, Py_file_input, global.ptr(), local.ptr());
59 if (!result) throw_error_already_set();
60 return object(detail::new_reference(result));
61 }
62
63 object BOOST_PYTHON_DECL exec_statement(str string, object global, object local)
64 {
65 return exec_statement(python::extract<char const *>(string), global, local);
66 }
67
68 object BOOST_PYTHON_DECL exec_statement(char const *string, object global, object local)
69 {
70 // Set suitable default values for global and local dicts.
71 if (global.is_none())
72 {
73 if (PyObject *g = PyEval_GetGlobals())
74 global = object(detail::borrowed_reference(g));
75 else
76 global = dict();
77 }
78 if (local.is_none()) local = global;
79 // should be 'char const *' but older python versions don't use 'const' yet.
80 char *s = const_cast<char *>(string);
81 PyObject* result = PyRun_String(s, Py_single_input, global.ptr(), local.ptr());
82 if (!result) throw_error_already_set();
83 return object(detail::new_reference(result));
84 }
85
86 // Execute python source code from file filename.
87 // global and local are the global and local scopes respectively,
88 // used during execution.
89 object BOOST_PYTHON_DECL exec_file(str filename, object global, object local)
90 {
91 return exec_file(python::extract<char const *>(filename), global, local);
92 }
93
94 object BOOST_PYTHON_DECL exec_file(char const *filename, object global, object local)
95 {
96 // Set suitable default values for global and local dicts.
97 if (global.is_none())
98 {
99 if (PyObject *g = PyEval_GetGlobals())
100 global = object(detail::borrowed_reference(g));
101 else
102 global = dict();
103 }
104 if (local.is_none()) local = global;
105 // should be 'char const *' but older python versions don't use 'const' yet.
106 char *f = const_cast<char *>(filename);
107 // Let python open the file to avoid potential binary incompatibilities.
108 #if PY_VERSION_HEX >= 0x03040000
109 FILE *fs = _Py_fopen(f, "r");
110 #elif PY_VERSION_HEX >= 0x03000000
111 PyObject *fo = Py_BuildValue("s", f);
112 FILE *fs = _Py_fopen(fo, "r");
113 Py_DECREF(fo);
114 #else
115 PyObject *pyfile = PyFile_FromString(f, const_cast<char*>("r"));
116 if (!pyfile) throw std::invalid_argument(std::string(f) + " : no such file");
117 python::handle<> file(pyfile);
118 FILE *fs = PyFile_AsFile(file.get());
119 #endif
120 PyObject* result = PyRun_File(fs,
121 f,
122 Py_file_input,
123 global.ptr(), local.ptr());
124 if (!result) throw_error_already_set();
125 return object(detail::new_reference(result));
126 }
127
128 } // namespace boost::python
129 } // namespace boost