Issue #18873: IDLE, 2to3, and the findnocoding.py script now detect Python
source code encoding only in comment lines.
diff --git a/Lib/idlelib/IOBinding.py b/Lib/idlelib/IOBinding.py
index ba45ee8..f5665b9 100644
--- a/Lib/idlelib/IOBinding.py
+++ b/Lib/idlelib/IOBinding.py
@@ -71,7 +71,7 @@
encoding = encoding.lower()
-coding_re = re.compile("coding[:=]\s*([-\w_.]+)")
+coding_re = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII)
class EncodingMessage(SimpleDialog):
"Inform user that an encoding declaration is needed."
@@ -125,11 +125,12 @@
Raise LookupError if the encoding is declared but unknown.
"""
# Only consider the first two lines
- str = str.split("\n")[:2]
- str = "\n".join(str)
-
- match = coding_re.search(str)
- if not match:
+ str = str.split("\n", 2)[:2]
+ for line in lst:
+ match = coding_re.match(line)
+ if match is not None:
+ break
+ else:
return None
name = match.group(1)
# Check whether the encoding is known