[Tutor] Multi-line code that uses \ in doctest

Kent Johnson kent37 at tds.net
Tue Feb 6 18:34:42 CET 2007


Don Taylor wrote:
> When I try to use something like:
> 
>  >>> hexStringNums = ('1', '2', '3', '4', '5', '6',\
> ...                  '7', '8', '9','0')
> 
> or:
> 
>  >>> for hexString in hexStrings:
> ...     for x in hexString:
> ...         if ((not x in hexStringChars) and
> ...             (not x in hexStringNums)):
> ...             print hexString+ \
> ...                 " is not a hex string."
> ...             break
> 
> in doctest (Python 2.4) I get an invalid syntax error around the line 
> continuation marker, e.g.
> 
> Failed example:
>      hexStringNums = ('1', '2', '3', '4', '5', '6',... 
> '7', '8', '9','0')
> Exception raised:
>      Traceback (most recent call last):
>        File "C:\PYTHON24\lib\doctest.py", line 1243, in __run
>          compileflags, 1) in test.globs
>        File "<doctest __main__[1]>", line 1
>           hexStringNums = ('1', '2', '3', '4', '5', '6',... 
>       '7', '8', '9','0')
>                                                         ^
>       SyntaxError: invalid syntax
> 
> I can fix this by not using \ line continuations, but is there a way to 
> get doctest to parse these lines as I expect?

I think the problem is that the normal Python string processor (not 
doctest) is swallowing the \<newline> in the string. Try putting your 
doctest string in a raw string (r'') or double the backslash (\\).

This page has more details:
file:///C:/Python25/Doc/lib/doctest-finding-examples.html

Kent



More information about the Tutor mailing list