Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
PythonCall.h
Go to the documentation of this file.
1
19#ifndef PYSTON_PYTHONCALL_H
20#define PYSTON_PYTHONCALL_H
21
22#include "Node.h"
23#include "Pyston/Exceptions.h"
24#include "Pyston/GIL.h"
26#include <boost/python/object.hpp>
27#include <boost/python/tuple.hpp>
28
29namespace Pyston {
30
35template <typename T>
36class PythonCall : public Node<T> {
37public:
38 explicit PythonCall(boost::python::object callable) : m_callable(callable) {}
39
40 std::string repr() const override {
41 return "PythonCall";
42 }
43
44 void visit(Visitor& visitor) const override {
45 visitor.enter(this);
46 visitor.exit(this);
47 }
48
49 T eval(const Context& context, const Arguments& arguments) const override {
50 GILLocker locker;
51 sharedContext = context;
52 try {
53 boost::python::list py_args;
54 for (auto& a : arguments) {
55 py_args.append(boost::apply_visitor(to_python_visitor(), a));
56 }
57 auto obj = m_callable(*boost::python::tuple(py_args));
58 return boost::python::extract<T>(obj);
59 } catch (const boost::python::error_already_set&) {
60 throw Exception();
61 }
62 }
63
64private:
68 struct to_python_visitor : public boost::static_visitor<boost::python::object> {
69 template <typename From>
70 boost::python::object operator()(From v) const {
71 return boost::python::object(v);
72 }
73 };
74
75 boost::python::object m_callable;
76};
77
78} // end of namespace Pyston
79
80#endif // PYSTON_PYTHONCALL_H
std::string repr() const override
Definition PythonCall.h:40
void visit(Visitor &visitor) const override
Definition PythonCall.h:44
T eval(const Context &context, const Arguments &arguments) const override
Definition PythonCall.h:49
boost::python::object m_callable
Definition PythonCall.h:75
PythonCall(boost::python::object callable)
Definition PythonCall.h:38
virtual void enter(const NodeBase *)=0
virtual void exit(const NodeBase *)=0
thread_local Context sharedContext
boost::python::object operator()(From v) const
Definition PythonCall.h:70