2025年python调用lua_Python - 调用Lua

python调用lua_Python - 调用LuaPython 调用 Lua lupa 将 Lua 和 LuaJIT2 集成进 CPython 可以在 Python 中执行 Lua 代码 Lupa 的主要特点 separate Lua runtime states through a LuaRuntime class Python coroutine wrapper for Lua

大家好,我是讯享网,很高兴认识大家。

Python 调用Lua

lupa将Lua和LuaJIT2集成进CPython,可以在Python中执行Lua代码.

Lupa的主要特点:

- separate Lua runtime states through a LuaRuntime class

- Python coroutine wrapper for Lua coroutines

- iteration support for Python objects in Lua and Lua objects in Python

- proper encoding and decoding of strings (configurable per runtime, UTF-8 by default)

- frees the GIL and supports threading in separate runtimes when calling into Lua解决了GIL问题,支持多线程

- tested with Python 2.6/3.2 and later 适用于Python 2.6/3.2以后的版本

- written for LuaJIT2 (tested with LuaJIT 2.0.2), but also works with the normal Lua interpreter (5.1 and 5.2)

- easy to hack on and extend as it is written in Cython, not C 扩展性好

1. lupa安装

这里基于Ubuntu14.04.

lupa官网提供的教程:

# ForDebian/Ubuntu + Lua 5.2

sudo apt-get install liblua5.2-dev # Install Lua 5.2 development package

sudo pip install lupa # Install lupa

# For Debian/Ubuntu + LuaJIT2

sudo apt-get install libluajit-5.1-dev # Install LuaJIT2 development package

sudo pip install lupa # Install lupa

源码安装方式如下:

1.1 安装lua环境

curl -R -O http://www.lua.org/ftp/lua-5.3.4.tar.gz

tar zxf lua-5.3.4.tar.gz

cd lua-5.3.4

make linux test

make install

注:如果遇到错误:fatal error: readline/readline.h: No such file or directory,则需要安装readline-dev:

sudo apt-get install libreadline-dev

测试是否安装成功:

新建 hellolua.lua脚本,代码如下:

print("Hello Lua!")

执行命令:

lua hellolua.lua

安装成功,则输出:

Hello Lua!

1.2 安装LuaJIT

wget -c http://luajit.org/download/LuaJIT-2.0.5.tar.gz

tar zxf LuaJIT-2.0.5.tar.gz

cd LuaJIT-2.0.5


讯享网

sudo make install PREFIX=/usr/local/luajit

echo "/usr/local/luajit/lib" > /etc/ld.so.conf.d/usr_local_luajit_lib.conf

sudo ldconfig

export LUAJIT_LIB=/usr/local/luajit/lib

export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0

1.3 lupa安装

sudo pip install lupa

2. 实例

基本用法

import lupa

from lupa import LuaRuntime

lua = LuaRuntime(unpack_returned_tuples=True)

lua.eval('1+1') # output: 2

lua_func = lua.eval('function(f, n) return f(n) end')

def py_add1(n):

return n+1

lua_func(py_add1, 2) # output: 3

lua.eval('python.eval(" 2 2 ")') == 4 # output: True

lua.eval('python.builtins.str(4)') == '4' # output: True

# lua_type(obj) 函数用于输出 type of a wrapped Lua object

lupa.lua_type(lua_func) # output: 'function'

lupa.lua_type(lua.eval('{}')) # output: 'table'

lupa.lua_type(123) is None # output: True

lupa.lua_type('abc') is None # output: True

lupa.lua_type({}) is None # output: True

# flag unpack_returned_tuples=True that is passed to create the Lua runtime

lua.execute('a,b,c = python.eval("(1,2)")')

g = lua.globals()

g.a # output: 1

g.b # output: 2

g.c is None # output: True

# flag unpack_returned_tuples=False, functions that return a tuple pass it through to the Lua code

non_explode_lua = lupa.LuaRuntime(unpack_returned_tuples=False)

non_explode_lua.execute('a,b,c = python.eval("(1,2)")')

g = non_explode_lua.globals()

g.a # output: (1, 2)

g.b is None # output: True

g.c is None # output: True

更多用法参考lupa官网.

小讯
上一篇 2025-01-24 13:34
下一篇 2025-01-26 12:36

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/101366.html