Spaces:
Sleeping
Sleeping
Reverting some attempts
Browse files
restrictedpython_code_eval.py
CHANGED
@@ -408,28 +408,16 @@ ALLOWED_SYS_NAMES = ['maxsize']
|
|
408 |
class AllowListImporter:
|
409 |
def __init__(self, allowed_imports: List[str]):
|
410 |
self.allowed_imports = allowed_imports
|
411 |
-
|
412 |
-
inner_sys = importlib.util.module_from_spec(sys_spec) # type: ignore
|
413 |
-
sys_spec.loader.exec_module(inner_sys) # type: ignore
|
414 |
-
|
415 |
-
for key in list(dir(inner_sys)):
|
416 |
-
if key not in ALLOWED_SYS_NAMES:
|
417 |
-
delattr(inner_sys, key)
|
418 |
-
self.inner_sys = inner_sys
|
419 |
-
|
420 |
def __call__(self, name, globals=None, locals=None, fromlist=(), level=0):
|
421 |
if name.startswith('.'):
|
422 |
raise ImportError("Relative imports are not allowed.")
|
423 |
|
424 |
if '.' in name:
|
425 |
-
package_name,
|
426 |
|
427 |
else:
|
428 |
package_name = name
|
429 |
-
sub_name = None
|
430 |
-
|
431 |
-
if package_name == 'sys':
|
432 |
-
return self.inner_sys
|
433 |
|
434 |
if package_name in self.allowed_imports:
|
435 |
return importlib.__import__(name, globals, locals, fromlist, level)
|
|
|
408 |
class AllowListImporter:
|
409 |
def __init__(self, allowed_imports: List[str]):
|
410 |
self.allowed_imports = allowed_imports
|
411 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
412 |
def __call__(self, name, globals=None, locals=None, fromlist=(), level=0):
|
413 |
if name.startswith('.'):
|
414 |
raise ImportError("Relative imports are not allowed.")
|
415 |
|
416 |
if '.' in name:
|
417 |
+
package_name, _ = name.split('.', 1)
|
418 |
|
419 |
else:
|
420 |
package_name = name
|
|
|
|
|
|
|
|
|
421 |
|
422 |
if package_name in self.allowed_imports:
|
423 |
return importlib.__import__(name, globals, locals, fromlist, level)
|