Loading...
Searching...
No Matches
pyinterp.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2023, John Wiegley. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of New Artisans LLC nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
44#pragma once
45
46#include "session.h"
47
48#if HAVE_BOOST_PYTHON
49
50namespace ledger {
51
52class python_module_t : public scope_t, public noncopyable
53{
54public:
55 string module_name;
56 boost::python::object module_object;
57 boost::python::dict module_globals;
58
59 explicit python_module_t(const string& name);
60 explicit python_module_t(const string& name, boost::python::object obj);
61
62 void import_module(const string& name, bool import_direct = false);
63
64 virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind,
65 const string& name);
66
67 void define_global(const string& name, boost::python::object obj) {
68 module_globals[name] = obj;
69 }
70
71 virtual string description() {
72 return module_name;
73 }
74};
75
76typedef std::map<PyObject *, shared_ptr<python_module_t> > python_module_map_t;
77
78class python_interpreter_t : public session_t
79{
80public:
81 bool is_initialized;
82
85
86 shared_ptr<python_module_t> import_module(const string& name) {
88 if (name != "__main__")
89 main_module->define_global(name, mod->module_object);
90 return mod;
91 }
92
93 python_interpreter_t() : session_t(), is_initialized(false) {
95 }
96 virtual ~python_interpreter_t() {
98 if (is_initialized)
100 }
101
102 void initialize();
103 void hack_system_paths();
104
105 boost::python::object import_option(const string& name);
106
107 enum py_eval_mode_t {
111 };
112
113 boost::python::object eval(std::istream& in, py_eval_mode_t mode = PY_EVAL_EXPR);
114 boost::python::object eval(const string& str, py_eval_mode_t mode = PY_EVAL_EXPR);
115 boost::python::object eval(const char * c_str, py_eval_mode_t mode = PY_EVAL_EXPR) {
116 return eval(string(c_str), mode);
117 }
118
119 value_t python_command(call_scope_t& scope);
120
121 class functor_t {
122 functor_t();
123
124 protected:
125 boost::python::object func;
126
127 public:
128 string name;
129
130 functor_t(boost::python::object _func, const string& _name)
131 : func(_func), name(_name) {
132 TRACE_CTOR(functor_t, "boost::python::object, const string&");
133 }
134 functor_t(const functor_t& other)
135 : func(other.func), name(other.name) {
136 TRACE_CTOR(functor_t, "copy");
137 }
138 virtual ~functor_t() throw() {
139 TRACE_DTOR(functor_t);
140 }
141 virtual value_t operator()(call_scope_t& args);
142 };
143
144 option_t<python_interpreter_t> * lookup_option(const char * p);
145
146 virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind,
147 const string& name);
148
150 parent->import_option(str);
151 });
152};
153
155
156} // namespace ledger
157
158#endif // HAVE_BOOST_PYTHON
#define TRACE_DTOR(cls)
Definition utils.h:144
#define TRACE_CTOR(cls, args)
Definition utils.h:143
#define DO_(var)
Definition option.h:219
#define OPTION_(type, name, body)
Definition option.h:273
T & downcast(U &object)
Definition utils.h:468
intrusive_ptr< op_t > ptr_op_t
Definition expr.h:57