Lunatic Python

lua-users home
wiki

LunaticPython is a two-way bridge between Python and Lua.

Web site: http://labix.org/lunatic-python

User Comments 1

Lunatic Python 1.0 does work on Python 2.5 with Lua 5.1.1 on Windows using the MinGW compiler on Cygwin, but some hacking was necessary.

First, this patch was made, otherwise gcc wouldn't compile it:

diff -ur src-orig/luainpython.c src/luainpython.c
--- src-orig/luainpython.c      2007-01-03 03:03:29.156250000 -0500
+++ src/luainpython.c   2007-01-03 03:05:11.203125000 -0500
@@ -374,7 +374,7 @@
         0,                      /*tp_clear*/
         0,                      /*tp_richcompare*/
         0,                      /*tp_weaklistoffset*/
-        PyObject_SelfIter,      /*tp_iter*/
+        0, /* set later: PyObject_SelfIter, */     /*tp_iter*/
         (iternextfunc)LuaObject_iternext, /*tp_iternext*/
         0,                     /*tp_methods*/
         0,                     /*tp_members*/
@@ -385,9 +385,9 @@
         0,                      /*tp_descr_set*/
         0,                      /*tp_dictoffset*/
         0,                     /*tp_init*/
-        PyType_GenericAlloc,    /*tp_alloc*/
-        PyType_GenericNew,      /*tp_new*/
-       _PyObject_Del,          /*tp_free*/
+        0, /* set later: PyType_GenericAlloc, */ /*tp_alloc*/
+        0, /* set later: PyType_GenericNew, */   /*tp_new*/
+       0, /* set later: _PyObject_Del, */      /*tp_free*/
         0,                      /*tp_is_gc*/
 };

@@ -493,7 +493,9 @@
                luaopen_io(L);
                luaopen_string(L);
                luaopen_debug(L);
-               luaopen_loadlib(L);
+
+                luaopen_package(L);
+
                luaopen_python(L);
                lua_settop(L, 0);
        }
Only in src: luainpython.c~
diff -ur src-orig/luainpython.h src/luainpython.h
--- src-orig/luainpython.h      2007-01-03 03:03:29.156250000 -0500
+++ src/luainpython.h   2007-01-03 03:04:31.359375000 -0500
@@ -29,7 +29,8 @@
        int refiter;
 } LuaObject;

-PyAPI_DATA(PyTypeObject) LuaObject_Type;
+/* PyAPI_DATA(PyTypeObject) LuaObject_Type;*/
+extern PyTypeObject LuaObject_Type;

 #define LuaObject_Check(op) PyObject_TypeCheck(op, &LuaObject_Type)

