[IPython-user] ipython not playing nicely with doctest?

Fernando Perez fperez.net at gmail.com
Thu Jan 18 01:01:39 CST 2007

On 1/16/07, Jeremy Dunck <jdunck at gmail.com> wrote:


> Am I doing something foolish, or is this a limitation of using ipython
> with doctest?

The latter, I'm afraid, but there's a workaround.  The reason, deep
down, is a clash between ipython's modification of sys.displayhook so
you get nice output prompts, and the fact that the exec builtin is
internally hardcoded to run sys.displayhook.  So ipython collides with
doctest (which uses 'exec') and all hell breaks loose.

Here's the workaround.  First, your error (I called your file 'dtest'):

In [5]: doctest.testmod(dtest)
Out[5]: 1
File "dtest.py", line 8, in dtest
Failed example:
Got nothing
1 items had failures:
   1 of   3 in dtest
***Test Failed*** 1 failures.
Out[5]: (1, 3)

Now the workaround:

In [6]: iphook = sys.displayhook

In [7]: sys.displayhook = sys.__displayhook__

In [8]: doctest.testmod(dtest)
*** DocTestRunner.merge: 'dtest' in both testers; summing outcomes.
(0, 3)

Now you can reactivate ipython's displayhook if you want:

In [9]: sys.displayhook = iphook

You could wrap this little sys.displayhook dance in a utility function
to ease things up.



ps - brownie points for you if you contribute the above to the ipython wiki FAQ:


More information about the IPython-user mailing list