Skip to content

Commit

Permalink
Merge pull request mitmproxy#3444 from BoboTiG/fix-resource-leaks
Browse files Browse the repository at this point in the history
Fix ResourceWarning: unclosed file, prevent resource leaks
  • Loading branch information
mhils authored Jan 6, 2019
2 parents 4f270b5 + c03b079 commit 82bc8c7
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions examples/addons/commands-paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def histogram(
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")

Expand Down
4 changes: 2 additions & 2 deletions mitmproxy/addons/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def _load_session(self, path):

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
Expand Down
5 changes: 3 additions & 2 deletions mitmproxy/contrib/wbxml/ASCommandResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ def decodeWBXML(self, body):
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)
Expand Down
3 changes: 2 additions & 1 deletion test/bench/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ async def procs(self):
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()
Expand Down
3 changes: 2 additions & 1 deletion test/mitmproxy/addons/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def test_session_load_existing(self, tdata):
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'
Expand Down

0 comments on commit 82bc8c7

Please sign in to comment.