Only in src: luainpython.h~
diff -ur src-orig/pythoninlua.c src/pythoninlua.c
--- src-orig/pythoninlua.c      2007-01-03 03:03:29.156250000 -0500
+++ src/pythoninlua.c   2007-01-03 03:01:20.343750000 -0500
@@ -559,6 +559,12 @@
 {
        int rc;

+        LuaObject_Type.tp_alloc = PyType_GenericAlloc;
+        LuaObject_Type.tp_new = PyType_GenericNew;
+        LuaObject_Type.tp_free = _PyObject_Del;
+        LuaObject_Type.tp_iter = PyObject_SelfIter;
+
+
        /* Register module */
        luaL_openlib(L, "python", py_lib, 0);

Then I compiled as such:

$ gcc -mno-cygwin -c -DLUA_BUILD_AS_DLL -DLUA_LIB -I/test/lib/Python25/include/ -I/test/lua-5.1.1/include src/luainpython.c src/pythoninlua.c

I then used the pexports trick [1] to generate a libpython25.a from the python25.dll in the c:\windows\system32 directory.

Then it was linked:

$ gcc -mno-cygwin -shared luainpython.o pythoninlua.o /test/lua-5.1.1/bin/lua51.dll libpython25.a -o python.dll

I moved python.py out of the way (this seems for older versions of Lua I suppose):

$ mv python.py junk.py

Then it was usable:

$ /test/lua-5.1.1/bin/lua
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
> require "python"
> print(python.globals())
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__d
oc__': None, 'lua': <module 'lua' (built-in)>}

--DavidManura

User Comments 2

In order to get Lua5.1 working with lunatic python on Unix platforms you don't need to to such elaborate lengths. The following patch does the job based on [2].

Without the following patch, python's readline modules has an internal contention within the terminal handling code which results in when lua module is loaded you get a error from malloc() saying that your memory is corrupt.

There is also another issue that luaopen_loadlib() has been deprecated which doesn't seem to be documented in the manual but is on the mailing list post LuaList:/2005-09/msg00322.html .

diff -ru lunatic-python-1.0/src/luainpython.c lunatic-python-1.0-lua5.1/src/luainpython.c
--- lunatic-python-1.0/src/luainpython.c	2005-10-19 00:07:02.000000000 +0100
+++ lunatic-python-1.0-lua5.1/src/luainpython.c	2007-02-05 20:04:33.869590048 +0000
@@ -488,12 +488,12 @@
 
 	if (!L) {
 		L = lua_open();
-		luaopen_base(L);
-		luaopen_table(L);
-		luaopen_io(L);
-		luaopen_string(L);
-		luaopen_debug(L);
-		luaopen_loadlib(L);
+
+		/* loading each lib separately has some deep conflict
+		 * with python's readline module so we obey the holy
+		 * docs by lua people and use the magic loader.
+		 */
+		luaL_openlibs(L);
 		luaopen_python(L);
 		lua_settop(L, 0);
 	}

AlexSayle?

User Comments - Compiling on Ubuntu with lua5.1.2

I tried the above patch and found that a few other were necessary to get everything working. python.lua calls loadlib directly rather than package.loadlib. Here is the patch for python.lua (with a patch for every file further below):

diff -ru lunatic-python-1.0/python.lua lunatic-python-1.0-lua5.1/python.lua 
--- lunatic-python-1.0/python.lua	2003-12-12 20:37:57.000000000 -0800
+++ lunatic-python-1.0-lua5.1/python.lua	2008-06-26 22:48:34.000000000 -0700
@@ -1,6 +1,6 @@
 local path = os.getenv("LUA_SOPATH")
 if path then
-	func = loadlib(path.."/lua-python.so", "luaopen_python")
+	func = package.loadlib(path.."/lua-python.so", "luaopen_python")
 	if func then
 		func()
 		return
@@ -10,7 +10,7 @@
 local loaded = false
 for i = 10, 2, -1 do
 	for j = 10, 2, -1 do
-		func = loadlib(string.format(modmask, i, j), "luaopen_python")
+		func = package.loadlib(string.format(modmask, i, j), "luaopen_python")
 		if func then
 			loaded = true
 			func()

I was also required fix the paths and library names from lua to lua5.1 equivalents. Here is all of the above patches combined into one. This worked for me on Ubuntu Hardy Heron lua5.1.2 with lua installed via apt-get.

diff -ru lunatic-python-1.0 lunatic-python-1.0-lua5.1Only in lunatic-python-1.0-lua5.1: build
diff -ru lunatic-python-1.0/python.lua lunatic-python-1.0-lua5.1/python.lua
--- lunatic-python-1.0/python.lua	2003-12-12 20:37:57.000000000 -0800
+++ lunatic-python-1.0-lua5.1/python.lua	2008-06-26 22:48:34.000000000 -0700
@@ -1,6 +1,6 @@
 local path = os.getenv("LUA_SOPATH")
 if path then
-	func = loadlib(path.."/lua-python.so", "luaopen_python")
+	func = package.loadlib(path.."/lua-python.so", "luaopen_python")
 	if func then
 		func()
 		return
@@ -10,7 +10,7 @@
 local loaded = false
 for i = 10, 2, -1 do
 	for j = 10, 2, -1 do
-		func = loadlib(string.format(modmask, i, j), "luaopen_python")
+		func = package.loadlib(string.format(modmask, i, j), "luaopen_python")
 		if func then
 			loaded = true
 			func()
diff -ru lunatic-python-1.0/setup.py lunatic-python-1.0-lua5.1/setup.py
--- lunatic-python-1.0/setup.py	2005-10-18 16:10:07.000000000 -0700
+++ lunatic-python-1.0-lua5.1/setup.py	2008-06-26 22:48:34.000000000 -0700
@@ -9,7 +9,7 @@
 # You may have to change these
 PYLIBS = ["python"+get_python_version(), "pthread", "util"]
 PYLIBDIR = [get_python_lib(standard_lib=True)+"/config"]
-LUALIBS = ["lua", "lualib"]
+LUALIBS = ["lua5.1"]
 LUALIBDIR = []
 
 setup(name="lunatic-python",
diff -ru lunatic-python-1.0/src/luainpython.c lunatic-python-1.0-lua5.1/src/luainpython.c
--- lunatic-python-1.0/src/luainpython.c	2005-10-18 16:07:02.000000000 -0700
+++ lunatic-python-1.0-lua5.1/src/luainpython.c	2008-06-26 22:48:34.000000000 -0700
@@ -22,9 +22,9 @@
 */
 #include <Python.h>
 
-#include <lua.h>
-#include <lauxlib.h>
-#include <lualib.h>
+#include <lua5.1/lua.h>
+#include <lua5.1/lauxlib.h>
+#include <lua5.1/lualib.h>
 
 #include "pythoninlua.h"
 #include "luainpython.h"
@@ -488,12 +488,12 @@
 
 	if (!L) {
 		L = lua_open();
-		luaopen_base(L);
-		luaopen_table(L);
-		luaopen_io(L);
-		luaopen_string(L);
-		luaopen_debug(L);
-		luaopen_loadlib(L);
+
+		/* loading each lib separately has some deep conflict
+		 * with python's readline module so we obey the holy
+		 * docs by lua people and use the magic loader.
+		 */
+		luaL_openlibs(L);
 		luaopen_python(L);
 		lua_settop(L, 0);
 	}
diff -ru lunatic-python-1.0/src/pythoninlua.c lunatic-python-1.0-lua5.1/src/pythoninlua.c
--- lunatic-python-1.0/src/pythoninlua.c	2005-10-18 16:07:07.000000000 -0700
+++ lunatic-python-1.0-lua5.1/src/pythoninlua.c	2008-06-26 22:48:34.000000000 -0700
@@ -22,8 +22,8 @@
 */
 #include <Python.h>
 
-#include <lua.h>
-#include <lauxlib.h>
+#include <lua5.1/lua.h>
+#include <lua5.1/lauxlib.h>
 
 #include "pythoninlua.h"
 #include "luainpython.h"

--ZachDwiel?

Forks


RecentChanges · preferences
edit · history
Last edited April 12, 2013 12:13 am GMT (diff)