Repository navigation
Expand file tree
/
Copy pathpythoninlua.c
More file actions
613 lines (515 loc) · 14.6 KB
/
Copy pathpythoninlua.c
File metadata and controls
613 lines (515 loc) · 14.6 KB
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*
Lunatic Python
--------------
Copyright (c) 2002-2005 Gustavo Niemeyer <gustavo@niemeyer.net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <Python.h>
/* need this to build with Lua 5.2: defines luaL_register() macro */
#define LUA_COMPAT_MODULE
#include <lua.h>
#include <lauxlib.h>
#include "pythoninlua.h"
#include "luainpython.h"
static int py_asfunc_call(lua_State *L);
static int py_convert_custom(lua_State *L, PyObject *o, int asindx)
{
int ret = 0;
py_object *obj = (py_object*) lua_newuserdata(L, sizeof(py_object));
if (obj) {
Py_INCREF(o);
obj->o = o;
obj->asindx = asindx;
luaL_getmetatable(L, POBJECT);
lua_setmetatable(L, -2);
ret = 1;
} else {
luaL_error(L, "failed to allocate userdata object");
}
return ret;
}
int py_convert(lua_State *L, PyObject *o, int withnone)
{
int ret = 0;
if (o == Py_None) {
if (withnone) {
lua_pushliteral(L, "Py_None");
lua_rawget(L, LUA_REGISTRYINDEX);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
luaL_error(L, "lost none from registry");
}
} else {
/* Not really needed, but this way we may check
* for errors with ret == 0. */
lua_pushnil(L);
ret = 1;
}
} else if (o == Py_True) {
lua_pushboolean(L, 1);
ret = 1;
} else if (o == Py_False) {
lua_pushboolean(L, 0);
ret = 1;
#if PY_MAJOR_VERSION >= 3
} else if (PyUnicode_Check(o)) {
Py_ssize_t len;
char *s = PyUnicode_AsUTF8AndSize(o, &len);
#else
} else if (PyString_Check(o)) {
Py_ssize_t len;
char *s;
PyString_AsStringAndSize(o, &s, &len);
#endif
lua_pushlstring(L, s, len);
ret = 1;
#if PY_MAJOR_VERSION < 3
} else if (PyInt_Check(o)) {
lua_pushnumber(L, (lua_Number)PyInt_AsLong(o));
ret = 1;
#endif
} else if (PyLong_Check(o)) {
lua_pushnumber(L, (lua_Number)PyLong_AsLong(o));
ret = 1;
} else if (PyFloat_Check(o)) {
lua_pushnumber(L, (lua_Number)PyFloat_AsDouble(o));
ret = 1;
} else if (LuaObject_Check(o)) {
lua_rawgeti(L, LUA_REGISTRYINDEX, ((LuaObject*)o)->ref);
ret = 1;
} else {
int asindx = 0;
if (PyDict_Check(o) || PyList_Check(o) || PyTuple_Check(o))
asindx = 1;
ret = py_convert_custom(L, o, asindx);
if (ret && !asindx &&
(PyFunction_Check(o) || PyCFunction_Check(o)))
lua_pushcclosure(L, py_asfunc_call, 1);
}
return ret;
}
static int py_object_call(lua_State *L)
{
py_object *obj = (py_object*) luaL_checkudata(L, 1, POBJECT);
PyObject *args;
PyObject *value;
int nargs = lua_gettop(L)-1;
int ret = 0;
int i;
if (!obj) {
return luaL_argerror(L, 1, "not a python object");
}
if (!PyCallable_Check(obj->o)) {
return luaL_error(L, "object is not callable");
}
args = PyTuple_New(nargs);
if (!args) {
PyErr_Print();
return luaL_error(L, "failed to create arguments tuple");
}
for (i = 0; i != nargs; i++) {
PyObject *arg = LuaConvert(L, i+2);
if (!arg) {
Py_DECREF(args);
return luaL_error(L, "failed to convert argument #%d", i+1);
}
PyTuple_SetItem(args, i, arg);
}
value = PyObject_CallObject(obj->o, args);
if (value) {
ret = py_convert(L, value, 0);
Py_DECREF(value);
} else {
PyErr_Print();
luaL_error(L, "error calling python function");
}
return ret;
}
static int _p_object_newindex_set(lua_State *L, py_object *obj,
int keyn, int valuen)
{
PyObject *value;
PyObject *key = LuaConvert(L, keyn);
if (!key) {
return luaL_argerror(L, 1, "failed to convert key");
}
if (!lua_isnil(L, valuen)) {
value = LuaConvert(L, valuen);
if (!value) {
Py_DECREF(key);
return luaL_argerror(L, 1, "failed to convert value");
}
if (PyObject_SetItem(obj->o, key, value) == -1) {
PyErr_Print();
luaL_error(L, "failed to set item");
}
Py_DECREF(value);
} else {
if (PyObject_DelItem(obj->o, key) == -1) {
PyErr_Print();
luaL_error(L, "failed to delete item");
}
}
Py_DECREF(key);
return 0;
}
static int py_object_newindex_set(lua_State *L)
{
py_object *obj = (py_object*) luaL_checkudata(L, lua_upvalueindex(1),
POBJECT);
if (lua_gettop(L) != 2) {
return luaL_error(L, "invalid arguments");
}
return _p_object_newindex_set(L, obj, 1, 2);
}
static int py_object_newindex(lua_State *L)
{
py_object *obj = (py_object*) luaL_checkudata(L, 1, POBJECT);
const char *attr;
PyObject *value;
if (!obj) {
return luaL_argerror(L, 1, "not a python object");
}
if (obj->asindx)
return _p_object_newindex_set(L, obj, 2, 3);
attr = luaL_checkstring(L, 2);
if (!attr) {
return luaL_argerror(L, 2, "string needed");
}
value = LuaConvert(L, 3);
if (!value) {
return luaL_argerror(L, 1, "failed to convert value");
}
if (PyObject_SetAttrString(obj->o, (char*)attr, value) == -1) {
Py_DECREF(value);
PyErr_Print();
return luaL_error(L, "failed to set value");
}
Py_DECREF(value);
return 0;
}
static int _p_object_index_get(lua_State *L, py_object *obj, int keyn)
{
PyObject *key = LuaConvert(L, keyn);
PyObject *item;
int ret = 0;
if (!key) {
return luaL_argerror(L, 1, "failed to convert key");
}
item = PyObject_GetItem(obj->o, key);
Py_DECREF(key);
if (item) {
ret = py_convert(L, item, 0);
Py_DECREF(item);
} else {
PyErr_Clear();
if (lua_gettop(L) > keyn) {
lua_pushvalue(L, keyn+1);
ret = 1;
}
}
return ret;
}
static int py_object_index_get(lua_State *L)
{
py_object *obj = (py_object*) luaL_checkudata(L, lua_upvalueindex(1),
POBJECT);
int top = lua_gettop(L);
if (top < 1 || top > 2) {
return luaL_error(L, "invalid arguments");
}
return _p_object_index_get(L, obj, 1);
}
static int py_object_index(lua_State *L)
{
py_object *obj = (py_object*) luaL_checkudata(L, 1, POBJECT);
const char *attr;
PyObject *value;
int ret = 0;
if (!obj) {
return luaL_argerror(L, 1, "not a python object");
}
if (obj->asindx)
return _p_object_index_get(L, obj, 2);
attr = luaL_checkstring(L, 2);
if (!attr) {
return luaL_argerror(L, 2, "string needed");
}
if (attr[0] == '_' && strcmp(attr, "__get") == 0) {
lua_pushvalue(L, 1);
lua_pushcclosure(L, py_object_index_get, 1);
return 1;
} else if (attr[0] == '_' && strcmp(attr, "__set") == 0) {
lua_pushvalue(L, 1);
lua_pushcclosure(L, py_object_newindex_set, 1);
return 1;
}
value = PyObject_GetAttrString(obj->o, (char*)attr);
if (value) {
ret = py_convert(L, value, 0);
Py_DECREF(value);
} else {
PyErr_Clear();
luaL_error(L, "unknown attribute in python object");
}
return ret;
}
static int py_object_gc(lua_State *L)
{
py_object *obj = (py_object*) luaL_checkudata(L, 1, POBJECT);
if (obj) {
Py_DECREF(obj->o);
}
return 0;
}
static int py_object_tostring(lua_State *L)
{
py_object *obj = (py_object*) luaL_checkudata(L, 1, POBJECT);
if (obj) {
PyObject *repr = PyObject_Str(obj->o);
if (!repr) {
char buf[256];
snprintf(buf, 256, "python object: %p", obj->o);
lua_pushstring(L, buf);
PyErr_Clear();
} else {
py_convert(L, repr, 0);
assert(lua_type(L, -1) == LUA_TSTRING);
Py_DECREF(repr);
}
}
return 1;
}
static const luaL_Reg py_object_lib[] = {
{"__call", py_object_call},
{"__index", py_object_index},
{"__newindex", py_object_newindex},
{"__gc", py_object_gc},
{"__tostring", py_object_tostring},
{NULL, NULL}
};
static int py_run(lua_State *L, int eval)
{
const char *s;
char *buffer = NULL;
PyObject *m, *d, *o;
int ret = 0;
int len;
s = luaL_checkstring(L, 1);
if (!s)
return 0;
if (!eval) {
len = strlen(s)+1;
buffer = (char *) malloc(len+1);
if (!buffer) {
return luaL_error(L, "Failed allocating buffer for execution");
}
strcpy(buffer, s);
buffer[len-1] = '\n';
buffer[len] = '\0';
s = buffer;
}
m = PyImport_AddModule("__main__");
if (!m) {
free(buffer);
return luaL_error(L, "Can't get __main__ module");
}
d = PyModule_GetDict(m);
o = PyRun_StringFlags(s, eval ? Py_eval_input : Py_single_input,
d, d, NULL);
free(buffer);
if (!o) {
PyErr_Print();
return 0;
}
if (py_convert(L, o, 0))
ret = 1;
Py_DECREF(o);
#if PY_MAJOR_VERSION < 3
if (Py_FlushLine())
#endif
PyErr_Clear();
return ret;
}
static int py_execute(lua_State *L)
{
return py_run(L, 0);
}
static int py_eval(lua_State *L)
{
return py_run(L, 1);
}
static int py_asindx(lua_State *L)
{
py_object *obj = (py_object*) luaL_checkudata(L, 1, POBJECT);
if (!obj)
return luaL_argerror(L, 1, "not a python object");
return py_convert_custom(L, obj->o, 1);
}
static int py_asattr(lua_State *L)
{
py_object *obj = (py_object*) luaL_checkudata(L, 1, POBJECT);
if (!obj)
return luaL_argerror(L, 1, "not a python object");
return py_convert_custom(L, obj->o, 0);
}
static int py_asfunc_call(lua_State *L)
{
lua_pushvalue(L, lua_upvalueindex(1));
lua_insert(L, 1);
return py_object_call(L);
}
static int py_asfunc(lua_State *L)
{
int ret = 0;
if (luaL_checkudata(L, 1, POBJECT)) {
lua_pushcclosure(L, py_asfunc_call, 1);
ret = 1;
} else {
luaL_argerror(L, 1, "not a python object");
}
return ret;
}
static int py_globals(lua_State *L)
{
PyObject *globals;
if (lua_gettop(L) != 0) {
return luaL_error(L, "invalid arguments");
}
globals = PyEval_GetGlobals();
if (!globals) {
PyObject *module = PyImport_AddModule("__main__");
if (!module) {
return luaL_error(L, "Can't get __main__ module");
}
globals = PyModule_GetDict(module);
}
if (!globals) {
PyErr_Print();
return luaL_error(L, "can't get globals");
}
return py_convert_custom(L, globals, 1);
}
static int py_locals(lua_State *L)
{
PyObject *locals;
if (lua_gettop(L) != 0) {
return luaL_error(L, "invalid arguments");
}
locals = PyEval_GetLocals();
if (!locals)
return py_globals(L);
return py_convert_custom(L, locals, 1);
}
static int py_builtins(lua_State *L)
{
PyObject *builtins;
if (lua_gettop(L) != 0) {
return luaL_error(L, "invalid arguments");
}
builtins = PyEval_GetBuiltins();
if (!builtins) {
PyErr_Print();
return luaL_error(L, "failed to get builtins");
}
return py_convert_custom(L, builtins, 1);
}
static int py_import(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
PyObject *module;
int ret;
if (!name) {
return luaL_argerror(L, 1, "module name expected");
}
module = PyImport_ImportModule((char*)name);
if (!module) {
PyErr_Print();
return luaL_error(L, "failed importing '%s'", name);
}
ret = py_convert_custom(L, module, 0);
Py_DECREF(module);
return ret;
}
py_object* luaPy_to_pobject(lua_State *L, int n)
{
if(!lua_getmetatable(L, n)) return NULL;
luaL_getmetatable(L, POBJECT);
int is_pobject = lua_rawequal(L, -1, -2);
lua_pop(L, 2);
return is_pobject ? (py_object *) lua_touserdata(L, n) : NULL;
}
static const luaL_Reg py_lib[] = {
{"execute", py_execute},
{"eval", py_eval},
{"asindx", py_asindx},
{"asattr", py_asattr},
{"asfunc", py_asfunc},
{"locals", py_locals},
{"globals", py_globals},
{"builtins", py_builtins},
{"import", py_import},
{NULL, NULL}
};
LUA_API int luaopen_python(lua_State *L)
{
int rc;
/* Register module */
luaL_register(L, "python", py_lib);
/* Register python object metatable */
luaL_newmetatable(L, POBJECT);
luaL_register(L, NULL, py_object_lib);
lua_pop(L, 1);
/* Initialize Lua state in Python territory */
if (!LuaState) LuaState = L;
/* Initialize Python interpreter */
if (!Py_IsInitialized()) {
PyObject *luam, *mainm, *maind;
#if PY_MAJOR_VERSION >= 3
wchar_t *argv[] = {L"<lua>", 0};
#else
char *argv[] = {"<lua>", 0};
#endif
Py_SetProgramName(argv[0]);
PyImport_AppendInittab("lua", PyInit_lua);
Py_Initialize();
PySys_SetArgv(1, argv);
/* Import 'lua' automatically. */
luam = PyImport_ImportModule("lua");
if (!luam) {
luaL_error(L, "Can't import lua module");
} else {
mainm = PyImport_AddModule("__main__");
if (!mainm) {
luaL_error(L, "Can't get __main__ module");
} else {
maind = PyModule_GetDict(mainm);
PyDict_SetItemString(maind, "lua", luam);
Py_DECREF(luam);
}
}
}
/* Register 'none' */
lua_pushliteral(L, "Py_None");
rc = py_convert_custom(L, Py_None, 0);
if (rc) {
lua_pushliteral(L, "none");
lua_pushvalue(L, -2);
lua_rawset(L, -5); /* python.none */
lua_rawset(L, LUA_REGISTRYINDEX); /* registry.Py_None */
} else {
lua_pop(L, 1);
luaL_error(L, "failed to convert none object");
}
return 0;
}