I'm trying to run doctest on a function that works with nulls. But doctest doesn't seem to like the nulls...
def do_something_with_hex(c):
    """
    >>> do_something_with_hex('\x00')
    '\x00'
    """
return repr(c)
import doctest
doctest.testmod()
I'm seeing these errors
Failed example:
    do_something_with_hex(' ')
Exception raised:
    Traceback (most recent call last):
      File "C:\Python27\lib\doctest.py", line 1254, in __run
        compileflags, 1) in test.globs
    TypeError: compile() expected string without null bytes
**********************************************************************
What can I do to allow nulls in test cases like this?
You could escape all of the backslashes, or alternatively change your docstring to a raw string literal:
def do_something_with_hex(c):
    r"""
    >>> do_something_with_hex('\x00')
    '\x00'
    """
    return repr(c)
With the r prefix on the string a character following a backslash is included in the string without change, and all backslashes are left in the string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With