Python wrapper around Lua and LuaJIT


License
MIT
Install
pip install lupa==2.1

Documentation

Lupa

image

Lupa integrates the runtimes of Lua or LuaJIT2 into CPython. It is a partial rewrite of LunaticPython in Cython with some additional features such as proper coroutine support.

For questions not answered here, please contact the Lupa mailing list.

local

Major features

  • separate Lua runtime states through a LuaRuntime class
  • Python coroutine wrapper for Lua coroutines
  • iteration support for Python objects in Lua and Lua objects in Python
  • proper encoding and decoding of strings (configurable per runtime, UTF-8 by default)
  • frees the GIL and supports threading in separate runtimes when calling into Lua
  • tested with Python 2.7/3.6 and later
  • ships with Lua 5.1, 5.2, 5.3 and 5.4 as well as LuaJIT 2.0 and 2.1 on systems that support it.
  • easy to hack on and extend as it is written in Cython, not C

Why the name?

In Latin, "lupa" is a female wolf, as elegant and wild as it sounds. If you don't like this kind of straight forward allegory to an endangered species, you may also happily assume it's just an amalgamation of the phonetic sounds that start the words "Lua" and "Python", two from each to keep the balance.

Why use it?

It complements Python very well. Lua is a language as dynamic as Python, but LuaJIT compiles it to very fast machine code, sometimes faster than many statically compiled languages for computational code. The language runtime is very small and carefully designed for embedding. The complete binary module of Lupa, including a statically linked LuaJIT2 runtime, only weighs some 800KB on a 64 bit machine. With standard Lua 5.2, it's less than 600KB.

However, the Lua ecosystem lacks many of the batteries that Python readily includes, either directly in its standard library or as third party packages. This makes real-world Lua applications harder to write than equivalent Python applications. Lua is therefore not commonly used as primary language for large applications, but it makes for a fast, high-level and resource-friendly backup language inside of Python when raw speed is required and the edit-compile-run cycle of binary extension modules is too heavy and too static for agile development or hot-deployment.

Lupa is a very fast and thin wrapper around Lua or LuaJIT. It makes it easy to write dynamic Lua code that accompanies dynamic Python code by switching between the two languages at runtime, based on the tradeoff between simplicity and speed.

Which Lua version?

The binary wheels include different Lua versions as well as LuaJIT, if supported. By default, import lupa uses the latest Lua version, but you can choose a specific one via import:

Examples

The function lua_type(obj) can be used to find out the type of a wrapped Lua object in Python code, as provided by Lua's type() function:

To help in distinguishing between wrapped Lua objects and normal Python objects, it returns None for the latter:

Note the flag unpack_returned_tuples=True that is passed to create the Lua runtime. It is new in Lupa 0.21 and changes the behaviour of tuples that get returned by Python functions. With this flag, they explode into separate Lua values:

When set to False, functions that return a tuple pass it through to the Lua code:

Since the default behaviour (to not explode tuples) might change in a later version of Lupa, it is best to always pass this flag explicitly.

Python objects in Lua

Python objects are either converted when passed into Lua (e.g. numbers and strings) or passed as wrapped object references.

Wrapped Lua objects get unwrapped when they are passed back into Lua, and arbitrary Python objects get wrapped in different ways:

Lua supports two main protocols on objects: calling and indexing. It does not distinguish between attribute access and item access like Python does, so the Lua operations obj[x] and obj.x both map to indexing. To decide which Python protocol to use for Lua wrapped objects, Lupa employs a simple heuristic.

Pratically all Python objects allow attribute access, so if the object also has a __getitem__ method, it is preferred when turning it into an indexable Lua object. Otherwise, it becomes a simple object that uses attribute access for indexing from inside Lua.

Obviously, this heuristic will fail to provide the required behaviour in many cases, e.g. when attribute access is required to an object that happens to support item access. To be explicit about the protocol that should be used, Lupa provides the helper functions as_attrgetter() and as_itemgetter() that restrict the view on an object to a certain protocol, both from Python and from inside Lua:

Note that unlike Lua function objects, callable Python objects support indexing in Lua:

Iteration in Lua

Iteration over Python objects from Lua's for-loop is fully supported. However, Python iterables need to be converted using one of the utility functions which are described here. This is similar to the functions like pairs() in Lua.

To iterate over a plain Python iterable, use the python.iter() function. For example, you can manually copy a Python list into a Lua table like this:

