f _ZPc @sdZddlZddlZddlZddlZddlTddlmZgdeZ[ejej ej e e e fZedZdedfed fed ffZed Zd Zd dZdVdddddZdWddddZddddddddddd Zd d!Zd"d#Zd$d%Zd&d'Zddd(d)Ze d*d+Z!d,e!j"_d-e!j_d.e!j#_d/e!j$_d0e!j%_d1e!j&_d2e!j'_d3e!j(_d4Z)d5Z*Gd6d7d7e!Z+dd8d9d:Z,d;d<Z-d=d>Z.dXd?d@Z/dYdddBdCZ0ddddDdEZ1dZdddFdGdHZ2dIdJZ3e0Z4dKdLZ5dMdNZ6dOdPZ7GdQdRdRZ8dSdTZ9e:dUkre9dS)[z0Disassembler of Python byte code into mnemonics.N)Z*)__all__) code_infodis disassembledistbdiscofindlinestarts findlabels show_codeget_instructions InstructionBytecode FORMAT_VALUE)Nstrreprascii MAKE_FUNCTION)ZdefaultsZ kwdefaultsZ annotationsZclosurecCs4zt||d}Wnty.t||d}Yn0|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 Zevalexec)compileZ SyntaxError)sourcenameZcr/usr/lib64/python3.10/dis.py _try_compiles  rfiledepthc Csj|durt|ddSt|dr&|j}t|dr8|j}n4t|drJ|j}n"t|dr\|j}nt|drl|j}t|drt|j }|D]r\}}t |t rt d ||dzt |||d Wn2ty}zt d ||dWYd}~n d}~00t |dqnht|d rt|||d nLt |ttfr8t||dn.t |trTt|||d ntd t|jdS)a0Disassemble classes, methods, functions, and other compiled objects. With no argument, disassemble the last traceback. Compiled objects currently include generator objects, async generator objects, and coroutine objects, all of which store their code object in a special attribute. Nr__func____code__gi_codeag_codecr_code__dict__zDisassembly of %s:rzSorry:co_code(don't know how to disassemble %s objects)rhasattrrrr r!r"Zsortedr#items isinstance _have_codeprintr TypeError_disassemble_recursiveZbytesZ bytearray_disassemble_bytesr_disassemble_strtype__name__)xrrr'rZx1Zmsgrrrr+s@          $  rrcCsV|dur>z tj}Wnty.tddYn0|jr>|j}q0t|jj|j|ddS)z2Disassemble a traceback (default: last traceback).Nz no last traceback to disassembler) sysZlast_tracebackZAttributeErrorZ RuntimeErrortb_nextrtb_framef_codetb_lasti)tbrrrrrXs  rZ OPTIMIZEDZ NEWLOCALSZVARARGSZ VARKEYWORDSZNESTEDZ GENERATORZNOFREEZ COROUTINEZITERABLE_COROUTINEZASYNC_GENERATOR) i i@icCs`g}tdD]:}d|>}||@r |t|t|||N}|s qVq |t|d|S)z+Return pretty representation of code flags.r<r8, )rangeappendCOMPILER_FLAG_NAMESgetZhexjoin)ZflagsnamesiZflagrrr pretty_flagsrs rGcCst|dr|j}t|dr"|j}n4t|dr4|j}n"t|drF|j}nt|drV|j}t|trjt|d}t|drx|St dt |j d S) zDHelper to handle methods, compiled or raw code objects, and strings.rrr r!r"z r$r%N) r&rrr r!r"r(rrr+r/r0r1rrr_get_code_objects"        rIcCs tt|S)z1Formatted details of methods, functions, or code.)_format_code_inforIrHrrrrrcCsg}|d|j|d|j|d|j|d|j|d|j|d|j|d|j|dt|j |j r|d t |j D]}|d |q|j r|d t |j D]}|d |q|j r|d t |j D]}|d |q|jrH|dt |jD]}|d |q2|jrz|dt |jD]}|d |qdd|S)NzName: %szFilename: %szArgument count: %szPositional-only arguments: %szKw-only arguments: %szNumber of locals: %szStack size: %szFlags: %sz Constants:z%4d: %rzNames:z%4d: %szVariable names:zFree variables:zCell variables:Z )rAZco_nameZ co_filenameZ co_argcountZco_posonlyargcountZco_kwonlyargcountZ co_nlocalsZ co_stacksizerGZco_flags co_consts enumerateco_names co_varnames co_freevars co_cellvarsrD)coZlinesZi_cZi_nrrrrJs<     rJcCstt||ddS)z}Print details of methods, functions, or code to *file*. If *file* is not provided, the output is printed on stdout. rN)r*r)rRrrrrr sr _InstructionzBopname opcode arg argval argrepr offset starts_line is_jump_targetz!Human readable name for operationzNumeric code for operationz6Numeric argument to operation (if any), otherwise Nonez4Resolved arg value (if known), otherwise same as argz0Human readable description of operation argumentz1Start index of operation within bytecode sequencez4Line started by this opcode (if any), otherwise Nonez1True if other code jumps to here, otherwise Falseiic@seZdZdZdddZdS) 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 Fr:cCsg}|r:|jdur,d|}|||jn|d||rJ|dn |d|jrf|dn |d|t|j|||jt|j dur|t|j t |j r|d|j d d | S) a%Format 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 *offset_width* sets the width of the instruction offset field Nz%%%ddz z-->z z>>z z(z)) starts_linerAis_jump_targetroffsetZrjustopnameZljust _OPNAME_WIDTHarg _OPARG_WIDTHargreprrDZrstrip)self lineno_widthZmark_as_current offset_widthZfieldsZ lineno_fmtrrr _disassembles&      zInstruction._disassembleN)rTFr:)r0 __module__ __qualname____doc__r`rrrrr s r ) first_linecCsTt|}|j|j}tt|}|dur4||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) rIrQrPdictrco_firstlineno_get_instructions_bytesr$rOrNrL)r1rdrR cell_names linestarts line_offsetrrrr s    r cCs |}|dur||}|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(). Nr)Z const_indexZ const_listargvalrrr_get_const_infosrmcCs*|}|dur||}|}nt|}||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(). Nrk)Z name_indexZ name_listrlr\rrr_get_name_info's rnc #st|}d}t|D]r\} } |durD|| d}|durD||7}| |v} d} d} durl} | tvrzt|\} } n| tvrt|\} } n| tvr| d} dt| } n| t vrt|\} } n| t vrt } | } n| t vrt|\} } nr| t krFtd@\} } | td@f} | drl| r<| d7} | d 7} n&| tkrldfd d ttD} tt| | | | | || VqdS) 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. Nrr9zto rTr:r8r?z with formatc3s"|]\}}d|>@r|VqdS)r8Nr)Z.0rFZsrZrrZ gs  z*_get_instructions_bytes..)r_unpack_opargsrCZhasconstrmZhasnamernhasjrelrZhaslocalZ hascompareZcmp_opZhasfreer FORMAT_VALUE_CONVERTERSZboolrrDrMMAKE_FUNCTION_FLAGSr rX)codevarnamesrE constantscellsrirjlabelsrUrWoprVrlr\rrorrg7sN         rgc Cs<|j|j}tt|}t|j||j|j|j|||ddS)zDisassemble a code object.rN) rQrPrerr-r$rOrNrL)rRlastirrhrirrrrms    rcCspt||d|dus|dkrl|dur,|d}|jD]8}t|dr2t|dtd|f|dt|||dq2dS)Nrrr8r$zDisassembly of %r:r)rrLr&r*r,)rRrrr1rrrr,ts    r,)rrjc Cs|du} | r8t||} | dkr2tt| } n d} nd} t|d} | dkr^tt| } nd} t|||||||dD]J}| o|jduo|jdk}|rt|d|j|k}t|| || |dqxdS) NirTrr9i'r:rjr) ZmaxZvalueslenrrgrUrWr*r`)rtr{rurErvrwrirrjZ show_linenoZ maxlinenor^Z maxoffsetr_ZinstrZnew_source_lineZis_current_instrrrrr-s4    r-cKstt|dfi|dS)zN)r,r)rZkwargsrrrr.rKr.ccsdd}tdt|dD]J}||}|tkrN||d|B}|tkrH|d>nd}nd}|||fVqdS)Nrr9r8r;)r@r}Z HAVE_ARGUMENTZ EXTENDED_ARG)rtZ extended_argrFryrZrrrrpsrpcCs\g}t|D]J\}}}|dur |tvr4|d|}n|tvr |}nq ||vr ||q |S)z`Detect all offsets in a byte code which are jump targets. Return the list of offsets. Nr9)rprqZhasjabsrA)rtrxrWryrZZlabelrrrrs rc cs|jddd}|jddd}t|j}d}|j}d}t||D]P\}}|rz||krd||fV|}||7}||krzdS|dkr|d8}||7}qB||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. rNr9r8r=r>)Z co_lnotabr}r$rfZzip) rtZbyte_incrementsZline_incrementsZ bytecode_lenZ lastlinenoZlinenoZaddrZ byte_incrZ line_incrrrrrs&   rc@sLeZdZdZdddddZddZdd Zed d Zd d Z ddZ dS)r aThe bytecode operations of a piece of code Instantiate this with a function, method, other compiled object, string of code, or a code object (as returned by compile()). Iterating over this yields the bytecode operations as Instruction instances. N)rdcurrent_offsetcCsdt||_}|dur&|j|_d|_n||_||j|_|j|j|_tt ||_ ||_ ||_ dS)Nr) rIcodeobjrfrd _line_offsetrQrP _cell_namesrer _linestarts_original_objectr~)r]r1rdr~rRrrr__init__s zBytecode.__init__c Cs*|j}t|j|j|j|j|j|j|jdS)Nr|) rrgr$rOrNrLrrr)r]rRrrr__iter__s zBytecode.__iter__cCsd|jj|jS)Nz{}({!r}))ZformatZ __class__r0rr]rrr__repr__s zBytecode.__repr__cCs |jr|j}q||jj|jdS)z/ Construct a Bytecode from the given traceback )r~)r3r4r5r6)Zclsr7rrrfrom_tracebackszBytecode.from_tracebackcCs t|jS)z3Return formatted information about the code object.)rJrrrrrinforKz Bytecode.infoc Csx|j}|jdur|j}nd}t@}t|j|j|j|j|j |j |j ||d | WdS1sj0YdS)z3Return a formatted view of the bytecode operations.Nrz)rurErvrwrirjrr{) rr~ioZStringIOr-r$rOrNrLrrrZgetvalue)r]rRrWZoutputrrrr s   z Bytecode.dis) r0rarbrcrrr classmethodrrrrrrrr s  r cCszddl}|}|jd|dddd|}|j}|}Wdn1sT0Yt||jjd}t |dS) z*Simple test program to disassemble a file.rNinfileZrbz?z-)r/ZnargsZdefaultr) argparseZArgumentParserZ add_argumentZFileTypeZ parse_argsrZreadrrr)rZparserZargsrrrtrrr_tests&rZ__main__Nr)NNNNNr)rz)rzNNNNN);rcr2ZtypesZ collectionsrZopcoderZ _opcodes_allZ MethodTypeZ FunctionTypeZCodeTyperZ staticmethodr/r)Zopmapr rrrrrrrsrrrrBrGrIrrJr Z namedtuplerSrXrZrlr\rWrUrVrYr[r r rmrnrgrr,r-r.rrprrr rr0rrrrZs   -  4  6   =