-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathpythoninlua.c
More file actions
740 lines (618 loc) · 19.1 KB
/
pythoninlua.c
File metadata and controls
740 lines (618 loc) · 19.1 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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*
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>
#if defined(__linux__)
# include <dlfcn.h>
#endif
/* 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 *);
static int py_eval(lua_State *);
static int py_convert_custom(lua_State *L, PyObject *o, int asindx)
{
py_object *obj = (py_object*) lua_newuserdata(L, sizeof(py_object));
if (!obj)
luaL_error(L, "failed to allocate userdata object");
Py_INCREF(o);
obj->o = o;
obj->asindx = asindx;
luaL_getmetatable(L, POBJECT);
lua_setmetatable(L, -2);
return 1;
}
int py_convert(lua_State *L, PyObject *o)
{
int ret = 0;
if (o == Py_None)
{
/* 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;
} else if (PyUnicode_Check(o) || PyBytes_Check(o)) {
PyObject *bstr = PyUnicode_AsEncodedString(o, "utf-8", NULL);
Py_ssize_t len;
char *s;
PyErr_Clear();
PyBytes_AsStringAndSize(bstr ? bstr : o, &s, &len);
lua_pushlstring(L, s, len);
if (bstr) Py_DECREF(bstr);
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 = PyDict_Check(o) || PyList_Check(o) || PyTuple_Check(o);
ret = py_convert_custom(L, o, asindx);
}
return ret;
}
static int py_object_call(lua_State *L)
{
PyObject *args;
PyObject *value;
PyObject *pKywdArgs = NULL;
int nargs = lua_gettop(L)-1;
int ret = 0;
int i;
py_object *obj = (py_object*) luaL_checkudata(L, 1, POBJECT);
assert(obj);
if (!PyCallable_Check(obj->o))
return luaL_error(L, "object is not callable");
// passing a single table forces named keyword call style, e.g. plt.plot{x, y, c='red'}
if (nargs==1 && lua_istable(L, 2)) {
lua_pushnil(L); /* first key */
nargs=0;
while (lua_next(L, 2) != 0) {
if (lua_isnumber(L, -2)) {
int i = lua_tointeger(L, -2);
nargs = i < nargs ? nargs : i;
}
lua_pop(L, 1);
}
args = PyTuple_New(nargs);
if (!args) {
PyErr_Print();
return luaL_error(L, "failed to create arguments tuple");
}
pKywdArgs = PyDict_New();
if (!pKywdArgs) {
Py_DECREF(args);
PyErr_Print();
return luaL_error(L, "failed to create arguments dictionary");
}
lua_pushnil(L); /* first key */
while (lua_next(L, 2) != 0) {
if (lua_isnumber(L, -2)) {
PyObject *arg = LuaConvert(L, -1);
if (!arg) {
Py_DECREF(args);
Py_DECREF(pKywdArgs);
return luaL_error(L, "failed to convert argument #%d", lua_tointeger(L, -2));
}
PyTuple_SetItem(args, lua_tointeger(L, -2)-1, arg);
}
else if (lua_isstring(L, -2)) {
PyObject *arg = LuaConvert(L, -1);
if (!arg) {
Py_DECREF(args);
Py_DECREF(pKywdArgs);
return luaL_error(L, "failed to convert argument '%s'", lua_tostring(L, -2));
}
PyDict_SetItemString(pKywdArgs, lua_tostring(L, -2), arg);
Py_DECREF(arg);
}
lua_pop(L, 1);
}
}
// regular call style e.g. plt.plot(x, y)
else {
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_Call(obj->o, args, pKywdArgs);
Py_DECREF(args);
if (pKywdArgs) Py_DECREF(pKywdArgs);
if (value) {
ret = py_convert(L, value);
Py_DECREF(value);
} else {
char s_exc[4096] = {0};
char s_traceback[sizeof(s_exc)] = {0};
PyObject *exc_type, *exc_value, *exc_traceback;
PyErr_Fetch(&exc_type, &exc_value, &exc_traceback);
PyErr_NormalizeException(&exc_type, &exc_value, &exc_traceback);
PyObject *exc_str = PyObject_Str(exc_value);
// Need not be garbage collected as per documentation of PyUnicode_AsUTF8
const char *exc_cstr = (exc_str)?PyUnicode_AsUTF8(exc_str):"";
strncpy(s_exc, (!(exc_cstr)?"UNKNOWN ERROR\n":exc_cstr), sizeof(s_exc)-1);
if (exc_value != NULL && exc_traceback != NULL) {
PyObject *traceback_module = PyImport_ImportModule("traceback");
if (traceback_module != NULL) {
PyObject *traceback_list = PyObject_CallMethod(traceback_module,
"format_exception", "OOO", exc_type, exc_value, exc_traceback);
if (traceback_list != NULL) {
PyObject *traceback_str = PyUnicode_Join(PyUnicode_FromString(""), traceback_list);
if (traceback_str != NULL) {
// Need not be garbage collected as per documentation of PyUnicode_AsUTF8
const char *traceback_cstr = PyUnicode_AsUTF8(traceback_str);
if (traceback_cstr != NULL) {
strncpy(s_traceback, traceback_cstr, sizeof(s_exc)-1);
}
Py_XDECREF(traceback_str);
}
Py_XDECREF(traceback_list);
}
Py_XDECREF(traceback_module);
}
}
Py_XDECREF(exc_type);
Py_XDECREF(exc_value);
Py_XDECREF(exc_traceback);
Py_XDECREF(exc_str);
if (*s_traceback == '\0') {
luaL_error(L, "error calling python function:\nException: %s", s_exc);
}
else {
luaL_error(L, "error calling python function:\nException: %s", s_traceback);
}
}
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)
{
PyObject *value;
const char *attr;
py_object *obj = (py_object*) luaL_checkudata(L, 1, POBJECT);
assert(obj);
if (obj->asindx || lua_type(L, 2) != LUA_TSTRING)
return _p_object_newindex_set(L, obj, 2, 3);
attr = luaL_checkstring(L, 2);
assert(attr);
value = LuaConvert(L, 3);
if (!value) {
return luaL_argerror(L, 1, "failed to convert value");
}
if (PyObject_SetAttrString(obj->o, 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);
if (!item) {
item = PyObject_GetAttr(obj->o, key);
}
Py_DECREF(key);
if (item) {
ret = py_convert(L, item);
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)
{
int ret = 0;
PyObject *value;
const char *attr;
py_object *obj = (py_object*) luaL_checkudata(L, 1, POBJECT);
assert(obj);
if (obj->asindx || lua_type(L, 2) != LUA_TSTRING)
return _p_object_index_get(L, obj, 2);
attr = luaL_checkstring(L, 2);
assert(attr);
if (strcmp(attr, "__get") == 0) {
lua_pushvalue(L, 1);
lua_pushcclosure(L, py_object_index_get, 1);
return 1;
} else if (strcmp(attr, "__set") == 0) {
lua_pushvalue(L, 1);
lua_pushcclosure(L, py_object_newindex_set, 1);
return 1;
}
value = PyObject_GetAttrString(obj->o, attr);
if (value) {
ret = py_convert(L, value);
Py_DECREF(value);
} else {
PyErr_Clear();
lua_pushnil(L);
ret = 1;
}
return ret;
}
static int py_object_gc(lua_State *L)
{
py_object *obj = (py_object*) luaL_checkudata(L, 1, POBJECT);
assert(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);
assert(obj);
PyObject *repr = PyObject_Str(obj->o);
if (!repr)
{
char buf[256];
snprintf(buf, 256, "python object: %p", (void *)obj->o);
lua_pushstring(L, buf);
PyErr_Clear();
}
else
{
py_convert(L, repr);
assert(lua_type(L, -1) == LUA_TSTRING);
Py_DECREF(repr);
}
return 1;
}
static int py_operator_lambda(lua_State *L, const char *op)
{
static char script_buff[] = "lambda a, b: a b";
static size_t len = sizeof(script_buff) / sizeof(script_buff[0]);
snprintf(script_buff, len, "lambda a, b: a %s b", op);
lua_pushcfunction(L, py_eval);
lua_pushlstring(L, script_buff, len);
lua_call(L, 1, 1);
return luaL_ref(L, LUA_REGISTRYINDEX);
}
#define make_pyoperator(opname, opliteral) \
static int py_object_ ## opname(lua_State *L) \
{ \
static int op_ref = LUA_REFNIL; \
if (op_ref == LUA_REFNIL) op_ref = py_operator_lambda(L, opliteral); \
\
lua_rawgeti(L, LUA_REGISTRYINDEX, op_ref); \
lua_insert(L, 1); \
return py_object_call(L); \
} \
struct opname ## __LINE__ // force semi
make_pyoperator(_pow, "**");
make_pyoperator(_mul, "*");
make_pyoperator(_div, "/");
make_pyoperator(_add, "+");
make_pyoperator(_sub, "-");
static const luaL_Reg py_object_mt[] =
{
{"__call", py_object_call},
{"__index", py_object_index},
{"__newindex", py_object_newindex},
{"__gc", py_object_gc},
{"__tostring", py_object_tostring},
{"__pow", py_object__pow},
{"__mul", py_object__mul},
{"__div", py_object__div},
{"__add", py_object__add},
{"__sub", py_object__sub},
{NULL, NULL}
};
static int py_run(lua_State *L, int eval)
{
const char *s = luaL_checkstring(L, 1);
PyObject *m, *d, *o;
int ret = 0;
lua_settop(L, 1);
if (!eval)
{
lua_pushliteral(L, "\n");
lua_concat(L, 2);
s = luaL_checkstring(L, 1);
}
m = PyImport_AddModule("__main__");
if (!m)
return luaL_error(L, "Can't get __main__ module");
d = PyModule_GetDict(m);
o = PyRun_String(s, eval ? Py_eval_input : Py_file_input,
d, d);
if (!o)
{
PyErr_Print();
return 0;
}
if (py_convert(L, o))
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);
assert(obj);
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);
assert(obj);
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)
{
py_object *obj = luaL_checkudata(L, 1, POBJECT);
if (!PyCallable_Check(obj->o))
return luaL_error(L, "object is not callable");
lua_settop(L, 1);
lua_pushcclosure(L, py_asfunc_call, 1);
return 1;
}
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 = PyImport_ImportModule((char*)name);
int ret;
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_newlib(L, py_lib);
/* Register python object metatable */
luaL_newmetatable(L, POBJECT);
luaL_setfuncs(L, py_object_mt, 0);
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 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 11)
// Python 3.11+ syntax
char *argv[] = {"python3", 0};
PyConfig config;
PyConfig_InitPythonConfig(&config);
config.isolated = 1;
PyConfig_SetBytesArgv(&config, 1, argv);
#else
#if PY_MAJOR_VERSION >= 3
// Python < 3.11 syntax
wchar_t *argv[] = {L"python3", 0};
#else
// Python 2 syntax
char *argv[] = {"python2", 0};
#endif
Py_SetProgramName(argv[0]);
#endif
PyImport_AppendInittab("lua", PyInit_lua);
/* Loading python library symbols so that dynamic extensions don't throw symbol not found error.
Ref Link: http://stackoverflow.com/questions/29880931/importerror-and-pyexc-systemerror-while-embedding-python-script-within-c-for-pam
*/
#if defined(__linux__)
# define STR(s) #s
# define PYLIB_STR(s) STR(s)
#if !defined(PYTHON_LIBRT)
# error PYTHON_LIBRT must be defined when building under Linux!
#endif
void *ok = dlopen(PYLIB_STR(PYTHON_LIBRT), RTLD_NOW | RTLD_GLOBAL);
assert(ok); (void) ok;
#endif
#if PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 11)
// Python 3.11+ syntax
Py_InitializeFromConfig(&config);
PyConfig_Clear(&config);
#else
// Python 2, < 3.11 syntax
Py_Initialize();
PySys_SetArgv(1, argv);
#endif
/* Import 'lua' automatically. */
luam = PyImport_ImportModule("lua");
if (!luam)
return luaL_error(L, "Can't import lua module");
mainm = PyImport_AddModule("__main__");
if (!mainm)
{
Py_DECREF(luam);
return luaL_error(L, "Can't get __main__ module");
}
maind = PyModule_GetDict(mainm);
PyDict_SetItemString(maind, "lua", luam);
Py_DECREF(luam);
}
/* Register 'none' */
rc = py_convert_custom(L, Py_None, 0);
if (!rc)
return luaL_error(L, "failed to convert none object");
lua_pushvalue(L, -1);
lua_setfield(L, LUA_REGISTRYINDEX, "Py_None"); /* registry.Py_None */
lua_setfield(L, -2, "none"); /* python.none */
return 1;
}