views:

67

answers:

2

When I run doctests on different Python versions (2.5 vs 2.6) and different plattforms (FreeBSD vs Mac OS) strings get quoted differently:

Failed example:
    decode('{"created_by":"test","guid":123,"num":5.00}')
Expected:
    {'guid': 123, 'num': Decimal("5.00"), 'created_by': 'test'}
Got:
    {'guid': 123, 'num': Decimal('5.00'), 'created_by': 'test'}

So on one box repr(decimal.Decimal('5.00')) results in 'Decimal("5.00")' on the other in "Decimal('5.00')". Is there any way to get arround the issue withyout creating more compliated test logic?

+4  A: 

This is actually because the decimal module's source code has changed: In python 2.4 and python2.5 the decimal.Decimal.__repr__ function contains:

return 'Decimal("%s")' % str(self)

whereas in python2.6 it contains:

return "Decimal('%s')" % str(self)

So in this case the best thing to do is just to print out str() of the result and check the type separately if necessary...

David Fraser
Thanks for the explanation. Unfortunately I also have a lot dictionaries as return value, so just comparing str() will not work.I edited ,y question accordingly.
mdorseif
Ah, in that case, it may be best for you to look at actual unittests not just doctests... doctests get too complicated when you're dealing with complexities like this IMHO
David Fraser
A: 

Following the hits by David Fraser i found this suggestion by Raymond Hettinger on the Python mailinglist.

I now use something like this:

import sys
if sys.version_info[:2] <= (2, 5):
    # ugly monkeypatch to make doctests work. For the reasons see
    # See http://mail.python.org/pipermail/python-dev/2008-July/081420.html
    # It can go away once all our boxes run python > 2.5
    decimal.Decimal.__repr__ = lambda s: "Decimal('%s')" % str(s)
mdorseif