-
Notifications
You must be signed in to change notification settings - Fork 5
/
python.cc
209 lines (177 loc) · 6.07 KB
/
python.cc
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
/*
$Id: python.cc,v 1.15 2009/04/16 21:06:09 ksterker Exp $
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.cc
* @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
*
* @brief Defines some common Python functions.
*
*
*/
#include "python/python.h"
#include "python/pool.h"
namespace python
{
// print stacktrace if a python error occurred
void show_traceback ()
{
if (PyErr_Occurred ())
{
PyErr_Print ();
fflush (stderr);
}
}
// start python interpreter
void init ()
{
Py_Initialize ();
pool::init ();
}
// shutdown python interpreter
void cleanup ()
{
pool::cleanup ();
Py_Finalize ();
}
// add path to python module search path
bool add_search_path (const std::string & path)
{
std::string buf = "import sys; sys.path.insert(0, \"";
buf += path;
buf += "\")";
return run_simple_string (buf);
}
// execute given python code
bool run_simple_string (const std::string & statements)
{
int ret = PyRun_SimpleString ((char *) statements.c_str ());
if (ret == -1)
{
show_traceback ();
return false;
}
return true;
}
// execute given python code
PyObject *run_string (const std::string & statement, const int & start, PyObject *globals)
{
if (globals == NULL) globals = PyDict_New();
Py_INCREF (globals);
PyObject *ret = PyRun_String ((char *) statement.c_str (), start, globals, NULL);
if (ret == NULL) show_traceback ();
Py_XDECREF (globals);
return ret;
}
// import a python module
PyObject *import_module (const std::string & name)
{
PyObject *ret = PyImport_ImportModule ((char *) name.c_str ());
if (ret == NULL) show_traceback ();
return ret;
}
// pad tuple
PyObject *pad_tuple (PyObject *tuple, const u_int16 & len)
{
// make sure the given arguments are a tuple
if (tuple && !PyTuple_Check (tuple))
{
fprintf (stderr, "*** error: python::pad_tuple: argument must be a tuple!\n");
return NULL;
}
// calculate size of argument tuple required
u_int16 size = tuple ? PyTuple_GET_SIZE (tuple) + len : len;
// prepare callback arguments
PyObject *new_tuple = PyTuple_New (size);
// pad with none object
for (u_int16 i = 0; i < len; i++)
{
PyTuple_SET_ITEM (new_tuple, i, Py_None);
}
// copy remaining objects, if any
for (u_int16 i = len; i < size; i++)
{
PyObject *o = PyTuple_GET_ITEM (tuple, i - len);
Py_XINCREF (o);
PyTuple_SET_ITEM (new_tuple, i, o);
}
return new_tuple;
}
// unflatten the contents of a tuple
PyObject *get_tuple (base::flat & in, const u_int16 & start)
{
u_int16 len = in.get_uint16 ("pln") + start;
PyObject *tuple = PyTuple_New (len);
void *value;
for (u_int16 i = start; i < len; i++)
{
switch (int type = in.next (&value))
{
case base::flat::T_CHAR:
{
PyTuple_SetItem (tuple, i, Py_None);
break;
}
case base::flat::T_STRING:
{
// Stolen reference
PyTuple_SetItem (tuple, i, pass_instance ((const char *) value, c_owns));
break;
}
case base::flat::T_SINT32:
{
// Stolen reference
PyTuple_SetItem (tuple, i, pass_instance<const s_int32> (*((s_int32*) value), c_owns));
break;
}
default:
{
fprintf (stderr, "*** python::get_tuple: unsupported type: %s!\n",
base::flat::name_for_type ((base::flat::data_type) type));
break;
}
}
}
return tuple;
}
// flatten the contents of a tuple
void put_tuple (PyObject * tuple, base::flat & out, const u_int16 & start)
{
u_int16 len = tuple == NULL ? 0 : PyTuple_Size (tuple);
out.put_uint16 ("pln", len - start);
for (u_int16 i = start; i < len; i++)
{
// Borrowed reference
PyObject *item = PyTuple_GetItem (tuple, i);
if (item == NULL)
out.put_char ("n", ' ');
// Check for the type of this object
// String?
else if (PyString_Check (item))
out.put_string ("s", PyString_AsString (item));
// Integer?
else if (PyInt_Check (item))
out.put_sint32 ("i", PyInt_AsLong (item));
// Not saveable
else
{
PyObject *repr = PyObject_Repr (item);
fprintf (stderr, "*** python::put_tuple: cannot save '%s'!\n", PyString_AsString (repr));
Py_DECREF (repr);
}
}
}
}