17 #ifndef polybori_embed_h_
18 #define polybori_embed_h_
23 #include <boost/python.hpp>
24 #include <boost/python/stl_iterator.hpp>
33 #ifndef PBORI_PYTHONPATH
34 #define PBORI_PYTHONPATH "."
40 using boost::python::str;
41 using boost::python::import;
42 using boost::python::handle;
43 using boost::python::borrowed;
44 using boost::python::extract;
45 using boost::python::stl_input_iterator;
46 using boost::python::error_already_set;
79 static void init() { instance(); }
82 static boost::python::object&
globals() {
return instance().m_globals; }
87 m_owns_python(false), m_globals() {
88 if (!Py_IsInitialized()) init_python();
89 set_python_defaults();
94 static Interpreter& instance() {
95 static Interpreter init_interpreter;
96 return init_interpreter;
102 m_owns_python =
true;
106 void set_python_defaults() {
108 const char* argv =
"";
109 PySys_SetArgv(1, (
char**)&argv);
111 PyRun_SimpleString(
"from polybori.frontend import *");
113 m_globals =
import(
"__main__").attr(
"__dict__");
122 boost::python::object m_globals;
125 template <
class Type>
135 const Type&
get()
const{
return m_val; }
145 public boost::python::dict {
147 typedef boost::python::dict base;
151 template <
class Type>
152 dict(
const Type& obj): base(obj) {}
167 public boost::python::tuple {
169 typedef boost::python::tuple base;
173 template <
class Type>
174 tuple(
const Type& obj): base(obj) {}
189 public boost::python::object {
192 typedef boost::python::object base;
196 template <
class Type>
206 return object(handle<>(borrowed(PyObject_Call(base::ptr(), args.
get().ptr(),
207 kwds.get().get().ptr()))));
211 template <
class Type>
213 return static_cast<const boost::python::object&
>(*this)(arg);
215 template <
class Type1,
class Type2>
216 self operator()(
const Type1& arg1,
const Type2& arg2)
const {
217 return static_cast<const boost::python::object&
>(*this)(arg1, arg2);
219 template <
class Type1,
class Type2,
class Type3>
220 self operator()(
const Type1& arg1,
const Type2& arg2,
const Type3& arg3)
const {
221 return static_cast<const boost::python::object&
>(*this)(arg1, arg2, arg3);
223 template <
class Type1,
class Type2,
class Type3,
class Type4>
224 self operator()(
const Type1& arg1,
const Type2& arg2,
const Type3& arg3,
225 const Type4& arg4)
const {
226 return static_cast<const boost::python::object&
>(*this)(arg1, arg2, arg3,
233 template <
class Type>
237 template <
class Type>
238 self operator()(
const Type& arg,
240 return self::operator()(
DerefProxy<tuple>(boost::python::make_tuple(arg)), kwds);
242 template <
class Type1,
class Type2>
243 self operator()(
const Type1& arg1,
const Type2& arg2,
245 return self::operator()(
DerefProxy<tuple>(boost::python::make_tuple(arg1, arg2)), kwds);
247 template <
class Type1,
class Type2,
class Type3>
248 self operator()(
const Type1& arg1,
const Type2& arg2,
const Type3& arg3,
250 return self::operator()(
DerefProxy<tuple>(boost::python::make_tuple(arg1, arg2, arg3)), kwds);
252 template <
class Type1,
class Type2,
class Type3,
class Type4>
253 self operator()(
const Type1& arg1,
const Type2& arg2,
const Type3& arg3,
255 return self::operator()(
DerefProxy<tuple>(boost::python::make_tuple(arg1, arg2, arg3,
262 operator bool()
const {
263 return extract<bool>(*this);
265 operator int()
const {
266 return extract<int>(*this);
268 operator long()
const {
269 return extract<long>(*this);
271 operator std::string()
const {
272 return extract<std::string>(*this);
276 return extract<const Ring&>(*this);
279 return extract<Polynomial>(*this);
282 return extract<Monomial>(*this);
285 return extract<const Variable&>(*this);
288 return extract<BooleSet>(*this);
290 template <
class Type>
291 operator std::vector<Type>()
const {
292 stl_input_iterator<self> begin(*
this), end;
293 return std::vector<Type>(begin, end);
295 template <
class Type>
296 operator std::list<Type>()
const {
297 stl_input_iterator<self> begin(*
this), end;
298 return std::list<Type>(begin, end);
300 template <
class Type>
301 operator std::set<Type>()
const {
302 stl_input_iterator<self> begin(*
this), end;
303 return std::set<Type>(begin, end);
314 Interpreter::globals(),
315 Interpreter::globals());
317 catch( error_already_set ) {
327 Interpreter::globals(),
328 Interpreter::globals());
330 catch( error_already_set ) {
340 Interpreter::globals(),
341 Interpreter::globals());
343 catch( error_already_set ) {
352 PyRun_SimpleString(code);
359 namespace boost {
namespace python {
namespace converter {
361 struct object_manager_traits<
PBORI::dict>:
362 object_manager_traits<boost::python::dict> {};
365 struct object_manager_traits<
PBORI::tuple>:
366 object_manager_traits<boost::python::tuple> {};
369 struct object_manager_traits<
PBORI::object>:
370 object_manager_traits<boost::python::object> {};
374 #define BEGIN_PBORI_EMBED() try { USING_NAMESPACE_PBORI; while(0)
375 #define END_PBORI_EMBED() } catch(PBORI::error_already_set) { PyErr_Print(); } while(0)