doctest problem with null byte

Discussion in 'Python' started by Stuart D. Gathman, Jan 26, 2007.

  1. I am trying to create a doctest test case for the following:

    def quote_value(s):
    """Quote the value for a key-value pair in Received-SPF header field
    if needed. No quoting needed for a dot-atom value.

    >>> quote_value(r'abc\def')

    '"abc\\\\\\\\def"'
    >>> quote_value('abc..def')

    '"abc..def"'
    >>> quote_value('')

    '""'
    >>> quote_value('-all\x00')

    '"-all\\\\\\\\x00"'
    ...

    However, doctest does *not* like the null byte in the example (yes, this
    happens with real life input):
    **********************************************************************
    File "/home/stuart/pyspf/spf.py", line 1453, in spf.quote_value
    Failed example:
    quote_value('-all')
    Exception raised:
    Traceback (most recent call last):
    File "/var/tmp/python2.4-2.4.4c1-root/usr/lib/python2.4/doctest.py",
    line 1248, in __run
    compileflags, 1) in test.globs
    TypeError: compile() expected string without null bytes
    **********************************************************************

    How can I construct a test cast for this?

    --
    Stuart D. Gathman <>
    Business Management Systems Inc. Phone: 703 591-0911 Fax: 703 591-6154
    "Confutatis maledictis, flamis acribus addictis" - background song for
    a Microsoft sponsored "Where do you want to go from here?" commercial.
    Stuart D. Gathman, Jan 26, 2007
    #1
    1. Advertising

  2. Stuart D. Gathman

    Tim Peters Guest

    [Stuart D. Gathman]
    > I am trying to create a doctest test case for the following:
    >
    > def quote_value(s):
    > """Quote the value for a key-value pair in Received-SPF header
    > field if needed. No quoting needed for a dot-atom value.
    >
    > >>> quote_value(r'abc\def')

    > '"abc\\\\\\\\def"'
    > >>> quote_value('abc..def')

    > '"abc..def"'
    > >>> quote_value('')

    > '""'
    > >>> quote_value('-all\x00')

    > '"-all\\\\\\\\x00"'
    > ...
    >
    > However, doctest does *not* like the null byte in the example (yes,
    > this happens with real life input):
    > **********************************************************************
    > File "/home/stuart/pyspf/spf.py", line 1453, in spf.quote_value
    > Failed example:
    > quote_value('-all')
    > Exception raised:
    > Traceback (most recent call last):
    > File
    > "/var/tmp/python2.4-2.4.4c1-root/usr/lib/python2.4/doctest.py",
    > line 1248, in __run
    > compileflags, 1) in test.globs
    > TypeError: compile() expected string without null bytes
    > **********************************************************************
    >
    > How can I construct a test cast for this?


    As the docs say, doctest examples are parsed /twice/: once when Python
    compiles the module and creates string objects, and again when doctest
    extracts and passes example substrings to the compile() function (which
    you can see in your traceback).

    The first time, the \x00 in the

    quote_value('-all\x00')

    portion of your docstring got changed into an honest-to-goodness NUL
    byte. Passing that to compile() can't work.

    To alleviate that, and the "leaning toothpick syndrome" in your other
    examples, a simple approach is to make your docstring a raw string
    (stick the letter 'r' in front of the opening triple-quote).

    For example, this works fine:

    def f():
    r"""
    >>> len("\\")

    1
    >>> ord("\x00")

    0
    """

    Because it's a raw string, Python does /not/ change the

    \x00

    portion into a NUL byte when it compile the module. Instead the
    docstring continues to contain the 4-character substring:

    \x00

    and that's what compile() needs to see.

    To perform the same test without using a raw string requires slamming in
    more backslashes (or other ad-hoc escaping tricks):

    def g():
    """
    >>> len("\\\\")

    1
    >>> ord("\\x00")

    0
    """
    Tim Peters, Jan 26, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter
    Replies:
    3
    Views:
    723
    Michael Borgwardt
    Aug 5, 2004
  2. Replies:
    5
    Views:
    26,584
    Mike Schilling
    Mar 29, 2006
  3. Tom McGlynn
    Replies:
    4
    Views:
    851
    Mark Space
    Apr 19, 2008
  4. Patricia Shanahan
    Replies:
    0
    Views:
    383
    Patricia Shanahan
    Apr 17, 2008
  5. Tom McGlynn
    Replies:
    2
    Views:
    404
    Andreas Leitgeb
    Apr 18, 2008
Loading...

Share This Page