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: IDLE's F5 Run Module doesn't transfer effects of future import
Type: behavior Stage: needs patch
Components: IDLE Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: rhettinger, terry.reedy
Priority: normal Keywords:

Created on 2014-12-17 02:30 by rhettinger, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
tmp_future.py rhettinger, 2014-12-17 02:30 Sample script
Messages (4)
msg232794 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-12-17 02:30
Future imports in a module aren't transferring to the interactive shell when executing Run Module.

Given the file tmp_future.py
----------------------------
$ cat tmp_future.py
from __future__ import division, print_function

print(32 / 5)

Run Python interactively
------------------------
$ python2.7 -i tmp_future.py 
6.4
>>> 32 / 5          # this works fine!
6.4

But open the file in IDLE and press F5:
---------------------------------------
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
6.4
>>> print(32 / 5)       # this is broken!
6

Note, the problem may also apply to IDLE Python 3.4 and 3.5 but it won't affect this example because the future imports have not effect.
msg232950 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-12-19 23:48
I agree that this is broken.  I verified that the following works in 2.7.9

>>> from __future__ import division, print_function
>>> 32 / 5
6.4
>>> print(2,3,sep='**')
2**3

#13296 was about self.compile.compiler.flags (in ModifiedInterpreter, which inherits .compile... from code.InteractiveInterpreter) not being *reset* to their original values upon Shell restart.  They are now.

This issue is about the same flags not being *set* by Run Module.  This will be tricky to fix, but I think it can be done using the previous flag issue as a hint.

self.compile (in InteractiveInterpreter) = codeop.CommandCompiler().
The docstring for the class says
    """Instances of this class have __call__ methods identical in
    signature to compile_command; the difference is that if the
    instance compiles program text containing a __future__ statement,
    the instance 'remembers' and compiles all subsequent program texts
    with the statement in force."""
The __call__ method handles partial or complete single statements.

Run -> Run Module F5, on the other hand,  is handled by the ScriptBindind.py extension.  ScriptBinding.run_module_event calls .checksyntax().  The latter calls builtin compile(), independent of the shell interpreter.compile, without saving the compile flags.

To save the compile flags, we could use codeop.Compile.__call__, which calls builtin compile() and then extracts and saves future compile flags from the resulting code object using constants defined in the module.

The next issue is timing.  The shell restarts (and resets its flags) only after the file passes compile and tabnanny steps.  After the restart, it should be possible to update the shell compile flags with any future flags saved by the Compile() instance.

I have skipped over some details (including the lack of tests for Run Module), but I *think* that the above outlines a safe solution.  As for 3.x, there are changes in 3.4 ScriptBinding.py, but the essential points for this issue seem the same.  3.x should be patched also to be ready for future __future__ imports.
msg296683 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-06-23 07:06
I am no longer patching 2.7 but there is one __future__ import active in 3.6: generator_stop.  If I remember right, there is code that would work without future import and fail with it.  In 3.7, it would always fail.
msg370918 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-06-07 22:31
The only currently active __future__ import is 'annotations'.
https://docs.python.org/3/reference/compound_stmts.html#index-23
The is currently scheduled to be needed indefinitely (until '4.0').
History
Date User Action Args
2022-04-11 14:58:11adminsetgithub: 67258
2020-06-07 22:31:02terry.reedysetmessages: + msg370918
versions: + Python 3.10, - Python 3.6, Python 3.7
2017-06-23 07:06:07terry.reedysetassignee: terry.reedy
messages: + msg296683
versions: + Python 3.6, Python 3.7, - Python 2.7, Python 3.4, Python 3.5
2014-12-19 23:48:56terry.reedysetnosy: + terry.reedy

messages: + msg232950
versions: + Python 3.4, Python 3.5
2014-12-17 02:30:30rhettingercreate