[PATCH 6 of 7 v2] import-checker: handle line continuation characters

timeless timeless at mozdev.org
Wed Apr 13 13:21:49 EDT 2016


# HG changeset patch
# User timeless <timeless at mozdev.org>
# Date 1460495587 0
#      Tue Apr 12 21:13:07 2016 +0000
# Node ID 4fb9d603c45a64c98061abd264b3f691a6f336d0
# Parent  f34b5885cd74f453a0ca5706468584dfecbc5cae
import-checker: handle line continuation characters

diff --git a/contrib/import-checker.py b/contrib/import-checker.py
--- a/contrib/import-checker.py
+++ b/contrib/import-checker.py
@@ -586,12 +586,18 @@
     ...   '  $ cat > foo.py <<EOF',
     ...   '  > from __future__ import print_function',
     ...   '  > EOF',
+    ...   'comment',
+    ...   '  >>> from __future__ import print_function\\\\',
+    ...   '      , absolute_import',
+    ...   '  ',
     ... ]
     >>> test("example.t", lines)
     example[2] doctest.py 2
     "from __future__ import print_function\\n' multiline\\nstring'\\n"
     example[7] foo.py 7
     'from __future__ import print_function\\n'
+    example[11] doctest.py 11
+    'from __future__ import print_function\\\\\\n, absolute_import\\n'
     """
     inlinepython = 0
     shpython = 0
@@ -599,6 +605,7 @@
     prefix = 6
     t = ''
     n = 0
+    linecont = False
     for l in src:
         n += 1
         if not l.endswith(b'\n'):
@@ -611,9 +618,11 @@
                 inlinepython = n
                 t = 'doctest.py'
             script.append(l[prefix:])
+            linecont = l.strip('\n').endswith('\\')
             continue
         if l.startswith(b'  ... '): # python inlines
             script.append(l[prefix:])
+            linecont = l.strip('\n').endswith('\\')
             continue
         cat = re.search("\$ \s*cat\s*>\s*(\S+.py)\s*<<\s*EOF", l)
         if cat:
@@ -622,6 +631,7 @@
                        (modname, inlinepython)), t, inlinepython
                 script = []
                 inlinepython = 0
+                linecont = False
             shpython = n
             t = cat.group(1)
             continue
@@ -631,14 +641,20 @@
                        (modname, shpython)), t, shpython
                 script = []
                 shpython = 0
+                linecont = False
             else:
                 script.append(l[4:])
             continue
-        if inlinepython and l == b'  \n':
+        if inlinepython and l.strip() == b'':
             yield ''.join(script), ("%s[%d]" %
                    (modname, inlinepython)), t, inlinepython
             script = []
             inlinepython = 0
+            linecont = False
+            continue
+        if linecont:
+            script.append(l[prefix:])
+            linecont = l.strip('\n').endswith('\\')
             continue
 
 def sources(f, modname):


More information about the Mercurial-devel mailing list