Skip to content

How to pass a python dictionary as an argument into a function defined in LUA code? #269

Description

@LeMoussel

Hi,

I'm trying to pass a python dictionary as an argument into a function defined in LUA code but it crashes without exception.
The Lua function returns the first non-empty parameter, or nil if they are all empty.

LUA code: lua/Article.lua

local Article = { }

function validTextArg( args, name, ... )
	local texte = args[name]
	if type( texte ) == 'string' and texte ~= '' then
		return texte
	elseif #{ ... } > 0 then
		return validTextArg( args, ... )
	end
end

function Article.article( args )
    -- validArg returns the first non-empty parameter, or nil if they are all empty.
    -- Only parameter names should be passed to the function.
    local validArg = function ( ... ) return validTextArg( args, ... ) or false end

    return validArg( 'périodique', 'revue', 'journal' )
end

return Article

Python code: main.py

    import lupa.lua54 as lupa

    lua = lupa.LuaRuntime(
        unpack_returned_tuples=True,
        register_eval=False,
    )
    print(
        f"Using {lupa.LuaRuntime().lua_implementation} (compiled with {lupa.LUA_VERSION})"
    )

    lua.execute("""
    local Article = require('lua/Article')
    function Tpl_Article(args)
        return Article.article(args)
    end
    """
    params = {
        'revue': 'Revue des langues romanes',
        'numéro': '105',
        'année': '2001',
        'pages': '503-517',
    }
    lua_result = lua.globals().Tpl_Article(params) # Crash

How do I invoke the LUA script and pass dictionary arguments ?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions