Merge pull request #3444 from BoboTiG/fix-resource-leaks

Fix ResourceWarning: unclosed file, prevent resource leaks
This commit is contained in:
Maximilian Hils 2019-01-06 17:43:47 +01:00 committed by GitHub
commit 82bc8c7ca2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 9 deletions

View File

@ -20,9 +20,9 @@ class MyAddon:
for f in flows: for f in flows:
totals[f.request.host] = totals.setdefault(f.request.host, 0) + 1 totals[f.request.host] = totals.setdefault(f.request.host, 0) + 1
fp = open(path, "w+") with open(path, "w+") as fp:
for cnt, dom in sorted([(v, k) for (k, v) in totals.items()]): for cnt, dom in sorted([(v, k) for (k, v) in totals.items()]):
fp.write("%s: %s\n" % (cnt, dom)) fp.write("%s: %s\n" % (cnt, dom))
ctx.log.alert("done") ctx.log.alert("done")

View File

@ -87,8 +87,8 @@ class SessionDB:
def _create_session(self): def _create_session(self):
script_path = pkg_data.path("io/sql/session_create.sql") script_path = pkg_data.path("io/sql/session_create.sql")
qry = open(script_path, 'r').read() with open(script_path, 'r') as qry:
self.con.executescript(qry) self.con.executescript(qry.read())
self.con.commit() self.con.commit()
@staticmethod @staticmethod

View File

@ -63,8 +63,9 @@ if __name__ == "__main__":
listOfSamples = os.listdir(samplesDir) listOfSamples = os.listdir(samplesDir)
for filename in listOfSamples: 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("-"*100)
logging.info(filename) logging.info(filename)
logging.info("-"*100) logging.info("-"*100)

View File

@ -31,7 +31,8 @@ class Benchmark:
stdout=asyncio.subprocess.PIPE stdout=asyncio.subprocess.PIPE
) )
stdout, _ = await traf.communicate() 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("Proxy saw %s requests, %s responses" % (self.reqs, self.resps))
ctx.log.error(stdout.decode("ascii")) ctx.log.error(stdout.decode("ascii"))
backend.kill() backend.kill()

View File

@ -68,7 +68,8 @@ class TestSession:
os.remove(path) os.remove(path)
con = sqlite3.connect(path) con = sqlite3.connect(path)
script_path = pkg_data.path("io/sql/session_create.sql") 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: with con:
con.executescript(qry) con.executescript(qry)
blob = b'blob_of_data' blob = b'blob_of_data'