~andrewsomething/ubuntu/precise/sphinx/1.1.3+dfsg-2ubuntu1

« back to all changes in this revision

Viewing changes to debian/patches/docstring_parse.diff

  • Committer: Bazaar Package Importer
  • Author(s): Jakub Wilk, Jakub Wilk, Nikolaus Rath
  • Date: 2011-06-19 14:44:49 UTC
  • mfrom: (5.3.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110619144449-tqs2d2q0w9ihepbn
Tags: 1.0.7-5
[ Jakub Wilk ]
* Bump standards version to 3.9.2 (no changes needed).
* Bump minimum required version of jQuery to 1.4.
* Use python (>= 2.6.6-14~) as an alternative build-dependency to
  python-simplejson. The latter package is only needed for python2.5, and
  python-defaults 2.6.6-14 doesn't support it anymore.
* Include jQuery source (closes: #630973).
  + Check at build time if versions of both jQuery copies match.

[ Nikolaus Rath ]
* Backport upstream changesets a8b0ef275438 and de340a6098c7 to allow
  extraction of function signature from docstring for extension modules.
  (closes: #630409). The feature is disabled by default for the moment.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Description: Allow extraction of function signature from docstring for extension modules.
 
2
 Backport upstream changesets a8b0ef275438 and de340a6098c7 to allow extraction
 
3
 of function signature from docstring for extension modules.
 
4
 .
 
5
 Please note that the feature is disabled by default for the time being.
 
6
Bug-Debian: http://bugs.debian.org/630409
 
7
Bug: https://bitbucket.org/birkenfeld/sphinx/issue/564
 
8
Forwarded: not-needed
 
9
 
 
10
--- a/doc/ext/autodoc.rst
 
11
+++ b/doc/ext/autodoc.rst
 
12
@@ -260,6 +260,22 @@
 
13
 
 
14
    .. versionadded:: 1.0
 
15
 
 
16
+.. confval:: autodoc_docstring_signature
 
17
+
 
18
+   Functions imported from C modules cannot be introspected, and therefore the
 
19
+   signature for such functions cannot be automatically determined.  However, it
 
20
+   is an often-used convention to put the signature into the first line of the
 
21
+   function's docstring.
 
22
+
 
23
+   If this boolean value is set to ``True``, autodoc will look at the
 
24
+   first line of the docstring for functions and methods, and if it
 
25
+   looks like a signature, use the line as the signature and remove it
 
26
+   from the docstring content.
 
27
+
 
28
+   The default is ``False``.
 
29
+
 
30
+   .. versionadded:: 1.1
 
31
+
 
32
 
 
33
 Docstring preprocessing
 
34
 -----------------------
 
35
--- a/sphinx/ext/autodoc.py
 
36
+++ b/sphinx/ext/autodoc.py
 
37
@@ -426,13 +426,13 @@
 
38
             # etc. don't support a prepended module name
 
39
             self.add_line(u'   :module: %s' % self.modname, '<autodoc>')
 
40
 
 
41
-    def get_doc(self, encoding=None):
 
42
+    def get_doc(self, encoding=None, ignore=1):
 
43
         """Decode and return lines of the docstring(s) for the object."""
 
44
         docstring = self.get_attr(self.object, '__doc__', None)
 
45
         if docstring:
 
46
             # make sure we have Unicode docstrings, then sanitize and split
 
47
             # into lines
 
48
-            return [prepare_docstring(force_decode(docstring, encoding))]
 
49
+            return [prepare_docstring(force_decode(docstring, encoding), ignore)]
 
50
         return []
 
51
 
 
52
     def process_doc(self, docstrings):
 
53
@@ -833,7 +833,53 @@
 
54
         return modname, parents + [base]
 
55
 
 
56
 
 
57
-class FunctionDocumenter(ModuleLevelDocumenter):
 
58
+class DocstringSignatureMixin(object):
 
59
+    """
 
60
+    Mixin for FunctionDocumenter and MethodDocumenter to provide the
 
61
+    feature of reading the signature from the docstring.
 
62
+    """
 
63
+
 
64
+    def _find_signature(self, encoding=None):
 
65
+        docstrings = Documenter.get_doc(self, encoding, 2)
 
66
+        if len(docstrings) != 1:
 
67
+            return
 
68
+        doclines = docstrings[0]
 
69
+        setattr(self, '__new_doclines', doclines)
 
70
+        if not doclines:
 
71
+            return
 
72
+        # match first line of docstring against signature RE
 
73
+        match = py_ext_sig_re.match(doclines[0])
 
74
+        if not match:
 
75
+            return
 
76
+        exmod, path, base, args, retann = match.groups()
 
77
+        # the base name must match ours
 
78
+        if not self.objpath or base != self.objpath[-1]:
 
79
+            return
 
80
+        # ok, now jump over remaining empty lines and set the remaining
 
81
+        # lines as the new doclines
 
82
+        i = 1
 
83
+        while i < len(doclines) and not doclines[i].strip():
 
84
+            i += 1
 
85
+        setattr(self, '__new_doclines', doclines[i:])
 
86
+        return args, retann
 
87
+
 
88
+    def get_doc(self, encoding=None, ignore=1):
 
89
+        lines = getattr(self, '__new_doclines', None)
 
90
+        if lines is not None:
 
91
+            return [lines]
 
92
+        return Documenter.get_doc(self, encoding, ignore)
 
93
+
 
94
+    def format_signature(self):
 
95
+        if self.args is None and self.env.config.autodoc_docstring_signature:
 
96
+            # only act if a signature is not explicitly given already, and if
 
97
+            # the feature is enabled
 
98
+            result = self._find_signature()
 
99
+            if result is not None:
 
100
+                self.args, self.retann = result
 
101
+        return Documenter.format_signature(self)
 
102
+
 
103
+
 
104
+class FunctionDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter):
 
105
     """
 
106
     Specialized Documenter subclass for functions.
 
107
     """
 
108
@@ -847,7 +893,7 @@
 
109
     def format_args(self):
 
110
         if inspect.isbuiltin(self.object) or \
 
111
                inspect.ismethoddescriptor(self.object):
 
112
-            # can never get arguments of a C function or method
 
113
+            # cannot introspect arguments of a C function or method
 
114
             return None
 
115
         try:
 
116
             argspec = inspect.getargspec(self.object)
 
117
@@ -937,7 +983,7 @@
 
118
                 self.add_line(_(u'   Bases: %s') % ', '.join(bases),
 
119
                               '<autodoc>')
 
120
 
 
121
-    def get_doc(self, encoding=None):
 
122
+    def get_doc(self, encoding=None, ignore=1):
 
123
         content = self.env.config.autoclass_content
 
124
 
 
125
         docstrings = []
 
126
@@ -1010,7 +1056,7 @@
 
127
         pass
 
128
 
 
129
 
 
130
-class MethodDocumenter(ClassLevelDocumenter):
 
131
+class MethodDocumenter(DocstringSignatureMixin, ClassLevelDocumenter):
 
132
     """
 
133
     Specialized Documenter subclass for methods (normal, static and class).
 
134
     """
 
135
@@ -1225,6 +1271,7 @@
 
136
     app.add_config_value('autoclass_content', 'class', True)
 
137
     app.add_config_value('autodoc_member_order', 'alphabetic', True)
 
138
     app.add_config_value('autodoc_default_flags', [], True)
 
139
+    app.add_config_value('autodoc_docstring_signature', False, True)
 
140
     app.add_event('autodoc-process-docstring')
 
141
     app.add_event('autodoc-process-signature')
 
142
     app.add_event('autodoc-skip-member')
 
143
--- a/sphinx/util/docstrings.py
 
144
+++ b/sphinx/util/docstrings.py
 
145
@@ -12,7 +12,7 @@
 
146
 import sys
 
147
 
 
148
 
 
149
-def prepare_docstring(s):
 
150
+def prepare_docstring(s, ignore=1):
 
151
     """
 
152
     Convert a docstring into lines of parseable reST.  Return it as a list of
 
153
     lines usable for inserting into a docutils ViewList (used as argument
 
154
@@ -20,18 +20,19 @@
 
155
     this docstring and following content.
 
156
     """
 
157
     lines = s.expandtabs().splitlines()
 
158
-    # Find minimum indentation of any non-blank lines after first line.
 
159
+    # Find minimum indentation of any non-blank lines after ignored lines.
 
160
     margin = sys.maxint
 
161
-    for line in lines[1:]:
 
162
+    for line in lines[ignore:]:
 
163
         content = len(line.lstrip())
 
164
         if content:
 
165
             indent = len(line) - content
 
166
             margin = min(margin, indent)
 
167
-    # Remove indentation.
 
168
-    if lines:
 
169
-        lines[0] = lines[0].lstrip()
 
170
+    # Remove indentation from ignored lines.
 
171
+    for i in range(ignore):
 
172
+        if i < len(lines):
 
173
+            lines[i] = lines[i].lstrip()
 
174
     if margin < sys.maxint:
 
175
-        for i in range(1, len(lines)): lines[i] = lines[i][margin:]
 
176
+        for i in range(ignore, len(lines)): lines[i] = lines[i][margin:]
 
177
     # Remove any leading blank lines.
 
178
     while lines and not lines[0]:
 
179
         lines.pop(0)
 
180
--- a/tests/test_autodoc.py
 
181
+++ b/tests/test_autodoc.py
 
182
@@ -501,6 +501,14 @@
 
183
                            'attribute', 'mdocattr')
 
184
     del directive.env.temp_data['autodoc:class']
 
185
 
 
186
+    # test autodoc_docstring_signature
 
187
+    directive.env.config.autodoc_docstring_signature = True 
 
188
+    assert_result_contains(
 
189
+        '.. py:method:: DocstringSig.meth(FOO, BAR=1) -> BAZ', 'method',
 
190
+        'test_autodoc.DocstringSig.meth')
 
191
+    assert_result_contains(
 
192
+        '   rest of docstring', 'method', 'test_autodoc.DocstringSig.meth')
 
193
+
 
194
 
 
195
 # --- generate fodder ------------
 
196
 
 
197
@@ -595,3 +603,12 @@
 
198
 
 
199
     # should be documented as an alias
 
200
     factory = dict
 
201
+
 
202
+
 
203
+class DocstringSig(object):
 
204
+    def meth(self):
 
205
+        """meth(FOO, BAR=1) -> BAZ
 
206
+First line of docstring
 
207
+
 
208
+        rest of docstring
 
209
+        """