doctest fails to NORMALIZE_WHITESPACE ?

D

David MacKay

Hello, I'm a python-list newbie. I've got a question about doctest; perhaps
a bug report.

I really like doctest, but sometimes doctest gives a failure when the output
looks absolutely fine to me -- indeed, even after I have gone to considerable
effort to make my documentation match the output perfectly.

http://www.aims.ac.za/~mackay/python/compression/huffman/Huffman3.py

The above file is an example.

It's self-contained, so you can plop it into emacs and hit C-cC-c to run the
doctests. One of them fails.
The piece of source code concerned is here:
c.append(node(0.5,1,'a')); \
c.append(node(0.25,2,'b')); \
c.append(node(0.125,3,'c')); \
c.append(node(0.125,4,'d')); \
iterate(c) ; reportcode(c) # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
#Symbol Count Codeword
a (0.5) 1
b (0.25) 01
c (0.12) 000
d (0.12) 001
"""

And the output is:

Failed example:
c = []; c.append(node(0.5,1,'a')); c.append(node(0.25,2,'b')); c.append(node(0.125,3,'c')); c.append(node(0.125,4,'d')); iterate(c) ; reportcode(c) # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
Expected:
#Symbol Count Codeword
a (0.5) 1
b (0.25) 01
c (0.12) 000
d (0.12) 001
Got:
<__main__.internalnode instance at 0xb7aee76c>
#Symbol Count Codeword
a (0.5) 1
b (0.25) 01
c (0.12) 000
d (0.12) 001

===================================

I have tried numerous tweaks, and am at a loss. I am wondering whether there
is some big in doctest involving the "#" character in the output.
Or maybe I made some silly mistake.

Any advice appreciated!

Many thanks again to the authors of doctest, it gives a great feeling
to write code in the way that doctest encourages. :)

David
 
P

Peter Otten

David said:
I really like doctest, but sometimes doctest gives a failure when the
output looks absolutely fine to me -- indeed, even after I have gone to
considerable effort to make my documentation match the output perfectly.
The piece of source code concerned is here:
c.append(node(0.5,1,'a')); \
c.append(node(0.25,2,'b')); \
c.append(node(0.125,3,'c')); \
c.append(node(0.125,4,'d')); \
iterate(c) ; reportcode(c) # doctest:

You have to suppress the return value of iterate()

dummy = iterate(c); reportcode(c)
+NORMALIZE_WHITESPACE, +ELLIPSIS
#Symbol Count Codeword
a (0.5) 1
b (0.25) 01
c (0.12) 000
d (0.12) 001
"""
And the output is:

Expected:
#Symbol Count Codeword
a (0.5) 1
Got:
<__main__.internalnode instance at 0xb7aee76c>

This is the result of iterate(c). The interactive interpreter ignores None
return values but prints the repr() of everything else. doctest emulates
that behaviour.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top