[Python-checkins] r65504 - in doctools/trunk: sphinx/application.py sphinx/ext/autodoc.py tests/test_autodoc.py

georg.brandl python-checkins at python.org
Mon Aug 4 22:16:18 CEST 2008


Author: georg.brandl
Date: Mon Aug  4 22:16:18 2008
New Revision: 65504

Log:
Add tests for between() and cut_lines() and fix them.
Also fix a bug in the application interface.


Modified:
   doctools/trunk/sphinx/application.py
   doctools/trunk/sphinx/ext/autodoc.py
   doctools/trunk/tests/test_autodoc.py

Modified: doctools/trunk/sphinx/application.py
==============================================================================
--- doctools/trunk/sphinx/application.py	(original)
+++ doctools/trunk/sphinx/application.py	Mon Aug  4 22:16:18 2008
@@ -159,7 +159,7 @@
         return listener_id
 
     def disconnect(self, listener_id):
-        for event in self._listeners:
+        for event in self._listeners.itervalues():
             event.pop(listener_id, None)
 
     def emit(self, event, *args):

Modified: doctools/trunk/sphinx/ext/autodoc.py
==============================================================================
--- doctools/trunk/sphinx/ext/autodoc.py	(original)
+++ doctools/trunk/sphinx/ext/autodoc.py	Mon Aug  4 22:16:18 2008
@@ -111,7 +111,13 @@
             return
         del lines[:pre]
         if post:
+            # remove one trailing blank line.
+            if lines and not lines[-1]:
+                lines.pop(-1)
             del lines[-post:]
+        # make sure there is a blank line at the end
+        if lines and lines[-1]:
+            lines.append('')
     return process
 
 def between(marker, what=None, keepempty=False):
@@ -141,6 +147,9 @@
                     deleted += 1
         if not lines and not keepempty:
             lines[:] = orig_lines
+        # make sure there is a blank line at the end
+        if lines and lines[-1]:
+            lines.append('')
     return process
 
 
@@ -340,10 +349,14 @@
                 if what == 'class':
                     # for classes, the relevant signature is the __init__ method's
                     obj = getattr(obj, '__init__', None)
-                    # classes without __init__ method?
+                    # classes without __init__ method, default __init__ or
+                    # __init__ written in C?
                     if obj is None or obj is object.__init__ or not \
                        (inspect.ismethod(obj) or inspect.isfunction(obj)):
                         getargs = False
+                elif inspect.isbuiltin(obj) or inspect.ismethoddescriptor(obj):
+                    # can never get arguments of a C function or method
+                    getargs = False
                 if getargs:
                     argspec = inspect.getargspec(obj)
                     if what in ('class', 'method') and argspec[0] and \

Modified: doctools/trunk/tests/test_autodoc.py
==============================================================================
--- doctools/trunk/tests/test_autodoc.py	(original)
+++ doctools/trunk/tests/test_autodoc.py	Mon Aug  4 22:16:18 2008
@@ -14,7 +14,7 @@
 
 from docutils.statemachine import ViewList
 
-from sphinx.ext.autodoc import RstGenerator
+from sphinx.ext.autodoc import RstGenerator, cut_lines, between
 
 
 def setup_module():
@@ -222,6 +222,30 @@
     assert getdocl('class', 'bar', D) == ['Init docstring', '', '42']
 
 
+def test_docstring_processing_functions():
+    lid = app.connect('autodoc-process-docstring', cut_lines(1, 1, ['function']))
+    def f():
+        """
+        first line
+        second line
+        third line
+        """
+    assert list(gen.get_doc('function', 'f', f)) == ['second line', '']
+    app.disconnect(lid)
+
+    lid = app.connect('autodoc-process-docstring', between('---', ['function']))
+    def f():
+        """
+        first line
+        ---
+        second line
+        ---
+        third line
+        """
+    assert list(gen.get_doc('function', 'f', f)) == ['second line', '']
+    app.disconnect(lid)
+
+
 def test_generate():
     def assert_warns(warn_str, *args):
         gen.generate(*args)
@@ -246,6 +270,7 @@
         gen.generate(*args)
         assert len(gen.warnings) == 0, gen.warnings
         assert item in gen.result
+        print '\n'.join(gen.result)
         del gen.result[:]
 
     # no module found?
@@ -303,6 +328,10 @@
     assert_result_contains('   :noindex:', 'module', 'test_autodoc', [], None)
     assert_result_contains('   :noindex:', 'class', 'Base', [], None)
 
+    # okay, now let's get serious about mixing Python and C signature stuff
+    assert_result_contains('.. class:: CustomDict', 'class', 'CustomDict',
+                           ['__all__'], None)
+
 
 # --- generate fodder ------------
 
@@ -329,3 +358,6 @@
     @property
     def prop(self):
         """Property."""
+
+class CustomDict(dict):
+    """Docstring."""


More information about the Python-checkins mailing list