o ?Og4N @sdZddlZddlZddlZddlZddlTddlmZgdeZ[ejej ej e e e fZedZdedfed fed ffZed Zd Zd dZdVdddddZdVddddZddddddddddd 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.  dWd?d@Z/dXdddBdCZ0ddddDdEZ1  dYdddFdGdHZ2dIdJZ3e0Z4dKdLZ5dMdNZ6dOdPZ7GdQdRdRZ8dSdTZ9e:dUkre9dSdS)Zz0Disassembler of Python byte code into mnemonics.N)*)__all__) code_infodis disassembledistbdiscofindlinestarts findlabels show_codeget_instructions InstructionBytecode FORMAT_VALUE)Nstrreprascii MAKE_FUNCTION)defaultsZ kwdefaultsZ annotationsZclosurecCs4z t||d}W|Styt||d}Y|Sw)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/python310/lib64/python3.10/dis.py _try_compiles rfiledepthc Csh|dur t|ddSt|dr|j}t|dr|j}nt|dr%|j}nt|dr.|j}nt|dr6|j}t|drt|j }|D]9\}}t |t r}t d ||dz t |||d Wntyw}z t d ||dWYd}~nd}~wwt |dqDdSt|d rt|||d dSt |ttfrt||ddSt |trt|||d dStd t|j)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:r zSorry:co_code(don't know how to disassemble %s objects)rhasattrr$r%r&r'r(sortedr)items isinstance _have_codeprintr TypeError_disassemble_recursivebytes bytearray_disassemble_bytesr_disassemble_strtype__name__)xr!r"r.rZx1msgrrrr+sH            rr#cCsV|durztj}Wn tytddw|jr|j}|jst|jj|j|ddS)z2Disassemble a traceback (default: last traceback).Nz no last traceback to disassembler#) syslast_tracebackAttributeError RuntimeErrortb_nextrtb_framef_codetb_lasti)tbr!rrrrXs   rZ OPTIMIZEDZ NEWLOCALSZVARARGSZ VARKEYWORDSZNESTEDZ GENERATORZNOFREEZ COROUTINEZITERABLE_COROUTINEZASYNC_GENERATOR)  @icCs`g}tdD]}d|>}||@r#|t|t|||N}|s#nq|t|d|S)z+Return pretty representation of code flags.rJrE, )rangeappendCOMPILER_FLAG_NAMESgethexjoin)flagsnamesiflagrrr pretty_flagsrs  rYcCst|dr|j}t|dr|j}nt|dr|j}nt|dr#|j}nt|dr+|j}t|tr5t|d}t|dr<|St dt |j ) zDHelper to handle methods, compiled or raw code objects, and strings.r$r%r&r'r(z r*r+) r,r$r%r&r'r(r/rrr2r8r9r:rrr_get_code_objects"        r[cCs tt|S)z1Formatted details of methods, functions, or code.)_format_code_infor[rZrrrrs rcCsxg}|d|j|d|j|d|j|d|j|d|j|d|j|d|j|dt|j |j r[|d t |j D] }|d |qQ|j rr|d t |j D] }|d |qh|j r|d t |j D] }|d |q|jr|dt |jD] }|d |q|jr|dt |jD] }|d |qd|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: )rPco_name co_filename co_argcountco_posonlyargcountco_kwonlyargcount co_nlocals co_stacksizerYco_flags co_consts enumerateco_names co_varnames co_freevars co_cellvarsrT)colinesZi_cZi_nrrrr\s<      r\cCstt||ddS)z}Print details of methods, functions, or code to *file*. If *file* is not provided, the output is printed on stdout. r#N)r1r)rlr!rrrr 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 Falsec@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 FrGcCsg}|r|jdurd|}|||jn|d||r%|dn|d|jr3|dn|d|t|j|||jt|j duri|t|j t |j ri|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%%%dd z-->z z>>z ()) starts_linerPis_jump_targetroffsetrjustopnameljust _OPNAME_WIDTHarg _OPARG_WIDTHargreprrTrstrip)self lineno_widthZmark_as_current offset_widthZfieldsZ lineno_fmtrrr _disassembles&      zInstruction._disassembleN)rqFrG)r9 __module__ __qualname____doc__rrrrrr s r ) first_linecCsTt|}|j|j}tt|}|dur||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) r[rkrjdictr co_firstlineno_get_instructions_bytesr*rirhrf)r:rrl 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_infos rcCs0|}|dur||}|}||fSt|}||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(). Nr)Z name_indexZ name_listrr~rrr_get_name_info'src #st|}d}t|D]\} } |dur"|| d}|dur"||7}| |v} d} d} durˆ} | tvr@r|VqdS)rENr).0rWsr|rr js z*_get_instructions_bytes..)r _unpack_opargsrRZhasconstrZhasnamerhasjabsrhasjrelZhaslocalZ hascompareZcmp_opZhasfreerFORMAT_VALUE_CONVERTERSboolrrTrgMAKE_FUNCTION_FLAGSr ry)codevarnamesrV constantscellsrrlabelsrurwoprvrr~rrrr7sZ   rc Cs<|j|j}tt|}t|j||j|j|j|||ddS)zDisassemble a code object.r#N) rkrjrr r6r*rirhrf)rllastir!rrrrrrps    rcCstt||d|dus|dkr6|dur|d}|jD]}t|dr5t|dtd|f|dt|||dqdSdS)Nr#rrEr*zDisassembly of %r:r )rrfr,r1r3)rlr!r"r:rrrr3ws    r3)r!rc Cst|} | rt||} | dkrtt| } nd} nd} t|d} | dkr/tt| } nd} t|||||||dD]%}| oI|jduoI|jdk}|rQt|d|j|k}t| | || |dqN)r3r)rkwargsrrrr7sr7ccsjd}tdt|dD]'}||}|tkr(||d|B}|tkr%|d>nd}nd}d}|||fVq dS)NrrFrErH)rOrZ HAVE_ARGUMENTZ EXTENDED_ARG)rZ extended_argrWrr|rrrrsrcCsdg}t|D])\}}}|dur/|tvr|d|d}n |tvr%|d}nq||vr/||q|S)z`Detect all offsets in a byte code which are jump targets. Return the list of offsets. NrF)rrrrP)rrrwrr|Zlabelrrrr s  r ccs<d}|D]\}}}|dur||kr|}||fVqdS)zqFind the offsets in a byte code which are start of lines in the source. Generate pairs (offset, lineno) N)co_lines)rZlastlinestartendlinerrrr s r c@sLeZdZdZdddddZddZdd Zed d Zd d Z ddZ dS)raThe 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)rcurrent_offsetcCsdt||_}|dur|j|_d|_n ||_||j|_|j|j|_tt ||_ ||_ ||_ dS)Nr) r[codeobjrr _line_offsetrkrj _cell_namesrr _linestarts_original_objectr)rr:rrrlrrr__init__s  zBytecode.__init__c Cs*|j}t|j|j|j|j|j|j|jdS)Nr) rrr*rirhrfrrr)rrlrrr__iter__s zBytecode.__iter__cCsd|jj|jS)Nz{}({!r}))format __class__r9rrrrr__repr__s zBytecode.__repr__cCs$|jr |j}|js||jj|jdS)z/ Construct a Bytecode from the given traceback )r)r@rArBrC)clsrDrrrfrom_tracebackszBytecode.from_tracebackcCs t|jS)z3Return formatted information about the code object.)r\rrrrrinfos z Bytecode.infoc Csx|j}|jdur |j}nd}t }t|j|j|j|j|j |j |j ||d | WdS1s5wYdS)z3Return a formatted view of the bytecode operations.Nr)rrVrrrrr!r) rrioStringIOr6r*rirhrfrrrgetvalue)rrlrwoutputrrrrs   $z Bytecode.dis) r9rrrrrr classmethodrrrrrrrrs   rcCszddl}|}|jd|dddd|}|j }|}Wdn1s*wYt||jjd}t |dS) z*Simple test program to disassemble a file.rNinfilerb?-)r8nargsdefaultr) argparseArgumentParser add_argumentZFileType parse_argsrreadrrr)rparserargsrrrrrr_tests  r__main__)N)NNNNNr)r)rNNNNN);rr<types collectionsrZopcoderZ _opcodes_all MethodType FunctionTypeCodeTyper staticmethodr8r0ZopmaprrrrrrrrrrrQrYr[rr\r namedtuplernryr|rr~rwrurvr{r}r r rrrrr3r6r7rrr r rrr9rrrrs   -  4  9    =