Skip to content

Instantly share code, notes, and snippets.

@jaseg
Created January 9, 2015 01:02
Show Gist options
  • Save jaseg/e739afa7082141c4347f to your computer and use it in GitHub Desktop.
Save jaseg/e739afa7082141c4347f to your computer and use it in GitHub Desktop.
How to run some lua from python via ctypes
import ctypes as ct
lua = ct.CDLL('liblua.so.5.2')
lua.luaL_newstate.restype = ct.c_void_p
lua.lua_tolstring.restype = ct.c_char_p
st = lua.luaL_newstate()
def foo(st):
print(lua.lua_tolstring(st, 1, 0).value.decode())
return 0
ft = ct.CFUNCTYPE(ct.c_int, ct.c_void_p)
func = ft(foo) # must not be garbage-collected
lua.lua_pushcclosure(st, func, 0)
lua.lua_setglobal(st, b'foo')
lua.luaL_loadstring(st, b'foo("Hello World!")')
lua.lua_pcallk(st, 0, -1, 0, 0, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment