Patchwork [5,of,6] import-checker: handle line continuation characters

login
register
mail settings
Submitter timeless@mozdev.org
Date April 12, 2016, 9:54 p.m.
Message ID <5ff1ae440687032163d8.1460498043@waste.org>
Download mbox | patch
Permalink /patch/14565/
State Superseded
Headers show

Comments

timeless@mozdev.org - April 12, 2016, 9:54 p.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1460495587 0
#      Tue Apr 12 21:13:07 2016 +0000
# Node ID 5ff1ae440687032163d88924da1b5c55394fb581
# Parent  4420d78ddf3ceb7cd4438627aacb6a68bd703a88
import-checker: handle line continuation characters
Simon Farnsworth - April 13, 2016, 11:35 a.m.
On 12/04/2016 22:54, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1460495587 0
> #      Tue Apr 12 21:13:07 2016 +0000
> # Node ID 5ff1ae440687032163d88924da1b5c55394fb581
> # Parent  4420d78ddf3ceb7cd4438627aacb6a68bd703a88
> 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 = 0

Here, you initialize linecont as a number...

>       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('\\')

Then here, it becomes a boolean, and remains a boolean for the rest of 
the file. Could you initialize it to False?

>               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):
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://urldefense.proofpoint.com/v2/url?u=https-3A__www.mercurial-2Dscm.org_mailman_listinfo_mercurial-2Ddevel&d=CwIGaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=mEgSWILcY4c4W3zjApBQLA&m=pD1GPBSv0KRo59PfkNbbT_2JSGZBk8meVEe0rZqRaAo&s=ylzybaSxJGmhFlX1c7nv2ouN0HPJym84hz6wIgeOgDI&e=
>

Patch

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 = 0
     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):