doctest and '\0'

jim.vickroy jim.vickroy at noaa.gov
Fri Aug 31 16:23:06 CEST 2001


I have installed Python 2.1 on a MS Windows computer.

It appears that doctest does not work with '\0'.

Below is a sample module that illustrates the:

    "TypeError: compile() argument 1 must be string without null bytes,
not string"

exception being raised.

Am I doing something wrong?  If not, is there a work-around for this?

Thanks for your time.


# begin: doctest_problem_01.py
=====================================================

"""
Illustrates a problem using *doctest* with '\0' characters.
"""


import array



def wrap ( this ) :
   """
   Returns *this* as a string suitable for transmission to an external
process.

   USAGE:
      >>> import doctest_problem_01 as message

   NOTES
      - The input parameter is expected to be a non-blank string having
a length
         in the range [1,1023].
         >>> aString = ''
         >>> result = message.wrap (aString)
         Exception raised:
         Traceback (most recent call last):
         AssertionError
         >>> aString = '012345'
         >>> result = message.wrap (aString)

      - The input parameter is not modified.
         >>> aString
         '012345'

      - The returned string is always 1024 bytes.
         >>> assert len (result) == 1024

      - The final byte, in the returned string, is always '\0'.
         >>> assert result [1023] == '\0'

      - A '\0' byte is used as the fill byte for input strings shorter
than
         1023 bytes.
         >>> for i in xrange ( len (aString), 1024 ):
         ...    assert result [i] = '\0'
         ...
         >>>
   """

   assert this
   assert len (this) in range (1, 1024)

   message_block = array.array ('c', this)
   for i in xrange ( len (this), 1024 ):
      message_block.append ('\0')

   return message_block.tostring()




def _validate_documentation_code_examples ():
   """
   Ensure the code examples in the documentation execute as expected.
   """

   import doctest
   import doctest_problem_01
   print '\n\n Validating %s documentation code examples -- no output
implies no errors ( ~2 seconds )\n' \
      % doctest_problem_01.__name__
   return doctest.testmod (doctest_problem_01)
#>    import sys
#>    return doctest.testmod (sys.modules["__main__"]) # use if there is
module-level code that must only execute once




if __name__ == "__main__":
   _validate_documentation_code_examples()

# end: doctest_problem_01.py
=====================================================





More information about the Python-list mailing list