Discussion:
Porting and doctests
Lennart Regebro
2009-01-05 13:27:13 UTC
Permalink
I've encountered two problems concerning doctests, which I nowadays
therefore like even less than before. ;-)

Problem number one:

When raising exceptions under 2.x, the last line of the error message
will look something like this:

ExceptionClass: The message

But under 3.0 it will look thusly:

module.ExceptionClass: The message

This means that the typical doctest pattern of:

Traceback (most recent call last):
...
ExceptionClass: The message

will fail. And I haven't been able to make a pattern that will work.
Sticking ... in front of ExceptionClass doesn't. What is the best way
to get around this? I could to a try/except in the doc test code and
raise an assertion error of the exception is not raised, but that's
UGLY.


Problem number two:

When porting iCalendar, the UTF-8 encoded data that should be returned
for interchange will be a string in Python2, but bytes in Python3.
Hence,
foo()
"bar"
foo()
b"bar"

I haven't found any reasonable way around that, making support for 2.5
and 3.0 from the same code base (even with 2to3) impossible.
Any ideas?
--
Lennart Regebro: Zope and Plone consulting.
http://www.colliberty.com/
+33 661 58 14 64
Daniel (ajax) Diniz
2009-01-05 17:41:19 UTC
Permalink
(forgot to Cc the list, and tested with 3.0)

Hi Lennart :)
Post by Lennart Regebro
When raising exceptions under 2.x, the last line of the error message
ExceptionClass: The message
module.ExceptionClass: The message
I've used "from module import ExceptionClass" in a similar case (for
matching in a except clause, perhaps?).
Post by Lennart Regebro
When porting iCalendar, the UTF-8 encoded data that should be returned
for interchange will be a string in Python2, but bytes in Python3.
Hence,
foo()
"bar"
foo()
b"bar"
I don't have 3.0 installed in this machine yet, but I believe
foo().encode('utf-8') should work. -> Doesn't work :)

But str(foo().decode('utf-8')) seems to work.

HTH,
Daniel
Lennart Regebro
2009-01-05 18:27:54 UTC
Permalink
Post by Daniel (ajax) Diniz
Hi Lennart :)
Post by Lennart Regebro
When raising exceptions under 2.x, the last line of the error message
ExceptionClass: The message
module.ExceptionClass: The message
I've used "from module import ExceptionClass" in a similar case (for
matching in a except clause, perhaps?).
Post by Lennart Regebro
When porting iCalendar, the UTF-8 encoded data that should be returned
for interchange will be a string in Python2, but bytes in Python3.
Hence,
foo()
"bar"
foo()
b"bar"
I don't have 3.0 installed in this machine yet, but I believe
foo().encode('utf-8') should work.
I seem to have explained my problems badly, as none of this answers
the questions.
My problem is that I can't write doctests that will run both under
Python 2 and Python3, because of the above mentioned changes in
behaviour, and I wonder if somebody has any hints on how to help with
that.
--
Lennart Regebro: Zope and Plone consulting.
http://www.colliberty.com/
+33 661 58 14 64
Loading...