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

question: using python functions on lua tables #259

Open
amm0x41r opened this issue Feb 29, 2024 · 2 comments
Open

question: using python functions on lua tables #259

amm0x41r opened this issue Feb 29, 2024 · 2 comments

Comments

@amm0x41r
Copy link

amm0x41r commented Feb 29, 2024

Hello,
Sorry for the noob question, but I can't quite figure it out.
I'm trying to use the Python JSON lib instead of any of the lua ones due to issues with package installation on target, but I can't get even the minimal example to work. Here's what I'm trying:

import lupa
import json

lua = lupa.LuaRuntime(unpack_returned_tuples=True)
lua.execute("json = {}")
lua.globals()["json"]["encode"] = json.dumps
lua.globals()["json"]["decode"] = json.loads
lua_function = """
function construct_json_list()
    local out = {"a", "b", "c"}
    return json.encode("throwaway", out)
end
"""
lua.execute(lua_function)
print(lua.globals()["construct_json_list"]())

This simply throws TypeError: Object of type _LuaTable is not JSON serializable. Obviously json.dumps does not support Lua Tables.

tl;dr how to convert a lua table to a python object?

Any help is appreciated.

@amm0x41r
Copy link
Author

amm0x41r commented Feb 29, 2024

Ok, so this seems to work:

def lua_table_to_python_obj(obj):
    if type(obj).__name__ == "_LuaTable":
        # Check if the Lua table is array-like
        keys = list(obj.keys())
        if keys == list(range(1, len(keys) + 1)):
            # Convert to Python list if keys are consecutive integers starting from 1
            return [lua_table_to_python(obj[key]) for key in keys]
        else:
            # Convert to Python dictionary otherwise
            return {key: lua_table_to_python(obj[key]) for key in obj}
    return obj

Is this truly what I'm meant to do? recursively convert tables to objects by hand? or am I missing something obvious?

@scoder
Copy link
Owner

scoder commented Mar 24, 2024

I'd use lupa.lua_type(obj) == 'table', and try to avoid the list(range()), but yes, it would usually be something like this, depending on your specific needs. It's not clear that an implementation in Lupa would cover a sufficiently large number of use cases, but since users can always implement their own mapping, it might still work out.

Want to provide a PR that adds this as a method of the _LuaTable class, say, .copy_to_python()?

I'd expect that this can be made quite fast when using the Lua C-API instead of going through Lupa's Python API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants