doctest: address in output

Paramjit Oberoi p_s_oberoi at hotmail.com
Mon May 3 22:34:05 EDT 2004


Recently (30 minutes ago...) I need to write a doctring
of the following type:

>>> import mymodule
>>> mymodule.do_something()
<mymodule.coolObj object at ...>

And I wanted to run doctest on this module. Now, I agree that avoiding
addresses in test output is a good thing in general; but, in this case I
wanted the '...' to match whatever address the object happened to be at.

Here's a patch to doctest.py to enable this functionality:
(against python-2.2.3)

--- /usr/lib/python2.2/doctest.py       2003-10-15 22:41:35.000000000 -0500
+++ mydoctest.py        2004-05-03 21:08:55.000000000 -0500
@@ -288,6 +288,7 @@
 _isPS2 = re.compile(r"(\s*)" + re.escape(PS2)).match
 _isEmpty = re.compile(r"\s*$").match
 _isComment = re.compile(r"\s*#").match
+_subAddress = re.compile(r"0x[0-9a-f]*>$").sub
 del re
  
 from types import StringTypes as _StringTypes
@@ -451,6 +452,11 @@
                 if verbose:
                     out("ok\n")
                 continue
+            got = _subAddress('...>', got)
+            if got == want:
+                if verbose:
+                    out("ok\n")
+                continue
             state = FAIL
  
         assert state in (FAIL, BOOM)


I was impressed by how easy it was to figure out how to make this
change...  I can't even *imagine* trying to fiddle with the standard
library in, say, C++.

In fact, it's only with python that I've really experienced the true
potential of Open Source: it's invariably too much work to change a
C/C++ program written by someone else.  It takes too long to understand
what is going on; you have to go through a lengthy compile; you have to
worry about installation.  Python makes it all so easy.

I've hardly ever modified a C/C++ application that I've downloaded from
somewhere---and I've almost always made small tweaks to the ones in python.
These days I prefer python applications simply because I know I'll be able
to *actually* change them to suit my needs, rather than theoretically have
the ability to change them.

-param



More information about the Python-list mailing list