["@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z GdddeZddddd d d d d g ZddZddZddZGdddeZdZGdddejejZedk rd ddddddddd d!d"d#d$d%d&d'd(dd)d*d+d,d-d.d/d0d1d2d3d4d5d6d7g"Zx2eD]*Zeeed8ejjd97ZqWeejj7Z[[nddd:dZddd;dZ d<d Z!d=d Z"d>d Z#dd?d Z$d@dZ%dAZ&dBdCZ'dDd Z(dEZ)dFdGZ*e+dHkrddl,Z,e,j*ndS)Ia^ The Python Debugger Pdb ======================= To use the debugger in its simplest form: >>> import pdb >>> pdb.run('') The debugger's prompt is '(Pdb) '. This will stop in the first function call in . Alternatively, if a statement terminated with an unhandled exception, you can use pdb's post-mortem facility to inspect the contents of the traceback: >>> >>> import pdb >>> pdb.pm() The commands recognized by the debugger are listed in the next section. Most can be abbreviated as indicated; e.g., h(elp) means that 'help' can be typed as 'h' or 'help' (but not as 'he' or 'hel', nor as 'H' or 'Help' or 'HELP'). Optional arguments are enclosed in square brackets. Alternatives in the command syntax are separated by a vertical bar (|). A blank line repeats the previous command literally, except for 'list', where it lists the next 11 lines. Commands that the debugger doesn't recognize are assumed to be Python statements and are executed in the context of the program being debugged. Python statements can also be prefixed with an exclamation point ('!'). This is a powerful way to inspect the program being debugged; it is even possible to change variables or call functions. When an exception occurs in such a statement, the exception name is printed but the debugger's state is not changed. The debugger supports aliases, which can save typing. And aliases can have parameters (see the alias help entry) which allows one a certain level of adaptability to the context under examination. Multiple commands may be entered on a single line, separated by the pair ';;'. No intelligence is applied to separating the commands; the input is split at the first ';;', even if it is in the middle of a quoted string. If a file ".pdbrc" exists in your home directory or in the current directory, it is read in and executed as if it had been typed at the debugger prompt. This is particularly useful for aliases. If both files exist, the one in the home directory is read first and aliases defined there can be overriden by the local file. Aside from aliases, the debugger is not directly programmable; but it is implemented as a class from which you can derive your own debugger class, which you can make as fancy as you like. Debugger commands ================= Nc@seZdZdZdS)RestartzBCauses a debugger to be restarted for the debugged python program.N)__name__ __module__ __qualname____doc__rr/usr/lib/python3.4/pdb.pyrTs rrunpmPdbrunevalrunctxruncall set_trace post_mortemhelpcCstjdtj|}yt|}Wntk rDdSYnX|Dx<t|ddD](\}}|j|r_|||fSq_WWdQXdS)Nzdef\s+%s\s*[(]start)recompileescapeopenOSError enumeratematch)funcnamefilenameZcrefplinenolinerrr find_function[s  r cCsztj|\}}tj|r@|j|jkr@|dfStj|rY|dfStj||d|dfS)Nr)inspectZ findsourceZisframe f_globalsf_localsZismoduleZgetblock)objlinesrrrrgetsourcelineshs !  r&cCsJttj|}|jx$|D]\}}||kr&|Sq&WdS)Nr)listdisZfindlinestartsreverse)codeZlastiZ linestartsirrrr lasti2linenoqs   r,c@s"eZdZdZddZdS)_rstrz#String that doesn't quote its repr.cCs|S)Nr)selfrrr__repr__|sz_rstr.__repr__N)rrrrr/rrrrr-zs r-z -> c@seZdZdddddddZddZdd Zd d Zd d ZddZddZ ddZ ddZ ddZ ddZ ddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5ZeZd6d7d8Zd9d:ZeZeZ eZ!d;d<Z"eZ#d=d>Z$d?d@Z%dAdBZ&eZ'dCdDZ(eZ)dEdFZ*eZ+dGdHZ,eZ-dIdJZ.e.Z/eZ0eZ1dKdLZ2e2Z3e2Z4dMdNZ5dOdPZ6e6Z7dQdRZ8e8Z9dSdTZ:e:Z;dUdVZ<e<Z=dWdXZ>e>Z?dYdZZ@e@ZAd[d\ZBeBZCd]d^ZDeDZEZFd_d`ZGeGZHdadbZIeZJdcddZKeKZLeKZMdedfZNdgdhZOeOZPdidjZQeQZRdkdlZSddmdnZTdodpZUdqdrZVeZWeZXeZYdsdtZZeZZ[dudvZ\e\Z]dwdxZ^eZ_fddydzZ`d{d|ZaeZbd}d~ZceZdddZeddZfddZgddZhddZiddZjddddddgZkddZlemddZnddZoeoZpddZqddZrddZsddZtdS)r ZtabNFc #Cstjj|d|tjj|||||rAd|_nd|_i|_i|_d|_ d|_ i|_ yddl }|j dWntk rYnXd|_||_g|_dtjkr0tjd}y8ttjj|d}|jj|WdQXWq0tk r,Yq0Xny)td}|jj|WdQXWntk rmYnXi|_i|_i|_d|_d|_dS) Nskiprz(Pdb) Fz `@#$%^&*()=+[{]}\|;:'",<>?HOMEz.pdbrc)bdbBdb__init__cmdCmdZ use_rawinputpromptaliases displaying mainpyfile_wait_for_mainpyfile tb_linenoreadlineZset_completer_delims ImportError allow_kbdintnosigintrcLinesosenvironrpathjoinextendrcommandscommands_dopromptcommands_silentcommands_defining commands_bnum) r. completekeystdinstdoutr0rAr>ZenvHomeZrcFilerrrr5sF                   z Pdb.__init__cCsP|jrtn|jd|j|j|tjtj|jdS)Nz- Program interrupted. (Use 'cont' to resume).)r@KeyboardInterruptmessageset_steprsignalSIGINT_previous_sigint_handler)r.Zsignumframerrrsigint_handlers      zPdb.sigint_handlercCstjj||jdS)N)r3r4resetforget)r.rrrrXsz Pdb.resetcCs5d|_g|_d|_d|_|jjdS)Nr)rstackcurindexcurframer=clear)r.rrrrYs     z Pdb.forgetcCs|j|j||\|_|_x;|ret|jj|j}||j|j<|j }q+W|j|jd|_ |j j |_ |j S)Nr)rYZ get_stackrZr[r,tb_framef_codetb_lastir=tb_nextr\r#curframe_locals execRcLines)r.ftbrrrrsetups   z Pdb.setupcCs|js dS|j}|jg|_x]|r|jj}|r,|ddkr,|j|r|jt|7_dSq,q,WdS)Nr#T)rBr)popstriponecmdreversed)r.rBrrrrrcs     zPdb.execRcLinescCs@|jr dS|j|r<|jd|j|dndS)znThis method is called when there is the remote possibility that we ever need to stop in this function.Nz--Call--)r<Z stop_hererQ interaction)r.rVZ argument_listrrr user_calls   z Pdb.user_callcCsl|jrF|j|j|jjks6|jdkr:dSd|_n|j|rh|j|dndS)z;This function is called when we stop or break at this line.rNF)r<r;canonicr_ co_filenamef_lineno bp_commandsrl)r.rVrrr user_lines  z Pdb.user_linecCst|ddr|j|jkr|j}d|_|j}|j|dx"|j|D]}|j|q]W||_|j|s|j|j|j n|j |r|j n|j dSdS)zCall every command that was set for the current active breakpoint (if there is one). Returns True if the normal interaction function must be called, False otherwise. currentbpFrNr) getattrrsrHlastcmdrfrjrJprint_stack_entryrZr[rI_cmdlooprY)r.rVrsZ lastcmd_backrrrrrqs         zPdb.bp_commandscCs;|jr dS||jd<|jd|j|ddS)z7This function is called when a return trap is set here.N __return__z --Return--)r<r#rQrl)r.rVZ return_valuerrr user_returns    zPdb.user_returncCs|jr dS|\}}}||f|jd<| rH|tkrHdnd}|jd|tj||djf|j||dS)zoThis function is called if an exception occurs, but only if we are to stop at or just below this level.NZ __exception__z Internal r1z%s%sr)r<r# StopIterationrQ tracebackformat_exception_onlyrirl)r.rVexc_infoexc_type exc_value exc_tracebackprefixrrruser_exception%s  !zPdb.user_exceptionc CsNxGy!d|_|jd|_PWqtk rE|jdYqXqWdS)NTFz--KeyboardInterrupt--)r@cmdlooprPrQ)r.rrrrw9s    z Pdb._cmdloopcCs|jj|j}|rxk|jD]Z\}}|j|}||k r(||kr(|||<|jd|||fq(q(WndS)Nzdisplay %s: %r [old: %r])r:getr\items_getval_exceptrQ)r.r:exprZoldvalueZnewvaluerrrpreloopFs  z Pdb.preloopcCsO|j||r |jdS|j|j|j|j|jdS)N)rfrYrvrZr[rw)r.rVr|rrrrlSs   zPdb.interactioncCs&|dk r"|jt|ndS)z{Custom displayhook for the exec in default(), which prevents assignment of the _ variable in the builtins. N)rQrepr)r.r$rrr displayhook]s zPdb.displayhookc Cs|dddkr)|dd}n|j}|jj}yt|ddd}tj}tj}tj}z8|jt_|jt_|jt_t|||Wd|t_|t_|t_XWn>tj dd}|j t j |dj YnXdS)Nr! zZsinglerz)rbr\r"rsysrOrNrexecr~errorr|r}ri) r.rlocalsglobalsr*Z save_stdoutZ save_stdinZsave_displayhookr~rrrdefaultes(          z Pdb.defaultcCs4|js|S|j}x|d|jkr|j|d}d}x>|ddD],}|jdt||}|d7}qZW|jddj|dd}|j}qW|ddkr0|jd}|dkr0||d dj}|jj ||d|j }q0n|S) z*Handle alias expansion and ';;' separator.rrN%z%* aliasz;;r) risplitr9replacestrrFfindlstripcmdqueueappendrstrip)r.rargsZiiZtmpArgZmarkernextrrrprecmd{s&   % z Pdb.precmdcCs-|jstjj||S|j|SdS)zInterpret the argument as though it had been typed in response to the prompt. Checks whether this line is typed at the normal prompt or in a breakpoint command list definition. N)rKr6r7rjhandle_command_def)r.rrrrrjs z Pdb.onecmdc Cs|j|\}}}|s"dS|dkrBd|j|js z*Pdb._complete_bpnumber..)rr3 Breakpoint bpbynumber)r.rrrrr)rr_complete_bpnumberszPdb._complete_bpnumberc s|js gS|jjj}|j|jdkrjdy<|d}x'ddD]}t||}qlWWnttfk rgSYnXdj dddfddt |DSfdd|j DSdS) N.rrcs-g|]#}|jdr|qS)rrz)r)rn)dottedrrrrs z,Pdb._complete_expression..cs%g|]}|jr|qSr)r)rr)rrrrs rzrz) r\r"copyupdaterbrrtKeyErrorrrFdirkeys)r.rrrrnsr$partr)rrrrrs    zPdb._complete_expressioncCsz|sttjjd}n,yt|}Wn|jddSYnX||_||jkr|j||j||j |f}nd}g|j|)r\r_ror;)r.rrrrrs zPdb.defaultFilecCs|j|ddS)ztbreak [ ([filename:]lineno | function) [, condition] ] Same arguments as break, but sets a temporary breakpoint: it is automatically deleted when first hit. rN)r)r.rrrr do_tbreaksz Pdb.do_tbreakc Cs"d}|jd}t|dkr:|dj}n)t|dkr_|dj}n|S|dkrs|S|jd}|ddkr|d=t|dkr|Sn|j}t|dkr|d}n,|j|d}|r|}n|d}t||} | p!|S) N'rrr1rr.)NNN)rrrirrr ) r.Z identifierZfailedZidstringidpartsZfnameitemrdZanswerrrrrs.     z Pdb.lineinfocCst|dr|jjnd}tj|||}|sM|jddS|j}| s|ddks|dddks|dddkr|jd dS|S) zCheck whether specified line seems to be executable. Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank line or EOF). Warning: testing is not comprehensive. r\Nz End of filerrgrz"""z'''zBlank or comment)rr\r" linecachegetlinerQrir)r.rrrrrrrrs!  , z Pdb.checklinecCs|j}xq|D]i}y|j|}Wn2tk r`}z|j|WYdd}~XqX|j|jd|qWdS)zenable bpnumber [bpnumber ...] Enables the breakpoints given as a space separated list of breakpoint numbers. Nz Enabled %s)rget_bpbynumberrrenablerQ)r.rrr+rrrrr do_enables    z Pdb.do_enablecCs|j}xq|D]i}y|j|}Wn2tk r`}z|j|WYdd}~XqX|j|jd|qWdS)aNdisable bpnumber [bpnumber ...] Disables the breakpoints given as a space separated list of breakpoint numbers. Disabling a breakpoint means it cannot cause the program to stop execution, but unlike clearing a breakpoint, it remains in the list of breakpoints and can be (re-)enabled. Nz Disabled %s)rrrrdisablerQ)r.rrr+rrrrr do_disables    zPdb.do_disablecCs|jdd}y|d}Wntk r:d}YnXy|j|dj}WnPtk ry|jdYnltk r}z|j|WYdd}~Xn;X||_|s|jd|jn|jd|jdS)a#condition bpnumber [condition] Set a new condition for the breakpoint, an expression which must evaluate to true before the breakpoint is honored. If condition is absent, any existing condition is removed; i.e., the breakpoint is made unconditional. rrNrzBreakpoint number expectedz#Breakpoint %d is now unconditional.z$New condition set for breakpoint %d.) r IndexErrorrrirrrrQr)r.rrrrrrrr do_conditions     zPdb.do_conditioncCs|j}yt|dj}Wnd}YnXy|j|dj}WnPtk ru|jdYntk r}z|j|WYdd}~XnfX||_|dkr|dkrd|}nd}|jd||j fn|jd|j dS) aignore bpnumber [count] Set the ignore count for the given breakpoint number. If count is omitted, the ignore count is set to 0. A breakpoint becomes active when the ignore count is zero. When non-zero, the count is decremented each time the breakpoint is reached and the breakpoint is not disabled and any associated condition evaluates to true. rrzBreakpoint number expectedNz %d crossingsz 1 crossingz%Will ignore next %s of breakpoint %d.z-Will stop next time breakpoint %d is reached.) rrrirrrrignorerQr)r.rrcountrrZcountstrrrr do_ignore6s(          z Pdb.do_ignorec !Cs|sytd}Wntk r0d}YnX|jj}|d krddtjjD}|jx"|D]}|jd|qyWndSd |kri|j d }|d|}||d d}yt |}Wnt k rd |}Yn%X|j ||}|j ||}|rC|j|n"x|D]}|jd|qJWdS|j} xt| D]l}y|j|}Wn2t k r}z|j|WYdd}~Xq|X|j||jd|q|WdS) a=cl(ear) filename:lineno cl(ear) [bpnumber [bpnumber...]] With a space separated list of breakpoint numbers, clear those breakpoints. Without argument, clear all breaks (but first ask confirmation). With a filename:lineno argument, clear all breaks at that line in that file. zClear all breaks? ZnoyyescSsg|]}|r|qSrr)rrrrrrgs z Pdb.do_clear..z Deleted %sNrrzInvalid line number (%s))rr)inputEOFErrorrirr3rrZclear_all_breaksrQrrrrZ clear_breakrrrZclear_bpbynumber) r.rZreplyZbplistrr+rrrZ numberlistrrrdo_clearYsF            z Pdb.do_clearcCs|jdS)zw(here) Print a stack trace, with the most recent frame at the bottom. An arrow indicates the "current frame", which determines the context of most commands. 'bt' is an alias for this command. N)print_stack_trace)r.rrrrdo_wheresz Pdb.do_wherecCs~d|ko t|jkns+t||_|j|jd|_|jj|_|j|j|jd|_dS)Nr) rrZAssertionErrorr[r\r#rbrvr)r.rrrr _select_frames + zPdb._select_framec Cs|jdkr |jddSyt|p/d}Wn'tk r_|jd|dSYnX|dkrud}ntd|j|}|j|dS)zu(p) [count] Move the current frame count (default one) levels up in the stack trace (to an older frame). rz Oldest frameNrzInvalid frame count (%s))r[rrrmaxr)r.rrnewframerrrdo_ups     z Pdb.do_upc Cs|jdt|jkr-|jddSyt|p<d}Wn'tk rl|jd|dSYnX|dkrt|jd}n#tt|jd|j|}|j|dS)zd(own) [count] Move the current frame count (default one) levels down in the stack trace (to a newer frame). rz Newest frameNzInvalid frame count (%s)r)r[rrZrrrminr)r.rrrrrrdo_downs    #z Pdb.do_downc Cs|rfyt|}Wn'tk r?|jd|dSYnX||jjkrl|jddSnd}|j|j|dS)aNunt(il) [lineno] Without argument, continue execution until the line with a number greater than the current one is reached. With a line number, continue execution until a line with a number greater or equal to that is reached. In both cases, also stop when the current frame returns. zError in argument: %rNz7"until" line number is smaller than current line numberr)rrrr\rpZ set_until)r.rrrrrdo_untils   z Pdb.do_untilcCs|jdS)zs(tep) Execute the current line, stop at the first possible occasion (either in a function that is called or in the current function). r)rR)r.rrrrdo_steps z Pdb.do_stepcCs|j|jdS)zxn(ext) Continue execution until the next line in the current function is reached or it returns. r)Zset_nextr\)r.rrrrdo_nextsz Pdb.do_nextcCsW|rMddl}tjdd}|j|t_|tjdd". If an exception is being debugged, the line where the exception was originally raised or propagated is indicated by ">>", if it differs from the current line. r'NrrrzError in argument: %r z[EOF])rurrrirrrrr\rpr_roget_file_breaksrgetlinesr" _print_linesrrrQrP)r.rlastfirstr breaklistr%rrrdo_lists>          z Pdb.do_listcCs|jjj}|j|}yt|j\}}Wn6tk ro}z|j|dSWYdd}~XnX|j||||jdS)z\longlist | ll List the whole source code for the current function or frame. N)r\r_ror*r&rrr,)r.rrr/r%rrrrr do_longlists zPdb.do_longlistcCsy|j|}Wn dSYnXyt|\}}Wn<ttfk rv}z|j|dSWYdd}~XnX|j||dS)z^source expression Try to get source code for the given object and display it. N)r$r&r TypeErrorrr,)r.rr$r%rrrrr do_sources  z Pdb.do_sourcec Cs|r'|j}|jj|d }n d }}xt||D]\}}t|jd} t| dkr| d7} n||kr| d7} n | d7} ||kr| d7} n||kr| d7} n|j| d|jqAWd S) zPrint a range of lines.rrrrBz->z>> Nrzrz) rpr=rrrrjustrrQr) r.r%rrrVZcurrent_linenoZ exc_linenorrsrrrr,s           zPdb._print_linescCsy|j|}Wn dSYnXd}y |j}Wntk rIYnX|rh|jd|jdSy|jj}Wntk rYnX|r|jd|jdS|jtkr|jd|j|j fdS|jt|dS)z;whatis arg Print the type of the argument. Nz Function %sz Method %sz Class %s.%s) r$rrrQrr __class__typerr)r.rvaluer*rrr do_whatis s.    z Pdb.do_whatiscCs|sP|jdx||jj|jijD]}|jd|q2WnB|j|}||jj|ji|<|jd||fdS)zdisplay [expression] Display the value of the expression if it changed, each time execution stops in the current frame. Without expression, list all display expressions for the current frame. zCurrently displaying:z%s: %rzdisplay %s: %rN)rQr:rr\rr setdefault)r.rrvalrrr do_display.s %zPdb.do_displayc Csf|rLy|jj|ji|=Wqbtk rH|jd|YqbXn|jj|jddS)zundisplay [expression] Do not display the expression any more in the current frame. Without expression, clear all display expressions for the current frame. znot displaying %sN)r:rr\rrrh)r.rrrr do_undisplayAs  zPdb.do_undisplaycs)fdd|jj|jiDS)Ncs%g|]}|jr|qSr)r)rr)rrrrQs z*Pdb.complete_undisplay..)r:rr\)r.rrrrr)rrcomplete_undisplayPszPdb.complete_undisplaycCs9|jjj}|j|jtjdd|dS)zinteract Start an interactive interpreter whose global namespace contains all the (global and local) names found in the current scope. z *interactive*ZlocalN)r\r"rrrbr*interact)r.rrrrr do_interactTszPdb.do_interactcCs|j}t|dkrft|jj}x,|D]$}|jd||j|fq:WdS|d|jkrt|dkr|jd|d|j|dfn$dj|dd|j|d