[C @s`dZddlZddlZddlZddlZddlTddlmZddddd d d d d ddg eZ[ejej ej e fZ ddZ dddddZdddddZidd6dd6dd6dd6dd6dd 6d!d"6Zd#d$Zd%d&Zd'dZd(d)Zddd*d Zejd+d,ZGd-ddeZd.dd/d Zd0d1Zd2d3Zddddddd4d5ZdBddd6dZdCdddddddd7dd8d9Zddd:d;ZeZ d<d Z!d=d Z"Gd>ddZ#d?d@Z$e%dAkr\e$ndS)Dz0Disassembler of Python byte code into mnemonics.N)*)__all__ code_infodis disassembledistbdiscofindlinestarts findlabels show_codeget_instructions InstructionBytecodec CsAyt||d}Wn$tk r<t||d}YnX|S)zAttempts to compile the given source, first as an expression and then as a statement if the first approach fails. Utility function to accept strings in functions that otherwise expect code objects evalexec)compile SyntaxError)sourcenamecr/usr/lib/python3.4/dis.py _try_compiles  rfilecCs|dkrtd|dSt|dr8|j}nt|drS|j}nt|drt|jj}x|D]\}}t|tr~t d|d|yt |d|Wn8t k r}zt d|d|WYdd}~XnXt d|q~q~Wnt|dr5t |d|nct|t tfr]t|d|n;t|trt|d|nt d t|jdS) znDisassemble classes, methods, functions, or code. With no argument, disassemble the last traceback. Nr__func____code____dict__zDisassembly of %s:zSorry:co_codez(don't know how to disassemble %s objects)rhasattrrrsortedritems isinstance _have_codeprintr TypeErrorrbytes bytearray_disassemble_bytesstr_disassemble_strtype__name__)xrr rZx1msgrrrrs2    &c Csv|dkrVy tj}Wntk r9tdYnXx|jrR|j}q=Wnt|jj|jd|dS)z2Disassemble a traceback (default: last traceback).Nz no last traceback to disassembler) syslast_tracebackAttributeError RuntimeErrortb_nextrtb_framef_codetb_lasti)tbrrrrr@s    Z OPTIMIZEDZ NEWLOCALSZVARARGSZ VARKEYWORDSZNESTEDZ GENERATOR ZNOFREE@cCsg}xqtdD]P}d|>}||@r|jtj|t|||N}|scPqcqqW|jt|dj|S)z+Return pretty representation of code flags.r<r7z, )rangeappendCOMPILER_FLAG_NAMESgethexjoin)flagsnamesiZflagrrr pretty_flagsWs    rGcCst|dr|j}nt|dr6|j}nt|trWt|d}nt|drj|Stdt|jdS)zAHelper to handle methods, functions, strings and raw code objectsrrz rz(don't know how to disassemble %s objectsN) rrrr!r(rr$r*r+)r,rrr_get_code_objectes  rHcCstt|S)z1Formatted details of methods, functions, or code.)_format_code_inforH)r,rrrrrscCsg}|jd|j|jd|j|jd|j|jd|j|jd|j|jd|j|jdt|j|j r|jdx+t |j D]}|jd |qWn|j r |jd x+t |j D]}|jd |qWn|j rd|jd x+t |j D]}|jd |qFWn|j r|jd x+t |j D]}|jd |qWn|jr|jdx+t |jD]}|jd |qWndj|S)NzName: %szFilename: %szArgument count: %szKw-only arguments: %szNumber of locals: %szStack size: %szFlags: %sz Constants:z%4d: %rzNames:z%4d: %szVariable names:zFree variables:zCell variables: )r?co_name co_filename co_argcountco_kwonlyargcount co_nlocals co_stacksizerGco_flags co_consts enumerateco_names co_varnames co_freevars co_cellvarsrC)colinesZi_cZi_nrrrrIvs:          rIcCstt|d|dS)z}Print details of methods, functions, or code to *file*. If *file* is not provided, the output is printed on stdout. rN)r#r)rXrrrrr s _InstructionzBopname opcode arg argval argrepr offset starts_line is_jump_targetc@s(eZdZdZddddZdS)r aKDetails for a bytecode operation Defined fields: opname - human readable name for operation opcode - numeric code for operation arg - numeric argument to operation (if any), otherwise None argval - resolved arg value (if known), otherwise same as arg argrepr - human readable description of operation argument offset - start index of operation within bytecode sequence starts_line - line started by this opcode (if any), otherwise None is_jump_target - True if other code jumps to here, otherwise False FcCs9g}|rP|jdk r<d|}|j||jqP|jd|n|rf|jdn |jd|jr|jdn |jd|jt|jjd|j|jjd |jdk r&|jt|jjd |j r&|jd |j d q&ndj |j S) zFormat instruction details for inclusion in disassembly output *lineno_width* sets the width of the line number field (0 omits it) *mark_as_current* inserts a '-->' marker arrow as part of the line Nz%%%dd z-->z z>>z r9()) starts_liner?is_jump_targetreproffsetrjustopnameljustargargreprrCrstrip)self lineno_widthZmark_as_currentZfieldsZ lineno_fmtrrr _disassembles&     zInstruction._disassembleN)r+ __module__ __qualname____doc__rmrrrrr s  first_linecCsxt|}|j|j}tt|}|dk rJ||j}nd}t|j|j|j |j |||S)aIterator for the opcodes in methods, functions or code Generates a series of Instruction named tuples giving the details of each operations in the supplied code. If *first_line* is not None, it indicates the line number that should be reported for the first source line in the disassembled code. Otherwise, the source line information (if any) is taken directly from the disassembled code object. Nr) rHrWrVdictr co_firstlineno_get_instructions_bytesrrUrTrR)r,rqrX cell_names linestarts line_offsetrrrr s   cCs/|}|dk r||}n|t|fS)zHelper to get optional details about const references Returns the dereferenced constant and its repr if the constant list is defined. Otherwise returns the constant index and its repr(). N)rc)Z const_indexZ const_listargvalrrr_get_const_infos  rycCs;|}|dk r%||}|}n t|}||fS)zHelper to get optional details about named references Returns the dereferenced name as both value and repr if the name list is defined. Otherwise returns the name index and its repr(). N)rc)Z name_indexZ name_listrxrirrr_get_name_infos     rzc cs@t|}d}d} d} t|} d} x | | kr;|| } | }|dk r|j| d} | dk r| |7} qn| |k}| d} d}d}d}| tkr|| || dd|}d}| d} | tkr|d}n|}| tkr.t||\}}q| tkrRt||\}}q| t kr{| |}dt |}q| t krt||\}}q| t krt |}|}q| tkrt||\}}q| tkrd || d|| df}qntt| | ||||| |Vq3WdS) a&Iterate over the instructions in a bytecode string. Generates a sequence of Instruction namedtuples giving the details of each opcode. Additional information about the code's runtime environment (e.g. variable names, constants) can be specified using optional arguments. rNr7r8izto z%d positional, %d keyword pair)r lenrA HAVE_ARGUMENTZ EXTENDED_ARGZhasconstryZhasnamerzhasjrelrcZhaslocalZ hascompareZcmp_opZhasfreeZhasnargsr rf)codevarnamesrE constantscellsrvrwlabelsZ extended_argraZfreenrFoprdrbrhrxrirrrrtsX                     &  rtc CsT|j|j}tt|}t|j||j|j|j||d|dS)zDisassemble a code object.rN) rWrVrrr r'rrUrTrR)rXlastirrurvrrrrAsrwc Cs|dk } | rdnd} xt||||||d|D]k} | og| jdk og| jdk} | rtd|n| j|k} t| j| | d|q@WdS)Nr[rrwr)rtrardr#rm)rrrrErrrvrrwZ show_linenorlZinstrZnew_source_lineZis_current_instrrrrr'Hs   r'cCstt|dd|dS)zrN)rr)rrrrrr)Zsr)cCsg}t|}d}x||kr||}|d}|tkr||||dd}|d}d}|tkr||}n|tkr|}n|dkr||kr|j|qqqqW|S)z`Detect all offsets in a byte code which are jump targets. Return the list of offsets. rr7r|r8)r}r~rZhasjabsr?)rrrrFrrhZlabelrrrr `s$           ccst|jddd}t|jddd}d}|j}d}xZt||D]I\}}|r||kr||fV|}n||7}n||7}q]W||kr||fVndS)zFind the offsets in a byte code which are start of lines in the source. Generate pairs (offset, lineno) as described in Python/compile.c. rNr8r7)list co_lnotabrszip)rZbyte_incrementsZline_incrementsZ lastlinenolinenoZaddrZ byte_incrZ line_incrrrrr {s      c@speZdZdZddddddZddZd d Zed d Zd dZ ddZ dS)rzThe bytecode operations of a piece of code Instantiate this with a function, method, string of code, or a code object (as returned by compile()). Iterating over this yields the bytecode operations as Instruction instances. rqNcurrent_offsetcCst||_}|dkr7|j|_d|_n||_||j|_|j|j|_tt ||_ ||_ ||_ dS)Nr) rHcodeobjrsrq _line_offsetrWrV _cell_namesrrr _linestarts_original_objectr)rkr,rqrrXrrr__init__s     zBytecode.__init__c Cs=|j}t|j|j|j|j|j|jd|jS)Nrw) rrtrrUrTrRrrr)rkrXrrr__iter__s    zBytecode.__iter__cCsdj|jj|jS)Nz{}({!r}))format __class__r+r)rkrrr__repr__szBytecode.__repr__cCs2x|jr|j}qW||jjd|jS)z/ Construct a Bytecode from the given traceback r)r2r3r4r5)clsr6rrrfrom_tracebacks  zBytecode.from_tracebackcCs t|jS)z3Return formatted information about the code object.)rIr)rkrrrinfosz Bytecode.infocCs|j}|jdk r$|j}nd }tj`}t|jd|jd|jd|jd|j d|j d|j d |d ||j SWdQXdS) z3Return a formatted view of the bytecode operations.Nr7rrErrrvrwrrr) rrioStringIOr'rrUrTrRrrrgetvalue)rkrXrdoutputrrrrs     z Bytecode.dis) r+rnrorprrr classmethodrrrrrrrrs    c Csddl}|j}|jdd|jdddd|j}|j}|j}WdQXt||jjd }t |dS) z*Simple test program to disassemble a file.rNinfiler*nargs?default-r) argparseArgumentParser add_argumentZFileType parse_argsrreadrrr)rparserargsrrrrrr_tests  %  r__main__rr)&rpr.types collectionsrZopcoderZ _opcodes_all MethodType FunctionTypeCodeTyper*r"rrrr@rGrHrrIr namedtuplerZr r ryrzrtrr'r)rr r rrr+rrrrs^       !       3  <    =