Commit 136ae775 authored by Victor Stinner's avatar Victor Stinner

IDLE: fix some RessourceWarning, reuse tokenize.open()

parent 1f12608a
......@@ -201,10 +201,11 @@ class PyShellEditorWindow(EditorWindow):
breaks = self.breakpoints
filename = self.io.filename
try:
lines = open(self.breakpointPath,"r").readlines()
with open(self.breakpointPath, "r") as fp:
lines = fp.readlines()
except IOError:
lines = []
new_file = open(self.breakpointPath,"w")
with open(self.breakpointPath, "w") as new_file:
for line in lines:
if not line.startswith(filename + '='):
new_file.write(line)
......@@ -212,7 +213,6 @@ class PyShellEditorWindow(EditorWindow):
breaks = self.breakpoints
if breaks:
new_file.write(filename + '=' + str(breaks) + '\n')
new_file.close()
def restore_file_breaks(self):
self.text.update() # this enables setting "BREAK" tags to be visible
......@@ -220,7 +220,8 @@ class PyShellEditorWindow(EditorWindow):
if filename is None:
return
if os.path.isfile(self.breakpointPath):
lines = open(self.breakpointPath,"r").readlines()
with open(self.breakpointPath, "r") as fp:
lines = fp.readlines()
for line in lines:
if line.startswith(filename + '='):
breakpoint_linenumbers = eval(line[len(filename)+1:])
......@@ -571,7 +572,8 @@ class ModifiedInterpreter(InteractiveInterpreter):
def execfile(self, filename, source=None):
"Execute an existing file"
if source is None:
source = open(filename, "r").read()
with open(filename, "r") as fp:
source = fp.read()
try:
code = compile(source, filename, "exec")
except (OverflowError, SyntaxError):
......
......@@ -67,12 +67,7 @@ class ScriptBinding:
def tabnanny(self, filename):
# XXX: tabnanny should work on binary files as well
with open(filename, 'r', encoding='iso-8859-1') as f:
two_lines = f.readline() + f.readline()
encoding = IOBinding.coding_spec(two_lines)
if not encoding:
encoding = 'utf-8'
f = open(filename, 'r', encoding=encoding)
with tokenize.open(filename) as f:
try:
tabnanny.process_tokens(tokenize.generate_tokens(f.readline))
except tokenize.TokenError as msg:
......
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