📄 c883.htm
字号:
<HTML><HEAD><TITLE>Debugging</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.72"><LINKREL="HOME"TITLE="GUI Programming with Python: QT Edition"HREF="book1.htm"><LINKREL="UP"TITLE="Introduction to the BlackAdder IDE"HREF="p266.htm"><LINKREL="PREVIOUS"TITLE="Conclusion"HREF="x879.htm"><LINKREL="NEXT"TITLE="Setting breakpoints"HREF="x945.htm"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GUI Programming with Python: QT Edition</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="x879.htm"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="x945.htm"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="CH23">Chapter 5. Debugging</A></H1><DIVCLASS="TOC"><DL><DT><B>Table of Contents</B></DT><DT><AHREF="c883.htm#AEN933">Running scripts</A></DT><DT><AHREF="x945.htm">Setting breakpoints</A></DT><DT><AHREF="x966.htm">Stepping along</A></DT><DT><AHREF="x993.htm">Debugging Techniques</A></DT><DT><AHREF="x1024.htm">If all else fails</A></DT></DL></DIV><P>At some point in their career, most programmers realize that their job title should be "senior debugger" instead of senior developer. Debugging is the art of getting your code to run as you intended, instead of running as you wrote it. That is the nub, really—in most cases it's your code that is wrong. Python itself is pretty flawless — there are hardly any noticeable bugs left. The same goes for Qt. PyQt might still have a few bugs in it, but you would have to be using decidedly advanced features to stumble onto them. In most cases, your own ‘undocumented features' will be your undoing.</P><P>In this chapter we'll use the debugger included in <SPANCLASS="APPLICATION">BlackAdder</SPAN> to find bugs in some simple scripts. If you don't understand the actual code yet, don't worry — you can always come back later. The main goal is to familiarize yourself with the BlackAdder environment and the concept of debugging.</P><P>There are two basic methods of debugging. The first is sprinkling your code with <TTCLASS="FUNCTION">print</TT> statements that dump the contents of the variables of your application. The second method is to follow your application as it executes using a good debugger, examining the application data using the tools the debugger provides.</P><P>Python has always been possessed of a basic command-line based debugger, <SPANCLASS="APPLICATION">pdb</SPAN>, similar to the infamous Unix debuggers, <SPANCLASS="APPLICATION">dbx</SPAN> and <SPANCLASS="APPLICATION">gdb</SPAN>. If you've ever tried to actually trace an application using one of these, you'll know the exact meaning of the word ‘inconvenient'. Using them is on a par with using <SPANCLASS="APPLICATION">ed</SPAN> or <SPANCLASS="APPLICATION">edlin</SPAN> — both line editors — for editing code.</P><P>To show a session with pdb:</P><PRECLASS="SCREEN">Python 2.1.1 (#1, Aug 11 2001, 20:14:53)[GCC 2.95.2 19991024 (release)] on linux2Type "copyright", "credits" or "license" for more information.>>> import pdb>>> import button>>> pdb.run("button.main([])")> /home/boudewijn/doc/pyqt/ch23/<string>(0)?()(Pdb) continue> /home/boudewijn/doc/pyqt/ch23/<string>(1)?()(Pdb) continueTraceback (most recent call last): File "button.py", line 26, in slotSlot i = 1/0ZeroDivisionError: integer division or modulo by zero--Return--> /home/boudewijn/doc/pyqt/ch23/<string>(1)?()->None(Pdb) w> /home/boudewijn/doc/pyqt/ch23/<string>(1)?()->None(Pdb) l[EOF](Pdb) q>>> </PRE><P>You can see why there have been many attempts to create a useful GUI fronted to <SPANCLASS="APPLICATION">pdb</SPAN>. Most have suffered from the fact that they don't know where to stop debugging. If you are debugging a piece of code that contains the statement <TTCLASS="FUNCTION">string.join()</TT>, you probably don't want to single-step into the <TTCLASS="FILENAME">string.py</TT> module, which is part of the Python system library—and yet this is exactly what happens very often.</P><P><SPANCLASS="APPLICATION">BlackAdder</SPAN> includes a very nice debugger, one that knows where to stop debugging. It includes all the usual facilities, like single-stepping, breakpoints and a watch panel for variable values.</P><P>Currently missing features include conditional breakpoints (a breakpoint that only breaks execution on certain values for certain variables) and runtime code changes. You can change variable values runtime using the Python interpreter window, though.</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>The BlackAdder debugger.</P></DIV></P></DIV><P>The PyQt library includes another, simpler debugger, called <SPANCLASS="APPLICATION">eric</SPAN>. This application is no longer maintained, so I won't spend much time here describing the way it works. It does, however, provide a very nice example of using regular expressions and PyQt's <TTCLASS="CLASSNAME">QCanvas</TT> widget. You can start <SPANCLASS="APPLICATION">eric</SPAN> by typing <TTCLASS="FILENAME">eric</TT> on the command-line.</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Eric, the debugger included in PyQt.</P></DIV></P></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="AEN933">Running scripts</A></H1><P>The BlackAdder toolbar has two buttons for debugging your code. You can either debug a single script, or the whole project.</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>The script execution toolbar. From left to right: run script, run project, debug script, debug project, restart debugger, continue, single step, set breakpoint, clear breakpoint and cancel debugging.</P></DIV></P></DIV><P>One thing to be aware of when running scripts or projects from <SPANCLASS="APPLICATION">BlackAdder</SPAN> is that everything you <TTCLASS="FUNCTION">print</TT> or write to standard error or standard output gets lost, unless you have the Python interpreter window active. Eric also prints the output to the Python shell window.</P></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="x879.htm"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="book1.htm"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="x945.htm"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Conclusion</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="p266.htm"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Setting breakpoints</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -