| 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="prev" href="debugger-commands.html" /> |
| 13 | <link rel="parent" href="module-pdb.html" /> |
| 14 | <link rel="next" href="profile.html" /> |
| 15 | <meta name='aesop' content='information' /> |
| 16 | <title>9.2 How It Works </title> |
| 17 | </head> |
| 18 | <body> |
| 19 | <DIV CLASS="navigation"> |
| 20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 22 | <tr> |
| 23 | <td class='online-navigation'><a rel="prev" title="9.1 Debugger Commands" |
| 24 | href="debugger-commands.html"><img src='../icons/previous.png' |
| 25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 26 | <td class='online-navigation'><a rel="parent" title="9. The Python Debugger" |
| 27 | href="module-pdb.html"><img src='../icons/up.png' |
| 28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 29 | <td class='online-navigation'><a rel="next" title="10. The Python Profiler" |
| 30 | href="profile.html"><img src='../icons/next.png' |
| 31 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 32 | <td align="center" width="100%">Python Library Reference</td> |
| 33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 34 | href="contents.html"><img src='../icons/contents.png' |
| 35 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 37 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 38 | <td class='online-navigation'><a rel="index" title="Index" |
| 39 | href="genindex.html"><img src='../icons/index.png' |
| 40 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 41 | </tr></table> |
| 42 | <div class='online-navigation'> |
| 43 | <b class="navlabel">Previous:</b> |
| 44 | <a class="sectref" rel="prev" href="debugger-commands.html">9.1 Debugger Commands</A> |
| 45 | <b class="navlabel">Up:</b> |
| 46 | <a class="sectref" rel="parent" href="module-pdb.html">9. The Python Debugger</A> |
| 47 | <b class="navlabel">Next:</b> |
| 48 | <a class="sectref" rel="next" href="profile.html">10. The Python Profiler</A> |
| 49 | </div> |
| 50 | <hr /></div> |
| 51 | </DIV> |
| 52 | <!--End of Navigation Panel--> |
| 53 | |
| 54 | <H1><A NAME="SECTION0011200000000000000000"></A><A NAME="debugger-hooks"></A> |
| 55 | <BR> |
| 56 | 9.2 How It Works |
| 57 | </H1> |
| 58 | |
| 59 | <P> |
| 60 | Some changes were made to the interpreter: |
| 61 | |
| 62 | <P> |
| 63 | |
| 64 | <UL> |
| 65 | <LI><code>sys.settrace(<var>func</var>)</code> sets the global trace function |
| 66 | </LI> |
| 67 | <LI>there can also a local trace function (see later) |
| 68 | </LI> |
| 69 | </UL> |
| 70 | |
| 71 | <P> |
| 72 | Trace functions have three arguments: <var>frame</var>, <var>event</var>, and |
| 73 | <var>arg</var>. <var>frame</var> is the current stack frame. <var>event</var> is a |
| 74 | string: <code>'call'</code>, <code>'line'</code>, <code>'return'</code>, <code>'exception'</code>, |
| 75 | <code>'c_call'</code>, <code>'c_return'</code>, or <code>'c_exception'</code>. <var>arg</var> |
| 76 | depends on the event type. |
| 77 | |
| 78 | <P> |
| 79 | The global trace function is invoked (with <var>event</var> set to |
| 80 | <code>'call'</code>) whenever a new local scope is entered; it should return |
| 81 | a reference to the local trace function to be used that scope, or |
| 82 | <code>None</code> if the scope shouldn't be traced. |
| 83 | |
| 84 | <P> |
| 85 | The local trace function should return a reference to itself (or to |
| 86 | another function for further tracing in that scope), or <code>None</code> to |
| 87 | turn off tracing in that scope. |
| 88 | |
| 89 | <P> |
| 90 | Instance methods are accepted (and very useful!) as trace functions. |
| 91 | |
| 92 | <P> |
| 93 | The events have the following meaning: |
| 94 | |
| 95 | <P> |
| 96 | <DL> |
| 97 | <DT><STRONG><code>'call'</code></STRONG></DT> |
| 98 | <DD>A function is called (or some other code block entered). The global |
| 99 | trace function is called; <var>arg</var> is <code>None</code>; |
| 100 | the return value specifies the local trace function. |
| 101 | |
| 102 | <P> |
| 103 | </DD> |
| 104 | <DT><STRONG><code>'line'</code></STRONG></DT> |
| 105 | <DD>The interpreter is about to execute a new line of code (sometimes |
| 106 | multiple line events on one line exist). The local trace function is |
| 107 | called; <var>arg</var> is <code>None</code>; the return value specifies the new |
| 108 | local trace function. |
| 109 | |
| 110 | <P> |
| 111 | </DD> |
| 112 | <DT><STRONG><code>'return'</code></STRONG></DT> |
| 113 | <DD>A function (or other code block) is about to return. The local trace |
| 114 | function is called; <var>arg</var> is the value that will be returned. The |
| 115 | trace function's return value is ignored. |
| 116 | |
| 117 | <P> |
| 118 | </DD> |
| 119 | <DT><STRONG><code>'exception'</code></STRONG></DT> |
| 120 | <DD>An exception has occurred. The local trace function is called; |
| 121 | <var>arg</var> is a triple <code>(<var>exception</var>, <var>value</var>, |
| 122 | <var>traceback</var>)</code>; the return value specifies the new local trace |
| 123 | function. |
| 124 | |
| 125 | <P> |
| 126 | </DD> |
| 127 | <DT><STRONG><code>'c_call'</code></STRONG></DT> |
| 128 | <DD>A C function is about to be called. This may be an extension function |
| 129 | or a builtin. <var>arg</var> is the C function object. |
| 130 | |
| 131 | <P> |
| 132 | </DD> |
| 133 | <DT><STRONG><code>'c_return'</code></STRONG></DT> |
| 134 | <DD>A C function has returned. <var>arg</var> is <code>None</code>. |
| 135 | |
| 136 | <P> |
| 137 | </DD> |
| 138 | <DT><STRONG><code>'c_exception'</code></STRONG></DT> |
| 139 | <DD>A C function has thrown an exception. <var>arg</var> is <code>None</code>. |
| 140 | |
| 141 | <P> |
| 142 | </DD> |
| 143 | </DL> |
| 144 | |
| 145 | <P> |
| 146 | Note that as an exception is propagated down the chain of callers, an |
| 147 | <code>'exception'</code> event is generated at each level. |
| 148 | |
| 149 | <P> |
| 150 | For more information on code and frame objects, refer to the |
| 151 | <em class="citetitle"><a |
| 152 | href="../ref/ref.html" |
| 153 | title="Python Reference Manual" |
| 154 | >Python Reference Manual</a></em>. |
| 155 | |
| 156 | <P> |
| 157 | |
| 158 | <DIV CLASS="navigation"> |
| 159 | <div class='online-navigation'> |
| 160 | <p></p><hr /> |
| 161 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 162 | <tr> |
| 163 | <td class='online-navigation'><a rel="prev" title="9.1 Debugger Commands" |
| 164 | href="debugger-commands.html"><img src='../icons/previous.png' |
| 165 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 166 | <td class='online-navigation'><a rel="parent" title="9. The Python Debugger" |
| 167 | href="module-pdb.html"><img src='../icons/up.png' |
| 168 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 169 | <td class='online-navigation'><a rel="next" title="10. The Python Profiler" |
| 170 | href="profile.html"><img src='../icons/next.png' |
| 171 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 172 | <td align="center" width="100%">Python Library Reference</td> |
| 173 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 174 | href="contents.html"><img src='../icons/contents.png' |
| 175 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 176 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 177 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 178 | <td class='online-navigation'><a rel="index" title="Index" |
| 179 | href="genindex.html"><img src='../icons/index.png' |
| 180 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 181 | </tr></table> |
| 182 | <div class='online-navigation'> |
| 183 | <b class="navlabel">Previous:</b> |
| 184 | <a class="sectref" rel="prev" href="debugger-commands.html">9.1 Debugger Commands</A> |
| 185 | <b class="navlabel">Up:</b> |
| 186 | <a class="sectref" rel="parent" href="module-pdb.html">9. The Python Debugger</A> |
| 187 | <b class="navlabel">Next:</b> |
| 188 | <a class="sectref" rel="next" href="profile.html">10. The Python Profiler</A> |
| 189 | </div> |
| 190 | </div> |
| 191 | <hr /> |
| 192 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 193 | </DIV> |
| 194 | <!--End of Navigation Panel--> |
| 195 | <ADDRESS> |
| 196 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 197 | </ADDRESS> |
| 198 | </BODY> |
| 199 | </HTML> |