Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix python.args usage and optimize call_python #183

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions lupa/_lupa.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,7 @@ cdef int py_object_gc(lua_State* L) nogil:
cdef bint call_python(LuaRuntime runtime, lua_State *L, py_object* py_obj) except -1:
# Callers must assure that py_obj.obj is not NULL, i.e. it points to a valid Python object.
cdef int i, nargs = lua.lua_gettop(L) - 1
cdef Py_ssize_t j
cdef tuple args
cdef dict kwargs

Expand All @@ -1657,16 +1658,31 @@ cdef bint call_python(LuaRuntime runtime, lua_State *L, py_object* py_obj) excep
lua.lua_settop(L, 0) # FIXME
result = f()
else:
args = ()
kwargs = {}

for i in range(nargs):
arg = py_from_lua(runtime, L, i+2)
if isinstance(arg, _PyArguments):
args += (<_PyArguments>arg).args
kwargs = dict(**kwargs, **(<_PyArguments>arg).kwargs)
else:
args += (arg, )
# Special treatment for the last argument
last_arg = py_from_lua(runtime, L, nargs + 1)

if isinstance(last_arg, _PyArguments):
# Calling a function and _PyArguments is the last argument
# Lua f(..., python.args{a, b, c=1, d=2}) => Python as f(..., a, b, c=1, d=2)
kwargs = (<_PyArguments>last_arg).kwargs
moreargs = (<_PyArguments>last_arg).args
args = cpython.tuple.PyTuple_New(nargs - 1 + len(moreargs))
for j, arg in enumerate(moreargs):
cpython.ref.Py_INCREF(arg)
cpython.tuple.PyTuple_SET_ITEM(args, nargs - 1 + j, arg)
else:
# Calling a function normally
# Lua f(...) => Python as f(...)
kwargs = None
args = cpython.tuple.PyTuple_New(nargs)
cpython.ref.Py_INCREF(last_arg)
cpython.tuple.PyTuple_SET_ITEM(args, nargs - 1, last_arg)

# Process the rest of the arguments
for i in range(nargs - 1):
arg = py_from_lua(runtime, L, i + 2)
cpython.ref.Py_INCREF(arg)
cpython.tuple.PyTuple_SET_ITEM(args, i, arg)

if args and PyMethod_Check(f) and (<PyObject*>args[0]) is PyMethod_GET_SELF(f):
# Calling a bound method and self is already the first argument.
Expand All @@ -1681,7 +1697,11 @@ cdef bint call_python(LuaRuntime runtime, lua_State *L, py_object* py_obj) excep
f = <object>PyMethod_GET_FUNCTION(f)

lua.lua_settop(L, 0) # FIXME
result = f(*args, **kwargs)

if kwargs:
result = f(*args, **kwargs)
else:
result = f(*args)

return py_function_result_to_lua(runtime, L, result)

Expand Down
24 changes: 20 additions & 4 deletions lupa/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2712,11 +2712,27 @@ def test_all_types(self):
self.assertIncorrect('python.args{[kwargs["%s"]] = true}' % objtype,
regex='table key is neither an integer nor a string')

def test_kwargs_merge(self):
self.assertResult('python.args{1, a=1}, python.args{2}, python.args{}, python.args{b=2}', (1, 2), dict(a=1, b=2))
def test_args_merge(self):
self.assertResult('1, 2, python.args{a=5, b=6}', (1, 2), dict(a=5, b=6))
self.assertResult('1, 2, python.args{3, 4, a=5, b=6}', (1, 2, 3, 4), dict(a=5, b=6))

def test_kwargs_merge_conflict(self):
self.assertIncorrect('python.args{a=1}, python.args{a=2}', regex='multiple values')

def test_multiple_args(self):
_G = self.lua.globals()

x = self.lua.eval('python.args{1, 2, a=5, b=6}')
_G.x = x
self.assertEqual(_G.x, x)

y = self.lua.eval('python.args{11, c=13}')
_G.y = y
self.assertEqual(_G.y, y)

callfxy = self.lua.eval('function(f) return f(x, y) end')

# contrary to y, x will not be unpacked
self.assertEqual(callfxy(self.get_args), (x, 11))
self.assertEqual(callfxy(self.get_kwargs), dict(c=13))


class PythonArgumentsInLuaMethodsTest(PythonArgumentsInLuaTest):
Expand Down