Python's enumerate() function is also supported, so the above could be simplified to:

For iterators that return tuples, such as dict.iteritems(), it is convenient to use the special python.iterex() function that automatically explodes the tuple items into separate Lua arguments:

Note that accessing the d.items method from Lua requires passing the dict as attrgetter. Otherwise, attribute access in Lua would use the getitem protocol of Python dicts and look up d['items'] instead.

None vs. nil

While None in Python and nil in Lua differ in their semantics, they usually just mean the same thing: no value. Lupa therefore tries to map one directly to the other whenever possible:

The only place where this cannot work is during iteration, because Lua considers a nil value the termination marker of iterators. Therefore, Lupa special cases None values here and replaces them by a constant python.none instead of returning nil:

Lupa avoids this value escaping whenever it's obviously not necessary. Thus, when unpacking tuples during iteration, only the first value will be subject to python.none replacement, as Lua does not look at the other items for loop termination anymore. And on enumerate() iteration, the first value is known to be always a number and never None, so no replacement is needed.

Note that this behaviour changed in Lupa 1.0. Previously, the python.none replacement was done in more places, which made it not always very predictable.

Lua Tables

Lua tables mimic Python's mapping protocol. For the special case of array tables, Lua automatically inserts integer indices as keys into the table. Therefore, indexing starts from 1 as in Lua instead of 0 as in Python. For the same reason, negative indexing does not work. It is best to think of Lua tables as mappings rather than arrays, even for plain array tables.

To simplify the table creation from Python, the LuaRuntime comes with a helper method that creates a Lua table from Python arguments:

A second helper method, .table_from(), was added in Lupa 1.1 and accepts any number of mappings and sequences/iterables as arguments. It collects all values and key-value pairs and builds a single Lua table from them. Any keys that appear in multiple mappings get overwritten with their last value (going from left to right).

Since Lupa 2.1, passing recursive=True will map data structures recursively to Lua tables.

A lookup of non-existing keys or indices returns None (actually nil inside of Lua). A lookup is therefore more similar to the .get() method of Python dicts than to a mapping lookup in Python.

Note that len() does the right thing for array tables but does not work on mappings:

This is because len() is based on the # (length) operator in Lua and because of the way Lua defines the length of a table. Remember that unset table indices always return nil, including indices outside of the table size. Thus, Lua basically looks for an index that returns nil and returns the index before that. This works well for array tables that do not contain nil values, gives barely predictable results for tables with 'holes' and does not work at all for mapping tables. For tables with both sequential and mapping content, this ignores the mapping part completely.

Note that it is best not to rely on the behaviour of len() for mappings. It might change in a later version of Lupa.

Similar to the table interface provided by Lua, Lupa also supports attribute access to table members:

This enables access to Lua 'methods' that are associated with a table, as used by the standard library modules:

Python Callables

As discussed earlier, Lupa allows Lua scripts to call Python functions and methods:

Lua doesn't have a dedicated syntax for named arguments, so by default Python callables can only be called using positional arguments.

A common pattern for implementing named arguments in Lua is passing them in a table as the first and only function argument. See http://lua-users.org/wiki/NamedParameters for more details. Lupa supports this pattern by providing two decorators: lupa.unpacks_lua_table for Python functions and lupa.unpacks_lua_table_method for methods of Python objects.

Python functions/methods wrapped in these decorators can be called from Lua code as func(foo, bar), func{foo=foo, bar=bar} or func{foo, bar=bar}. Example:

If you do not control the function implementation, you can also just manually wrap a callable object when passing it into Lupa:

There are some limitations:

  1. Avoid using lupa.unpacks_lua_table and lupa.unpacks_lua_table_method for functions where the first argument can be a Lua table. In this case py_func{foo=bar} (which is the same as py_func({foo=bar}) in Lua) becomes ambiguous: it could mean either "call py_func with a named foo argument" or "call py_func with a positional {foo=bar} argument".
  2. One should be careful with passing nil values to callables wrapped in lupa.unpacks_lua_table or lupa.unpacks_lua_table_method decorators. Depending on the context, passing nil as a parameter can mean either "omit a parameter" or "pass None". This even depends on the Lua version.

    It is possible to use python.none instead of nil to pass None values robustly. Arguments with nil values are also fine when standard braces func(a, b, c) syntax is used.

