~crunch.io/ubuntu/precise/codespeak-lib/unstable

« back to all changes in this revision

Viewing changes to doc/code.html

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-08-01 16:24:01 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20100801162401-g37v49d1p148alpm
Tags: 1.3.3-1
* New upstream release.
* Bump Standards-Version to 3.9.1.
* Fix typo in py.test manpage.
* Prefer Breaks: over Conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
 
<html>
3
 
  <head>
4
 
    <title>code</title>
5
 
    <meta content="text/html;charset=ISO-8859-1" name="Content-Type"/>
6
 
    <link href="style.css" media="screen" rel="stylesheet" type="text/css"/></head>
7
 
  <body>
8
 
    <div id="navspace">
9
 
      <div><a href="http://pylib.org"><img alt="py lib" height="57" id="pyimg" src="http://codespeak.net/img/pylib.png" width="77"/></a></div>
10
 
      <div id="menubar">
11
 
        <div>
12
 
          <div><a class="menu" href="announce/release-1.2.1.html">1.2.1 ANN</a></div></div>
13
 
        <div>
14
 
          <div><a class="menu" href="install.html">INSTALL</a></div></div>
15
 
        <div>
16
 
          <div><a class="menu" href="contact.html">CONTACT</a></div></div>
17
 
        <div>
18
 
          <div><a class="menu" href="changelog.html">CHANGELOG</a></div></div>
19
 
        <div>
20
 
          <div><a class="menu" href="faq.html">FAQ</a></div></div>
21
 
        <div>
22
 
          <div>
23
 
            <h3>py.test:</h3>
24
 
            <div><a class="menu" href="test/index.html">Index</a></div>
25
 
            <div><a class="menu" href="test/quickstart.html">Quickstart</a></div>
26
 
            <div><a class="menu" href="test/features.html">Features</a></div>
27
 
            <div><a class="menu" href="test/plugin/index.html">Plugins</a></div>
28
 
            <div><a class="menu" href="test/funcargs.html">Funcargs</a></div>
29
 
            <div><a class="menu" href="test/customize.html">Customize</a></div>
30
 
            <div><a class="menu" href="test/talks.html">Tutorials</a></div></div></div>
31
 
        <div>
32
 
          <div>
33
 
            <h3>supporting APIs:</h3>
34
 
            <div><a class="menu" href="index.html">Index</a></div>
35
 
            <div><a class="menu" href="path.html">py.path</a></div>
36
 
            <div><a class="menu" href="code.html">py.code</a></div></div></div></div></div>
37
 
    <div id="contentspace">
38
 
<div class="document" id="py-code-higher-level-python-code-and-introspection-objects">
39
 
<h1 class="title">py.code: higher level python code and introspection objects</h1>
40
 
 
41
 
<p>The <tt class="docutils literal">py.code</tt> part of the pylib contains some functionality to help
42
 
dealing with Python code objects. Even though working with Python's internal
43
 
code objects (as found on frames and callables) can be very powerful, it's
44
 
usually also quite cumbersome, because the API provided by core Python is
45
 
relatively low level and not very accessible.</p>
46
 
<p>The <tt class="docutils literal">py.code</tt> library tries to simplify accessing the code objects as well
47
 
as creating them. There is a small set of interfaces a user needs to deal with,
48
 
all nicely bundled together, and with a rich set of 'Pythonic' functionality.</p>
49
 
<div class="section" id="contents-of-the-library">
50
 
<h1>Contents of the library</h1>
51
 
<p>Every object in the <tt class="docutils literal">py.code</tt> library wraps a code Python object related
52
 
to code objects, source code, frames and tracebacks: the <tt class="docutils literal">py.code.Code</tt>
53
 
class wraps code objects, <tt class="docutils literal">py.code.Source</tt> source snippets,
54
 
