473,702 Members | 2,381 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

doctest problem with null byte

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\\\\\\\\de f"'
>>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 <st****@bmsi.co m>
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.

Jan 26 '07 #1
1 2268
[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\\\\\\\\de f"'
>>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
"""
Jan 26 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2616
by: Pierre Rouleau | last post by:
I have a problem writing self-testable modules using doctest when these modules have internationalized strings using gettext _('...'). - The main module of an application (say app.py) calls gettext.install() to install the special _ function inside Python builtin. Other modules, taken from a general purpose collection of Python modules, also support internationalisation and doctest testing. For example:
2
1414
by: Alan G Isaac | last post by:
> python doctest.py -v Running doctest.__doc__ Trying: .remove(42) Expecting: Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: list.remove(x): x not in list ok Trying: x = 12 Expecting: nothing
2
1676
by: Michele Simionato | last post by:
Some time ago I hacked a custom solution to run doctests on text files containing documentation. The solution involved this kind of game: tester=doctest.Tester(globs={},verbose=1) tester.runstring(mytest) It worked fine, but now with Python 2.4.a3 I get DeprecationWarning: class Tester is deprecated; use class
5
1800
by: Michele Simionato | last post by:
I am getting a strange error with this script: $ cat doctest-threads.py """ >>> import time, threading >>> def example(): .... thread.out = .... while thread.running: .... time.sleep(.01) .... thread.out.append(".")
1
1247
by: Runsun Pan | last post by:
I intend to use the doctect heavily. For this I am thinking of coding a class that comes with a built-in doctest functionality. I'd like to seek for input before I start. The idea is to have a class MyObj from where all my other classes will subclass. lets say: class C(MyObj):
0
1063
by: Leo | last post by:
Could someone please spare a .i file with an example doctest that works? I tried as follows and it fails: 1) I have an osi.i file that generates osi.py and _osi.so. At the very end I added: .... %pythoncode %{ def _test(): import doctest doctest.testfile('testOsi.txt')
0
1123
by: Eric Mahurin | last post by:
Noob here. Just got into python a little over a week ago... One of the (unique?) things I really like about python is the concept of doctesting. But, now I want more! Here's what I'd like to see: * easy debugging. As soon as there is a failure (unexpected exception or mismatch), drop down into the debugger in a way to isolates the bug down to the nearest test/example as much as possible. * integrated with code coverage. I'd like...
12
2116
by: thomas.guest | last post by:
I'm not making progress with the following and would appreciate any help. Here's an interpreted Python session. .... Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'f' is not defined
6
2486
by: Bzyczek | last post by:
Hello, I have problems with running doctests if I use czech national characters in UTF-8 encoding. I have Python script, which begin with encoding definition: # -*- coding: utf-8 -*- I have this function with doctest:
0
8739
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9234
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9089
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8983
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6575
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5907
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2036
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.