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,35 +35,40 @@ def make_command_file(path_to_debug_info, prefix_code='', no_import=False): ...@@ -35,35 +35,40 @@ 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')
f.write(prefix_code) try:
f.write('set breakpoint pending on\n') f.write(prefix_code)
f.write("set print pretty on\n") f.write('set breakpoint pending on\n')
f.write('python from Cython.Debugger import libcython, libpython\n') f.write("set print pretty on\n")
f.write('python from Cython.Debugger import libcython, libpython\n')
if no_import:
# don't do this, this overrides file command in .gdbinit if no_import:
# f.write("file %s\n" % sys.executable) # don't do this, this overrides file command in .gdbinit
pass # f.write("file %s\n" % sys.executable)
else: pass
path = os.path.join(path_to_debug_info, "cython_debug", "interpreter") else:
interpreter = open(path).read() path = os.path.join(path_to_debug_info, "cython_debug", "interpreter")
f.write("file %s\n" % interpreter) interpreter_file = open(path)
f.write('\n'.join('cy import %s\n' % fn for fn in debug_files))
f.write(textwrap.dedent('''\
python
import sys
try: try:
gdb.lookup_type('PyModuleObject') interpreter = interpreter_file.read()
except RuntimeError: finally:
sys.stderr.write( interpreter_file.close()
'Python was not compiled with debug symbols (or it was ' f.write("file %s\n" % interpreter)
'stripped). Some functionality may not work (properly).\\n') f.write('\n'.join('cy import %s\n' % fn for fn in debug_files))
end f.write(textwrap.dedent('''\
python
source .cygdbinit import sys
''')) try:
gdb.lookup_type('PyModuleObject')
f.close() except RuntimeError:
sys.stderr.write(
'Python was not compiled with debug symbols (or it was '
'stripped). Some functionality may not work (properly).\\n')
end
source .cygdbinit
'''))
finally:
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