Doctest processing
==================

Imports in doctest sections inside docstrings are also handled

    >>> from findimports import find_imports

    >>> open('marmalade.py', 'w').write('''
    ... import doctest
    ... def example():
    ...     """
    ...         >>> import sys
    ...     """
    ... class Example:
    ...     """
    ...         >>> import gc
    ...     """
    ... ''')

    >>> for imp in find_imports('marmalade.py'):
    ...     print imp
    ImportInfo('doctest', 'marmalade.py', 2)
    ImportInfo('sys', 'marmalade.py', 5)
    ImportInfo('gc', 'marmalade.py', 9)

Note that we carefully adjust line numbers to make sure they match reality.


Module docstrings
-----------------

Module docstrings are special in that the corresponding module AST node has
no line number.  We don't crash:

    >>> open('jam.py', 'w').write('''
    ... """
    ...         >>> import sys
    ... """
    ... ''')

    >>> for imp in find_imports('jam.py'):
    ...     print imp
    ImportInfo('sys', 'jam.py', 3)


Doctest errors
--------------

Doctest errors abort processing, after showing where the erroneous docstring
is

    >>> open('peanut_butter.py', 'w').write('''
    ... def example():
    ...     """
    ...         >>> import sys
    ...     missing blank line after doctest section
    ...     """
    ... ''')

    >>> try:
    ...     for imp in find_imports('peanut_butter.py'):
    ...         print imp
    ... except:
    ...     import traceback
    ...     traceback.print_exc()
    ... # doctest: +ELLIPSIS
    peanut_butter.py:2: error while parsing doctest
    Traceback (most recent call last):
      ...
    ValueError: line 3 of the docstring for <string> has inconsistent leading whitespace: 'missing blank line after doctest section'

    >>> open('jelly.py', 'w').write('''
    ... def example1():
    ...     """
    ...         >>> if something:
    ...         >>> import sys
    ...     """
    ... def example1():
    ...     """
    ...         >>> import os
    ...     """
    ... ''')

    >>> for imp in find_imports('jelly.py'):
    ...     print imp
    jelly.py:2: syntax error in doctest
    ImportInfo('sys', 'jelly.py', 5)
    ImportInfo('os', 'jelly.py', 9)


Nested docstrings
-----------------

This is probably overkill, but we handle them recursively:

    >>> open('sandwitch.py', 'w').write('''
    ... def example():
    ...     """
    ...         >>> class Example:
    ...         ...    '>>> import sys'
    ...     """
    ... ''')
    >>> for imp in find_imports('sandwitch.py'):
    ...     print imp
    ImportInfo('sys', 'sandwitch.py', 5)

