Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="module-pprint.html" /> | |
13 | <link rel="prev" href="module-code.html" /> | |
14 | <link rel="parent" href="python.html" /> | |
15 | <link rel="next" href="module-pprint.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>3.26 codeop -- Compile Python code</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="3.25.2 Interactive Console Objects" | |
25 | href="console-objects.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" | |
28 | href="python.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="3.27 pprint " | |
31 | href="module-pprint.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="console-objects.html">3.25.2 Interactive Console Objects</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="module-pprint.html">3.27 pprint </A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION0052600000000000000000"> | |
56 | 3.26 <tt class="module">codeop</tt> -- | |
57 | Compile Python code</A> | |
58 | </H1> | |
59 | ||
60 | <P> | |
61 | <A NAME="module-codeop"></A> | |
62 | ||
63 | <P> | |
64 | The <tt class="module">codeop</tt> module provides utilities upon which the Python | |
65 | read-eval-print loop can be emulated, as is done in the | |
66 | <tt class="module"><a href="module-code.html">code</a></tt> module. As a result, you probably don't want to use | |
67 | the module directly; if you want to include such a loop in your | |
68 | program you probably want to use the <tt class="module"><a href="module-code.html">code</a></tt> module instead. | |
69 | ||
70 | <P> | |
71 | There are two parts to this job: | |
72 | ||
73 | <P> | |
74 | ||
75 | <OL> | |
76 | <LI>Being able to tell if a line of input completes a Python | |
77 | statement: in short, telling whether to print | |
78 | `<code>><code>></code>> </code>' or `<code>... </code>' next. | |
79 | </LI> | |
80 | <LI>Remembering which future statements the user has entered, so | |
81 | subsequent input can be compiled with these in effect. | |
82 | </LI> | |
83 | </OL> | |
84 | ||
85 | <P> | |
86 | The <tt class="module">codeop</tt> module provides a way of doing each of these | |
87 | things, and a way of doing them both. | |
88 | ||
89 | <P> | |
90 | To do just the former: | |
91 | ||
92 | <P> | |
93 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
94 | <td><nobr><b><tt id='l2h-758' xml:id='l2h-758' class="function">compile_command</tt></b>(</nobr></td> | |
95 | <td><var>source</var><big>[</big><var>, filename</var><big>[</big><var>, symbol</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
96 | <dd> | |
97 | Tries to compile <var>source</var>, which should be a string of Python | |
98 | code and return a code object if <var>source</var> is valid | |
99 | Python code. In that case, the filename attribute of the code object | |
100 | will be <var>filename</var>, which defaults to <code>'<input>'</code>. | |
101 | Returns <code>None</code> if <var>source</var> is <em>not</em> valid Python | |
102 | code, but is a prefix of valid Python code. | |
103 | ||
104 | <P> | |
105 | If there is a problem with <var>source</var>, an exception will be raised. | |
106 | <tt class="exception">SyntaxError</tt> is raised if there is invalid Python syntax, | |
107 | and <tt class="exception">OverflowError</tt> or <tt class="exception">ValueError</tt> if there is an | |
108 | invalid literal. | |
109 | ||
110 | <P> | |
111 | The <var>symbol</var> argument determines whether <var>source</var> is compiled | |
112 | as a statement (<code>'single'</code>, the default) or as an expression | |
113 | (<code>'eval'</code>). Any other value will cause <tt class="exception">ValueError</tt> to | |
114 | be raised. | |
115 | ||
116 | <P> | |
117 | <strong>Caveat:</strong> | |
118 | It is possible (but not likely) that the parser stops parsing | |
119 | with a successful outcome before reaching the end of the source; | |
120 | in this case, trailing symbols may be ignored instead of causing an | |
121 | error. For example, a backslash followed by two newlines may be | |
122 | followed by arbitrary garbage. This will be fixed once the API | |
123 | for the parser is better. | |
124 | </dl> | |
125 | ||
126 | <P> | |
127 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
128 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-759' xml:id='l2h-759' class="class">Compile</tt></b>(</nobr></td> | |
129 | <td><var></var>)</td></tr></table></dt> | |
130 | <dd> | |
131 | Instances of this class have <tt class="method">__call__()</tt> methods identical in | |
132 | signature to the built-in function <tt class="function">compile()</tt>, but with the | |
133 | difference that if the instance compiles program text containing a | |
134 | <tt class="module">__future__</tt> statement, the instance 'remembers' and compiles | |
135 | all subsequent program texts with the statement in force. | |
136 | </dl> | |
137 | ||
138 | <P> | |
139 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
140 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-760' xml:id='l2h-760' class="class">CommandCompiler</tt></b>(</nobr></td> | |
141 | <td><var></var>)</td></tr></table></dt> | |
142 | <dd> | |
143 | Instances of this class have <tt class="method">__call__()</tt> methods identical in | |
144 | signature to <tt class="function">compile_command()</tt>; the difference is that if | |
145 | the instance compiles program text containing a <code>__future__</code> | |
146 | statement, the instance 'remembers' and compiles all subsequent | |
147 | program texts with the statement in force. | |
148 | </dl> | |
149 | ||
150 | <P> | |
151 | A note on version compatibility: the <tt class="class">Compile</tt> and | |
152 | <tt class="class">CommandCompiler</tt> are new in Python 2.2. If you want to enable | |
153 | the future-tracking features of 2.2 but also retain compatibility with | |
154 | 2.1 and earlier versions of Python you can either write | |
155 | ||
156 | <P> | |
157 | <div class="verbatim"><pre> | |
158 | try: | |
159 | from codeop import CommandCompiler | |
160 | compile_command = CommandCompiler() | |
161 | del CommandCompiler | |
162 | except ImportError: | |
163 | from codeop import compile_command | |
164 | </pre></div> | |
165 | ||
166 | <P> | |
167 | which is a low-impact change, but introduces possibly unwanted global | |
168 | state into your program, or you can write: | |
169 | ||
170 | <P> | |
171 | <div class="verbatim"><pre> | |
172 | try: | |
173 | from codeop import CommandCompiler | |
174 | except ImportError: | |
175 | def CommandCompiler(): | |
176 | from codeop import compile_command | |
177 | return compile_command | |
178 | </pre></div> | |
179 | ||
180 | <P> | |
181 | and then call <code>CommandCompiler</code> every time you need a fresh | |
182 | compiler object. | |
183 | ||
184 | <DIV CLASS="navigation"> | |
185 | <div class='online-navigation'> | |
186 | <p></p><hr /> | |
187 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
188 | <tr> | |
189 | <td class='online-navigation'><a rel="prev" title="3.25.2 Interactive Console Objects" | |
190 | href="console-objects.html"><img src='../icons/previous.png' | |
191 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
192 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" | |
193 | href="python.html"><img src='../icons/up.png' | |
194 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
195 | <td class='online-navigation'><a rel="next" title="3.27 pprint " | |
196 | href="module-pprint.html"><img src='../icons/next.png' | |
197 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
198 | <td align="center" width="100%">Python Library Reference</td> | |
199 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
200 | href="contents.html"><img src='../icons/contents.png' | |
201 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
202 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
203 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
204 | <td class='online-navigation'><a rel="index" title="Index" | |
205 | href="genindex.html"><img src='../icons/index.png' | |
206 | border='0' height='32' alt='Index' width='32' /></A></td> | |
207 | </tr></table> | |
208 | <div class='online-navigation'> | |
209 | <b class="navlabel">Previous:</b> | |
210 | <a class="sectref" rel="prev" href="console-objects.html">3.25.2 Interactive Console Objects</A> | |
211 | <b class="navlabel">Up:</b> | |
212 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> | |
213 | <b class="navlabel">Next:</b> | |
214 | <a class="sectref" rel="next" href="module-pprint.html">3.27 pprint </A> | |
215 | </div> | |
216 | </div> | |
217 | <hr /> | |
218 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
219 | </DIV> | |
220 | <!--End of Navigation Panel--> | |
221 | <ADDRESS> | |
222 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
223 | </ADDRESS> | |
224 | </BODY> | |
225 | </HTML> |