Going from Python to Lua

class mehtap.py2lua.SupportsLua(Protocol[LV])

Bases: Protocol[LV]

Protocol for objects that can be converted to LuaValue.

py2lua() will call the __lua__ method of the object if it exists. The result is considered the result of the conversion, so it is expected to be a LuaValue instance.

mehtap.py2lua.PyLuaNative

Types that py2lua() knows how to convert natively.

alias of None | bool | int | float | str | bytes | Mapping | Iterable | Callable

mehtap.py2lua.Py2LuaAccepts

Types that py2lua() knows how to convert.

alias of None | bool | int | float | str | bytes | Mapping | Iterable | Callable | SupportsLua[LV]

mehtap.py2lua.py2lua(value: None) LuaNilType
mehtap.py2lua.py2lua(value: bool) LuaBool
mehtap.py2lua.py2lua(value: int | float) LuaNumber
mehtap.py2lua.py2lua(value: str | bytes) LuaString
mehtap.py2lua.py2lua(value: Mapping) LuaTable
mehtap.py2lua.py2lua(value: Iterable) LuaTable
mehtap.py2lua.py2lua(value: Callable) LuaFunction
mehtap.py2lua.py2lua(value: SupportsLua[LV]) LV

Convert a plain Python value to a LuaValue.

If the value (or a member of the value) has a __lua__ dunder method, (or, in other words implements the SupportsLua protocol) the converter will call it and convert its return value instead.

Iterables will be converted to sequence tables starting from the index 1.

Functions are converted using @lua_function(wrap_values=True). See lua_function() for more information.

This function is implemented using memoization, so it can convert recursive data structures.

Raises:

TypeError – if the value can’t be converted

mehtap.py2lua.lua_function(*, name: str | None = None, gets_scope: Literal[False] = False, wrap_values: Literal[False] = False, rename_args: list[str] | None = None) Callable[[LuaCallback], LuaFunction]
mehtap.py2lua.lua_function(*, name: str | None = None, gets_scope: Literal[False] = False, wrap_values: Literal[True] = True, rename_args: list[str] | None = None) Callable[[PyCallback], LuaFunction]
mehtap.py2lua.lua_function(*, name: str | None = None, gets_scope: Literal[True] = True, wrap_values: Literal[False] = False, rename_args: list[str] | None = None) Callable[[LuaScopeCallback], LuaFunction]
mehtap.py2lua.lua_function(*, name: str | None = None, gets_scope: Literal[True] = True, wrap_values: Literal[True] = True, rename_args: list[str] | None = None) Callable[[PyScopeCallback], LuaFunction]
mehtap.py2lua.lua_function(function: Callable) LuaFunction

Convert a Python callable to a LuaFunction instance.

Parameters:
  • function – The function to convert. Allows decorator usage without parentheses.

  • name – Allows to rename the function.

  • gets_scope – Whether the function requires a Scope as its first argument.

  • wrap_values – Whether the values should be converted to/from Lua/Python when passing them to/from the function.

  • rename_args – Allows to rename the arguments of the function.

Returns:

A decorator that turns Python functions to LuaFunction instances.

The arguments of the decorated function must be positional-only. The function may have a variadic parameter as the last one. For example, def f(a, b, c, /): ... or def f(a, b, /, *args): ....

If the function throws an exception, it will be caught and a similar error will be re-raised in Lua.

If gets_scope is set to True, the function will receive a scope as its first argument.

When wrap_values is set to True, the function will receive and return Python values. If the function returns a list or tuple, it will be returned in Lua as a multires. If it returns a single value, it will be returned as a single value.

When wrap_values is set to False, the function’s arguments will be instances of LuaValue and the function will return a list of LuaValue instances. Note that since function calls in Lua are multires expressions, functions always return a list of values. Returning None is equivalent to returning an empty list.

If rename_args is provided, it should be a list of strings with the same length as the number of arguments of the function. This change is only cosmetic since only parameter order is used to bind arguments to the function.

If preserve is set to True, table must not be left empty.