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: __future__ statements break doctest
Type: Stage:
Components: Library (Lib) Versions: Python 3.1
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, pooryorick
Priority: normal Keywords:

Created on 2009-06-29 02:27 by pooryorick, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg89807 - (view) Author: Poor Yorick (pooryorick) * Date: 2009-06-29 02:27
(this error also occurs with "print_function")

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit
(Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import unicode_literals
>>> import doctest
>>> def func1():
...    '''
...    >>> func1()
...    hello
...    '''
...    print('hello')
...
>>> doctest.testmod()
**********************************************************************
File "__main__", line 3, in __main__.func1
Failed example:
    func1()
Exception raised:
    Traceback (most recent call last):
      File "c:\Python31\lib\doctest.py", line 1242, in __run
        compileflags, 1), test.globs)
    ValueError: compile(): unrecognised flags
**********************************************************************
1 items had failures:
   1 of   1 in __main__.func1
***Test Failed*** 1 failures.
TestResults(failed=1, attempted=1)
msg89925 - (view) Author: Poor Yorick (pooryorick) * Date: 2009-06-30 14:18
It seems that "compile" does not recognize some flags:

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit
(Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import __future__
>>> compile('a=5', '<string>', 'single',
__future__.print_function.compiler_flag
)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: compile(): unrecognised flags
msg89929 - (view) Author: Poor Yorick (pooryorick) * Date: 2009-06-30 15:09
here is a monkey patch to work around the problem:

import __future__
import doctest
if sys.version_info.major > 2:
        _extract_future_flags_old = doctest._extract_future_flags
        def _extract_future_flags(globs):
                flags = _extract_future_flags_old(globs)
                flags = flags & ~__future__.division.compiler_flag
                flags = flags & ~__future__.absolute_import.compiler_
                flags = flags & ~__future__.nested_scopes.compiler_fl
                flags = flags & ~__future__.print_function.compiler_f
                flags = flags & ~__future__.unicode_literals.compiler
                flags = flags & ~__future__.with_statement.compiler_f
                return flags
        doctest._extract_future_flags = _extract_future_flags
msg90035 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-07-02 21:59
Fixed in r73782 and r73783.
History
Date User Action Args
2022-04-11 14:56:50adminsetgithub: 50612
2009-07-02 21:59:22benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg90035

resolution: fixed
2009-06-30 15:09:09pooryoricksetmessages: + msg89929
2009-06-30 14:18:01pooryoricksetmessages: + msg89925
2009-06-29 02:27:28pooryorickcreate