-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvalidations.py
More file actions
40 lines (32 loc) · 1.12 KB
/
Copy pathvalidations.py
File metadata and controls
40 lines (32 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import importlib.util
import tempfile
from types import ModuleType
from typing import Optional
def execute_code(*, path=None, code=None, allowed_exceptions=None):
"""
Ensure that code written in `path` or in `code` str is executable.
"""
assert (path is None) != (
code is None
), "Must pass either path to code or code as a str."
if path:
return _run_code(path, allowed_exceptions)
with tempfile.NamedTemporaryFile(suffix=".py", mode="w+t") as temp:
temp.write(code)
return _run_code(temp.name, allowed_exceptions)
def _run_code(path, allowed_exceptions=None) -> Optional[ModuleType]:
"""
Execute the code in `path` in its own namespace.
Return loaded module for any additional testing later on.
"""
allowed_exceptions = allowed_exceptions or ()
if not (spec := importlib.util.spec_from_file_location("output_code", path)):
return None
module = importlib.util.module_from_spec(spec)
if not spec.loader:
return None
try:
spec.loader.exec_module(module)
except allowed_exceptions:
pass
return module