<tt class="docutils literal">py.code.Traceback` exception tracebacks, ``py.code.Frame</tt> frame
55
 
objects (as found in e.g. tracebacks) and <tt class="docutils literal">py.code.ExceptionInfo</tt> the
56
 
tuple provided by sys.exc_info() (containing exception and traceback
57
 
information when an exception occurs). Also in the library is a helper function
58
 
<tt class="docutils literal">py.code.compile()</tt> that provides the same functionality as Python's
59
 
built-in 'compile()' function, but returns a wrapped code object.</p>
60
 
</div>
61
 
<div class="section" id="the-wrappers">
62
 
<h1>The wrappers</h1>
63
 
<div class="section" id="py-code-code">
64
 
<h2><tt class="docutils literal">py.code.Code</tt></h2>
65
 
<p>Code objects are instantiated with a code object or a callable as argument,
66
 
and provide functionality to compare themselves with other Code objects, get to
67
 
the source file or its contents, create new Code objects from scratch, etc.</p>
68
 
<p>A quick example:</p>
69
 
<pre class="literal-block">
70
 
&gt;&gt;&gt; import py
71
 
&gt;&gt;&gt; c = py.code.Code(py.path.local.read)
72
 
&gt;&gt;&gt; c.path.basename
73
 
'common.py'
74
 
&gt;&gt;&gt; isinstance(c.source(), py.code.Source)
75
 
True
76
 
&gt;&gt;&gt; str(c.source()).split('\n')[0]
77
 
&quot;def read(self, mode='r'):&quot;
78
 
</pre>
79
 
</div>
80
 
<div class="section" id="py-code-source">
81
 
<h2><tt class="docutils literal">py.code.Source</tt></h2>
82
 
<p>Source objects wrap snippets of Python source code, providing a simple yet
83
 
powerful interface to read, deindent, slice, compare, compile and manipulate
84
 
them, things that are not so easy in core Python.</p>
85
 
<p>Example:</p>
86
 
<pre class="literal-block">
87
 
&gt;&gt;&gt; s = py.code.Source(&quot;&quot;&quot;\
88
 
...   def foo():
89
 
...     print &quot;foo&quot;
90
 
... &quot;&quot;&quot;)
91
 
&gt;&gt;&gt; str(s).startswith('def') # automatic de-indentation!
92
 
True
93
 
&gt;&gt;&gt; s.isparseable()
94
 
True
95
 
&gt;&gt;&gt; sub = s.getstatement(1) # get the statement starting at line 1
96
 
&gt;&gt;&gt; str(sub).strip() # XXX why is the strip() required?!?
97
 
'print &quot;foo&quot;'
98
 
</pre>
99
 
</div>
100
 
<div class="section" id="py-code-traceback">
101
 
<h2><tt class="docutils literal">py.code.Traceback</tt></h2>
102
 
<p>Tracebacks are usually not very easy to examine, you need to access certain
103
 
somewhat hidden attributes of the traceback's items (resulting in expressions
104
 
such as 'fname = tb.tb_next.tb_frame.f_code.co_filename'). The Traceback
105
 
interface (and its TracebackItem children) tries to improve this.</p>
106
 
<p>Example:</p>
107
 
<pre class="literal-block">
108
 
&gt;&gt;&gt; import sys
109
 
&gt;&gt;&gt; try:
110
 
...   py.path.local(100) # illegal argument
111
 
... except:
112
 
...   exc, e, tb = sys.exc_info()
113
 
&gt;&gt;&gt; t = py.code.Traceback(tb)
114
 
&gt;&gt;&gt; first = t[1] # get the second entry (first is in this doc)
115
 
&gt;&gt;&gt; first.path.basename # second is in py/path/local.py
116
 
'local.py'
117
 
&gt;&gt;&gt; isinstance(first.statement, py.code.Source)
118
 
True
119
 
&gt;&gt;&gt; str(first.statement).strip().startswith('raise ValueError')
120
 
True
121
 
</pre>
122
 
</div>
123
 
<div class="section" id="py-code-frame">
124
 
<h2><tt class="docutils literal">py.code.Frame</tt></h2>
125
 
<p>Frame wrappers are used in <tt class="docutils literal">py.code.Traceback</tt> items, and will usually not
126
 
directly be instantiated. They provide some nice methods to evaluate code
127
 
'inside' the frame (using the frame's local variables), get to the underlying
128
 
code (frames have a code attribute that points to a <tt class="docutils literal">py.code.Code</tt> object)
129
 
and examine the arguments.</p>
130
 
<p>Example (using the 'first' TracebackItem instance created above):</p>
131
 
<pre class="literal-block">
132
 
&gt;&gt;&gt; frame = first.frame
133
 
&gt;&gt;&gt; isinstance(frame.code, py.code.Code)
134
 
True
135
 
&gt;&gt;&gt; isinstance(frame.eval('self'), py.path.local)
136
 
True
137
 
&gt;&gt;&gt; [namevalue[0] for namevalue in frame.getargs()]
138
 
['cls', 'path']
139
 
</pre>
140
 
</div>
141
 
<div class="section" id="py-code-exceptioninfo">
142
 
<h2><tt class="docutils literal">py.code.ExceptionInfo</tt></h2>
143
 
<p>A wrapper around the tuple returned by sys.exc_info() (will call sys.exc_info()
144
 
itself if the tuple is not provided as an argument), provides some handy
145
 
attributes to easily access the traceback and exception string.</p>
146
 
<p>Example:</p>
147
 
<pre class="literal-block">
148
 
&gt;&gt;&gt; import sys
149
 
&gt;&gt;&gt; try:
150
 
...   foobar()
151
 
... except:
152
 
...   excinfo = py.code.ExceptionInfo()
153
 
&gt;&gt;&gt; excinfo.typename
154
 
'NameError'
155
 
&gt;&gt;&gt; isinstance(excinfo.traceback, py.code.Traceback)
156
 
True
157
 
&gt;&gt;&gt; excinfo.exconly()
158
 
&quot;NameError: name 'foobar' is not defined&quot;
159
 
</pre>
160
 
</div>
161
 
</div>
162
 
</div>
163
 
</div>
164
 
<script type="text/javascript">
165
 
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
166
 
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
167
 
</script>
168
 
<script type="text/javascript">
169
 
try {
170
 
var pageTracker = _gat._getTracker("UA-7597274-3");
171
 
pageTracker._trackPageview();
172
 
} catch(err) {}</script>
173
 
</body></html>
 
 
b'\\ No newline at end of file'