~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.2-docs-html/library/codeop.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
2
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
3
 
 
4
 
 
5
<html xmlns="http://www.w3.org/1999/xhtml">
 
6
  <head>
 
7
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
8
    
 
9
    <title>30.2. codeop — Compile Python code &mdash; Python 3.5.2 documentation</title>
 
10
    
 
11
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
 
12
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
 
13
    
 
14
    <script type="text/javascript">
 
15
      var DOCUMENTATION_OPTIONS = {
 
16
        URL_ROOT:    '../',
 
17
        VERSION:     '3.5.2',
 
18
        COLLAPSE_INDEX: false,
 
19
        FILE_SUFFIX: '.html',
 
20
        HAS_SOURCE:  true
 
21
      };
 
22
    </script>
 
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" />
 
37
    
 
38
    <script type="text/javascript" src="../_static/copybutton.js"></script>
 
39
    <script type="text/javascript" src="../_static/version_switch.js"></script>
 
40
    
 
41
    
 
42
 
 
43
 
 
44
  </head>
 
45
  <body role="document">  
 
46
    <div class="related" role="navigation" aria-label="related navigation">
 
47
      <h3>Navigation</h3>
 
48
      <ul>
 
49
        <li class="right" style="margin-right: 10px">
 
50
          <a href="../genindex.html" title="General Index"
 
51
             accesskey="I">index</a></li>
 
52
        <li class="right" >
 
53
          <a href="../py-modindex.html" title="Python Module Index"
 
54
             >modules</a> |</li>
 
55
        <li class="right" >
 
56
          <a href="modules.html" title="31. Importing Modules"
 
57
             accesskey="N">next</a> |</li>
 
58
        <li class="right" >
 
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> &raquo;</li>
 
64
        <li>
 
65
          <span class="version_switcher_placeholder">3.5.2</span>
 
66
          <a href="../index.html">Documentation </a> &raquo;
 
67
        </li>
 
68
 
 
69
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
70
          <li class="nav-item nav-item-2"><a href="custominterp.html" accesskey="U">30. Custom Python Interpreters</a> &raquo;</li>
 
71
    <li class="right">
 
72
        
 
73
 
 
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" />
 
80
        </form>
 
81
    </div>
 
82
    <script type="text/javascript">$('.inline-search').show(0);</script>
 
83
         |
 
84
    </li>
 
85
 
 
86
      </ul>
 
87
    </div>    
 
88
 
 
89
    <div class="document">
 
90
      <div class="documentwrapper">
 
91
        <div class="bodywrapper">
 
92
          <div class="body" role="main">
 
93
            
 
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> &#8212; 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&#8217;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>
 
102
module instead.</p>
 
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 &#8216;<code class="docutils literal"><span class="pre">&gt;&gt;&gt;</span></code>&#8216; or &#8216;<code class="docutils literal"><span class="pre">...</span></code>&#8216; next.</li>
 
107
<li>Remembering which future statements the user has entered, so  subsequent
 
108
input can be compiled with these in effect.</li>
 
109
</ol>
 
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=&quot;&lt;input&gt;&quot;</em>, <em>symbol=&quot;single&quot;</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">'&lt;input&gt;'</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>
 
134
</div>
 
135
</dd></dl>
 
136
 
 
137
<dl class="class">
 
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 &#8216;remembers&#8217; and compiles all subsequent program texts with the
 
144
statement in force.</p>
 
145
</dd></dl>
 
146
 
 
147
<dl class="class">
 
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 &#8216;remembers&#8217; and
 
153
compiles all subsequent program texts with the statement in force.</p>
 
154
</dd></dl>
 
155
 
 
156
</div>
 
157
 
 
158
 
 
159
          </div>
 
160
        </div>
 
161
      </div>
 
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> &#8212; Interpreter base classes</a></p>
 
167
  <h4>Next topic</h4>
 
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">
 
171
    <h3>This Page</h3>
 
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>
 
176
    </ul>
 
177
  </div>
 
178
        </div>
 
179
      </div>
 
180
      <div class="clearer"></div>
 
181
    </div>  
 
182
    <div class="related" role="navigation" aria-label="related navigation">
 
183
      <h3>Navigation</h3>
 
184
      <ul>
 
185
        <li class="right" style="margin-right: 10px">
 
186
          <a href="../genindex.html" title="General Index"
 
187
             >index</a></li>
 
188
        <li class="right" >
 
189
          <a href="../py-modindex.html" title="Python Module Index"
 
190
             >modules</a> |</li>
 
191
        <li class="right" >
 
192
          <a href="modules.html" title="31. Importing Modules"
 
193
             >next</a> |</li>
 
194
        <li class="right" >
 
195
          <a href="code.html" title="30.1. code — Interpreter base classes"
 
196
             >previous</a> |</li>
 
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> &raquo;</li>
 
200
        <li>
 
201
          <span class="version_switcher_placeholder">3.5.2</span>
 
202
          <a href="../index.html">Documentation </a> &raquo;
 
203
        </li>
 
204
 
 
205
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
206
          <li class="nav-item nav-item-2"><a href="custominterp.html" >30. Custom Python Interpreters</a> &raquo;</li>
 
207
    <li class="right">
 
208
        
 
209
 
 
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" />
 
216
        </form>
 
217
    </div>
 
218
    <script type="text/javascript">$('.inline-search').show(0);</script>
 
219
         |
 
220
    </li>
 
221
 
 
222
      </ul>
 
223
    </div>  
 
224
    <div class="footer">
 
225
    &copy; <a href="../copyright.html">Copyright</a> 2001-2016, Python Software Foundation.
 
226
    <br />
 
227
    The Python Software Foundation is a non-profit corporation.
 
228
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
 
229
    <br />
 
230
    Last updated on Sep 23, 2016.
 
231
    <a href="../bugs.html">Found a bug</a>?
 
232
    <br />
 
233
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
 
234
    </div>
 
235
 
 
236
  </body>
 
237
</html>
 
 
b'\\ No newline at end of file'