doctest bug?

E

Edward K. Ream

Hi,

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

"""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
.....<< 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
 
H

Heiko Wundram

Am Montag, 19. Juli 2004 18:24 schrieb Edward K. Ream:
Hi,

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

"""


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.
 
E

Edward K. Ream

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
 
T

Tim Peters

[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.
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top