Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 53da317

Browse files
author
Michael W. Hudson
committed
Docs for the PEP 264 changes.
1 parent 71b6af9 commit 53da317

4 files changed

Lines changed: 112 additions & 22 deletions

File tree

Doc/lib/libcode.tex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ \section{\module{code} ---
6060
\var{filename}, \var{symbol})}) if the command is complete and
6161
valid; \code{None} if the command is incomplete; raises
6262
\exception{SyntaxError} if the command is complete and contains a
63-
syntax error, or raises \exception{OverflowError} if the command
64-
includes a numeric constant which exceeds the range of the
65-
appropriate numeric type.
63+
syntax error, or raises \exception{OverflowError} or
64+
\exception{ValueError} if the command cotains an invalid literal.
6665
\end{funcdesc}
6766

6867

Doc/lib/libcodeop.tex

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,29 @@ \section{\module{codeop} ---
55

66
\declaremodule{standard}{codeop}
77
\sectionauthor{Moshe Zadka}{[email protected]}
8+
\sectionauthor{Michael Hudson}{[email protected]}
89
\modulesynopsis{Compile (possibly incomplete) Python code.}
910

10-
The \module{codeop} module provides a function to compile Python code
11-
with hints on whether it is certainly complete, possibly complete or
12-
definitely incomplete. This is used by the \refmodule{code} module
13-
and should not normally be used directly.
11+
The \module{codeop} module provides utilities upon which the Python
12+
read-eval-print loop can be emulated -- as in the \refmodule{code}
13+
module. As a result, you probably don't want to use the module
14+
directly -- if you want to include such a loop in your program you
15+
probably want to use the \refmodule{code} instead.
1416

15-
The \module{codeop} module defines the following function:
17+
There are two parts to this job:
18+
19+
\begin{list}
20+
\listitem Being able to tell if a line of input completes a Python
21+
statement -- in short telling whether to print ``>>> '' or
22+
``... '' next.
23+
\listitem Remembering which future statements the user has entered, so
24+
subsequent input can be compiled wiht these in effect.
25+
\end{list}
26+
27+
The \module{codeop} module provides a way of doing each of these
28+
things, and a way of doing them both.
29+
30+
To do just the former:
1631

1732
\begin{funcdesc}{compile_command}
1833
{source\optional{, filename\optional{, symbol}}}
@@ -25,8 +40,8 @@ \section{\module{codeop} ---
2540

2641
If there is a problem with \var{source}, an exception will be raised.
2742
\exception{SyntaxError} is raised if there is invalid Python syntax,
28-
and \exception{OverflowError} if there is an invalid numeric
29-
constant.
43+
and \exception{OverflowError} or \exception{ValueError} if there is an
44+
invalid literal.
3045

3146
The \var{symbol} argument determines whether \var{source} is compiled
3247
as a statement (\code{'single'}, the default) or as an expression
@@ -41,3 +56,48 @@ \section{\module{codeop} ---
4156
followed by arbitrary garbage. This will be fixed once the API
4257
for the parser is better.
4358
\end{funcdesc}
59+
60+
\begin{classdesc}{Compile}{}
61+
Instances of this class have \method{__call__} methods indentical in
62+
signature to the built-in function \function{compile}, but with the
63+
difference that if the instance compiles program text containing a
64+
\module{__future__} statement, the instance 'remembers' and compiles
65+
all subsequent program texts with the statement in force.
66+
\end{classdesc}
67+
68+
\begin{classdesc}{CommandCompiler}{}
69+
Instances of this class have \method{__call__} methods identical in
70+
signature to \function{compile_command}; the difference is that if the
71+
instance compiles program text containing a \method{__future__}
72+
statement, the instance 'remembers' and compiles all subsequent
73+
program texts with the statement in force.
74+
\end{classdesc}
75+
76+
A note on version compatibility: the \class{Compile} and
77+
\class{CommandCompiler} are new in Python 2.2. If you want to enable
78+
the future-tracking features of 2.2 but also retain compatibility with
79+
2.1 and earlier versions of Python you can either write
80+
81+
\begin{verbatim}
82+
try:
83+
from codeop import CommandCompiler
84+
compile_command = CommandCompiler()
85+
del CommandCompiler
86+
except ImportError:
87+
from codeop import compile_command
88+
\end{verbatim}
89+
90+
which is a low-impact change, but introduces possibly unwanted global
91+
state into your program, or you can write:
92+
93+
\begin{verbatim}
94+
try:
95+
from codeop import CommandCompiler
96+
except ImportError:
97+
def CommandCompiler():
98+
from codeop import compile_command
99+
return compile_comamnd
100+
\end{verbatim}
101+
102+
and then call \code{CommandCompiler} every time you need a fresh
103+
compiler object.

Doc/lib/libfuncs.tex

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ \section{Built-in Functions \label{built-in-funcs}}
118118
operations.
119119
\end{funcdesc}
120120

121-
\begin{funcdesc}{compile}{string, filename, kind}
121+
\begin{funcdesc}{compile}{string, filename, kind\optional{,
122+
flags\optional{, dont_inherit}}}
122123
Compile the \var{string} into a code object. Code objects can be
123124
executed by an \keyword{exec} statement or evaluated by a call to
124125
\function{eval()}. The \var{filename} argument should
@@ -130,6 +131,24 @@ \section{Built-in Functions \label{built-in-funcs}}
130131
expression, or \code{'single'} if it consists of a single
131132
interactive statement (in the latter case, expression statements
132133
that evaluate to something else than \code{None} will printed).
134+
135+
The optional arguments \var{flags} and \optional{dont_inherit}
136+
(which are new in Python 2.2) control which future statements (see
137+
\pep{236}) affect the compilation of \var{string}. If neither is
138+
present (or both are zero) the code is compiled with those future
139+
statements that are in effect in the code that is calling compile.
140+
If the \var{flags} argument is given and \var{dont_inherit} is not
141+
(or is zero) then the future statements specified by the \var{flags}
142+
argument are used in addition to those that would be used anyway.
143+
If \var{dont_inherit} is a non-zero integer then the \var{flags}
144+
argument is it -- the future statements in effect around the call to
145+
compile are ignored.
146+
147+
Future statemants are specified by bits which can be bitwise or-ed
148+
together to specify multiple statements. The bitfield required to
149+
specify a given feature can be found as the \member{compiler_flag}
150+
attribute on the \class{_Feature} instance in the
151+
\module{__future__} module.
133152
\end{funcdesc}
134153

135154
\begin{funcdesc}{complex}{real\optional{, imag}}

Doc/ref/refa1.tex

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,17 @@ \section{Future statements \label{future-statements}}
4040

4141
\end{itemize}
4242

43-
The only feature recognized by Python 2.1 is \samp{nested_scopes}.
44-
45-
A future statement is recognized and treated specially at compile time:
46-
Changes to the semantics of core constructs are often implemented by
47-
generating different code. It may even be the case that a new feature
48-
introduces new incompatible syntax (such as a new reserved word), in
49-
which case the compiler may need to parse the module differently. Such
50-
decisions cannot be pushed off until runtime.
43+
The features recognized by Python 2.2 are \samp{generators},
44+
\samp{division} and \samp{nested_scopes}. \samp{nested_scopes}
45+
is redundant in 2.2 as the nested scopes feature is active by default.
46+
47+
A future statement is recognized and treated specially at compile
48+
time: Changes to the semantics of core constructs are often
49+
implemented by generating different code. It may even be the case
50+
that a new feature introduces new incompatible syntax (such as a new
51+
reserved word), in which case the compiler may need to parse the
52+
module differently. Such decisions cannot be pushed off until
53+
runtime.
5154

5255
For any given release, the compiler knows which feature names have been
5356
defined, and raises a compile-time error if a future statement contains
@@ -72,8 +75,11 @@ \section{Future statements \label{future-statements}}
7275

7376
Code compiled by an exec statement or calls to the builtin functions
7477
\function{compile()} and \function{execfile()} that occur in a module
75-
\module{M} containing a future statement will use the new syntax or
76-
semantics associated with the future statement.
78+
\module{M} containing a future statement will, by default, use the new
79+
syntax or semantics associated with the future statement. This can,
80+
starting with Python 2.2 be controlled by optional arguments to
81+
\function{compile()} --- see the documentation of that function in the
82+
library reference for details.
7783

7884
A future statement typed at an interactive interpreter prompt will
7985
take effect for the rest of the interpreter session. If an
@@ -110,7 +116,8 @@ \section{\module{__future__} ---
110116
Each statment in \file{__future__.py} is of the form:
111117

112118
\begin{verbatim}
113-
FeatureName = "_Feature(" OptionalRelease "," MandatoryRelease ")"
119+
FeatureName = "_Feature(" OptionalRelease "," MandatoryRelease ","
120+
CompilerFlag ")"
114121
\end{verbatim}
115122

116123
where, normally, OptionalRelease is less then MandatoryRelease, and
@@ -143,6 +150,11 @@ \section{\module{__future__} ---
143150
Instances of class \class{_Feature} have two corresponding methods,
144151
\method{getOptionalRelease()} and \method{getMandatoryRelease()}.
145152

153+
CompilerFlag is the (bitfield) flag that should be passed in the
154+
fourth argument to the builtin function \function{compile()} to enable
155+
the feature in dynamically compiled code. This flag is stored in the
156+
\member{compiler_flag} attribute on \class{_Future} instances.
157+
146158
No feature description will ever be deleted from \module{__future__}.
147159

148160
\section{Nested scopes \label{nested-scopes}}

0 commit comments

Comments
 (0)