Lunatic Python |
|
Python 是 Python 和 Lua 之間一個雙向橋樑。網站: http://labix.org/lunatic-python
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);
然後,我用以下方式編譯
接著,我使用 pexports 技巧 [1] 從 c:\windows\system32 目錄中的 python25.dll 產生一個 libpython25.a。
然後進行連結
我移出了 python.py(我認為這是對於舊版 Lua 的做法)
現在就可以使用了
$ /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)>}
在你使用 Unix 平台上的 Lua5.1 執行 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?
我試著套用以上的修補程式,發現需要再進行一些其他修補程式,才能順利執行。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 的路徑和程式庫名稱。以下是將上述所有修補程式合併成一個的結果。對於安裝了 apt-get 的 Ubuntu Hardy Heron lua5.1.2,這個方法對我有用。
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?