[Zope3-dev] Re: [Zope-Checkins] CVS: Zope3/src/persistent/tests- test_persistent.py:1.5 persistent.txt:1.2

Tim Peters tim at zope.com
Thu Mar 11 11:37:08 EST 2004


[Tres Seaver]
> Chris is asking whether the Python interpreter will choke when cutting
> and pasting an entire multi-line doctest example.  E.g., try cutting
> and pasting the following text into your Python interpreter session::
>
> --------------- 8< --------------------------------
>  >>> class Foo:
> ...     def __init__(self, bar):
> ...         self._bar = bar
> ...     def bar(self):
> ...         return self._bar
> ...
>  >>> foo = Foo('baz')
>  >>> foo.bar()
> baz
> --------------- 8< --------------------------------

Of course the Python interpreter will complain if you feed it lines
beginning with sys.PS1 or sys.PS2.  A rectangle copy (instead of a line
copy) works.  Python 2.3's doctest.py also contains Jim's testsource()
function:

def testsource(module, name):
    """Extract the doctest examples from a docstring.

    Provide the module (or dotted name of the module) containing the
    tests to be extracted, and the name (within the module) of the object
    with the docstring containing the tests to be extracted.

    The doctest examples are returned as a string containing Python
    code.  The expected output blocks in the examples are converted
    to Python comments.
    """

There's also:

def debug(module, name):
    """Debug a single docstring containing doctests.

    Provide the module (or dotted name of the module) containing the
    docstring to be debugged, and the name (within the module) of the
    object with the docstring to be debugged.

    The doctest examples are extracted (see function testsource()),
    and written to a temp file.  The Python debugger (pdb) is then
    invoked on that file.
    """

which is a layer around testsource().

I've used doctest for years without those, and have to say I've never had an
itch to paste a doctest example into an interactive Python shell.  For that
matter, neither have I had an itch to paste a unittest body into an
interactive Python shell.




More information about the Zope3-dev mailing list