home *** CD-ROM | disk | FTP | other *** search
- <TITLE>How It Works -- Python library reference</TITLE>
- Prev: <A HREF="../d/debugger_commands" TYPE="Prev">Debugger Commands</A>
- Up: <A HREF="../t/the_python_debugger" TYPE="Up">The Python Debugger</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H1>8.2. How It Works</H1>
- Some changes were made to the interpreter:
- <P>
- <UL>
- <LI>• sys.settrace(func) sets the global trace function
- <LI>• there can also a local trace function (see later)
- </UL>
- Trace functions have three arguments: (<VAR>frame</VAR>, <VAR>event</VAR>, <VAR>arg</VAR>)
- <P>
- <DL>
- <DT><B><VAR>frame</VAR></B><DD>is the current stack frame
- <P>
- <DT><B><VAR>event</VAR></B><DD>is a string: <CODE>'call'</CODE>, <CODE>'line'</CODE>, <CODE>'return'</CODE>
- or <CODE>'exception'</CODE>
- <P>
- <DT><B><VAR>arg</VAR></B><DD>is dependent on the event type
- <P>
- </DL>
- A trace function should return a new trace function or None.
- Class methods are accepted (and most useful!) as trace methods.
- <P>
- The events have the following meaning:
- <P>
- <DL>
- <DT><B><CODE>'call'</CODE></B><DD>A function is called (or some other code block entered). The global
- trace function is called; arg is the argument list to the function;
- the return value specifies the local trace function.
- <P>
- <DT><B><CODE>'line'</CODE></B><DD>The interpreter is about to execute a new line of code (sometimes
- multiple line events on one line exist). The local trace function is
- called; arg in None; the return value specifies the new local trace
- function.
- <P>
- <DT><B><CODE>'return'</CODE></B><DD>A function (or other code block) is about to return. The local trace
- function is called; arg is the value that will be returned. The trace
- function's return value is ignored.
- <P>
- <DT><B><CODE>'exception'</CODE></B><DD>An exception has occurred. The local trace function is called; arg is
- a triple (exception, value, traceback); the return value specifies the
- new local trace function
- <P>
- </DL>
- Note that as an exception is propagated down the chain of callers, an
- <CODE>'exception'</CODE> event is generated at each level.
- <P>
- Stack frame objects have the following read-only attributes:
- <P>
- <DL>
- <DT><B>f_code</B><DD>the code object being executed
- <DT><B>f_lineno</B><DD>the current line number (<CODE>-1</CODE> for <CODE>'call'</CODE> events)
- <DT><B>f_back</B><DD>the stack frame of the caller, or None
- <DT><B>f_locals</B><DD>dictionary containing local name bindings
- <DT><B>f_globals</B><DD>dictionary containing global name bindings
- </DL>
- Code objects have the following read-only attributes:
- <P>
- <DL>
- <DT><B>co_code</B><DD>the code string
- <DT><B>co_names</B><DD>the list of names used by the code
- <DT><B>co_consts</B><DD>the list of (literal) constants used by the code
- <DT><B>co_filename</B><DD>the filename from which the code was compiled
- </DL>
-