Funny behaviour with __future__ and doctest between 2.6 and 3.1

Discussion in 'Python' started by Mattsteel, Jan 29, 2010.

  1. Mattsteel

    Mattsteel Guest

    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
    '''
    >>> concat('hello','world')

    '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
    '''
    >>> concat('hello','world')

    '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'

    ---------------------------------------

    Regards.

    Matt.
    Mattsteel, Jan 29, 2010
    #1
    1. Advertising

  2. Mattsteel

    Peter Otten Guest

    Mattsteel wrote:

    > 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:

    >>> from __future__ import unicode_literals
    >>> "yadda"

    u'yadda'

    > Comments are welcome.
    >
    > ---------------------------------------
    > #!/usr/bin/env python
    > '''
    > >>> concat('hello','world')

    > '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
    > '''
    > >>> concat('hello','world')

    > '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'
    >
    > ---------------------------------------
    >
    > Regards.
    >
    > Matt.
    Peter Otten, Jan 29, 2010
    #2
    1. Advertising

  3. Mattsteel

    Mattsteel Guest

    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
    '''
    >>> str(concat('hello','world'))

    '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
    '''
    >>> concat('hello','world')

    'hello world'
    '''
    from __future__ import unicode_literals
    def concat( first, second ):
    return first + ' ' + second
    if __name__ == "__main__":
    import doctest
    doctest.testmod()


    On 29 Gen, 16:50, Peter Otten <> wrote:
    > Mattsteel wrote:
    > > 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:
    >
    > >>> from __future__ import unicode_literals
    > >>> "yadda"

    >
    > u'yadda'
    >
    > > Comments are welcome.

    >
    > > ---------------------------------------
    > >     #!/usr/bin/env python
    > >     '''
    > >     >>> concat('hello','world')
    > >     '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
    > >     '''
    > >     >>> concat('hello','world')
    > >     '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'

    >
    > > ---------------------------------------

    >
    > > Regards.

    >
    > > Matt.

    >
    >
    Mattsteel, Jan 29, 2010
    #3
  4. Mattsteel

    Peter Otten Guest

    Mattsteel wrote:

    > 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
    > '''
    >>>> str(concat('hello','world'))

    > '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:

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

    True
    '''
    from __future__ import unicode_literals

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

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

    Peter
    Peter Otten, Jan 29, 2010
    #4
  5. Mattsteel

    Mattsteel Guest

    On 29 Gen, 17:30, Peter Otten <> wrote:
    > 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.
    Mattsteel, Jan 30, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron
    Replies:
    2
    Views:
    1,230
    William F. Robertson, Jr.
    Jun 25, 2003
  2. Replies:
    9
    Views:
    960
  3. André

    very puzzling doctest behaviour

    André, Dec 18, 2007, in forum: Python
    Replies:
    3
    Views:
    229
    André
    Dec 18, 2007
  4. Edvin Fuglebakk

    funny generator behaviour

    Edvin Fuglebakk, Dec 4, 2008, in forum: Python
    Replies:
    5
    Views:
    279
    Arnaud Delobelle
    Dec 4, 2008
  5. Jeremy Henty
    Replies:
    7
    Views:
    103
    Jeremy Henty
    Jun 5, 2006
Loading...

Share This Page