doctest.script_from_examples() can sometimes return results that
cannot be passed to exec. The docstring for script_from_examples()
itself is an example:
guyer% python2.4
Python 2.4 (#1, Mar 10 2005, 18:08:38)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1671)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> text = '''
... Here are examples of simple math.
... Python has super accurate integer addition
... >>> 2 + 2
... 5
...
... And very friendly error messages:
...
... >>> 1/0
... To Infinity
... And
... Beyond
...
... You can use logic if you want:
...
... >>> if 0:
... ... blay
... ... blah
... ...
...
... Ho hum
... '''
>>> exec doctest.script_from_examples(text)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 21
# Ho hum
^
SyntaxError: invalid syntax
The issue seems to be the lack of trailing '\n', which is documented
as required by compile(), although not for exec.
We never saw a problem with this in Python 2.3's doctest, probably
because comment lines, such as "Ho hum", were stripped out and
apparently adequate '\n' were appended.
Python 2.4 on Mac OS X 10.3.8
|