DebugClients/Python/coverage/phystokens.py

changeset 6219
d6c795b5ce33
parent 5178
878ce843ca9f
diff -r bedab77d0fa3 -r d6c795b5ce33 DebugClients/Python/coverage/phystokens.py
--- a/DebugClients/Python/coverage/phystokens.py	Sat Apr 07 13:17:06 2018 +0200
+++ b/DebugClients/Python/coverage/phystokens.py	Sat Apr 07 13:35:10 2018 +0200
@@ -11,7 +11,7 @@
 import tokenize
 
 from coverage import env
-from coverage.backward import iternext
+from coverage.backward import iternext, unicode_class
 from coverage.misc import contract
 
 
@@ -281,7 +281,7 @@
 
     """
     source = neuter_encoding_declaration(source)
-    if env.PY2 and isinstance(filename, unicode):
+    if env.PY2 and isinstance(filename, unicode_class):
         filename = filename.encode(sys.getfilesystemencoding(), "replace")
     code = compile(source, filename, mode)
     return code
@@ -290,5 +290,9 @@
 @contract(source='unicode', returns='unicode')
 def neuter_encoding_declaration(source):
     """Return `source`, with any encoding declaration neutered."""
-    source = COOKIE_RE.sub("# (deleted declaration)", source, count=2)
+    if COOKIE_RE.search(source):
+        source_lines = source.splitlines(True)
+        for lineno in range(min(2, len(source_lines))):
+            source_lines[lineno] = COOKIE_RE.sub("# (deleted declaration)", source_lines[lineno])
+        source = "".join(source_lines)
     return source

eric ide

mercurial