YfC@stdZddlZddlZddlZddlZddlTddlmZddddd d d d d ddg eZ[ejej ej e e e fZddZdddddZdddddZdddddddddddd d!d"d#d$d%d&i Zd'd(Zd)d*Zd+dZd,d-Zddd.d Zejd/d0ZGd1ddeZd2dd3d Zd4d5Zd6d7Zddddddd8d9ZdHddd:dZdIdddddddd;dd<d=Z ddd>d?Z!eZ"d@dAZ#dBd Z$dCd Z%GdDddZ&dEdFZ'e(dGkrpe'dS)Jz0Disassembler 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(/opt/alt/python35/lib64/python3.5/dis.py _try_compiles  rfilecCs|dkrtd|dSt|dr5|j}t|drM|j}t|dre|j}t|dr"t|jj}x|D]\}}t|t rt d|d|yt |d|Wn8t k r }zt d|d|WYdd}~XnXt d|qWnt|d rDt |d|nct|ttfrlt|d|n;t|trt|d|nt d t|jdS) zzDisassemble classes, methods, functions, generators, or code. With no argument, disassemble the last traceback. Nr__func____code__gi_code__dict__zDisassembly of %s:zSorry:co_codez(don't know how to disassemble %s objects)rhasattrrrrsortedritems isinstance _have_codeprintr TypeErrorrbytes bytearray_disassemble_bytesstr_disassemble_strtype__name__)xrr!rZx1msgrrrr s6     &c Css|dkrSy tj}Wntk r9tdYnXx|jrR|j}q=Wt|jj|jd|dS)z2Disassemble a traceback (default: last traceback).Nz no last traceback to disassembler) syslast_tracebackAttributeError RuntimeErrortb_nextrtb_framef_codetb_lasti)tbrrrrrCs    Z OPTIMIZEDZ NEWLOCALSZVARARGSZ VARKEYWORDSZNESTED Z GENERATOR@ZNOFREEZ COROUTINEZITERABLE_COROUTINEcCsg}xktdD]J}d|>}||@r|jtj|t|||N}|sPqW|jt|dj|S)z+Return pretty representation of code flags.r=r8z, )rangeappendCOMPILER_FLAG_NAMESgethexjoin)flagsnamesiZflagrrr pretty_flags\s   rJcCst|dr|j}t|dr0|j}t|drH|j}t|trft|d}t|dry|Stdt|j dS)zMHelper to handle methods, functions, generators, strings and raw code objectsrrrz rz(don't know how to disassemble %s objectsN) rrrrr"r)rr%r+r,)r-rrr_get_code_objectjs   rKcCstt|S)z1Formatted details of methods, functions, or code.)_format_code_inforK)r-rrrryscCsg}|jd|j|jd|j|jd|j|jd|j|jd|j|jd|j|jdt|j|j r|jdx(t |j D]}|jd |qW|j r|jd x(t |j D]}|jd |qW|j r[|jd x(t |j D]}|jd |q@W|j r|jd x(t |j D]}|jd |qW|jr|jdx(t |jD]}|jd |qWdj|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: )rBco_name co_filename co_argcountco_kwonlyargcount co_nlocals co_stacksizerJco_flags co_consts enumerateco_names co_varnames co_freevars co_cellvarsrF)colinesZi_cZi_nrrrrL}s:          rLcCstt|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)r[rrrrr 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 FcCs0g}|rM|jdk r<d|}|j||jn|jd||rc|jdn |jd|jr|jdn |jd|jt|jjd|j|jjd |jdk r|jt|jjd |j r|jd |j d dj |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 r:()) starts_linerBis_jump_targetreproffsetrjustopnameljustargargreprrFrstrip)self lineno_widthZmark_as_currentZfieldsZ lineno_fmtrrr _disassembles&     zInstruction._disassembleN)r, __module__ __qualname____doc__rprrrrr 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) rKrZrYdictr co_firstlineno_get_instructions_bytesrrXrWrU)r-rtr[ cell_names linestarts line_offsetrrrr s   cCs,|}|dk r||}|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)rf)Z const_indexZ const_listargvalrrr_get_const_infos  r|cCs;|}|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)rf)Z name_indexZ name_listr{rlrrr_get_name_infos     r}c cst|}d}d} xt|D]\} } } |dk rh|j| d}|dk rh||7}| |k} d}d}| dk r| }| tkrt| |\}}n| tkrt| |\}}n| tkr| d| }dt|}n| t kr+t| |\}}ng| t krJt | }|}nH| t krnt| |\}}n$| t krd| d| df}tt| | | ||| || Vq%WdS)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. Nr^zto z%d positional, %d keyword pairr@)r _unpack_opargsrDZhasconstr|Zhasnamer}hasjrelrfZhaslocalZ hascompareZcmp_opZhasfreeZhasnargsr ri)codevarnamesrH constantscellsryrzlabelsrdZfreergoprkrer{rlrrrrw s@                 rwc CsT|j|j}tt|}t|j||j|j|j||d|dS)zDisassemble a code object.rN) rZrYrur r(rrXrWrU)r[lastirrxryrrrr:srzc Cs|dk } | rdnd} xt||||||d|D]h} | og| jdk og| jdk} | r}td|| j|k} t| j| | d|q@WdS)Nr^rrzr)rwrdrgr$rp)rrrrHrrryrrzZ show_linenoroZinstrZnew_source_lineZis_current_instrrrrr(As    r(cCstt|dd|dS)zrN)rr)rrrrrr*Ssr*ccsd}t|}d}x||kr||}|}|d}d}|tkr||||dd|}d}|d}|tkr|d}|||fVqWdS)Nrr8r@r9i)lenZ HAVE_ARGUMENTZ EXTENDED_ARG)rZ extended_argnrIrrgrkrrrrYs       rcCsg}xt|D]u\}}}|dk rd}|tkrQ|d|}n|tkrc|}|dkr||kr|j|qW|S)z`Detect all offsets in a byte code which are jump targets. Return the list of offsets. Nr8r^r)rrZhasjabsrB)rrrgrrkZlabelrrrr ls     ccst|jddd}t|jddd}d}|j}d}xTt||D]C\}}|r||kr||fV|}||7}||7}q]W||kr||fVdS)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. rNr9r8)list co_lnotabrvzip)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. rtNcurrent_offsetcCst||_}|dkr7|j|_d|_n||_||j|_|j|j|_tt ||_ ||_ ||_ dS)Nr) rKcodeobjrvrt _line_offsetrZrY _cell_namesrur _linestarts_original_objectr)rnr-rtrr[rrr__init__s     zBytecode.__init__c Cs=|j}t|j|j|j|j|j|jd|jS)Nrz) rrwrrXrWrUrrr)rnr[rrr__iter__s    zBytecode.__iter__cCsdj|jj|jS)Nz{}({!r}))format __class__r,r)rnrrr__repr__szBytecode.__repr__cCs2x|jr|j}qW||jjd|jS)z/ Construct a Bytecode from the given traceback r)r3r4r5r6)clsr7rrrfrom_tracebacks  zBytecode.from_tracebackcCs t|jS)z3Return formatted information about the code object.)rLr)rnrrrinfosz Bytecode.infocCs|j}|jdk r$|j}nd }tj`}t|jd|jd|jd|jd|j d|j d|j d |d ||j SWdQRXdS) z3Return a formatted view of the bytecode operations.Nr8rrHrrryrzrrr) rrioStringIOr(rrXrWrUrrrgetvalue)rnr[rgoutputrrrrs     z Bytecode.dis) r,rqrrrsrrr classmethodrrrrrrrrs    c Csddl}|j}|jdd|jdddd|j}|j}|j}WdQRXt||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))rsr/types collectionsrZopcoderZ _opcodes_all MethodType FunctionTypeCodeTyper staticmethodr+r#rrrrCrJrKrrLr namedtupler]r r r|r}rwrr(r*rrr r rrr,rrrrsd       #       3  .     =