Improve error message for missing packages in frozen binaries (#5740)

This commit is contained in:
Maximilian Hils 2022-11-18 10:42:48 +01:00 committed by GitHub
parent 0bbb0215c1
commit d5f1d1c623
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View file

@ -39,6 +39,17 @@ def load_script(path: str) -> Optional[types.ModuleType]:
loader.exec_module(m)
if not getattr(m, "name", None):
m.name = path # type: ignore
except ImportError as e:
err_msg = str(e)
if getattr(sys, "frozen", False):
err_msg = (
f"{err_msg}. \n"
f"Note that mitmproxy's binaries include their own Python environment. "
f"If your addon requires the installation of additional dependencies, "
f"please install mitmproxy from PyPI "
f"(https://docs.mitmproxy.org/stable/overview-installation/#installation-from-the-python-package-index-pypi)."
)
script_error_handler(path, e, msg=err_msg)
except Exception as e:
script_error_handler(path, e, msg=str(e))
finally:

View file

@ -123,6 +123,14 @@ class TestScript:
await caplog_async.await_log("error.py")
sc.done()
async def test_import_error(self, monkeypatch, tdata, caplog):
monkeypatch.setattr(sys, "frozen", True, raising=False)
script.Script(
tdata.path("mitmproxy/data/addonscripts/import_error.py"),
False,
)
assert "Note that mitmproxy's binaries include their own Python environment" in caplog.text
async def test_optionexceptions(self, tdata, caplog_async):
with taddons.context() as tctx:
sc = script.Script(

View file

@ -0,0 +1 @@
import nonexistent