[util] add 'std' object to global eval namespace (#6330)

allows accessing standard library modules (and other external modules)
in a more straightforward manner than '__import__(...)'

* std.os.getcwd()
* std["os"].getcwd()
This commit is contained in:
Mike Fährmann
2024-10-17 21:01:34 +02:00
parent 9b4c94fda8
commit 4667833195
2 changed files with 48 additions and 0 deletions

View File

@@ -532,6 +532,24 @@ class HTTPBasicAuth():
return request
class ModuleProxy():
__slots__ = ()
def __getitem__(self, key, modules=sys.modules):
try:
return modules[key]
except KeyError:
pass
try:
__import__(key)
except ImportError:
modules[key] = NONE
return NONE
return modules[key]
__getattr__ = __getitem__
class LazyPrompt():
__slots__ = ()
@@ -540,6 +558,7 @@ class LazyPrompt():
class NullContext():
__slots__ = ()
def __enter__(self):
return None
@@ -646,6 +665,7 @@ GLOBALS = {
"restart" : raises(exception.RestartExtraction),
"hash_sha1": sha1,
"hash_md5" : md5,
"std" : ModuleProxy(),
"re" : re,
}