ValueError: source code string cannot contain null bytes

Viewed 15180
import hashlib


def my_function(bytes_):
    """
    >>> my_function(b'\0')
    '6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d'
    """
    return hashlib.sha256(bytes_).hexdigest()


if __name__ == "__main__":
    import doctest
    doctest.testmod()

Expected behaviour: 1 test passes

Actual behaviour: ValueError exception is raised from within doctest.py.

I've tried to use b'\x00' literal as well with the same result. How to use null bytes in doctest? What exactly is the problem here, and is there any fix or not-too-ugly workaround?

2 Answers
Related