This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: doctest ignores "from __future__ import print_function"
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Julien.Palard, fva
Priority: normal Keywords:

Created on 2014-12-12 21:28 by fva, last changed 2022-04-11 14:58 by admin.

Messages (4)
msg232577 - (view) Author: Vjacheslav (fva) Date: 2014-12-12 21:28
>>> from __future__ import print_function
>>> print (1,2)
1 2

in interactive session, but, with this 3 lines in tmp.txt:

python -m doctest tmp.txt 

fails (prints tuple)
msg233053 - (view) Author: Julien Palard (Julien.Palard) Date: 2014-12-23 17:25
Works for me in 2.7.8:

$ python --version
Python 2.7.8
# cat /tmp/test.py
#!/usr/bin/env python

from __future__ import print_function


def toto():
    """
    >>> print (42, 43)
    42 43
    """
    return 42
$ python -m doctest -v /tmp/test.py 
Trying:
    print (42, 43)
Expecting:
    42 43
ok
1 items had no tests:
    test
1 items passed all tests:
   1 tests in test.toto
1 tests in 2 items.
1 passed and 0 failed.
Test passed.
msg233054 - (view) Author: Demian Brecht (demian.brecht) * (Python triager) Date: 2014-12-23 17:59
@Julien.Palard: There's a subtle difference between your test and the issue as written. Your test lives within a module and therefore executes testmodule (see https://hg.python.org/cpython/file/9f60d024e586/Lib/doctest.py#l1819) whereas the issue reported uses testfile (see https://hg.python.org/cpython/file/9f60d024e586/Lib/doctest.py#l1923). I believe the issue is that the __future__ import doesn't make it into compile (https://hg.python.org/cpython/file/9f60d024e586/Lib/doctest.py#l1314). I've been able to confirm the issue on 2.7 and that it's been resolved in 3.5. Unfortunately, I haven't had time to dig into this any further.
msg233055 - (view) Author: Demian Brecht (demian.brecht) * (Python triager) Date: 2014-12-23 18:03
> it's been resolved in 3.5

Sorry, that statement can be a little misleading, possibly indicating that something may have changed in the doctest globals handling. It was resolved in 3.5 because print is no longer a statement so this ambiguous behaviour resolved by the print_function import no longer exists.
History
Date User Action Args
2022-04-11 14:58:11adminsetgithub: 67232
2015-02-13 01:26:02demian.brechtsetnosy: - demian.brecht
2014-12-23 18:03:08demian.brechtsetmessages: + msg233055
2014-12-23 17:59:16demian.brechtsetmessages: + msg233054
2014-12-23 17:25:06Julien.Palardsetnosy: + Julien.Palard
messages: + msg233053
2014-12-13 05:08:42demian.brechtsetnosy: + demian.brecht
2014-12-12 21:28:18fvacreate