Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work with nulls in docutils

Tags:

python

doctest

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?

like image 364
Mark Irvine Avatar asked Mar 14 '12 22:03

Mark Irvine


1 Answers

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.

like image 90
Andrew Clark Avatar answered Sep 19 '22 17:09

Andrew Clark