235.html
来自「Python Ebook Python&XML」· HTML 代码 · 共 751 行 · 第 1/3 页
HTML
751 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -> Debugging the Application</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> > <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> > <a href="227.html" class="navtitle">17. Development Tools</a> > <span class="nonavtitle">Debugging the Application</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="234.html" title="Interpreter"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=235" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="235.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="236.html" title="Profiling Python"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="6%2F1%2F2002+6%3A22%3A26+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039196038240039088173205162105045222218066183102005081107212</font><a href="read4.asp?bookname=0672319942&snode=235&now=6%2F1%2F2002+6%3A22%3A26+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>
Debugging the Application</h3>
<p>Debugging a Python program is something that doesn't require too much work. The Standard Python Library comes with a debugger module called <tT CLAss="monofont">bdb</tt> that can be used by you to subclass your own debuggers. If you don't want to spend time writing your own debug, you can use the Python debugger (the <TT CLass="monofont">pdb</tT> module), which is also part of the Python distribution. For those who need high-specialized debugging information, Python provides a disassembler module. And for those who only want to debug the value of variables, nothing works better than spreading a couple of <TT Class="monofont">print</tt> statements throughout your program.</p>
<p>If you decide to use the Python debugger, you will not regret it. This debugger allows you to set breakpoints, trace the values of local and global variables, step through the code, and many other attractive features.</p>
<p>Because it is written in Python, the debugger exemplifies a powerful feature of the Python language: the ability to create introspective applications, which means that we are able to write programs in Python that can handle and manipulate the execution of other programs.</p>
<h4>
The Base Debugger Module (<tt class="monofont">bdb</tT>)</h4>
<p>The <Tt clAss="monofont">bdb</tt> module exposes a framework for creating debuggers. This module provides a base class called <Tt clASS="monofont">bdb</Tt> that allows you to create your own debuggers by subclassing the base class.</p>
<p>The following <a NAME="idx1073750263"></a>methods are available in this class. Note that derived classes should override the following four methods to gain control of the application.</p>
<blOCKQuote>
<P>
<A NAme="idx1073750264"></a>
<a name="idx1073750265"></a>
<p><tt class="monofont">user_call(frame, argument_list)</tt>棤
This method is called when there is the remote possibility that we ever need to stop in this function pass.<a NamE="idx1073750266"></a>
<a nAme="idx1073750267"></a>
<a Name="idx1073750268"></A>
<A NAme="idx1073750269"></a>
<a NAME="idx1073750270"></a>
<a naME="idx1073750271"></A>
<A name="idx1073750272"></A>
<A NAme="idx1073750273"></a>
</p>
</p>
<p>
<a name="idx1073750274"></a>
<a name="idx1073750275"></a>
<p><tT clAss="monofont">user_line(frame)</tT>棤
This method is called when we stop or break at this line pass.</p>
</p>
<p>
<a Name="idx1073750276"></A>
<A NAme="idx1073750277"></a>
<p><TT CLass="monofont">user_return(frame, return_value)</tT>棤
This method is called when a return trap is set here.</P>
</P>
<P>
<a namE="idx1073750278"></A>
<A Name="idx1073750279"></a>
<p><tt class="monofont">user_exception(frame, (exc_type, exc_value, exc_traceback))</tt>棤
This method is called if an exception occurs, but only if we are to stop at or just below this level pass.</p>
</p>
</blocKquOte>
<p>The following methods can be called by the derived classes and by the clients in order to affect the stepping state:</P>
<blocKquoTE>
<P>
<A name="idx1073750280"></A>
<A NAme="idx1073750281"></a>
<p><TT CLass="monofont">set_step()</tT>棤
Stops after one line of code</P>
</P>
<P>
<a name="idx1073750282"></a>
<a name="idx1073750283"></a>
<p><tt class="monofont">set_next(frame)</Tt>棤
Stops on the next line in or below the given frame</p>
</P>
<p>
<a nAme="idx1073750284"></a>
<a Name="idx1073750285"></A>
<P><TT clasS="monofont">set_return(frame)</TT>棤
Stops when returning from the given frame</P>
</p>
<p>
<a nAME="idx1073750286"></A>
<a namE="idx1073750287"></A>
<P><Tt class="monofont">set_trace()</tt>棤
Starts debugging from here</p>
</p>
<p>
<a name="idx1073750288"></a>
<a naMe="idx1073750289"></a>
<P><tt cLass="monofont">set_continue()</tT>棤
Doesn't stop except at breakpoints or when finished</p>
</p>
<p>
<A NAMe="idx1073750290"></a>
<a nAME="idx1073750291"></A>
<p><tt cLASS="monofont">set_quit()</tt>棤
Quits the debugging process</p>
</p>
</BLOCkquote>
<p>Derived classes and clients can call the following methods in order to manipulate breakpoints. These methods return an error message if something went wrong, and <tt class="monofont">None</tt> if everything goes well.</p>
<bloCkqUote>
<P>
<a namE="idx1073750292"></a>
<a nAME="idx1073750293"></A>
<p><tt cLASS="monofont">set_break(filename, lineno, temporary=0, cond = None)</tt>棤
This method prints out the breakpoint line and filename:lineno.</p>
</p>
<P>
<A NAme="idx1073750294"></a>
<a NAME="idx1073750295"></a>
<p><tt class="monofont">clear_break(filename, lineno)</tt>棤
This method removes the breakpoint entry.</p>
</p>
<p>
<a name="idx1073750296"></a>
<A naMe="idx1073750297"></a>
<p><Tt claSs="monofont">clear_bpbynumber(arg)</tt>棤
This method removes the breakpoint identified by the given number.</P>
</P>
<P>
<A name="idx1073750298"></A>
<A NAme="idx1073750299"></a>
<p><TT CLass="monofont">clear_all_file_breaks(filename)</tT>棤
This method removes all the breakpoints found in the given file.</P>
</P>
<P>
<a name="idx1073750300"></a>
<a name="idx1073750301"></a>
<p><tt class="monofont">clear_all_breaks()</Tt>棤
This method removes all the active breakpoints from the current program.</p>
</P>
<p>
<a nAme="idx1073750302"></a>
<a Name="idx1073750303"></A>
<P><TT clasS="monofont">get_break(filename, lineno)</TT>棤
This method returns true if the given file has a breakpoint in the given line number.</P>
</p>
<p>
<a nAME="idx1073750304"></A>
<a namE="idx1073750305"></A>
<P><Tt class="monofont">get_breaks(filename, lineno)</tt>棤
This method returns true if the given file has a breakpoint in the given line number.<a name="idx1073750306"></a>
<a name="idx1073750307"></a>
<A naMe="idx1073750308"></a>
<a Name="idx1073750309"></a>
<A namE="idx1073750310"></A>
<A Name="idx1073750311"></a>
<A NAMe="idx1073750312"></a>
<a nAME="idx1073750313"></A>
</p>
</p>
<p>
<a NAME="idx1073750314"></a>
<a name="idx1073750315"></a>
<p><tt class="monofont">get_file_breaks(filename)</tt>棤
This method returns a list of all breakpoints found in the given file.</p>
</p>
<p>
<A naMe="idx1073750316"></a>
<a Name="idx1073750317"></a>
<P><tt cLASS="monofont">get_all_breaks()</tt>棤
This method returns a list of all active breakpoints from the current program.</p>
</p>
</BLOCkquoTE>
<P>The following methods can be called by clients to use a debugger to debug a statement given as a string:</P>
<blocKQUOte>
<p>
<a name="idx1073750318"></a>
<a name="idx1073750319"></a>
<p><tt clAss="monofont">run(command, globals=None, locals=None)</Tt>棤
Executes the string <tt Class="monofont">command</Tt>, under the debugger control.</p>
</p>
<P>
<A NAme="idx1073750320"></a>
<a NAME="idx1073750321"></a>
<p><tt CLASs="monofont">runeval(expr, globals=None, locals=None)</tt>棤
Evaluates the expression <tT CLAss="monofont">expr</tt> under the debugger control.</p>
</p>
<p>
<a name="idx1073750322"></a>
<a name="idx1073750323"></a>
<p><Tt cLass="monofont">runcall(func, *args)</Tt>棤
This method calls the single function <tt cLass="monofont">func</TT> under the debugger control.</P>
</P>
<p>
<a naME="idx1073750324"></A>
<A name="idx1073750325"></A>
<P><TT clasS="monofont">set_trace()</TT>棤
This method starts the debugger at the point at which this function is called. It is used to hard-code a debugger breakpoint into a specific code location.<A name="idx1073750326"></a>
</p>
</p>
</blockquote>
<p>The following example demonstrates how we can subclass the <tt ClaSs="monofont">bdb</tt> class in order to design our own debug. This example is based on the testing routine included in the <Tt claSs="monofont">bdb</tt> module file.</P>
<PRE>
import bdb
class Tdb(bdb.Bdb):
def user_call(self, frame, args):
name = frame.f_code.co_name
if not name:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?