Skip to content

Commit

Permalink
Explicitly import "lupa.luaXY" wheen looking up a submodule as module…
Browse files Browse the repository at this point in the history
… attribute in order to control how the import is done.
  • Loading branch information
scoder committed Oct 10, 2023
1 parent 96c1e3e commit 9e91522
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lupa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,23 @@ def __getattr__(name):
Get a name from the latest available Lua (or LuaJIT) module.
Imports the module as needed.
"""
if name.startswith('lua'):
import re
if re.match(r"((lua[a-z]*)([0-9]*))$", name):
# "from lupa import lua54" etc.
assert name not in globals()
try:
module = __import__(name, globals=globals(), locals=locals(), level=1)
except ImportError:
raise AttributeError(name)
else:
assert name in globals()
return module

# Import the default Lua implementation and look up the attribute there.
lua = _newest_lib if _newest_lib is not None else _import_newest_lib()
return getattr(lua, name)
globals()[name] = attr = getattr(lua, name)
return attr


import sys
Expand Down

0 comments on commit 9e91522

Please sign in to comment.