Kaydet (Commit) 6c78de55 authored tarafından Victor Stinner's avatar Victor Stinner

Merge 3.2: Issue #12636: IDLE reads the coding cookie when executing a Python script.

And "IDLE: fix some RessourceWarning, reuse tokenize.open()"
#! /usr/bin/env python3 #! /usr/bin/env python3
import getopt
import os import os
import os.path import os.path
import sys
import getopt
import re import re
import socket import socket
import time import subprocess
import sys
import threading import threading
import time
import tokenize
import traceback import traceback
import types import types
import subprocess
import linecache import linecache
from code import InteractiveInterpreter from code import InteractiveInterpreter
...@@ -201,18 +202,18 @@ class PyShellEditorWindow(EditorWindow): ...@@ -201,18 +202,18 @@ class PyShellEditorWindow(EditorWindow):
breaks = self.breakpoints breaks = self.breakpoints
filename = self.io.filename filename = self.io.filename
try: try:
lines = open(self.breakpointPath,"r").readlines() with open(self.breakpointPath, "r") as fp:
lines = fp.readlines()
except IOError: except IOError:
lines = [] lines = []
new_file = open(self.breakpointPath,"w") with open(self.breakpointPath, "w") as new_file:
for line in lines: for line in lines:
if not line.startswith(filename + '='): if not line.startswith(filename + '='):
new_file.write(line) new_file.write(line)
self.update_breakpoints() self.update_breakpoints()
breaks = self.breakpoints breaks = self.breakpoints
if breaks: if breaks:
new_file.write(filename + '=' + str(breaks) + '\n') new_file.write(filename + '=' + str(breaks) + '\n')
new_file.close()
def restore_file_breaks(self): def restore_file_breaks(self):
self.text.update() # this enables setting "BREAK" tags to be visible self.text.update() # this enables setting "BREAK" tags to be visible
...@@ -220,7 +221,8 @@ class PyShellEditorWindow(EditorWindow): ...@@ -220,7 +221,8 @@ class PyShellEditorWindow(EditorWindow):
if filename is None: if filename is None:
return return
if os.path.isfile(self.breakpointPath): 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: for line in lines:
if line.startswith(filename + '='): if line.startswith(filename + '='):
breakpoint_linenumbers = eval(line[len(filename)+1:]) breakpoint_linenumbers = eval(line[len(filename)+1:])
...@@ -571,7 +573,8 @@ class ModifiedInterpreter(InteractiveInterpreter): ...@@ -571,7 +573,8 @@ class ModifiedInterpreter(InteractiveInterpreter):
def execfile(self, filename, source=None): def execfile(self, filename, source=None):
"Execute an existing file" "Execute an existing file"
if source is None: if source is None:
source = open(filename, "r").read() with tokenize.open(filename) as fp:
source = fp.read()
try: try:
code = compile(source, filename, "exec") code = compile(source, filename, "exec")
except (OverflowError, SyntaxError): except (OverflowError, SyntaxError):
......
...@@ -67,25 +67,20 @@ class ScriptBinding: ...@@ -67,25 +67,20 @@ class ScriptBinding:
def tabnanny(self, filename): def tabnanny(self, filename):
# XXX: tabnanny should work on binary files as well # XXX: tabnanny should work on binary files as well
with open(filename, 'r', encoding='iso-8859-1') as f: with tokenize.open(filename) as f:
two_lines = f.readline() + f.readline() try:
encoding = IOBinding.coding_spec(two_lines) tabnanny.process_tokens(tokenize.generate_tokens(f.readline))
if not encoding: except tokenize.TokenError as msg:
encoding = 'utf-8' msgtxt, (lineno, start) = msg
f = open(filename, 'r', encoding=encoding) self.editwin.gotoline(lineno)
try: self.errorbox("Tabnanny Tokenizing Error",
tabnanny.process_tokens(tokenize.generate_tokens(f.readline)) "Token Error: %s" % msgtxt)
except tokenize.TokenError as msg: return False
msgtxt, (lineno, start) = msg except tabnanny.NannyNag as nag:
self.editwin.gotoline(lineno) # The error messages from tabnanny are too confusing...
self.errorbox("Tabnanny Tokenizing Error", self.editwin.gotoline(nag.get_lineno())
"Token Error: %s" % msgtxt) self.errorbox("Tab/space error", indent_message)
return False 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 return True
def checksyntax(self, filename): def checksyntax(self, filename):
......
...@@ -271,6 +271,8 @@ Core and Builtins ...@@ -271,6 +271,8 @@ Core and Builtins
Library Library
------- -------
- Issue #12636: IDLE reads the coding cookie when executing a Python script.
- Issue #12494: On error, call(), check_call(), check_output() and - Issue #12494: On error, call(), check_call(), check_output() and
getstatusoutput() functions of the subprocess module now kill the process, getstatusoutput() functions of the subprocess module now kill the process,
read its status (to avoid zombis) and close pipes. read its status (to avoid zombis) and close pipes.
......
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