[Tutor] A slight bug in IDLE
eryksun
eryksun at gmail.com
Sat Jul 13 14:43:45 CEST 2013
On Sat, Jul 13, 2013 at 4:39 AM, Jim Mooney <cybervigilante at gmail.com> wrote:
> On 13 July 2013 00:54, eryksun <eryksun at gmail.com> wrote:
>
>>
>> A __future__ import modifies compilation of the current module.
>
>
> Hmm, so if I import a module that uses truncated division, that's what I
> get, even though I imported __future__ division. OTOH, a non-future import
> will be used by a module imported after it. That's a gotcha to avoid ;')
You wouldn't want to force true division on a module written for
classic division -- or any other __future__ feature for that matter.
FWIW, here's a simple overview of what's happening with IDLE. It's
using a subclass of code.InteractiveInterpreter:
import code
interp = code.InteractiveInterpreter()
interp.runsource('from __future__ import division')
This saves the flags corresponding to __future__ imports in a
codeop.Compiler instance:
>>> ok = interp.runsource('print 5 / 3')
1.66666666667
>>> interp.compile.compiler.flags & CO_FUTURE_DIVISION
8192
However, "Run Module" just uses a vanilla compile() call. See the
checksyntax() and run_module_event() methods in ScriptBinding.py [1]:
run_module_event could update the flags before returning, like so:
from codeop import _features
compiler = interp.compile.compiler
for feature in _features:
if code.co_flags & feature.compiler_flag:
compiler.flags |= feature.compiler_flag
This would approximate running a script with -i (inspect) from the
command line, which drops into the interactive loop using the current
compiler flags (cf) [2].
[1] http://hg.python.org/cpython/file/2.7/Lib/idlelib/ScriptBinding.py
[2] http://hg.python.org/cpython/file/ab05e7dd2788/Modules/main.c#l648
More information about the Tutor
mailing list