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 aLuaValueinstance.
- 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 theSupportsLua 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). Seelua_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
LuaFunctioninstance.- Parameters:
function – The function to convert. Allows decorator usage without parentheses.
name – Allows to rename the function.
gets_scope – Whether the function requires a
Scopeas 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
LuaFunctioninstances.
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, /): ...ordef 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
LuaValueand the function will return a list ofLuaValueinstances. Note that since function calls in Lua are multires expressions, functions always return a list of values. ReturningNoneis 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.