diff --git a/mitmproxy/addons/view.py b/mitmproxy/addons/view.py index d43194681..b2db01711 100644 --- a/mitmproxy/addons/view.py +++ b/mitmproxy/addons/view.py @@ -10,6 +10,7 @@ The View: """ import collections import typing +import os import blinker import sortedcontainers @@ -339,12 +340,17 @@ class View(collections.Sequence): """ Load flows into the view, without processing them with addons. """ - with open(path, "rb") as f: - for i in io.FlowReader(f).stream(): - # Do this to get a new ID, so we can load the same file N times and - # get new flows each time. It would be more efficient to just have a - # .newid() method or something. - self.add([i.copy()]) + path = os.path.expanduser(path) + try: + with open(path, "rb") as f: + for i in io.FlowReader(f).stream(): + # Do this to get a new ID, so we can load the same file N times and + # get new flows each time. It would be more efficient to just have a + # .newid() method or something. + self.add([i.copy()]) + except IOError as e: + ctx.log.error(e.strerror) + return @command.command("view.go") def go(self, dst: int) -> None: diff --git a/mitmproxy/optmanager.py b/mitmproxy/optmanager.py index e1d74b8e0..c28ec685f 100644 --- a/mitmproxy/optmanager.py +++ b/mitmproxy/optmanager.py @@ -518,6 +518,7 @@ def save(opts, path, defaults=False): Raises OptionsError if the existing data is corrupt. """ + path = os.path.expanduser(path) if os.path.exists(path) and os.path.isfile(path): with open(path, "rt", encoding="utf8") as f: try: diff --git a/pathod/language/generators.py b/pathod/language/generators.py index 1961df74d..70c6ad164 100644 --- a/pathod/language/generators.py +++ b/pathod/language/generators.py @@ -75,7 +75,7 @@ class RandomGenerator: class FileGenerator: def __init__(self, path): - self.path = path + self.path = os.path.expanduser(path) def __len__(self): return os.path.getsize(self.path) diff --git a/pathod/pathoc_cmdline.py b/pathod/pathoc_cmdline.py index 3b738d47a..0854f6ad9 100644 --- a/pathod/pathoc_cmdline.py +++ b/pathod/pathoc_cmdline.py @@ -208,6 +208,7 @@ def args_pathoc(argv, stdout=sys.stdout, stderr=sys.stderr): reqs = [] for r in args.requests: + r = os.path.expanduser(r) if os.path.isfile(r): with open(r) as f: r = f.read() diff --git a/pathod/pathod_cmdline.py b/pathod/pathod_cmdline.py index dee19f4fc..c646aaeef 100644 --- a/pathod/pathod_cmdline.py +++ b/pathod/pathod_cmdline.py @@ -215,6 +215,7 @@ def args_pathod(argv, stdout_=sys.stdout, stderr_=sys.stderr): anchors = [] for patt, spec in args.anchors: + spec = os.path.expanduser(spec) if os.path.isfile(spec): with open(spec) as f: data = f.read() diff --git a/test/mitmproxy/addons/test_view.py b/test/mitmproxy/addons/test_view.py index 40136f1ff..e8eeb5911 100644 --- a/test/mitmproxy/addons/test_view.py +++ b/test/mitmproxy/addons/test_view.py @@ -170,6 +170,10 @@ def test_load(tmpdir): assert len(v) == 2 v.load_file(path) assert len(v) == 4 + try: + v.load_file("nonexistent_file_path") + except IOError: + assert False def test_resolve():