| 1 |
from __future__ import absolute_import |
| 2 |
|
| 3 |
from contextlib import contextmanager as _contextmanager |
| 4 |
|
| 5 |
|
| 6 |
_newest_lib = None |
| 7 |
|
| 8 |
|
| 9 |
@_contextmanager |
| 10 |
def allow_lua_module_loading(): |
| 11 |
""" |
| 12 |
A context manager for enabling binary Lua module loading when importing Lua. |
| 13 |
|
| 14 |
This can only be used once within a Python runtime and must wrap the import of the |
| 15 |
``lupa.*`` Lua module, e.g.:: |
| 16 |
|
| 17 |
import lupa |
| 18 |
with lupa.allow_lua_module_loading() |
| 19 |
from lupa import lua54 |
| 20 |
|
| 21 |
lua = lua54.LuaRuntime() |
| 22 |
lua.require('cjson') |
| 23 |
""" |
| 24 |
try: |
| 25 |
from os import RTLD_NOW, RTLD_GLOBAL |
| 26 |
except ImportError: |
| 27 |
try: |
| 28 |
from DLFCN import RTLD_NOW, RTLD_GLOBAL |
| 29 |
except ImportError: |
| 30 |
|
| 31 |
yield |
| 32 |
return |
| 33 |
|
| 34 |
dlopen_flags = RTLD_NOW | RTLD_GLOBAL |
| 35 |
|
| 36 |
import sys |
| 37 |
old_flags = sys.getdlopenflags() |
| 38 |
|
| 39 |
try: |
| 40 |
sys.setdlopenflags(dlopen_flags) |
| 41 |
yield |
| 42 |
finally: |
| 43 |
sys.setdlopenflags(old_flags) |
| 44 |
|
| 45 |
|
| 46 |
def _import_newest_lib(): |
| 47 |
global _newest_lib |
| 48 |
if _newest_lib is not None: |
| 49 |
return _newest_lib |
| 50 |
|
| 51 |
import os.path |
| 52 |
import re |
| 53 |
|
| 54 |
package_dir = os.path.dirname(__file__) |
| 55 |
modules = [ |
| 56 |
match.groups() for match in ( |
| 57 |
re.match(r"((lua[a-z]*)([0-9]*))\..*", filename) |
| 58 |
for filename in os.listdir(package_dir) |
| 59 |
) |
| 60 |
if match |
| 61 |
] |
| 62 |
if not modules: |
| 63 |
raise RuntimeError("Failed to import Lupa binary module.") |
| 64 |
|
| 65 |
module_name = max(modules, key=lambda m: (m[1] == 'lua', tuple(map(int, m[2] or '0')))) |
| 66 |
|
| 67 |
_newest_lib = __import__(module_name[0], level=1, fromlist="*", globals=globals()) |
| 68 |
return _newest_lib |
| 69 |
|
| 70 |
|
| 71 |
def __getattr__(name): |
| 72 |
""" |
| 73 |
Get a name from the latest available Lua (or LuaJIT) module. |
| 74 |
Imports the module as needed. |
| 75 |
""" |
| 76 |
if name.startswith('lua'): |
| 77 |
import re |
| 78 |
if re.match(r"((lua[a-z]*)([0-9]*))$", name): |
| 79 |
|
| 80 |
assert name not in globals() |
| 81 |
try: |
| 82 |
module = __import__(name, globals=globals(), locals=locals(), level=1) |
| 83 |
except ImportError: |
| 84 |
raise AttributeError(name) |
| 85 |
else: |
| 86 |
assert name in globals() |
| 87 |
return module |
| 88 |
|
| 89 |
|
| 90 |
lua = _newest_lib if _newest_lib is not None else _import_newest_lib() |
| 91 |
globals()[name] = attr = getattr(lua, name) |
| 92 |
return attr |
| 93 |
|
| 94 |
|
| 95 |
import sys |
| 96 |
if sys.version_info < (3, 7): |
| 97 |
|
| 98 |
_import_newest_lib() |
| 99 |
globals().update( |
| 100 |
(name, getattr(_newest_lib, name)) |
| 101 |
for name in _newest_lib.__all__ |
| 102 |
) |
| 103 |
del sys |
| 104 |
|
| 105 |
try: |
| 106 |
from lupa.version import __version__ |
| 107 |
except ImportError: |
| 108 |
pass |