Commit d66375b2 authored by Stefan Behnel's avatar Stefan Behnel

replace open(filepath).read() anti-pattern by code that safely and timely closes the file

parent 9cec5b0e
...@@ -91,7 +91,14 @@ def file_hash(filename): ...@@ -91,7 +91,14 @@ def file_hash(filename):
path = os.path.normpath(filename.encode("UTF-8")) path = os.path.normpath(filename.encode("UTF-8"))
m = hashlib.md5(str(len(path)) + ":") m = hashlib.md5(str(len(path)) + ":")
m.update(path) m.update(path)
m.update(open(filename).read()) f = open(filename, 'rb')
try:
data = f.read(65000)
while data:
m.update(data)
data = f.read(65000)
finally:
f.close()
return m.hexdigest() return m.hexdigest()
def parse_list(s): def parse_list(s):
......
...@@ -309,7 +309,14 @@ class Context(object): ...@@ -309,7 +309,14 @@ class Context(object):
position = e.args[2] position = e.args[2]
encoding = e.args[0] encoding = e.args[0]
for idx, c in enumerate(open(source_filename, "rb").read()): f = open(source_filename, "rb")
try:
byte_data = f.read()
finally:
f.close()
# FIXME: make this at least a little less inefficient
for idx, c in enumerate(byte_data):
if c in (ord('\n'), '\n'): if c in (ord('\n'), '\n'):
line += 1 line += 1
column = 0 column = 0
......
...@@ -271,13 +271,15 @@ class TestDebugTransform(DebuggerTestCase): ...@@ -271,13 +271,15 @@ class TestDebugTransform(DebuggerTestCase):
assert 'puts' in spam_stepinto assert 'puts' in spam_stepinto
assert 'some_c_function' in spam_stepinto assert 'some_c_function' in spam_stepinto
except: except:
print open(self.debug_dest).read() f = open(self.debug_dest)
try:
print(f.read())
finally:
f.close()
raise raise
if __name__ == "__main__": if __name__ == "__main__":
import unittest import unittest
unittest.main() unittest.main()
...@@ -35,6 +35,7 @@ def make_command_file(path_to_debug_info, prefix_code='', no_import=False): ...@@ -35,6 +35,7 @@ def make_command_file(path_to_debug_info, prefix_code='', no_import=False):
fd, tempfilename = tempfile.mkstemp() fd, tempfilename = tempfile.mkstemp()
f = os.fdopen(fd, 'w') f = os.fdopen(fd, 'w')
try:
f.write(prefix_code) f.write(prefix_code)
f.write('set breakpoint pending on\n') f.write('set breakpoint pending on\n')
f.write("set print pretty on\n") f.write("set print pretty on\n")
...@@ -46,7 +47,11 @@ def make_command_file(path_to_debug_info, prefix_code='', no_import=False): ...@@ -46,7 +47,11 @@ def make_command_file(path_to_debug_info, prefix_code='', no_import=False):
pass pass
else: else:
path = os.path.join(path_to_debug_info, "cython_debug", "interpreter") path = os.path.join(path_to_debug_info, "cython_debug", "interpreter")
interpreter = open(path).read() interpreter_file = open(path)
try:
interpreter = interpreter_file.read()
finally:
interpreter_file.close()
f.write("file %s\n" % interpreter) f.write("file %s\n" % interpreter)
f.write('\n'.join('cy import %s\n' % fn for fn in debug_files)) f.write('\n'.join('cy import %s\n' % fn for fn in debug_files))
f.write(textwrap.dedent('''\ f.write(textwrap.dedent('''\
...@@ -62,7 +67,7 @@ def make_command_file(path_to_debug_info, prefix_code='', no_import=False): ...@@ -62,7 +67,7 @@ def make_command_file(path_to_debug_info, prefix_code='', no_import=False):
source .cygdbinit source .cygdbinit
''')) '''))
finally:
f.close() f.close()
return tempfilename return tempfilename
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment