MCPcopy
hub / github.com/mitmproxy/mitmproxy / load_script

Function load_script

mitmproxy/addons/script.py:23–57  ·  view source on GitHub ↗
(path: str)

Source from the content-addressed store, hash-verified

21
22
23def 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
60def script_error_handler(path: str, exc: Exception) -> None:

Callers 2

loadscriptMethod · 0.85
script_runMethod · 0.85

Calls 4

script_error_handlerFunction · 0.85
formatMethod · 0.80
popMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…