]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/python/src/exec.cpp
bump version to 18.2.4-pve3
[ceph.git] / ceph / src / boost / libs / python / src / exec.cpp
CommitLineData
7c673cae
FG
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
12namespace boost
13{
14namespace python
15{
16
17object BOOST_PYTHON_DECL eval(str string, object global, object local)
b32b8144
FG
18{
19 return eval(python::extract<char const *>(string), global, local);
20}
21
22object BOOST_PYTHON_DECL eval(char const *string, object global, object local)
7c673cae
FG
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.
b32b8144 34 char *s = const_cast<char *>(string);
7c673cae
FG
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
40object BOOST_PYTHON_DECL exec(str string, object global, object local)
b32b8144
FG
41{
42 return exec(python::extract<char const *>(string), global, local);
43}
44
45object BOOST_PYTHON_DECL exec(char const *string, object global, object local)
7c673cae
FG
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.
b32b8144 57 char *s = const_cast<char *>(string);
7c673cae
FG
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
63object BOOST_PYTHON_DECL exec_statement(str string, object global, object local)
b32b8144
FG
64{
65 return exec_statement(python::extract<char const *>(string), global, local);
66}
67
68object BOOST_PYTHON_DECL exec_statement(char const *string, object global, object local)
7c673cae
FG
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.
b32b8144 80 char *s = const_cast<char *>(string);
7c673cae
FG
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.
89object BOOST_PYTHON_DECL exec_file(str filename, object global, object local)
b32b8144
FG
90{
91 return exec_file(python::extract<char const *>(filename), global, local);
92}
93
94object BOOST_PYTHON_DECL exec_file(char const *filename, object global, object local)
7c673cae
FG
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.
b32b8144 106 char *f = const_cast<char *>(filename);
20effc67
TL
107#if PY_VERSION_HEX >= 0x03010000
108 // Let python manage any UTF bits to avoid potential incompatibilities.
109 PyObject *fo = Py_BuildValue("s", f);
110 PyObject *fb = Py_None;
111 PyUnicode_FSConverter(fo, &fb);
112 f = PyBytes_AsString(fb);
113 FILE *fs = fopen(f, "r");
114 Py_DECREF(fo);
115 Py_DECREF(fb);
7c673cae 116#elif PY_VERSION_HEX >= 0x03000000
20effc67 117 // Let python open the file to avoid potential binary incompatibilities.
7c673cae 118 PyObject *fo = Py_BuildValue("s", f);
1e59de90 119 FILE *fs = fopen(fo, "r");
7c673cae
FG
120 Py_DECREF(fo);
121#else
20effc67 122 // Let python open the file to avoid potential binary incompatibilities.
7c673cae
FG
123 PyObject *pyfile = PyFile_FromString(f, const_cast<char*>("r"));
124 if (!pyfile) throw std::invalid_argument(std::string(f) + " : no such file");
125 python::handle<> file(pyfile);
126 FILE *fs = PyFile_AsFile(file.get());
127#endif
128 PyObject* result = PyRun_File(fs,
129 f,
130 Py_file_input,
131 global.ptr(), local.ptr());
132 if (!result) throw_error_already_set();
133 return object(detail::new_reference(result));
134}
135
136} // namespace boost::python
137} // namespace boost