/
python.h
347 lines (310 loc) · 9.83 KB
/
python.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
Copyright (C) 2003/2004 Alexandre Courbot <alexandrecourbot@linuxgames.com>
Part of the Adonthell Project http://adonthell.linuxgames.com
Adonthell is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Adonthell is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Adonthell; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* @file python.h
* @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
*
* @brief The python module main include file.
*
*
*/
#ifndef PYTHON_PYTHON_H_
#define PYTHON_PYTHON_H_
#include <Python.h>
#include "base/flat.h"
extern "C" {
PyObject *cxx_to_py (void *instance, const char *name, const bool & ownership);
void py_to_cxx (PyObject *instance, const char *name, void **retval);
}
/**
* Provides a bridge between C++ and Python,
* as well as some convenience classes to ease
* the use of the Adonthell library from Python.
*
*/
namespace python
{
/**
* @name Initialization and cleanup.
*
*/
//@{
/**
* Initialize the embedded Python interpreter.
*/
void init();
/**
* Shutdown the embedded Python interpreter.
*/
void cleanup();
//@}
/**
* @name Debugging.
*
*/
//@{
/**
* Dump any error information to stderr.
*
* This function should be called after every operation
* involving Python from C++, so the user can get informed if
* something went wrong. By default, all the methods of this
* module call it when appropriate.
*
*/
void show_traceback();
//@}
/**
* @name High-level functions.
*
*/
//@{
/**
* Add given path to Python's list of module directories.
* @param path the path to add.
* @return \b true on success, \b false otherwise.
*/
bool add_search_path(const std::string & path);
/**
* Execute a python statement and return resulting object.
* This is a new reference that needs to be Py_DECREF'd when done.
* @param statement Python code to execute.
* @param start one of Py_eval_input, Py_file_input or Py_single_input
* @param globals the global dictionary for the Python interpreter.
* @return \b result on success, \b NULL otherwise.
*/
PyObject* run_string (const std::string & statement, const int & start = Py_eval_input, PyObject *globals = NULL);
/**
* Execute a python statement and return success.
* @param statement Python code to execute.
* @return \b true on success, \b false otherwise.
*/
bool run_simple_string (const std::string & statement);
/**
* Import %python module with given name.
* @param name name of module to import
* @return imported module or \c NULL on error.
*/
PyObject * import_module(const std::string & name);
//@}
/**
* @name C++ to Python instance passing functions.
*
*/
//@{
/**
* Ownership of a python object made with pass_instance. Value
* python::c_owns means that once the Python object is deleted, the
* C++ instance will remain valid. On the contrary, python::python_owns
* will cause the C++ instance to be destroyed when the Python object is
* deleted.
*
*/
typedef enum { c_owns = 0, python_owns = 1 } ownership;
/**
* Default version of pass_instance - it will fetch the name of the class
* that is passed using a specialized version of get_type_name to create
* a Python object from a pointer.
*
* @param arg a pointer to the object to pass to Python.
* @param own ownership of the C++ object.
*
* @return a Python object representing \e arg.
*/
template <class A> inline
PyObject * pass_instance(A arg, const ownership own = c_owns)
{
// swig_type_info * tt = SWIG_Python_TypeQuery (arg->get_type_name ());
// if (tt) return SWIG_Python_NewPointerObj(arg, tt, own);
// fprintf (stderr, "*** pass_instance: type '%s' unknown to SWIG\n", arg->get_type_name ());
return cxx_to_py ((void *) arg, arg->get_type_name(), own);
}
/**
* Specialized version of pass_instance which makes a Python integer
* from a C++ one.
*
* @param arg the integer to be passed to Python.
* @param own useless here.
*
* @return a Python object representing \e arg.
*/
template <> inline
PyObject * pass_instance<s_int32>(s_int32 arg, const ownership own)
{
return PyInt_FromLong(arg);
show_traceback();
}
/**
* Specialized version of pass_instance which makes a Python boolean
* from a C++ one.
*
* @param arg the boolean to be passed to Python.
* @param own useless here.
*
* @return a Python object representing \e arg.
*/
template <> inline
PyObject * pass_instance<bool>(bool arg, const ownership own)
{
return PyInt_FromLong((s_int32)arg);
show_traceback();
}
/**
* Specialized version of pass_instance which makes a Python string
* from a C++ std::string.
*
* @param arg the std::string to be passed to Python.
* @param own useless here.
*
* @return a Python object representing \e arg.
*/
template <> inline
PyObject * pass_instance<const std::string &>(const std::string & arg, const ownership own)
{
return PyString_FromString(arg.c_str());
show_traceback();
}
/**
* Specialized version of pass_instance which makes a Python string
* from a C++ char *.
*
* @param arg the char * to be passed to Python.
* @param own useless here.
*
* @return a Python object representing \e arg.
*/
template <> inline
PyObject * pass_instance<const char *>(const char * arg, const ownership own)
{
return PyString_FromString((char *) arg);
show_traceback();
}
/**
* Sets the ownership of an object created with pass_instance.
*
* @param obj the object to change ownership of.
* @param o the new ownership.
*/
inline void set_ownership(PyObject * obj, const ownership o)
{
PyObject * n = PyInt_FromLong(o);
PyObject_SetAttrString (obj, (char*)"thisown", n);
Py_DECREF(n);
show_traceback();
}
//@}
/**
* @name Python to C++ instance passing functions.
*
*/
//@{
/**
* Retrieves the C++ intance of a Python object created with pass_instance.
*
* This is the default version of retrieve_instance. The template element A must
* be a pointer type.
*
* @param pyinstance The Python instance of the object to retrieve.
*
* @return A borrowed pointer to the C++ instance of the object.
*/
template <class A, class B> inline
A retrieve_instance(PyObject * pyinstance)
{
B *retvalue = NULL;
py_to_cxx (pyinstance, B::get_type_name_s(), (void **) &retvalue);
return retvalue;
}
/**
* Retrieves the C++ value of a Python integer.
*
* @param pyinstance The Python integer to retrieve.
*
* @return C++ value of pyinstance.
*/
template <> inline
s_int32 retrieve_instance<s_int32, s_int32>(PyObject * pyinstance)
{
s_int32 retval = PyInt_AsLong(pyinstance);
show_traceback();
return retval;
}
/**
* Retrieves the C++ value of a Python boolean.
*
* @param pyinstance The Python boolean to retrieve.
*
* @return C++ value of pyinstance.
*/
template <> inline
bool retrieve_instance<bool, bool>(PyObject * pyinstance)
{
bool retval = (bool)PyInt_AsLong(pyinstance);
show_traceback();
return retval;
}
/**
* Retrieves the C++ value of a Python string as a char *.
*
* @param pyinstance The Python string to retrieve.
*
* @return char * string value of pyinstance.
*/
template <> inline
const char * retrieve_instance<const char *, const char *>(PyObject * pyinstance)
{
const char * retval = PyString_AsString(pyinstance);
show_traceback();
return retval;
}
/**
* Retrieves the C++ value of a Python string as a std::string.
*
* @param pyinstance The Python string to retrieve.
*
* @return std::string value of pyinstance.
*/
template <> inline
std::string retrieve_instance<std::string, std::string>(PyObject * pyinstance)
{
const std::string &retval = std::string(PyString_AsString(pyinstance));
show_traceback();
return retval;
}
//@}
/**
* @name Loading / Saving
*/
//@{
/**
* Read the contents of a tuple from given stream.
* @param in flattener to read the tuple from.
* @param start index from where to start filling the tuple.
* @return a new tuple filled with the data read from stream.
*/
PyObject *get_tuple (base::flat & in, u_int16 start = 0);
/**
* Write the contents of a tuple to given stream. Only supports integer and
* string objects.
* @param tuple Python tuple whose contents to save.
* @param out flattener to store the tuple in.
* @param start index from where to start flattening the tuple.
*/
void put_tuple (PyObject *tuple, base::flat & out, u_int16 start = 0);
//@}
}
#include "python/callback.h"
#endif