[Python-checkins] r85999 - python/branches/py3k/Lib/trace.py
benjamin.peterson
python-checkins at python.org
Sun Oct 31 01:51:40 CEST 2010
Author: benjamin.peterson
Date: Sun Oct 31 01:51:34 2010
New Revision: 85999
Log:
close files correctly
Modified:
python/branches/py3k/Lib/trace.py
Modified: python/branches/py3k/Lib/trace.py
==============================================================================
--- python/branches/py3k/Lib/trace.py (original)
+++ python/branches/py3k/Lib/trace.py Sun Oct 31 01:51:34 2010
@@ -404,16 +404,16 @@
# 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 @@
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)
More information about the Python-checkins
mailing list