470,581 Members | 2,460 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,581 developers. It's quick & easy.

doctest bug?

Hi,

I am trying to run the following docstring using docutils.DocTestSuite:

"""
s = "a\n \t\n\t\t \t\nb"

etc...
"""

However, docutils complains about "inconsistent leading whitespace". Well,
that's not the problem.

I inserted traces in various places in Lib/Python23/doctest.py. This is
what I found:

1. Tester._get_doctest returns __doc__, and in this case repr(__doc__) is:

' >>> s = "a\n \t\n\t\t \t\nb"\n etc...'

So you can see the problem: all newlines (all the '\n' characters) are equal
here, whether or not they are inside strings!

Note: this came from a an actual dump: I hope it has survived the cut/paste
relatively intact.

2. Naturally, doctest._extract_examples(s) complains because it thinks the
lines in s are improperly indented.

Indeed, doctest._extract_examples contains the following code:

lines = s.split("\n")
i, n = 0, len(lines)
while i < n:
.....line = lines[i]
.....<< do something with line i >>

Printing "EKR:", repr(line) in this code yields:

EKR: ' >>> s = "a'
EKR: ' \t'
EKR: '\t\t \t'
EKR: 'b"'

This should make it perfectly clear what is happening and why
doctest._extract_examples complains about indentation.

Is this a bug? If so, is there a workaround? If this isn't a bug, why not?

I don't see anything in the doctest docs about this kind of problem. Am I
doing something obviously wrong?

Thanks,

Edward
--------------------------------------------------------------------
Edward K. Ream email: ed*******@charter.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 18 '05 #1
3 1869
Am Montag, 19. Juli 2004 18:24 schrieb Edward K. Ream:
Hi,

I am trying to run the following docstring using docutils.DocTestSuite:

"""
s = "a\n \t\n\t\t \t\nb"


etc...
"""


Check out what

print """I am here.\nAnd here again."""

prints out. Normal escaping rules apply to triple quotes, so what you want is
actually:

print """I am here.\\nAnd here again."""

Now, if you replace \n and \t in your example by \\n and \\t, doctest won't
complain, and a pydoc <module> will also display the strings correctly (and
also run).

HTH!

Heiko.
Jul 18 '05 #2
> Normal escaping rules apply to triple quotes, so what you want is
actually:...

Arggh. I failed to find the following from the docs the first time around:

"If you continue a line via backslashing in an interactive session, or for
any other reason use a backslash, you need to double the backslash in the
docstring version."

Thanks for your help.

Edward
--------------------------------------------------------------------
Edward K. Ream email: ed*******@charter.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 18 '05 #3
[Edward K. Ream]
Arggh. I failed to find the following from the docs the first time around:

"If you continue a line via backslashing in an interactive session, or for
any other reason use a backslash, you need to double the backslash in the
docstring version."


Note that "the rules" here follow inescapably from the mechanics of
how doctests are written: first Python processes escapes as part of
compiling the file containing doctests, and then doctest later sends
the strings thru compile(), which processes escapes again.

A sometimes (very) useful alternative is to put doctests in
triple-quoted r-strings. Then Python leaves all the backslashes alone
when compiling the file. If you have a lot of backslashes in
doctests, that can make life a lot easier.
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Pierre Rouleau | last post: by
2 posts views Thread by Alan G Isaac | last post: by
2 posts views Thread by Michele Simionato | last post: by
5 posts views Thread by Michele Simionato | last post: by
1 post views Thread by David MacKay | last post: by
1 post views Thread by Runsun Pan | last post: by
2 posts views Thread by p.lavarre | last post: by
reply views Thread by Eric Mahurin | last post: by
12 posts views Thread by thomas.guest | last post: by
6 posts views Thread by Bzyczek | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.