From 779677bcc6177c71891cd7847929a3aa4774c857 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Thu, 20 Jul 2017 15:18:13 +0200 Subject: [PATCH] fix loading scripts with same filename --- mitmproxy/addons/script.py | 13 +++++++--- test/mitmproxy/addons/test_script.py | 24 +++++++++++++++++++ .../data/addonscripts/same_filename/addon.py | 1 + 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 test/mitmproxy/data/addonscripts/same_filename/addon.py diff --git a/mitmproxy/addons/script.py b/mitmproxy/addons/script.py index b4274f8c3..30fa740ba 100644 --- a/mitmproxy/addons/script.py +++ b/mitmproxy/addons/script.py @@ -1,5 +1,6 @@ import os -import importlib +import importlib.util +import importlib.machinery import time import sys import typing @@ -16,7 +17,14 @@ def load_script(actx, path): if not os.path.exists(path): ctx.log.info("No such file: %s" % path) return - loader = importlib.machinery.SourceFileLoader(os.path.basename(path), path) + + fullname = "__mitmproxy_script__.{}".format( + os.path.splitext(os.path.basename(path))[0] + ) + # the fullname is not unique among scripts, so if there already is an existing script with said + # fullname, remove it. + sys.modules.pop(fullname, None) + loader = importlib.machinery.SourceFileLoader(fullname, path) try: oldpath = sys.path sys.path.insert(0, os.path.dirname(path)) @@ -64,7 +72,6 @@ class Script: ctx.log.info("Loading script: %s" % self.path) if self.ns: ctx.master.addons.remove(self.ns) - del sys.modules[self.ns.__name__] self.ns = load_script(ctx, self.fullpath) if self.ns: # We're already running, so we have to explicitly register and diff --git a/test/mitmproxy/addons/test_script.py b/test/mitmproxy/addons/test_script.py index 03b1f6208..b7e6c82ab 100644 --- a/test/mitmproxy/addons/test_script.py +++ b/test/mitmproxy/addons/test_script.py @@ -30,6 +30,30 @@ def test_load_script(): assert not ns +def test_load_fullname(): + """ + Test that loading two scripts at locations a/foo.py and b/foo.py works. + This only succeeds if they get assigned different basenames. + + """ + with taddons.context() as tctx: + ns = script.load_script( + tctx.ctx(), + tutils.test_data.path( + "mitmproxy/data/addonscripts/addon.py" + ) + ) + assert ns.addons + ns2 = script.load_script( + tctx.ctx(), + tutils.test_data.path( + "mitmproxy/data/addonscripts/same_filename/addon.py" + ) + ) + assert ns.name != ns2.name + assert not hasattr(ns2, "addons") + + def test_script_print_stdout(): with taddons.context() as tctx: with mock.patch('mitmproxy.ctx.log.warn') as mock_warn: diff --git a/test/mitmproxy/data/addonscripts/same_filename/addon.py b/test/mitmproxy/data/addonscripts/same_filename/addon.py new file mode 100644 index 000000000..c84a9b135 --- /dev/null +++ b/test/mitmproxy/data/addonscripts/same_filename/addon.py @@ -0,0 +1 @@ +foo = 42