python / cpython

The Python programming language
https://www.python.org
Other
63.42k stars 30.37k forks source link

doctest ignores "from __future__ import print_function" #67232

Open ca14c2ea-4cac-4ea8-8092-c7154477324e opened 9 years ago

ca14c2ea-4cac-4ea8-8092-c7154477324e commented 9 years ago
BPO 23043

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['type-bug', 'library'] title = 'doctest ignores "from __future__ import print_function"' updated_at = user = 'https://bugs.python.org/fva' ``` bugs.python.org fields: ```python activity = actor = 'demian.brecht' assignee = 'none' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = creator = 'fva' dependencies = [] files = [] hgrepos = [] issue_num = 23043 keywords = [] message_count = 4.0 messages = ['232577', '233053', '233054', '233055'] nosy_count = 2.0 nosy_names = ['Julien.Palard', 'fva'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue23043' versions = ['Python 2.7'] ```

ca14c2ea-4cac-4ea8-8092-c7154477324e commented 9 years ago
>>> 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)

JulienPalard commented 9 years ago

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.
bb8bd63d-cf82-41f3-a63e-9703d695cb16 commented 9 years ago

@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.

bb8bd63d-cf82-41f3-a63e-9703d695cb16 commented 9 years ago

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.