summaryrefslogtreecommitdiff
path: root/Lib/idlelib/PyShell.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-09-02 00:57:04 +0200
committerVictor Stinner <victor.stinner@haypocalc.com>2011-09-02 00:57:04 +0200
commit85c6772aecd9cdcd87c9f4ede62dce041f581890 (patch)
tree241e1aa79315cac3ffb69ea5731e999a7ae3b999 /Lib/idlelib/PyShell.py
parent0af0306396eb05c8c55951bc3dc04aa0a4c007ae (diff)
downloadcpython-git-85c6772aecd9cdcd87c9f4ede62dce041f581890.tar.gz
IDLE: fix some RessourceWarning, reuse tokenize.open()
Diffstat (limited to 'Lib/idlelib/PyShell.py')
-rw-r--r--Lib/idlelib/PyShell.py26
1 files changed, 14 insertions, 12 deletions
diff --git a/Lib/idlelib/PyShell.py b/Lib/idlelib/PyShell.py
index dcce2eb8ba..d15034f929 100644
--- a/Lib/idlelib/PyShell.py
+++ b/Lib/idlelib/PyShell.py
@@ -201,18 +201,18 @@ 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")
- for line in lines:
- if not line.startswith(filename + '='):
- new_file.write(line)
- self.update_breakpoints()
- breaks = self.breakpoints
- if breaks:
- new_file.write(filename + '=' + str(breaks) + '\n')
- new_file.close()
+ with open(self.breakpointPath, "w") as new_file:
+ for line in lines:
+ if not line.startswith(filename + '='):
+ new_file.write(line)
+ self.update_breakpoints()
+ breaks = self.breakpoints
+ if breaks:
+ new_file.write(filename + '=' + str(breaks) + '\n')
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):