Funny behaviour with __future__ and doctest between 2.6 and 3.1

M

Mattsteel

Hello all.
I'm using Python 2.6.4 and Python 3.1.1.
My wish is to code in a 3.1-compliant way using 2.6, so I'm importing
the __future__ module.
I've found a funny thing comparing the two folliwing snippets that
differ for one line only, that is the position of __future__ import
(before or after the doc string).

Well, I understand the subtle difference but still I wander what
really happen behind the scenes.
Comments are welcome.

---------------------------------------
#!/usr/bin/env python
''' 'hello world'
'''
from __future__ import unicode_literals
def concat( first, second ):
return first + ' ' + second
if __name__ == "__main__":
import doctest
doctest.testmod()
---------------------------------------
#!/usr/bin/env python
from __future__ import unicode_literals
''' 'hello world'
'''
def concat( first, second ):
return first + ' ' + second
if __name__ == "__main__":
import doctest
doctest.testmod()
---------------------------------------


The first way shows the following failure:

---------------------------------------
Failed example:
concat('hello','world')
Expected:
'hello world'
Got:
u'hello world'
 
P

Peter Otten

Mattsteel said:
Hello all.
I'm using Python 2.6.4 and Python 3.1.1.
My wish is to code in a 3.1-compliant way using 2.6, so I'm importing
the __future__ module.
I've found a funny thing comparing the two folliwing snippets that
differ for one line only, that is the position of __future__ import
(before or after the doc string).

Well, I understand the subtle difference but still I wander what
really happen behind the scenes.

Are you sure? The second script has no module docstring, just a string
literal somewhere in the module, and therefore no tests. You can see that by
running it with the verbose option -v. Also,

from __future__ import unicode_literals

doesn't affect the repr() of a unicode instance. But the interactive
interpreter invokes repr() on the result before it is printed:
 
M

Mattsteel

Hi Peter.
Sadly (for me), you're right... then the only way to use doctest to
work both in 2.6 and 3.1 (without modifications between them) is
something like this:

#!/usr/bin/env python
''''hello world'
'''
from __future__ import unicode_literals
def concat( first, second ):
return first + ' ' + second
if __name__ == "__main__":
import doctest
doctest.testmod()

Is there any way to avoid using str(...) to protect the string?
M.


---------------------------------------
#!/usr/bin/env python
''' 'hello world'
'''
from __future__ import unicode_literals
def concat( first, second ):
return first + ' ' + second
if __name__ == "__main__":
import doctest
doctest.testmod()
 
P

Peter Otten

Mattsteel said:
Sadly (for me), you're right... then the only way to use doctest to
work both in 2.6 and 3.1 (without modifications between them) is
something like this:

#!/usr/bin/env python
'''
'hello world'
'''
from __future__ import unicode_literals
def concat( first, second ):
return first + ' ' + second
if __name__ == "__main__":
import doctest
doctest.testmod()

Is there any way to avoid using str(...) to protect the string?
M.

I think you can work around the problem. The following should pass in Python
2.6 and 3.1:

'''True
'''
from __future__ import unicode_literals

def concat( first, second ):
return first + ' ' + second

if __name__ == "__main__":
import doctest
doctest.testmod()

Peter
 
M

Mattsteel

I think you can work around the problem. The following should pass in Python
2.6 and 3.1:

'''>>> concat('hello','world') == 'hello world'
True
'''

I see. Thank for the concern.
M.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top