]>
git.proxmox.com Git - ceph.git/blob - 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)
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>
17 object BOOST_PYTHON_DECL
eval(str string
, object global
, object local
)
19 // Set suitable default values for global and local dicts.
22 if (PyObject
*g
= PyEval_GetGlobals())
23 global
= object(detail::borrowed_reference(g
));
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
));
35 object BOOST_PYTHON_DECL
exec(str string
, object global
, object local
)
37 // Set suitable default values for global and local dicts.
40 if (PyObject
*g
= PyEval_GetGlobals())
41 global
= object(detail::borrowed_reference(g
));
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
));
53 object BOOST_PYTHON_DECL
exec_statement(str string
, object global
, object local
)
55 // Set suitable default values for global and local dicts.
58 if (PyObject
*g
= PyEval_GetGlobals())
59 global
= object(detail::borrowed_reference(g
));
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
));
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
)
76 // Set suitable default values for global and local dicts.
79 if (PyObject
*g
= PyEval_GetGlobals())
80 global
= object(detail::borrowed_reference(g
));
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");
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());
100 PyObject
* result
= PyRun_File(fs
,
103 global
.ptr(), local
.ptr());
104 if (!result
) throw_error_already_set();
105 return object(detail::new_reference(result
));
108 } // namespace boost::python