Because of these limitations lupa doesn't enable named arguments for all Python callables automatically. Decorators allow to enable named arguments on a per-callable basis.

Lua Coroutines

The next is an example of Lua coroutines. A wrapped Lua coroutine behaves exactly like a Python coroutine. It needs to get created at the beginning, either by using the .coroutine() method of a function or by creating it in Lua code. Then, values can be sent into it using the .send() method or it can be iterated over. Note that the .throw() method is not supported, though.

An example where values are passed into the coroutine using its .send() method:

It also works to create coroutines in Lua and to pass them back into Python space:

Threading

The following example calculates a mandelbrot image in parallel threads and displays the result in PIL. It is based on a benchmark implementation for the Computer Language Benchmarks Game.

Note how the example creates a separate LuaRuntime for each thread to enable parallel execution. Each LuaRuntime is protected by a global lock that prevents concurrent access to it. The low memory footprint of Lua makes it reasonable to use multiple runtimes, but this setup also means that values cannot easily be exchanged between threads inside of Lua. They must either get copied through Python space (passing table references will not work, either) or use some Lua mechanism for explicit communication, such as a pipe or some kind of shared memory setup.

Restricting Lua access to Python objects

Lupa provides a simple mechanism to control access to Python objects. Each attribute access can be passed through a filter function as follows:

The is_setting flag indicates whether the attribute is being read or set.

Note that the attributes of Python functions provide access to the current globals() and therefore to the builtins etc. If you want to safely restrict access to a known set of Python objects, it is best to work with a whitelist of safe attribute names. One way to do that could be to use a well selected list of dedicated API objects that you provide to Lua code, and to only allow Python attribute access to the set of public attribute/method names of these objects.

Since Lupa 1.0, you can alternatively provide dedicated getter and setter function implementations for a LuaRuntime:

Restricting Lua Memory Usage

Lupa provides a simple mechanism to control the maximum memory usage of the Lua Runtime since version 2.0. By default Lupa does not interfere with Lua's memory allocation, to opt-in you must set the max_memory when creating the LuaRuntime.

The LuaRuntime provides three methods for controlling and reading the memory usage:

  1. get_memory_used(total=False) to get the current memory usage of the LuaRuntime.
  2. get_max_memory(total=False) to get the current memory limit. 0 means there is no memory limitation.
  3. set_max_memory(max_memory, total=False) to change the memory limit. Values below or equal to 0 mean no limit.

There is always some memory used by the LuaRuntime itself (around ~20KiB, depending on your lua version and other factors) which is excluded from all calculations unless you specify total=True.

Lua code hitting the memory limit will receive memory errors:

LuaMemoryError inherits from LuaError and MemoryError.

Importing Lua binary modules

This will usually work as is, but here are the details, in case anything goes wrong for you.

To use binary modules in Lua, you need to compile them against the header files of the LuaJIT sources that you used to build Lupa, but do not link them against the LuaJIT library.

Furthermore, CPython needs to enable global symbol visibility for shared libraries before loading the Lupa module. This can be done by calling sys.setdlopenflags(flag_values). Importing the lupa module will automatically try to set up the correct dlopen flags if it can find the platform specific DLFCN Python module that defines the necessary flag constants. In that case, using binary modules in Lua should work out of the box.

If this setup fails, however, you have to set the flags manually. When using the above configuration call, the argument flag_values must represent the sum of your system's values for RTLD_NEW and RTLD_GLOBAL. If RTLD_NEW is 2 and RTLD_GLOBAL is 256, you need to call sys.setdlopenflags(258).

Assuming that the Lua luaposix (posix) module is available, the following should work on a Linux system:

Building with different Lua versions

The build is configured to automatically search for an installed version of first LuaJIT and then Lua, and failing to find either, to use the bundled LuaJIT or Lua version.

If you wish to build Lupa with a specific version of Lua, you can configure the following options on setup:

Option Description
--lua-lib <libfile> Lua library file path, e.g. --lua-lib /usr/local/lib/lualib.a
--lua-includes <incdir> Lua include directory, e.g. --lua-includes /usr/local/include
--use-bundle Use bundled LuaJIT or Lua instead of searching for an installed version.
--no-bundle Don't use the bundled LuaJIT/Lua, search for an installed version of LuaJIT or Lua, e.g. using pkg-config.
--no-lua-jit Don't use or search for LuaJIT, only use or search Lua instead.