473,699 Members | 2,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

doctest bug?

Hi,

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

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

etc...
"""

However, docutils complains about "inconsiste nt 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_doc test 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._extrac t_examples(s) complains because it thinks the
lines in s are improperly indented.

Indeed, doctest._extrac t_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._extrac t_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*******@chart er.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 18 '05 #1
3 2014
Am Montag, 19. Juli 2004 18:24 schrieb Edward K. Ream:
Hi,

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

"""
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*******@chart er.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2614
by: Pierre Rouleau | last post by:
I have a problem writing self-testable modules using doctest when these modules have internationalized strings using gettext _('...'). - The main module of an application (say app.py) calls gettext.install() to install the special _ function inside Python builtin. Other modules, taken from a general purpose collection of Python modules, also support internationalisation and doctest testing. For example:
2
1414
by: Alan G Isaac | last post by:
> python doctest.py -v Running doctest.__doc__ Trying: .remove(42) Expecting: Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: list.remove(x): x not in list ok Trying: x = 12 Expecting: nothing
2
1675
by: Michele Simionato | last post by:
Some time ago I hacked a custom solution to run doctests on text files containing documentation. The solution involved this kind of game: tester=doctest.Tester(globs={},verbose=1) tester.runstring(mytest) It worked fine, but now with Python 2.4.a3 I get DeprecationWarning: class Tester is deprecated; use class
5
1799
by: Michele Simionato | last post by:
I am getting a strange error with this script: $ cat doctest-threads.py """ >>> import time, threading >>> def example(): .... thread.out = .... while thread.running: .... time.sleep(.01) .... thread.out.append(".")
1
1920
by: David MacKay | last post by:
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.
1
1247
by: Runsun Pan | last post by:
I intend to use the doctect heavily. For this I am thinking of coding a class that comes with a built-in doctest functionality. I'd like to seek for input before I start. The idea is to have a class MyObj from where all my other classes will subclass. lets say: class C(MyObj):
2
2345
by: p.lavarre | last post by:
From: http://docs.python.org/lib/doctest-soapbox.html ... Can I somehow tell doctest that it's time to quit? I ask because not all doctest examples are created equal. Some failures are catastrophic, making all subsequent failures at least uninteresting, and maybe painfully slow. Other failures are negligible, making the print of any subsequent failure valuable. So sys.exit() doesn't do what I mean: it raises SystemExit, but doctest
0
1123
by: Eric Mahurin | last post by:
Noob here. Just got into python a little over a week ago... One of the (unique?) things I really like about python is the concept of doctesting. But, now I want more! Here's what I'd like to see: * easy debugging. As soon as there is a failure (unexpected exception or mismatch), drop down into the debugger in a way to isolates the bug down to the nearest test/example as much as possible. * integrated with code coverage. I'd like...
12
2115
by: thomas.guest | last post by:
I'm not making progress with the following and would appreciate any help. Here's an interpreted Python session. .... Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'f' is not defined
6
2483
by: Bzyczek | last post by:
Hello, I have problems with running doctests if I use czech national characters in UTF-8 encoding. I have Python script, which begin with encoding definition: # -*- coding: utf-8 -*- I have this function with doctest:
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.