How to work with zeros in docutils

I am trying to run doctest for a function that works with zeros. But the teaching is not like zeros ...

def do_something_with_hex(c): """ >>> do_something_with_hex('\x00') '\x00' """ return repr(c) import doctest doctest.testmod() 

I see 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 resolve zeros in test cases?

+6
source share
2 answers

You can avoid all backslashes or change your docstring to a string literal string :

 def do_something_with_hex(c): r""" >>> do_something_with_hex('\x00') '\x00' """ return repr(c) 

With the r prefix in the string, the character following the backslash is included in the string without changes, and all backslashes remain in the string.

+7
source

Use \\x instead of \x . When you write \x , the Python interpreter interprets it as a null byte, and the null byte itself is inserted into the docstring .. For example,

 >>> def func(x): ... """\x00""" ... >>> print func.__doc__ # this will print a null byte >>> def func(x): ... """\\x00""" ... >>> print func.__doc__ \x00 
+3
source

Source: https://habr.com/ru/post/910749/


All Articles