doctest and '\0'

Tim Peters at
Fri Aug 31 20:08:22 CEST 2001

> 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
>>> eval(r"'\0' * 5")  # or use a raw string

> Am I doing something wrong?

Yes, as above.  If you want the code to contain the 2-character sequence


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




will probably make the problem go away.

More information about the Python-list mailing list