[Python-checkins] cpython (merge 3.2 -> default): Merge 3.2: Issue #12636: IDLE reads the coding cookie when executing a Python

victor.stinner python-checkins at python.org
Fri Sep 2 01:05:23 CEST 2011


http://hg.python.org/cpython/rev/a8748022504f
changeset: 72174:a8748022504f
parent: 72170:aaab27d4c99c
parent: 72173:fe7777f1ed14
user: Victor Stinner <victor.stinner at haypocalc.com>
date: Fri Sep 02 01:02:23 2011 +0200
summary:
 Merge 3.2: Issue #12636: IDLE reads the coding cookie when executing a Python script.
And "IDLE: fix some RessourceWarning, reuse tokenize.open()"
files:
 Lib/idlelib/PyShell.py | 35 +++++++++++++----------
 Lib/idlelib/ScriptBinding.py | 33 +++++++++-------------
 Misc/NEWS | 2 +
 3 files changed, 35 insertions(+), 35 deletions(-)
diff --git a/Lib/idlelib/PyShell.py b/Lib/idlelib/PyShell.py
--- a/Lib/idlelib/PyShell.py
+++ b/Lib/idlelib/PyShell.py
@@ -1,16 +1,17 @@
 #! /usr/bin/env python3
 
+import getopt
 import os
 import os.path
-import sys
-import getopt
 import re
 import socket
+import subprocess
+import sys
+import threading
 import time
-import threading
+import tokenize
 import traceback
 import types
-import subprocess
 
 import linecache
 from code import InteractiveInterpreter
@@ -201,18 +202,18 @@
 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 +221,8 @@
 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 +573,8 @@
 def execfile(self, filename, source=None):
 "Execute an existing file"
 if source is None:
- source = open(filename, "r").read()
+ with tokenize.open(filename) as fp:
+ source = fp.read()
 try:
 code = compile(source, filename, "exec")
 except (OverflowError, SyntaxError):
diff --git a/Lib/idlelib/ScriptBinding.py b/Lib/idlelib/ScriptBinding.py
--- a/Lib/idlelib/ScriptBinding.py
+++ b/Lib/idlelib/ScriptBinding.py
@@ -67,25 +67,20 @@
 
 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)
- try:
- tabnanny.process_tokens(tokenize.generate_tokens(f.readline))
- except tokenize.TokenError as msg:
- msgtxt, (lineno, start) = msg
- self.editwin.gotoline(lineno)
- self.errorbox("Tabnanny Tokenizing Error",
- "Token Error: %s" % msgtxt)
- return False
- except tabnanny.NannyNag as nag:
- # The error messages from tabnanny are too confusing...
- self.editwin.gotoline(nag.get_lineno())
- self.errorbox("Tab/space error", indent_message)
- return False
+ with tokenize.open(filename) as f:
+ try:
+ tabnanny.process_tokens(tokenize.generate_tokens(f.readline))
+ except tokenize.TokenError as msg:
+ msgtxt, (lineno, start) = msg
+ self.editwin.gotoline(lineno)
+ self.errorbox("Tabnanny Tokenizing Error",
+ "Token Error: %s" % msgtxt)
+ return False
+ except tabnanny.NannyNag as nag:
+ # The error messages from tabnanny are too confusing...
+ self.editwin.gotoline(nag.get_lineno())
+ self.errorbox("Tab/space error", indent_message)
+ return False
 return True
 
 def checksyntax(self, filename):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -271,6 +271,8 @@
 Library
 -------
 
+- Issue #12636: IDLE reads the coding cookie when executing a Python script.
+
 - Issue #12494: On error, call(), check_call(), check_output() and
 getstatusoutput() functions of the subprocess module now kill the process,
 read its status (to avoid zombis) and close pipes.
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /