doctest and '\0'

robin and jim robinjim at earthlink.net
Sat Sep 1 17:38:46 EDT 2001


Thanks Tim for your response and thanks for doctest -- its a very nice tool!


"Tim Peters" <tim.one at home.com> wrote in message
news:mailman.999281389.6969.python-list at python.org...
> [jim.vickroy]
> > I have installed Python 2.1 on a MS Windows computer.
> >
> > It appears that doctest does not work with '\0'.
> >
> > Below is a sample module that illustrates the:
> >
> >     "TypeError: compile() argument 1 must be string without null bytes,
> > not string"
> >
> > exception being raised.
>
> Consider this isolated snippet:
>
> >>> eval("'\0' *  5")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: expected string without null bytes
> >>>
>
> Same thing.  A doctest is a *string*, and sticking \0 in a string creates
a
> null byte, and a null byte is never legitimate in program source code.
>
> These alternatives work fine:
>
> >>> eval("'\\0' * 5")  # double the backslash
> '\x00\x00\x00\x00\x00'
> >>> eval(r"'\0' * 5")  # or use a raw string
> '\x00\x00\x00\x00\x00'
> >>>
>
> > Am I doing something wrong?
>
> Yes, as above.  If you want the code to contain the 2-character sequence
>
>     \0
>
> then you have to either escape the backslash, or use a raw string to
prevent
> translation of \0 to chr(0) at the time the string is created (which
happens
> long before doctest ever sees it).
>
> I haven't tried it, but changing the opening of your docstring from
>
>     """
>
> to
>
>     r"""
>
> will probably make the problem go away.
>
>
>





More information about the Python-list mailing list