]>
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 return eval(python::extract
<char const *>(string
), global
, local
);
22 object BOOST_PYTHON_DECL
eval(char const *string
, object global
, object local
)
24 // Set suitable default values for global and local dicts.
27 if (PyObject
*g
= PyEval_GetGlobals())
28 global
= object(detail::borrowed_reference(g
));
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
));
40 object BOOST_PYTHON_DECL
exec(str string
, object global
, object local
)
42 return exec(python::extract
<char const *>(string
), global
, local
);
45 object BOOST_PYTHON_DECL
exec(char const *string
, object global
, object local
)
47 // Set suitable default values for global and local dicts.
50 if (PyObject
*g
= PyEval_GetGlobals())
51 global
= object(detail::borrowed_reference(g
));
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
));
63 object BOOST_PYTHON_DECL
exec_statement(str string
, object global
, object local
)
65 return exec_statement(python::extract
<char const *>(string
), global
, local
);
68 object BOOST_PYTHON_DECL
exec_statement(char const *string
, object global
, object local
)
70 // Set suitable default values for global and local dicts.
73 if (PyObject
*g
= PyEval_GetGlobals())
74 global
= object(detail::borrowed_reference(g
));
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
));
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
)
91 return exec_file(python::extract
<char const *>(filename
), global
, local
);
94 object BOOST_PYTHON_DECL
exec_file(char const *filename
, object global
, object local
)
96 // Set suitable default values for global and local dicts.
99 if (PyObject
*g
= PyEval_GetGlobals())
100 global
= object(detail::borrowed_reference(g
));
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");
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());
120 PyObject
* result
= PyRun_File(fs
,
123 global
.ptr(), local
.ptr());
124 if (!result
) throw_error_already_set();
125 return object(detail::new_reference(result
));
128 } // namespace boost::python