1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>30.2. codeop — Compile Python code — Python 3.5.2 documentation</title>
11
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 3.5.2 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 3.5.2 documentation" href="../contents.html" />
33
<link rel="up" title="30. Custom Python Interpreters" href="custominterp.html" />
34
<link rel="next" title="31. Importing Modules" href="modules.html" />
35
<link rel="prev" title="30.1. code — Interpreter base classes" href="code.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
38
<script type="text/javascript" src="../_static/copybutton.js"></script>
39
<script type="text/javascript" src="../_static/version_switch.js"></script>
45
<body role="document">
46
<div class="related" role="navigation" aria-label="related navigation">
49
<li class="right" style="margin-right: 10px">
50
<a href="../genindex.html" title="General Index"
51
accesskey="I">index</a></li>
53
<a href="../py-modindex.html" title="Python Module Index"
56
<a href="modules.html" title="31. Importing Modules"
57
accesskey="N">next</a> |</li>
59
<a href="code.html" title="30.1. code — Interpreter base classes"
60
accesskey="P">previous</a> |</li>
61
<li><img src="../_static/py.png" alt=""
62
style="vertical-align: middle; margin-top: -1px"/></li>
63
<li><a href="https://www.python.org/">Python</a> »</li>
65
<span class="version_switcher_placeholder">3.5.2</span>
66
<a href="../index.html">Documentation </a> »
69
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
70
<li class="nav-item nav-item-2"><a href="custominterp.html" accesskey="U">30. Custom Python Interpreters</a> »</li>
74
<div class="inline-search" style="display: none" role="search">
75
<form class="inline-search" action="../search.html" method="get">
76
<input placeholder="Quick search" type="text" name="q" />
77
<input type="submit" value="Go" />
78
<input type="hidden" name="check_keywords" value="yes" />
79
<input type="hidden" name="area" value="default" />
82
<script type="text/javascript">$('.inline-search').show(0);</script>
89
<div class="document">
90
<div class="documentwrapper">
91
<div class="bodywrapper">
92
<div class="body" role="main">
94
<div class="section" id="module-codeop">
95
<span id="codeop-compile-python-code"></span><h1>30.2. <a class="reference internal" href="#module-codeop" title="codeop: Compile (possibly incomplete) Python code."><code class="xref py py-mod docutils literal"><span class="pre">codeop</span></code></a> — Compile Python code<a class="headerlink" href="#module-codeop" title="Permalink to this headline">¶</a></h1>
96
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/codeop.py">Lib/codeop.py</a></p>
97
<hr class="docutils" />
98
<p>The <a class="reference internal" href="#module-codeop" title="codeop: Compile (possibly incomplete) Python code."><code class="xref py py-mod docutils literal"><span class="pre">codeop</span></code></a> module provides utilities upon which the Python
99
read-eval-print loop can be emulated, as is done in the <a class="reference internal" href="code.html#module-code" title="code: Facilities to implement read-eval-print loops."><code class="xref py py-mod docutils literal"><span class="pre">code</span></code></a> module. As
100
a result, you probably don’t want to use the module directly; if you want to
101
include such a loop in your program you probably want to use the <a class="reference internal" href="code.html#module-code" title="code: Facilities to implement read-eval-print loops."><code class="xref py py-mod docutils literal"><span class="pre">code</span></code></a>
103
<p>There are two parts to this job:</p>
104
<ol class="arabic simple">
105
<li>Being able to tell if a line of input completes a Python statement: in
106
short, telling whether to print ‘<code class="docutils literal"><span class="pre">>>></span></code>‘ or ‘<code class="docutils literal"><span class="pre">...</span></code>‘ next.</li>
107
<li>Remembering which future statements the user has entered, so subsequent
108
input can be compiled with these in effect.</li>
110
<p>The <a class="reference internal" href="#module-codeop" title="codeop: Compile (possibly incomplete) Python code."><code class="xref py py-mod docutils literal"><span class="pre">codeop</span></code></a> module provides a way of doing each of these things, and a way
111
of doing them both.</p>
112
<p>To do just the former:</p>
113
<dl class="function">
114
<dt id="codeop.compile_command">
115
<code class="descclassname">codeop.</code><code class="descname">compile_command</code><span class="sig-paren">(</span><em>source</em>, <em>filename="<input>"</em>, <em>symbol="single"</em><span class="sig-paren">)</span><a class="headerlink" href="#codeop.compile_command" title="Permalink to this definition">¶</a></dt>
116
<dd><p>Tries to compile <em>source</em>, which should be a string of Python code and return a
117
code object if <em>source</em> is valid Python code. In that case, the filename
118
attribute of the code object will be <em>filename</em>, which defaults to
119
<code class="docutils literal"><span class="pre">'<input>'</span></code>. Returns <code class="docutils literal"><span class="pre">None</span></code> if <em>source</em> is <em>not</em> valid Python code, but is a
120
prefix of valid Python code.</p>
121
<p>If there is a problem with <em>source</em>, an exception will be raised.
122
<a class="reference internal" href="exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal"><span class="pre">SyntaxError</span></code></a> is raised if there is invalid Python syntax, and
123
<a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></code></a> or <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> if there is an invalid literal.</p>
124
<p>The <em>symbol</em> argument determines whether <em>source</em> is compiled as a statement
125
(<code class="docutils literal"><span class="pre">'single'</span></code>, the default) or as an <a class="reference internal" href="../glossary.html#term-expression"><span class="xref std std-term">expression</span></a> (<code class="docutils literal"><span class="pre">'eval'</span></code>). Any
126
other value will cause <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> to be raised.</p>
127
<div class="admonition note">
128
<p class="first admonition-title">Note</p>
129
<p class="last">It is possible (but not likely) that the parser stops parsing with a
130
successful outcome before reaching the end of the source; in this case,
131
trailing symbols may be ignored instead of causing an error. For example,
132
a backslash followed by two newlines may be followed by arbitrary garbage.
133
This will be fixed once the API for the parser is better.</p>
138
<dt id="codeop.Compile">
139
<em class="property">class </em><code class="descclassname">codeop.</code><code class="descname">Compile</code><a class="headerlink" href="#codeop.Compile" title="Permalink to this definition">¶</a></dt>
140
<dd><p>Instances of this class have <a class="reference internal" href="../reference/datamodel.html#object.__call__" title="object.__call__"><code class="xref py py-meth docutils literal"><span class="pre">__call__()</span></code></a> methods identical in signature to
141
the built-in function <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal"><span class="pre">compile()</span></code></a>, but with the difference that if the
142
instance compiles program text containing a <a class="reference internal" href="__future__.html#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal"><span class="pre">__future__</span></code></a> statement, the
143
instance ‘remembers’ and compiles all subsequent program texts with the
144
statement in force.</p>
148
<dt id="codeop.CommandCompiler">
149
<em class="property">class </em><code class="descclassname">codeop.</code><code class="descname">CommandCompiler</code><a class="headerlink" href="#codeop.CommandCompiler" title="Permalink to this definition">¶</a></dt>
150
<dd><p>Instances of this class have <a class="reference internal" href="../reference/datamodel.html#object.__call__" title="object.__call__"><code class="xref py py-meth docutils literal"><span class="pre">__call__()</span></code></a> methods identical in signature to
151
<a class="reference internal" href="#codeop.compile_command" title="codeop.compile_command"><code class="xref py py-func docutils literal"><span class="pre">compile_command()</span></code></a>; the difference is that if the instance compiles program
152
text containing a <code class="docutils literal"><span class="pre">__future__</span></code> statement, the instance ‘remembers’ and
153
compiles all subsequent program texts with the statement in force.</p>
162
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
163
<div class="sphinxsidebarwrapper">
164
<h4>Previous topic</h4>
165
<p class="topless"><a href="code.html"
166
title="previous chapter">30.1. <code class="docutils literal"><span class="pre">code</span></code> — Interpreter base classes</a></p>
168
<p class="topless"><a href="modules.html"
169
title="next chapter">31. Importing Modules</a></p>
170
<div role="note" aria-label="source link">
172
<ul class="this-page-menu">
173
<li><a href="../bugs.html">Report a Bug</a></li>
174
<li><a href="../_sources/library/codeop.txt"
175
rel="nofollow">Show Source</a></li>
180
<div class="clearer"></div>
182
<div class="related" role="navigation" aria-label="related navigation">
185
<li class="right" style="margin-right: 10px">
186
<a href="../genindex.html" title="General Index"
189
<a href="../py-modindex.html" title="Python Module Index"
192
<a href="modules.html" title="31. Importing Modules"
195
<a href="code.html" title="30.1. code — Interpreter base classes"
197
<li><img src="../_static/py.png" alt=""
198
style="vertical-align: middle; margin-top: -1px"/></li>
199
<li><a href="https://www.python.org/">Python</a> »</li>
201
<span class="version_switcher_placeholder">3.5.2</span>
202
<a href="../index.html">Documentation </a> »
205
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
206
<li class="nav-item nav-item-2"><a href="custominterp.html" >30. Custom Python Interpreters</a> »</li>
210
<div class="inline-search" style="display: none" role="search">
211
<form class="inline-search" action="../search.html" method="get">
212
<input placeholder="Quick search" type="text" name="q" />
213
<input type="submit" value="Go" />
214
<input type="hidden" name="check_keywords" value="yes" />
215
<input type="hidden" name="area" value="default" />
218
<script type="text/javascript">$('.inline-search').show(0);</script>
225
© <a href="../copyright.html">Copyright</a> 2001-2016, Python Software Foundation.
227
The Python Software Foundation is a non-profit corporation.
228
<a href="https://www.python.org/psf/donations/">Please donate.</a>
230
Last updated on Sep 23, 2016.
231
<a href="../bugs.html">Found a bug</a>?
233
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'