Cannot execute lua code, read from file

1.7k Views Asked by At

I have some sample of lua code. For example:

for i=1,4 do
   print(i)
end

and I need to execute it using Lupa library on Python 3.4.

For doing this, I have write the following code:

import lupa
lua = lupa.LuaRuntime(unpack_returned_tuples=True)
lua.eval(open('test.lua').read())

where test.lua is a my sample lua file.

But if I try to execute this code, I get following:

Traceback (most recent call last):
  File "/Job/HW-2/testing/test1.py", line 3, in <module>
    lua.eval(open('test.lua').read())
  File "lupa/_lupa.pyx", line 251, in lupa._lupa.LuaRuntime.eval (lupa/_lupa.c:4579)
  File "lupa/_lupa.pyx", line 1250, in lupa._lupa.run_lua (lupa/_lupa.c:18295)
lupa._lupa.LuaSyntaxError: error loading code: [string "<python>"]:1: unexpected symbol near 'for'

It seems, this is a very simple issue, but I don't know how to fix it. So, how I can execute Lua file on Python 3 using Lupa?

1

There are 1 best solutions below

2
On BEST ANSWER

The eval method requires a Lua expression that returns a value. In order to execute Lua statements, call execute instead:

>>> lua.execute("""for i=1,4 do
...    print(i)
... end""") 
1
2
3
4