[PATCH 6 of 6] import-checker: check docstrings in python

Simon Farnsworth simonfar at fb.com
Wed Apr 13 07:37:11 EDT 2016


Apart from the nitpicks in other mails, the series looks OK to me.

Out of interest, how many more import problems does this show in the 
current Mercurial codebase?

On 12/04/2016 22:54, timeless wrote:
> # HG changeset patch
> # User timeless <timeless at mozdev.org>
> # Date 1460497442 0
> #      Tue Apr 12 21:44:02 2016 +0000
> # Node ID 45700c5521d9d77ccaf6fa535ca2f195a52f0b75
> # Parent  5ff1ae440687032163d88924da1b5c55394fb581
> import-checker: check docstrings in python
>
> diff --git a/contrib/import-checker.py b/contrib/import-checker.py
> --- a/contrib/import-checker.py
> +++ b/contrib/import-checker.py
> @@ -598,19 +598,41 @@
>       'from __future__ import print_function\\n'
>       example[11] doctest.py 11
>       'from __future__ import print_function\\\\\\n, absolute_import\\n'
> +
> +    >>> lines = [
> +    ... 'def x():',
> +    ... "    ''' # docstring",
> +    ... '    x()',
> +    ... '    >>> x() + 1',
> +    ... '    3',
> +    ... "    '''",
> +    ... '    return 2',
> +    ... ]
> +
> +    >>> test("example.py", lines)
> +    example[4] doctest.py 4
> +    'x() + 1\\n'
>       """
>       inlinepython = 0
>       shpython = 0
>       script = []
> -    prefix = 6
>       t = ''
> +    pystr = ''
>       n = 0
>       linecont = 0
> +    if f.endswith('.t'):
> +        pyinlinestart = b'  >>> '
> +        pyinlinecont = b'  ... '
> +        prefix = 6
> +    else:
> +        pyinlinestart = b'>>> '
> +        pyinlinecont = b'... '
> +        prefix = 4
>       for l in src:
>           n += 1
>           if not l.endswith(b'\n'):
>               l += b'\n'
> -        if l.startswith(b'  >>> '): # python inlines
> +        if l.startswith(pyinlinestart): # python inlines
>               if shpython:
>                   print("Parse Error %s:%d\n" % (f, n))
>               if not inlinepython:
> @@ -620,7 +642,7 @@
>               script.append(l[prefix:])
>               linecont = l.strip('\n').endswith('\\')
>               continue
> -        if l.startswith(b'  ... '): # python inlines
> +        if l.startswith(pyinlinecont): # python inlines
>               script.append(l[prefix:])
>               linecont = l.strip('\n').endswith('\\')
>               continue
> @@ -652,6 +674,36 @@
>               inlinepython = 0
>               linecont = False
>               continue
> +        marker = l.lstrip()[:3]
> +        if pystr:
> +            if marker == pystr:
> +                pystr = ''
> +                if not inlinepython:
> +                    continue
> +                yield ''.join(script), ("%s[%d]" %
> +                       (modname, inlinepython)), t, inlinepython
> +                script = []
> +                inlinepython = 0
> +                linecont = False
> +                continue
> +            pydoc = re.search('^(\s*)>>> ', l)
> +            if pydoc:
> +                pyinlinestart = pydoc.group()
> +                pyinlinecont = pyinlinestart.replace('>>>', '...')
> +                prefix = len(pyinlinestart)
> +                inlinepython = n
> +                t = 'doctest.py'
> +                script.append(l[prefix:])
> +                linecont = l.strip('\n').endswith('\\')
> +                continue
> +            if pystr == l.strip()[-3:]:
> +                pystr = ''
> +                continue
> +        if marker in ("'''", '"""'):
> +            if l.lstrip()[3:].strip().endswith(marker):
> +                continue
> +            pystr = marker
> +            continue
>           if linecont:
>               script.append(l[prefix:])
>               linecont = l.strip('\n').endswith('\\')
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at 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=-tkaXBS16riF5eKtud65I0gpcKvpUpg4aQ0T3soZoj8&s=foAyFrMhEc_4_ny7LnQ237dPamR2E9BTdngueApEd8Q&e=
>

-- 
Simon Farnsworth


More information about the Mercurial-devel mailing list