(path: str)
| 21 | |
| 22 | |
| 23 | def load_script(path: str) -> types.ModuleType | None: |
| 24 | fullname = "__mitmproxy_script__.{}".format( |
| 25 | os.path.splitext(os.path.basename(path))[0] |
| 26 | ) |
| 27 | # the fullname is not unique among scripts, so if there already is an existing script with said |
| 28 | # fullname, remove it. |
| 29 | sys.modules.pop(fullname, None) |
| 30 | oldpath = sys.path |
| 31 | sys.path.insert(0, os.path.dirname(path)) |
| 32 | |
| 33 | try: |
| 34 | loader = importlib.machinery.SourceFileLoader(fullname, path) |
| 35 | spec = importlib.util.spec_from_loader(fullname, loader=loader) |
| 36 | assert spec |
| 37 | m = importlib.util.module_from_spec(spec) |
| 38 | loader.exec_module(m) |
| 39 | if not getattr(m, "name", None): |
| 40 | m.name = path # type: ignore |
| 41 | return m |
| 42 | except ImportError as e: |
| 43 | if getattr(sys, "frozen", False): |
| 44 | e.msg += ( |
| 45 | f".\n" |
| 46 | f"Note that mitmproxy's binaries include their own Python environment. " |
| 47 | f"If your addon requires the installation of additional dependencies, " |
| 48 | f"please install mitmproxy from PyPI " |
| 49 | f"(https://docs.mitmproxy.org/stable/overview-installation/#installation-from-the-python-package-index-pypi)." |
| 50 | ) |
| 51 | script_error_handler(path, e) |
| 52 | return None |
| 53 | except Exception as e: |
| 54 | script_error_handler(path, e) |
| 55 | return None |
| 56 | finally: |
| 57 | sys.path[:] = oldpath |
| 58 | |
| 59 | |
| 60 | def script_error_handler(path: str, exc: Exception) -> None: |
no test coverage detected
searching dependent graphs…