grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026î   fCã @s`dZddlZddlZddlZddlZddlTddlmZddddd d d d d ddg eZ[ejej ej e fZ dd„Z ddddd„Zddddd„Zidd6dd6dd6dd6dd6dd 6d!d"6Zd#d$„Zd%d&„Zd'd„Zd(d)„Zddd*d „Zejd+d,ƒZGd-d„deƒZd.dd/d „Zd0d1„Zd2d3„Zddddddd4d5„ZdBddd6d„ZdCdddddddd7dd8d9„Zddd:d;„ZeZ d<d „Z!d=d „Z"Gd>d„dƒZ#d?d@„Z$e%dAkr\e$ƒndS)Dz0Disassembler of Python byte code into mnemonics.éN)Ú*)Ú__all__Ú code_infoÚdisÚ disassembleÚdistbÚdiscoÚfindlinestartsÚ findlabelsÚ show_codeÚget_instructionsÚ InstructionÚBytecodec CsAyt||dƒ}Wn$tk r<t||dƒ}YnX|S)zßAttempts 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 ÚevalÚexec)ÚcompileÚ SyntaxError)ÚsourceÚnameÚc©rú(/opt/alt/python34/lib64/python3.4/dis.pyÚ _try_compiles  rÚfilecCsœ|dkrtd|ƒdSt|dƒr8|j}nt|dƒrS|j}nt|dƒrt|jjƒƒ}x|D]Ž\}}t|tƒr~t d|d|ƒyt |d|ƒWn8t k rû}zt d|d|ƒWYdd}~XnXt d|ƒq~q~Wn…t|dƒr5t |d|ƒnct|t tfƒr]t|d|ƒn;t|tƒrt|d|ƒnt d t|ƒjƒ‚dS) 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)rÚhasattrrrÚsortedrÚitemsÚ isinstanceÚ _have_codeÚprintrÚ TypeErrorrÚbytesÚ bytearrayÚ_disassemble_bytesÚstrÚ_disassemble_strÚtypeÚ__name__)Úxrr rZx1Úmsgrrrrs2    &c Csv|dkrVy tj}Wntk r9tdƒ‚YnXx|jrR|j}q=Wnt|jj|jd|ƒdS)z2Disassemble a traceback (default: last traceback).Nz no last traceback to disassembler) ÚsysÚlast_tracebackÚAttributeErrorÚ RuntimeErrorÚtb_nextrÚtb_frameÚf_codeÚtb_lasti)Útbrrrrr@s    Z OPTIMIZEDéZ NEWLOCALSéZVARARGSéZ VARKEYWORDSéZNESTEDéZ GENERATORé ZNOFREEé@cCs‡g}xqtdƒD]P}d|>}||@r|jtj|t|ƒƒƒ||N}|scPqcqqW|jt|ƒƒdj|ƒS)z+Return pretty representation of code flags.r<r7z, )ÚrangeÚappendÚCOMPILER_FLAG_NAMESÚgetÚhexÚjoin)ÚflagsÚnamesÚiZflagrrrÚ pretty_flagsWs    rGcCs‡t|dƒr|j}nt|dƒr6|j}nt|tƒrWt|dƒ}nt|dƒrj|Stdt|ƒjƒ‚dS)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,rrrrrscCsùg}|jd|jƒ|jd|jƒ|jd|jƒ|jd|jƒ|jd|jƒ|jd|jƒ|jdt|jƒƒ|j rÜ|jdƒx+t |j ƒD]}|jd |ƒq¾Wn|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 |ƒqŠWn|jrì|jdƒx+t |jƒD]}|jd |ƒqÎWndj|ƒ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_argcountÚco_kwonlyargcountÚ co_nlocalsÚ co_stacksizerGÚco_flagsÚ co_constsÚ enumerateÚco_namesÚ co_varnamesÚ co_freevarsÚ co_cellvarsrC)ÚcoÚlinesZi_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(eZdZdZdddd„ZdS)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||jƒqP|jd|ƒn|rf|jdƒn |jdƒ|jrŒ|jdƒn |jdƒ|jt|jƒjdƒƒ|j|jjd ƒƒ|jdk r&|jt|jƒjd ƒƒ|j r&|jd |j d ƒq&ndj |ƒj ƒS) zß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 Nz%%%ddú z-->z z>>z r9ééú(ú)) Ú starts_liner?Úis_jump_targetÚreprÚoffsetÚrjustÚopnameÚljustÚargÚargreprrCÚrstrip)ÚselfÚ lineno_widthZmark_as_currentZfieldsZ lineno_fmtrrrÚ _disassemble­s&     zInstruction._disassembleN)r+Ú __module__Ú __qualname__Ú__doc__rmrrrrr Ÿs Ú first_linecCsxt|ƒ}|j|j}tt|ƒƒ}|dk rJ||j}nd}t|j|j|j |j |||ƒS)a«Iterator 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) rHrWrVÚdictr Úco_firstlinenoÚ_get_instructions_bytesrrUrTrR)r,rqrXÚ cell_namesÚ linestartsÚ line_offsetrrrr Òs   cCs/|}|dk r||}n|t|ƒfS)zÕHelper 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_listÚargvalrrrÚ_get_const_infoès  rycCs;|}|dk r%||}|}n t|ƒ}||fS)zÓHelper 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_infoôs     rzc cs@t|ƒ}d}d} d} t|ƒ} d} x | | kr;|| } | }|dk r‰|j| dƒ} | dk r‰| |7} q‰n| |k}| d} d}d}d}| tkr|| || dd|}d}| d} | tkr|d}n|}| tkr.t||ƒ\}}q| tkrRt||ƒ\}}q| t kr{| |}dt |ƒ}q| t krŸt||ƒ\}}q| t kr¾t |}|}q| tkrât||ƒ\}}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. rNr7Úér8izto z%d positional, %d keyword pair)r ÚlenrAÚ HAVE_ARGUMENTZ EXTENDED_ARGZhasconstryZhasnamerzÚhasjrelrcZhaslocalZ hascompareZcmp_opZhasfreeZhasnargsr rf)ÚcodeÚvarnamesrEÚ constantsÚcellsrvrwÚlabelsZ extended_argraZfreeÚnrFÚoprdrbrhrxrirrrrtsX                     &  rtc CsT|j|j}tt|ƒƒ}t|j||j|j|j||d|ƒdS)zDisassemble a code object.rN) rWrVrrr r'rrUrTrR)rXÚlastirrurvrrrrAsrwc Cs³|dk } | rdnd} xŽt||||||d|ƒD]k} | og| jdk og| jdk} | r€td|ƒn| j|k} t| j| | ƒd|ƒq@WdS)Nr[rrwr)rtrardr#rm)r€r‡r�rEr‚rƒrvrrwZ show_linenorlZinstrZnew_source_lineZis_current_instrrrrr'Hs   r'cCstt|dƒd|ƒdS)zrN)rr)rrrrrr)Zsr)cCsÕg}t|ƒ}d}x¶||krÐ||}|d}|tkr||||dd}|d}d}|tkrŠ||}n|tkrŸ|}n|dkrÍ||krÊ|j|ƒqÊqÍqqW|S)z`Detect all offsets in a byte code which are jump targets. Return the list of offsets. rr7r|r8éÿÿÿÿ)r}r~rZhasjabsr?)r€r„r…rFr†rhZlabelrrrr `s$           ccsÈt|jddd…ƒ}t|jddd…ƒ}d}|j}d}xZt||ƒD]I\}}|rœ||kr�||fV|}n||7}n||7}q]W||krÄ||fVndS)z”Find 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_lnotabrsÚzip)r€Zbyte_incrementsZline_incrementsZ lastlinenoÚlinenoZaddrZ byte_incrZ line_incrrrrr {s      c@speZdZdZdddddd„Zdd„Zd d „Zed d „ƒZd d„Z dd„Z dS)rzñThe 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. rqNÚcurrent_offsetcCsŽt|ƒ|_}|dkr7|j|_d|_n||_||j|_|j|j|_tt |ƒƒ|_ ||_ ||_ dS)Nr) rHÚcodeobjrsrqÚ _line_offsetrWrVÚ _cell_namesrrr Ú _linestartsÚ_original_objectr�)rkr,rqr�rXrrrÚ__init__™s     zBytecode.__init__c Cs=|j}t|j|j|j|j|j|jd|jƒS)Nrw) rŽrtrrUrTrRr�r‘r�)rkrXrrrÚ__iter__¦s    zBytecode.__iter__cCsdj|jj|jƒS)Nz{}({!r}))ÚformatÚ __class__r+r’)rkrrrÚ__repr__­szBytecode.__repr__cCs2x|jr|j}qW||jjd|jƒS)z/ Construct a Bytecode from the given traceback r�)r2r3r4r5)Úclsr6rrrÚfrom_traceback±s  zBytecode.from_tracebackcCs t|jƒS)z3Return formatted information about the code object.)rIrŽ)rkrrrÚinfo¸sz 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.Nr7r�rEr‚rƒrvrwrr‡rˆ) rŽr�ÚioÚStringIOr'rrUrTrRr�r‘r�Úgetvalue)rkrXrdÚoutputrrrr¼s     z Bytecode.dis) r+rnrorpr“r”r—Ú classmethodr™ršrrrrrr‘s    c Cs�ddl}|jƒ}|jdd|jƒddddƒ|jƒ}|j�}|jƒ}WdQXt||jjd ƒ}t |ƒdS) z*Simple test program to disassemble a file.rNÚinfiler*Únargsú?Údefaultú-r) ÚargparseÚArgumentParserÚ add_argumentZFileTypeÚ parse_argsr Úreadrrr)r¥ÚparserÚargsr rr€rrrÚ_testÎs  %  r¬Ú__main__rˆrˆ)&rpr.ÚtypesÚ collectionsr›ZopcoderZ _opcodes_allÚ MethodTypeÚ FunctionTypeÚCodeTyper*r"rrrr@rGrHrrIr Ú namedtuplerZr r ryrzrtrr'r)rr r rr¬r+rrrrÚs^       !       3  <    =