From b082b23995a90c13fc9fdf95534cd101f5b551ce Mon Sep 17 00:00:00 2001 From: scoder Date: Thu, 2 Nov 2023 09:13:15 +0100 Subject: [PATCH] Do not allow module loading by default at all, add docstring. --- lupa/__init__.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lupa/__init__.py b/lupa/__init__.py index e2876390..8b9edffc 100644 --- a/lupa/__init__.py +++ b/lupa/__init__.py @@ -8,6 +8,18 @@ @contextmanager def allow_lua_module_loading(): + """ + A context manager for enabling binary Lua module loading when importing Lua. + + This can only be used once within a Python runtime and must wrap the import of the + ``lupa.*`` Lua module, e.g.:: + + with lupa.allow_lua_module_loading() + from lupa import lua54 + + lua = lua54.LuaRuntime() + lua.require('cjson') + """ try: from os import RTLD_NOW, RTLD_GLOBAL except ImportError: @@ -51,15 +63,7 @@ def _import_newest_lib(): # prefer Lua over LuaJIT and high versions over low versions. module_name = max(modules, key=lambda m: (m[1] == 'lua', tuple(map(int, m[2] or '0')))) - # Allowing module loading using dlopenflags by default doesn't work when there are multiple - # Lua modules because the symbols collide with each other when loaded with RTLD_GLOBAL. - # Enable this by default only if there is exactly one lua module available. - if len(modules) == 1: - with allow_lua_module_loading(): - _newest_lib = __import__(module_name[0], level=1, fromlist="*", globals=globals()) - else: - _newest_lib = __import__(module_name[0], level=1, fromlist="*", globals=globals()) - + _newest_lib = __import__(module_name[0], level=1, fromlist="*", globals=globals()) return _newest_lib