aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2019-01-06 17:43:47 +0100
committerGitHub <noreply@github.com>2019-01-06 17:43:47 +0100
commit82bc8c7ca2b946e7f022b92ab16ced2924feb284 (patch)
treec65361f74b829edf9181752c4f7cec9e9374a0e3
parent4f270b5506fe2b1314ee277d9f7c93763cb2d8e5 (diff)
parentc03b07930ccf41b696ae02c363d116ba602313d3 (diff)
downloadmitmproxy-82bc8c7ca2b946e7f022b92ab16ced2924feb284.tar.gz
mitmproxy-82bc8c7ca2b946e7f022b92ab16ced2924feb284.tar.bz2
mitmproxy-82bc8c7ca2b946e7f022b92ab16ced2924feb284.zip
Merge pull request #3444 from BoboTiG/fix-resource-leaks
Fix ResourceWarning: unclosed file, prevent resource leaks
-rw-r--r--examples/addons/commands-paths.py6
-rw-r--r--mitmproxy/addons/session.py4
-rw-r--r--mitmproxy/contrib/wbxml/ASCommandResponse.py5
-rw-r--r--test/bench/benchmark.py3
-rw-r--r--test/mitmproxy/addons/test_session.py3
5 files changed, 12 insertions, 9 deletions
diff --git a/examples/addons/commands-paths.py b/examples/addons/commands-paths.py
index f37a0fbc..4d9535b9 100644
--- a/examples/addons/commands-paths.py
+++ b/examples/addons/commands-paths.py
@@ -20,9 +20,9 @@ class MyAddon:
for f in flows:
totals[f.request.host] = totals.setdefault(f.request.host, 0) + 1
- fp = open(path, "w+")
- for cnt, dom in sorted([(v, k) for (k, v) in totals.items()]):
- fp.write("%s: %s\n" % (cnt, dom))
+ with open(path, "w+") as fp:
+ for cnt, dom in sorted([(v, k) for (k, v) in totals.items()]):
+ fp.write("%s: %s\n" % (cnt, dom))
ctx.log.alert("done")
diff --git a/mitmproxy/addons/session.py b/mitmproxy/addons/session.py
index 63e382ec..f9073c3e 100644
--- a/mitmproxy/addons/session.py
+++ b/mitmproxy/addons/session.py
@@ -87,8 +87,8 @@ class SessionDB:
def _create_session(self):
script_path = pkg_data.path("io/sql/session_create.sql")
- qry = open(script_path, 'r').read()
- self.con.executescript(qry)
+ with open(script_path, 'r') as qry:
+ self.con.executescript(qry.read())
self.con.commit()
@staticmethod
diff --git a/mitmproxy/contrib/wbxml/ASCommandResponse.py b/mitmproxy/contrib/wbxml/ASCommandResponse.py
index 2d60eb2d..34755cbe 100644
--- a/mitmproxy/contrib/wbxml/ASCommandResponse.py
+++ b/mitmproxy/contrib/wbxml/ASCommandResponse.py
@@ -63,8 +63,9 @@ if __name__ == "__main__":
listOfSamples = os.listdir(samplesDir)
for filename in listOfSamples:
- byteWBXML = open(samplesDir + os.sep + filename, "rb").read()
-
+ with open(samplesDir + os.sep + filename, "rb") as f:
+ byteWBXML = f.read()
+
logging.info("-"*100)
logging.info(filename)
logging.info("-"*100)
diff --git a/test/bench/benchmark.py b/test/bench/benchmark.py
index 84ec6005..076ad6c9 100644
--- a/test/bench/benchmark.py
+++ b/test/bench/benchmark.py
@@ -31,7 +31,8 @@ class Benchmark:
stdout=asyncio.subprocess.PIPE
)
stdout, _ = await traf.communicate()
- open(ctx.options.benchmark_save_path + ".bench", mode="wb").write(stdout)
+ with open(ctx.options.benchmark_save_path + ".bench", mode="wb") as f:
+ f.write(stdout)
ctx.log.error("Proxy saw %s requests, %s responses" % (self.reqs, self.resps))
ctx.log.error(stdout.decode("ascii"))
backend.kill()
diff --git a/test/mitmproxy/addons/test_session.py b/test/mitmproxy/addons/test_session.py
index 20feb69d..97351426 100644
--- a/test/mitmproxy/addons/test_session.py
+++ b/test/mitmproxy/addons/test_session.py
@@ -68,7 +68,8 @@ class TestSession:
os.remove(path)
con = sqlite3.connect(path)
script_path = pkg_data.path("io/sql/session_create.sql")
- qry = open(script_path, 'r').read()
+ with open(script_path) as f:
+ qry = f.read()
with con:
con.executescript(qry)
blob = b'blob_of_data'