2001-08-08 11:30:13 -04:00
|
|
|
PEP: 264
|
2001-10-26 10:36:22 -04:00
|
|
|
Title: Future statements in simulated shells
|
|
|
|
Version: $Revision$
|
|
|
|
Last-Modified: $Date$
|
2001-08-08 11:30:13 -04:00
|
|
|
Author: Michael Hudson <mwh@python.net>
|
2001-10-26 10:36:22 -04:00
|
|
|
Status: Final
|
2001-08-08 11:30:13 -04:00
|
|
|
Type: Standards Track
|
2017-01-10 14:30:39 -05:00
|
|
|
Content-Type: text/x-rst
|
2001-08-08 11:30:13 -04:00
|
|
|
Requires: 236
|
|
|
|
Created: 30-Jul-2001
|
|
|
|
Python-Version: 2.2
|
|
|
|
Post-History: 30-Jul-2001
|
|
|
|
|
2001-10-26 10:36:22 -04:00
|
|
|
|
2001-08-08 11:30:13 -04:00
|
|
|
Abstract
|
2017-01-10 14:30:39 -05:00
|
|
|
========
|
2001-08-08 11:30:13 -04:00
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
As noted in :pep:`236`, there is no clear way for "simulated
|
2017-01-10 14:30:39 -05:00
|
|
|
interactive shells" to simulate the behaviour of ``__future__``
|
|
|
|
statements in "real" interactive shells, i.e. have ``__future__``
|
|
|
|
statements' effects last the life of the shell.
|
2001-08-08 11:30:13 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
The PEP also takes the opportunity to clean up the other
|
2022-01-21 06:03:51 -05:00
|
|
|
unresolved issue mentioned in :pep:`236`, the inability to stop
|
2017-01-10 14:30:39 -05:00
|
|
|
``compile()`` inheriting the effect of future statements affecting the
|
|
|
|
code calling ``compile()``.
|
2001-08-10 18:45:19 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
This PEP proposes to address the first problem by adding an
|
|
|
|
optional fourth argument to the builtin function "compile", adding
|
|
|
|
information to the ``_Feature`` instances defined in ``__future__.py`` and
|
|
|
|
adding machinery to the standard library modules "codeop" and
|
|
|
|
"code" to make the construction of such shells easy.
|
2001-08-08 11:30:13 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
The second problem is dealt with by simply adding *another*
|
|
|
|
optional argument to ``compile()``, which if non-zero suppresses the
|
|
|
|
inheriting of future statements' effects.
|
2001-08-10 18:45:19 -04:00
|
|
|
|
2001-08-08 11:30:13 -04:00
|
|
|
|
|
|
|
Specification
|
2017-01-10 14:30:39 -05:00
|
|
|
=============
|
2001-08-08 11:30:13 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
I propose adding a fourth, optional, "flags" argument to the
|
|
|
|
builtin "compile" function. If this argument is omitted,
|
|
|
|
there will be no change in behaviour from that of Python 2.1.
|
2001-08-08 11:30:13 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
If it is present it is expected to be an integer, representing
|
|
|
|
various possible compile time options as a bitfield. The
|
|
|
|
bitfields will have the same values as the ``CO_*`` flags already used
|
|
|
|
by the C part of Python interpreter to refer to future statements.
|
2001-08-10 18:45:19 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
``compile()`` shall raise a ``ValueError`` exception if it does not
|
|
|
|
recognize any of the bits set in the supplied flags.
|
2001-08-10 18:45:19 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
The flags supplied will be bitwise-"or"ed with the flags that
|
|
|
|
would be set anyway, unless the new fifth optional argument is a
|
2017-04-05 12:14:26 -04:00
|
|
|
non-zero integer, in which case the flags supplied will be exactly
|
2017-01-10 14:30:39 -05:00
|
|
|
the set used.
|
2001-08-10 18:45:19 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
The above-mentioned flags are not currently exposed to Python. I
|
|
|
|
propose adding ``.compiler_flag`` attributes to the ``_Feature`` objects
|
|
|
|
in ``__future__.py`` that contain the necessary bits, so one might
|
|
|
|
write code such as::
|
2001-08-08 11:30:13 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
import __future__
|
|
|
|
def compile_generator(func_def):
|
|
|
|
return compile(func_def, "<input>", "suite",
|
|
|
|
__future__.generators.compiler_flag)
|
2001-08-10 18:45:19 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
A recent change means that these same bits can be used to tell if
|
2017-04-05 12:14:26 -04:00
|
|
|
a code object was compiled with a given feature; for instance ::
|
2001-08-10 18:45:19 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
codeob.co_flags & __future__.generators.compiler_flag``
|
2001-08-10 18:45:19 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
will be non-zero if and only if the code object "codeob" was
|
|
|
|
compiled in an environment where generators were allowed.
|
2001-08-10 18:45:19 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
I will also add a ``.all_feature_flags`` attribute to the ``__future__``
|
|
|
|
module, giving a low-effort way of enumerating all the ``__future__``
|
|
|
|
options supported by the running interpreter.
|
2001-08-08 11:30:13 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
I also propose adding a pair of classes to the standard library
|
|
|
|
module codeop.
|
2001-08-08 11:30:13 -04:00
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
One - ``Compile`` - will sport a ``__call__`` method which will act much
|
2017-01-10 14:30:39 -05:00
|
|
|
like the builtin "compile" of 2.1 with the difference that after
|
|
|
|
it has compiled a ``__future__`` statement, it "remembers" it and
|
|
|
|
compiles all subsequent code with the ``__future__`` option in effect.
|
2001-08-08 11:30:13 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
It will do this by using the new features of the ``__future__`` module
|
|
|
|
mentioned above.
|
2001-08-08 11:30:13 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
Objects of the other class added to codeop - ``CommandCompiler`` -
|
|
|
|
will do the job of the existing ``codeop.compile_command`` function,
|
|
|
|
but in a ``__future__``-aware way.
|
2001-08-08 11:30:13 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
Finally, I propose to modify the class ``InteractiveInterpreter`` in
|
|
|
|
the standard library module code to use a ``CommandCompiler`` to
|
|
|
|
emulate still more closely the behaviour of the default Python
|
|
|
|
shell.
|
2001-08-08 11:30:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
Backward Compatibility
|
2017-01-10 14:30:39 -05:00
|
|
|
======================
|
2001-08-08 11:30:13 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
Should be very few or none; the changes to compile will make no
|
|
|
|
difference to existing code, nor will adding new functions or
|
|
|
|
classes to codeop. Existing code using
|
|
|
|
``code.InteractiveInterpreter`` may change in behaviour, but only for
|
|
|
|
the better in that the "real" Python shell will be being better
|
|
|
|
impersonated.
|
2001-08-08 11:30:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
Forward Compatibility
|
2017-01-10 14:30:39 -05:00
|
|
|
=====================
|
2001-08-08 11:30:13 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
The fiddling that needs to be done to ``Lib/__future__.py`` when
|
|
|
|
adding a ``__future__`` feature will be a touch more complicated.
|
|
|
|
Everything else should just work.
|
2001-08-08 11:30:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
Issues
|
2017-01-10 14:30:39 -05:00
|
|
|
======
|
2001-08-08 11:30:13 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
I hope the above interface is not too disruptive to implement for
|
|
|
|
Jython.
|
2001-08-08 11:30:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
Implementation
|
2017-01-10 14:30:39 -05:00
|
|
|
==============
|
2001-08-08 11:30:13 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
A series of preliminary implementations are at [1]_.
|
2001-08-08 11:30:13 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
After light massaging by Tim Peters, they have now been checked in.
|
2001-08-08 11:30:13 -04:00
|
|
|
|
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
References
|
|
|
|
==========
|
|
|
|
|
|
|
|
.. [1] http://sourceforge.net/tracker/?func=detail&atid=305470&aid=449043&group_id=5470
|
2001-08-08 11:30:13 -04:00
|
|
|
|
|
|
|
Copyright
|
2017-01-10 14:30:39 -05:00
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|
2001-08-08 11:30:13 -04:00
|
|
|
|
|
|
|
|
2001-10-26 10:36:22 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
..
|
|
|
|
Local Variables:
|
|
|
|
mode: indented-text
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
End:
|