-
Notifications
You must be signed in to change notification settings - Fork 15
/
pyhelper.c
392 lines (315 loc) · 10.3 KB
/
pyhelper.c
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "Python.h"
#include "datetime.h"
PyObject* PyInit_perl6(void);
PyObject *(*call_p6_object)(int, PyObject *, PyObject **);
PyObject *(*call_p6_method)(int, char * , PyObject *, PyObject **);
void py_init_python(PyObject *(*call_object)(int, PyObject *, PyObject **), PyObject *(*call_method)(int, char * , PyObject *, PyObject **)) {
/* sometimes Python needs to know about argc and argv to be happy */
int _python_argc = 1;
wchar_t** _python_argv = PyMem_Malloc(sizeof(wchar_t*)* _python_argc);
_python_argv[0] = L"";
call_p6_object = call_object;
call_p6_method = call_method;
Py_SetProgramName(L"python");
PyImport_AppendInittab("perl6", &PyInit_perl6);
Py_Initialize();
PySys_SetArgv(_python_argc, _python_argv); /* Tk needs this */
PyDateTime_IMPORT;
}
int py_destroy_python() {
return Py_FinalizeEx();
}
PyObject *perl6object;
void py_init_perl6object(void) {
PyObject *main_module = PyImport_AddModule("__main__");
PyObject *globals = PyModule_GetDict(main_module);
perl6object = PyDict_GetItemString(globals, "Perl6Object");
}
PyObject *py_eval(const char* p, int type) {
PyObject * main_module;
PyObject * globals;
PyObject * locals;
PyObject * py_result;
int context;
/* doc: if the module wasn't already loaded, you will get an empty
* module object. */
main_module = PyImport_AddModule("__main__");
if(main_module == NULL) {
printf("Error -- Import_AddModule of __main__ failed");
}
globals = PyModule_GetDict(main_module);
locals = globals;
context = (type == 0)
? Py_eval_input :
(type == 1)
? Py_file_input
: Py_single_input;
py_result = PyRun_StringFlags(p, context, globals, locals, NULL);
return py_result;
}
PyObject *py_import(char *module) {
return PyImport_ImportModule(module);
}
int py_instance_check(PyObject *obj) {
return ((obj->ob_type->tp_flags & Py_TPFLAGS_HEAPTYPE) || !PyType_Check(obj) || PyDate_Check(obj));
}
int py_is_instance(PyObject *obj, PyObject *class) {
if (obj == NULL || class == NULL) {
return 0;
}
return PyObject_IsInstance(obj, class);
}
int py_int_check(PyObject *obj) {
return PyLong_Check(obj);
}
int py_float_check(PyObject *obj) {
return PyFloat_Check(obj);
}
int py_unicode_check(PyObject *obj) {
return PyUnicode_Check(obj);
}
int py_buffer_check(PyObject *obj) {
return PyBytes_Check(obj);
}
int py_sequence_check(PyObject *obj) {
return PySequence_Check(obj);
}
int py_mapping_check(PyObject *obj) {
return PyMapping_Check(obj);
}
int py_callable_check(PyObject *obj) {
return PyFunction_Check(obj) || PyMethod_Check(obj);
}
int py_is_none(PyObject *obj) {
return obj == Py_None;
}
long py_int_as_long(PyObject *obj) {
return PyLong_AsLong(obj);
}
double py_float_as_double(PyObject *obj) {
return PyFloat_AsDouble(obj);
}
PyObject *py_int_to_py(long num) {
return PyLong_FromLong(num);
}
PyObject *py_float_to_py(double num) {
return PyFloat_FromDouble(num);
}
PyObject *py_str_to_py(int len, char *str) {
return PyUnicode_DecodeUTF8(str, len, "replace");
}
PyObject *py_bytearray_from_py(PyObject *obj) {
return PyByteArray_FromObject(obj);
}
Py_ssize_t py_size_from_bytearray(PyObject *obj) {
return PyByteArray_Size(obj);
}
char *py_contents_from_bytearray(PyObject *obj) {
return PyByteArray_AsString(obj);
}
PyObject *py_buf_to_py(int len, char *buf) {
return PyBytes_FromStringAndSize(buf, len);
}
char *py_string_as_string(PyObject *obj) {
return PyUnicode_AsUTF8(obj);
}
int py_sequence_length(PyObject *obj) {
return PySequence_Length(obj);
}
PyObject *py_sequence_get_item(PyObject *obj, int item) {
return PySequence_GetItem(obj, item);
}
PyObject *py_mapping_items(PyObject *obj) {
return PyMapping_Items(obj);
}
PyObject *py_tuple_new(int len) {
return PyTuple_New(len);
}
void py_tuple_set_item(PyObject *tuple, int i, PyObject *item) {
PyTuple_SetItem(tuple, i, item);
}
PyObject *py_list_new(int len) {
return PyList_New(len);
}
void py_list_set_item(PyObject *list, int i, PyObject *item) {
PyList_SetItem(list, i, item);
}
PyObject *py_dict_new(void) {
return PyDict_New();
}
void py_dict_set_item(PyObject *dict, PyObject *key, PyObject *item) {
PyDict_SetItem(dict, key, item);
}
PyObject *py_none(void) {
Py_INCREF(Py_None);
return Py_None;
}
void py_dec_ref(PyObject *obj) {
Py_DECREF(obj);
}
void py_inc_ref(PyObject *obj) {
Py_INCREF(obj);
}
PyObject *py_getattr(PyObject *obj, char *name) {
if (name == NULL)
return NULL;
return PyObject_GetAttrString(obj, name);
}
PyObject *py_dir(PyObject *obj) {
return PyObject_Dir(obj);
}
PyObject *py_call_function(char *pkg, char *name, PyObject *args) {
PyObject * const mod = PyImport_AddModule(pkg);
PyObject * const dict = mod ? PyModule_GetDict(mod) : NULL;
PyObject * const func = dict ? PyMapping_GetItemString(dict, name) : NULL;
PyObject *py_retval = NULL;
if (func == NULL) {
PyErr_Format(PyExc_NameError, "name '%s' is not defined", name);
goto cleanup;
}
py_retval = PyObject_CallObject(func, args);
Py_DECREF(func);
cleanup:
Py_DECREF(args);
return py_retval;
}
PyObject *py_call_function_kw(char *pkg, char *name, PyObject *args, PyObject *kw) {
PyObject * const mod = PyImport_AddModule(pkg);
PyObject * const dict = PyModule_GetDict(mod);
PyObject * const func = PyMapping_GetItemString(dict, name);
PyObject *py_retval = NULL;
if (func == NULL) {
PyErr_Format(PyExc_NameError, "name '%s' is not defined", name);
goto cleanup;
}
py_retval = PyObject_Call(func, args, kw);
Py_DECREF(func);
cleanup:
Py_DECREF(args);
Py_DECREF(kw);
return py_retval;
}
void py_fetch_error(PyObject **exception) {
/* ex_type, ex_value, ex_trace, ex_message */
PyErr_Fetch(&exception[0], &exception[1], &exception[2]);
if (exception[0] == NULL)
return;
PyErr_NormalizeException(&exception[0], &exception[1], &exception[2]);
exception[3] = PyObject_Str(exception[1]); /* new reference */
}
void py_raise_missing_method(PyObject *obj, char *name) {
PyObject *class = PyObject_GetAttrString(obj, "__class__");
if (class) {
PyObject *class_name = PyObject_GetAttrString(class, "__name__");
char *c_class_name = PyUnicode_AsUTF8(class_name);
PyErr_Format(PyExc_NameError, "%s instance has no attribute '%s'", c_class_name, name);
Py_DECREF(class_name);
Py_DECREF(class);
}
else {
PyErr_Format(PyExc_NameError, "instance has no attribute '%s'", name);
}
}
PyObject *py_call_static_method_kw(char *pkg, char *class, char *name, PyObject *args, PyObject *kw) {
PyObject *py_retval = NULL;
PyObject * const mod = PyImport_AddModule(pkg);
PyObject * const dict = PyModule_GetDict(mod);
PyObject * const obj = PyMapping_GetItemString(dict, class);
PyObject *method = PyObject_GetAttrString(obj, name);
if (method == NULL) {
py_raise_missing_method(obj, name);
goto cleanup;
}
py_retval = PyObject_Call(method, args, kw);
Py_DECREF(method);
cleanup:
Py_DECREF(args);
Py_DECREF(kw);
return py_retval;
}
PyObject *py_call_static_method(char *pkg, char *class, char *name, PyObject *args) {
PyObject *py_retval = NULL;
PyObject * const mod = PyImport_AddModule(pkg);
PyObject * const dict = PyModule_GetDict(mod);
PyObject * const obj = PyMapping_GetItemString(dict, class);
PyObject *method = PyObject_GetAttrString(obj, name);
if (method == NULL) {
py_raise_missing_method(obj, name);
goto cleanup;
}
py_retval = PyObject_CallObject(method, args);
Py_DECREF(method);
cleanup:
Py_DECREF(args);
return py_retval;
}
PyObject *py_call_method(PyObject *obj, char *name, PyObject *args) {
PyObject *method = PyObject_GetAttrString(obj, name);
if (method == NULL) {
py_raise_missing_method(obj, name);
goto cleanup;
}
PyObject *py_retval = PyObject_CallObject(method, args);
Py_DECREF(method);
cleanup:
Py_DECREF(args);
return py_retval;
}
PyObject *py_call_method_kw(PyObject *obj, char *name, PyObject *args, PyObject *kw) {
PyObject *method = PyObject_GetAttrString(obj, name);
PyObject * py_retval = NULL;
if (method == NULL) {
py_raise_missing_method(obj, name);
goto cleanup;
}
py_retval = PyObject_Call(method, args, kw);
Py_DECREF(method);
cleanup:
Py_DECREF(args);
return py_retval;
}
static PyObject *perl6_call(PyObject *self, PyObject *args) {
PyObject * const index = PyTuple_GetItem(args, 0);
PyObject * const params = PyTuple_GetItem(args, 1);
PyObject * error = NULL;
PyObject *retval = call_p6_object(PyLong_AsLong(index), params, &error);
if (error != NULL) {
PyErr_SetObject(PyExc_Exception, error);
return NULL;
}
return retval;
}
static PyObject *perl6_invoke(PyObject *self, PyObject *args) {
PyObject * const index = PyTuple_GetItem(args, 0);
PyObject * const name = PyTuple_GetItem(args, 1);
PyObject * const params = PyTuple_GetItem(args, 2);
PyObject * error = NULL;
Py_ssize_t length;
char * buf = PyUnicode_AsUTF8AndSize(name, &length);
PyObject *retval = call_p6_method(PyLong_AsLong(index), buf, params, &error);
if (error != NULL) {
PyErr_SetObject(PyExc_Exception, error);
return NULL;
}
return retval;
}
static PyMethodDef perl_functions[] = {
{"call", perl6_call, METH_VARARGS, PyDoc_STR("invoke(object, *args) -> retval")},
{"invoke", perl6_invoke, METH_VARARGS, PyDoc_STR("invoke(object, method_name, *args) -> retval")},
{NULL, NULL} /* sentinel */
};
PyObject* PyInit_perl6(void){
/* Create the module and add the functions */
static struct PyModuleDef perl_module = {
PyModuleDef_HEAD_INIT,
"perl6",
"perl6 -- Access a Perl 6 interpreter transparently",
-1, /* m_size */
perl_functions, /* m_methods */
NULL, /* m_slots */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL /* m_free */
};
return PyModule_Create(&perl_module);
}