aboutsummaryrefslogtreecommitdiffstats
path: root/pathod
diff options
context:
space:
mode:
authorThomas Kriechbaumer <thomas@kriechbaumer.name>2017-05-24 17:25:12 +0200
committerThomas Kriechbaumer <thomas@kriechbaumer.name>2017-05-24 19:32:20 +0200
commitcfed4432a0d8daf335fe0891ac55e520bac580c0 (patch)
treeac020f2cc21fec692f3a8ad9f21e44cd37046337 /pathod
parent673ed5b45e92be8919b29ee033770913dc7c0ba9 (diff)
downloadmitmproxy-cfed4432a0d8daf335fe0891ac55e520bac580c0.tar.gz
mitmproxy-cfed4432a0d8daf335fe0891ac55e520bac580c0.tar.bz2
mitmproxy-cfed4432a0d8daf335fe0891ac55e520bac580c0.zip
pathod: fix leaking fds
Diffstat (limited to 'pathod')
-rw-r--r--pathod/language/generators.py22
-rw-r--r--pathod/pathod_cmdline.py3
2 files changed, 11 insertions, 14 deletions
diff --git a/pathod/language/generators.py b/pathod/language/generators.py
index 93db3014..1961df74 100644
--- a/pathod/language/generators.py
+++ b/pathod/language/generators.py
@@ -1,7 +1,7 @@
+import os
import string
import random
import mmap
-
import sys
DATATYPES = dict(
@@ -74,24 +74,20 @@ class RandomGenerator:
class FileGenerator:
-
def __init__(self, path):
self.path = path
- self.fp = open(path, "rb")
- self.map = mmap.mmap(self.fp.fileno(), 0, access=mmap.ACCESS_READ)
def __len__(self):
- return len(self.map)
+ return os.path.getsize(self.path)
def __getitem__(self, x):
- if isinstance(x, slice):
- return self.map.__getitem__(x)
- # A slice of length 1 returns a byte object (not an integer)
- return self.map.__getitem__(slice(x, x + 1 or self.map.size()))
+ with open(self.path, mode="rb") as f:
+ if isinstance(x, slice):
+ with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mapped:
+ return mapped.__getitem__(x)
+ else:
+ f.seek(x)
+ return f.read(1)
def __repr__(self):
return "<%s" % self.path
-
- def close(self):
- self.map.close()
- self.fp.close()
diff --git a/pathod/pathod_cmdline.py b/pathod/pathod_cmdline.py
index ef1e983f..dee19f4f 100644
--- a/pathod/pathod_cmdline.py
+++ b/pathod/pathod_cmdline.py
@@ -216,7 +216,8 @@ def args_pathod(argv, stdout_=sys.stdout, stderr_=sys.stderr):
anchors = []
for patt, spec in args.anchors:
if os.path.isfile(spec):
- data = open(spec).read()
+ with open(spec) as f:
+ data = f.read()
spec = data
try:
arex = re.compile(patt)