summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2010-10-30 23:51:34 (GMT)
committerBenjamin Peterson <benjamin@python.org>2010-10-30 23:51:34 (GMT)
commitb2fda239225413ae5658f2409ac4d7922b3c7e7c (patch)
tree6b915c1c088d17ee589cfbfbc789489e848eb0b2 /Lib
parentbbb0412ad179b4a94d03ce592a5bc1d40724998e (diff)
downloadcpython-b2fda239225413ae5658f2409ac4d7922b3c7e7c.zip
cpython-b2fda239225413ae5658f2409ac4d7922b3c7e7c.tar.gz
cpython-b2fda239225413ae5658f2409ac4d7922b3c7e7c.tar.bz2
close files correctly
Diffstat (limited to 'Lib')
-rw-r--r--Lib/trace.py23
1 files changed, 12 insertions, 11 deletions
diff --git a/Lib/trace.py b/Lib/trace.py
index fa24fc1..ec45d81 100644
--- a/Lib/trace.py
+++ b/Lib/trace.py
@@ -404,16 +404,16 @@ def find_strings(filename, encoding=None):
# If the first token is a string, then it's the module docstring.
# Add this special case so that the test in the loop passes.
prev_ttype = token.INDENT
- f = open(filename, encoding=encoding)
- for ttype, tstr, start, end, line in tokenize.generate_tokens(f.readline):
- if ttype == token.STRING:
- if prev_ttype == token.INDENT:
- sline, scol = start
- eline, ecol = end
- for i in range(sline, eline + 1):
- d[i] = 1
- prev_ttype = ttype
- f.close()
+ with open(filename, encoding=encoding) as f:
+ tok = tokenize.generate_tokens(f.readline)
+ for ttype, tstr, start, end, line in tok:
+ if ttype == token.STRING:
+ if prev_ttype == token.INDENT:
+ sline, scol = start
+ eline, ecol = end
+ for i in range(sline, eline + 1):
+ d[i] = 1
+ prev_ttype = ttype
return d
def find_executable_linenos(filename):
@@ -421,7 +421,8 @@ def find_executable_linenos(filename):
try:
with io.FileIO(filename, 'r') as file:
encoding, lines = tokenize.detect_encoding(file.readline)
- prog = open(filename, "r", encoding=encoding).read()
+ with open(filename, "r", encoding=encoding) as f:
+ prog = f.read()
except IOError as err:
print(("Not printing coverage data for %r: %s"
% (filename, err)), file=sys.stderr)