Mercurial > cpython-lru_cache-weakref
annotate Doc/lib/libcodeop.tex @ 24042:63fbbed10714 legacy-trunk
Simplify HTTPSConnection constructor.
See discussion in SF bug 458463.
| author | Jeremy Hylton <jeremy@alum.mit.edu> |
|---|---|
| date | Fri, 28 Jun 2002 23:38:14 +0000 |
| parents | ec51e10a6229 |
| children | 0ce2722a3e15 |
| rev | line source |
|---|---|
| 11036 | 1 \section{\module{codeop} --- |
| 2 Compile Python code} | |
| 3 | |
|
12223
e82e82b3b9ad
Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents:
11036
diff
changeset
|
4 % LaTeXed from excellent doc-string. |
|
e82e82b3b9ad
Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents:
11036
diff
changeset
|
5 |
| 11036 | 6 \declaremodule{standard}{codeop} |
|
15467
d7864345b1c8
Update Moshe Zadka's email address to one works and might last a while.
Fred Drake <fdrake@acm.org>
parents:
12223
diff
changeset
|
7 \sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il} |
|
19503
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
8 \sectionauthor{Michael Hudson}{mwh@python.net} |
| 11036 | 9 \modulesynopsis{Compile (possibly incomplete) Python code.} |
| 10 | |
|
19503
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
11 The \module{codeop} module provides utilities upon which the Python |
|
19527
ec51e10a6229
Fix a number of minor markup errors, and improve the consistency a bit.
Fred Drake <fdrake@acm.org>
parents:
19503
diff
changeset
|
12 read-eval-print loop can be emulated, as is done in the |
|
ec51e10a6229
Fix a number of minor markup errors, and improve the consistency a bit.
Fred Drake <fdrake@acm.org>
parents:
19503
diff
changeset
|
13 \refmodule{code} module. As a result, you probably don't want to use |
|
ec51e10a6229
Fix a number of minor markup errors, and improve the consistency a bit.
Fred Drake <fdrake@acm.org>
parents:
19503
diff
changeset
|
14 the module directly; if you want to include such a loop in your |
|
ec51e10a6229
Fix a number of minor markup errors, and improve the consistency a bit.
Fred Drake <fdrake@acm.org>
parents:
19503
diff
changeset
|
15 program you probably want to use the \refmodule{code} module instead. |
|
19503
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
16 |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
17 There are two parts to this job: |
| 11036 | 18 |
|
19527
ec51e10a6229
Fix a number of minor markup errors, and improve the consistency a bit.
Fred Drake <fdrake@acm.org>
parents:
19503
diff
changeset
|
19 \begin{enumerate} |
|
ec51e10a6229
Fix a number of minor markup errors, and improve the consistency a bit.
Fred Drake <fdrake@acm.org>
parents:
19503
diff
changeset
|
20 \item Being able to tell if a line of input completes a Python |
|
ec51e10a6229
Fix a number of minor markup errors, and improve the consistency a bit.
Fred Drake <fdrake@acm.org>
parents:
19503
diff
changeset
|
21 statement: in short, telling whether to print |
|
ec51e10a6229
Fix a number of minor markup errors, and improve the consistency a bit.
Fred Drake <fdrake@acm.org>
parents:
19503
diff
changeset
|
22 `\code{>\code{>}>~} or `\code{...~}' next. |
|
ec51e10a6229
Fix a number of minor markup errors, and improve the consistency a bit.
Fred Drake <fdrake@acm.org>
parents:
19503
diff
changeset
|
23 \item Remembering which future statements the user has entered, so |
|
ec51e10a6229
Fix a number of minor markup errors, and improve the consistency a bit.
Fred Drake <fdrake@acm.org>
parents:
19503
diff
changeset
|
24 subsequent input can be compiled with these in effect. |
|
ec51e10a6229
Fix a number of minor markup errors, and improve the consistency a bit.
Fred Drake <fdrake@acm.org>
parents:
19503
diff
changeset
|
25 \end{enumerate} |
|
19503
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
26 |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
27 The \module{codeop} module provides a way of doing each of these |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
28 things, and a way of doing them both. |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
29 |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
30 To do just the former: |
| 11036 | 31 |
| 32 \begin{funcdesc}{compile_command} | |
| 33 {source\optional{, filename\optional{, symbol}}} | |
|
12223
e82e82b3b9ad
Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents:
11036
diff
changeset
|
34 Tries to compile \var{source}, which should be a string of Python |
|
e82e82b3b9ad
Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents:
11036
diff
changeset
|
35 code and return a code object if \var{source} is valid |
| 11036 | 36 Python code. In that case, the filename attribute of the code object |
| 37 will be \var{filename}, which defaults to \code{'<input>'}. | |
|
12223
e82e82b3b9ad
Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents:
11036
diff
changeset
|
38 Returns \code{None} if \var{source} is \emph{not} valid Python |
| 11036 | 39 code, but is a prefix of valid Python code. |
| 40 | |
|
12223
e82e82b3b9ad
Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents:
11036
diff
changeset
|
41 If there is a problem with \var{source}, an exception will be raised. |
|
e82e82b3b9ad
Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents:
11036
diff
changeset
|
42 \exception{SyntaxError} is raised if there is invalid Python syntax, |
|
19503
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
43 and \exception{OverflowError} or \exception{ValueError} if there is an |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
44 invalid literal. |
| 11036 | 45 |
|
12223
e82e82b3b9ad
Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents:
11036
diff
changeset
|
46 The \var{symbol} argument determines whether \var{source} is compiled |
|
e82e82b3b9ad
Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents:
11036
diff
changeset
|
47 as a statement (\code{'single'}, the default) or as an expression |
|
e82e82b3b9ad
Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents:
11036
diff
changeset
|
48 (\code{'eval'}). Any other value will cause \exception{ValueError} to |
|
e82e82b3b9ad
Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents:
11036
diff
changeset
|
49 be raised. |
| 11036 | 50 |
| 51 \strong{Caveat:} | |
| 52 It is possible (but not likely) that the parser stops parsing | |
| 53 with a successful outcome before reaching the end of the source; | |
| 54 in this case, trailing symbols may be ignored instead of causing an | |
| 55 error. For example, a backslash followed by two newlines may be | |
| 56 followed by arbitrary garbage. This will be fixed once the API | |
| 57 for the parser is better. | |
| 58 \end{funcdesc} | |
|
19503
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
59 |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
60 \begin{classdesc}{Compile}{} |
|
19527
ec51e10a6229
Fix a number of minor markup errors, and improve the consistency a bit.
Fred Drake <fdrake@acm.org>
parents:
19503
diff
changeset
|
61 Instances of this class have \method{__call__()} methods indentical in |
|
ec51e10a6229
Fix a number of minor markup errors, and improve the consistency a bit.
Fred Drake <fdrake@acm.org>
parents:
19503
diff
changeset
|
62 signature to the built-in function \function{compile()}, but with the |
|
19503
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
63 difference that if the instance compiles program text containing a |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
64 \module{__future__} statement, the instance 'remembers' and compiles |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
65 all subsequent program texts with the statement in force. |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
66 \end{classdesc} |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
67 |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
68 \begin{classdesc}{CommandCompiler}{} |
|
19527
ec51e10a6229
Fix a number of minor markup errors, and improve the consistency a bit.
Fred Drake <fdrake@acm.org>
parents:
19503
diff
changeset
|
69 Instances of this class have \method{__call__()} methods identical in |
|
ec51e10a6229
Fix a number of minor markup errors, and improve the consistency a bit.
Fred Drake <fdrake@acm.org>
parents:
19503
diff
changeset
|
70 signature to \function{compile_command()}; the difference is that if |
|
ec51e10a6229
Fix a number of minor markup errors, and improve the consistency a bit.
Fred Drake <fdrake@acm.org>
parents:
19503
diff
changeset
|
71 the instance compiles program text containing a \code{__future__} |
|
19503
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
72 statement, the instance 'remembers' and compiles all subsequent |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
73 program texts with the statement in force. |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
74 \end{classdesc} |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
75 |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
76 A note on version compatibility: the \class{Compile} and |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
77 \class{CommandCompiler} are new in Python 2.2. If you want to enable |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
78 the future-tracking features of 2.2 but also retain compatibility with |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
79 2.1 and earlier versions of Python you can either write |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
80 |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
81 \begin{verbatim} |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
82 try: |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
83 from codeop import CommandCompiler |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
84 compile_command = CommandCompiler() |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
85 del CommandCompiler |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
86 except ImportError: |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
87 from codeop import compile_command |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
88 \end{verbatim} |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
89 |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
90 which is a low-impact change, but introduces possibly unwanted global |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
91 state into your program, or you can write: |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
92 |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
93 \begin{verbatim} |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
94 try: |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
95 from codeop import CommandCompiler |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
96 except ImportError: |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
97 def CommandCompiler(): |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
98 from codeop import compile_command |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
99 return compile_comamnd |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
100 \end{verbatim} |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
101 |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
102 and then call \code{CommandCompiler} every time you need a fresh |
|
17b2afba7a6f
Docs for the PEP 264 changes.
Michael W. Hudson <mwh@python.net>
parents:
15467
diff
changeset
|
103 compiler object. |
