Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(10866)

Side by Side Diff: Lib/idlelib/IOBinding.py

Issue 18873: "Encoding" detected in non-comment lines
Patch Set: Created 6 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Lib/lib2to3/pgen2/tokenize.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import os 1 import os
2 import types 2 import types
3 import shlex 3 import shlex
4 import sys 4 import sys
5 import codecs 5 import codecs
6 import tempfile 6 import tempfile
7 import tkinter.filedialog as tkFileDialog 7 import tkinter.filedialog as tkFileDialog
8 import tkinter.messagebox as tkMessageBox 8 import tkinter.messagebox as tkMessageBox
9 import re 9 import re
10 from tkinter import * 10 from tkinter import *
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 locale_encoding = 'ascii' 56 locale_encoding = 'ascii'
57 codecs.lookup(locale_encoding) 57 codecs.lookup(locale_encoding)
58 except (ValueError, LookupError): 58 except (ValueError, LookupError):
59 pass 59 pass
60 60
61 locale_encoding = locale_encoding.lower() 61 locale_encoding = locale_encoding.lower()
62 62
63 encoding = locale_encoding ### KBK 07Sep07 This is used all over IDLE, check! 63 encoding = locale_encoding ### KBK 07Sep07 This is used all over IDLE, check!
64 ### 'encoding' is used below in encode(), check! 64 ### 'encoding' is used below in encode(), check!
65 65
66 coding_re = re.compile("coding[:=]\s*([-\w_.]+)") 66 coding_re = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII)
67 67
68 def coding_spec(data): 68 def coding_spec(data):
69 """Return the encoding declaration according to PEP 263. 69 """Return the encoding declaration according to PEP 263.
70 70
71 When checking encoded data, only the first two lines should be passed 71 When checking encoded data, only the first two lines should be passed
72 in to avoid a UnicodeDecodeError if the rest of the data is not unicode. 72 in to avoid a UnicodeDecodeError if the rest of the data is not unicode.
73 The first two lines would contain the encoding specification. 73 The first two lines would contain the encoding specification.
74 74
75 Raise a LookupError if the encoding is declared but unknown. 75 Raise a LookupError if the encoding is declared but unknown.
76 """ 76 """
77 if isinstance(data, bytes): 77 if isinstance(data, bytes):
78 # This encoding might be wrong. However, the coding 78 # This encoding might be wrong. However, the coding
79 # spec must be ASCII-only, so any non-ASCII characters 79 # spec must be ASCII-only, so any non-ASCII characters
80 # around here will be ignored. Decoding to Latin-1 should 80 # around here will be ignored. Decoding to Latin-1 should
81 # never fail (except for memory outage) 81 # never fail (except for memory outage)
82 lines = data.decode('iso-8859-1') 82 lines = data.decode('iso-8859-1')
83 else: 83 else:
84 lines = data 84 lines = data
85 # consider only the first two lines 85 # consider only the first two lines
86 if '\n' in lines: 86 if '\n' in lines:
87 lst = lines.split('\n')[:2] 87 lst = lines.split('\n', 2)[:2]
88 elif '\r' in lines: 88 elif '\r' in lines:
89 lst = lines.split('\r')[:2] 89 lst = lines.split('\r', 2)[:2]
90 else: 90 else:
91 lst = list(lines) 91 lst = [lines]
92 str = '\n'.join(lst) 92 for line in lst:
93 match = coding_re.search(str) 93 match = coding_re.match(line)
94 if not match: 94 if match is not None:
95 break
96 else:
95 return None 97 return None
96 name = match.group(1) 98 name = match.group(1)
97 try: 99 try:
98 codecs.lookup(name) 100 codecs.lookup(name)
99 except LookupError: 101 except LookupError:
100 # The standard encoding error does not indicate the encoding 102 # The standard encoding error does not indicate the encoding
101 raise LookupError("Unknown encoding: "+name) 103 raise LookupError("Unknown encoding: "+name)
102 return name 104 return name
103 105
104 106
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after
543 self.text.event_generate("<<save-copy-of-window-as-file>>") 545 self.text.event_generate("<<save-copy-of-window-as-file>>")
544 text = Text(root) 546 text = Text(root)
545 text.pack() 547 text.pack()
546 text.focus_set() 548 text.focus_set()
547 editwin = MyEditWin(text) 549 editwin = MyEditWin(text)
548 io = IOBinding(editwin) 550 io = IOBinding(editwin)
549 root.mainloop() 551 root.mainloop()
550 552
551 if __name__ == "__main__": 553 if __name__ == "__main__":
552 test() 554 test()
OLDNEW
« no previous file with comments | « no previous file | Lib/lib2to3/pgen2/tokenize.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+