Lunatic Python

lua-users home
wiki

LunaticPython 是 Python 和 Lua 之间的双向桥梁。

网站: http://labix.org/lunatic-python

用户评论 1

Lunatic Python 1.0 确实可以在 Windows 上使用 MinGW 编译器在 Cygwin 上使用 Python 2.5 和 Lua 5.1.1 上运行,但需要进行一些修改。

首先,进行了此修补,否则 gcc 无法编译它

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);

然后我这样编译

$ 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

然后我使用 pexports 技巧 [1] 从 c:\windows\system32 目录中的 python25.dll 生成 libpython25.a。

然后链接

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

我将 python.py 移开了(我认为这适用于旧版本的 Lua)

$ mv python.py junk.py

然后它就可以使用了

$ /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

用户评论 2

为了让 Lua5.1 在 Unix 平台上与 lunatic python 一起工作,您不需要进行如此复杂的步骤。以下修补程序基于 [2] 完成了这项工作。

如果没有以下修补程序,python 的 readline 模块在终端处理代码中存在内部冲突,这会导致加载 lua 模块时,您会收到来自 malloc() 的错误,提示您的内存已损坏。

还有一个问题是 luaopen_loadlib() 已被弃用,这似乎在手册中没有记录,但在邮件列表帖子 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?

用户评论 - 在 Ubuntu 上使用 lua5.1.2 编译

我尝试了上面的修补程序,发现还需要其他一些修补程序才能使一切正常工作。python.lua 直接调用 loadlib 而不是 package.loadlib。以下是 python.lua 的修补程序(下面每个文件都有一个修补程序)

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()

我还需要将路径和库名称从 lua 更改为 lua5.1 等效项。以下是所有上述补丁合并成一个。这在我的 Ubuntu Hardy Heron lua5.1.2 上通过 apt-get 安装的 lua 运行良好。

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?

分支


最近更改 · 偏好设置
编辑 · 历史记录
最后编辑于 2013 年 4 月 12 日凌晨 12:13 GMT (差异)