lupa icon indicating copy to clipboard operation
lupa copied to clipboard

convert a python function in py_to_lua

Open oeway opened this issue 9 years ago • 1 comments

Hi,

I am trying to pass a python function as argument into a function defined in lua code, it complaining that: LuaError: XXXXXX.lua:44: attempt to call local 'opfunc' (a table value).

def tuple_fun():
    return "one", "two", "three", "four"
lua.globals()['fun'] = tuple_fun

lua.eval('type(fun)')
# output userdata

lua.eval('type(python.as_function(fun))')
# output function

I know that by using python.as_function in lua code can make lupa recognize the object as a function, but in my case, the lua code is not possible to change because it's in a third party library, I try to call the function in lupa code.

I checked the code in _lupa.pyx, in the function named py_to_lua, why not just convert the python function into a lua object? or add the python.as_function automatically?

Can anyone help?

oeway avatar Nov 03 '16 21:11 oeway

It's hard to say what was the problem exactly without seeing the third party library code which results in the error. But it's perfectly fine to run python function from lua without any conversions, here is a continuation for your example:

>>> lua.eval('function(f) return f() end')(tuple_fun)
Out[11]: (u'one', u'two', u'three', u'four')

jbinary avatar Nov 04 '16 09:11 jbinary