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

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


# HG changeset patch
# User timeless <timeless at mozdev.org>
# Date 1460497442 0
#      Tue Apr 12 21:44:02 2016 +0000
# Node ID 7e891620eaded8f66262d7007307f1b986a1ba5f
# Parent  4fb9d603c45a64c98061abd264b3f691a6f336d0
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 = False
+    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('\\')


More information about the Mercurial-devel mailing list