a �DOgZP� @sdZddlZddlZddlZddlZddlTddlmZgd�eZ[ejej ej e e e fZedZdedfed fed ffZed Zd Zd d�ZdVddd�dd�ZdWdd�dd�Zddddddddddd� Zd d!�Zd"d#�Zd$d%�Zd&d'�Zdd�d(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*Gd6d7�d7e!�Z+dd8�d9d:�Z,d;d<�Z-d=d>�Z.dXd?d@�Z/dYdd�dBdC�Z0ddd�dDdE�Z1dZdddF�dGdH�Z2dIdJ�Z3e0Z4dKdL�Z5dMdN�Z6dOdP�Z7GdQdR�dR�Z8dSdT�Z9e:dUk�re9�dS)[z0Disassembler of Python byte code into mnemonics.�N)�*)�__all__) � code_info�dis� disassemble�distb�disco�findlinestarts� findlabels� show_code�get_instructions� Instruction�Bytecode� FORMAT_VALUE)N��str�repr�ascii� MAKE_FUNCTION)�defaults� kwdefaults� annotationsZclosurecCs4zt||d�}Wnty.t||d�}Yn0|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�/usr/lib64/python3.9/dis.py� _try_compiles  r!��file�depthc Csj|durt|d�dSt|d�r&|j}t|d�r8|j}n4t|d�rJ|j}n"t|d�r\|j}nt|d�rl|j}t|d�r�t|j� ��}|D]r\}}t |t �r�t d ||d�zt |||d �Wn2ty�}zt d ||d�WYd}~n d}~00t |d�q�nht|d ��rt|||d �nLt |ttf��r8t||d�n.t |t��rTt|||d �ntd t|�j��dS)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. N�r#�__func__�__code__�gi_code�ag_code�cr_code�__dict__zDisassembly of %s:r"zSorry:�co_code�(don't know how to disassemble %s objects)r�hasattrr&r'r(r)r*�sortedr+�items� isinstance� _have_code�printr� TypeError�_disassemble_recursive�bytes� bytearray�_disassemble_bytesr�_disassemble_str�type�__name__)�xr#r$r0rZx1�msgrrr r+s@          $  �rr%cCsV|dur>z tj}Wnty.td�d�Yn0|jr>|j}q0t|jj|j|d�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)�tbr#rrr rXs  rZ OPTIMIZEDZ NEWLOCALSZVARARGSZ VARKEYWORDSZNESTEDZ GENERATORZNOFREEZ COROUTINEZITERABLE_COROUTINEZASYNC_GENERATOR) ������ �@��icCs`g}td�D]:}d|>}||@r |�t�|t|���||N}|s qVq |�t|��d�|�S)z+Return pretty representation of code flags.rLrG�, )�range�append�COMPILER_FLAG_NAMES�get�hex�join)�flags�names�i�flagrrr � pretty_flagsrs r[cCs�t|d�r|j}t|d�r"|j}n4t|d�r4|j}n"t|d�rF|j}nt|d�rV|j}t|t�rjt|d�}t|d�rx|St dt |�j ��d S) zDHelper to handle methods, compiled or raw code objects, and strings.r&r'r(r)r*z r,r-N) r.r&r'r(r)r*r1rr!r4r:r;�r<rrr �_get_code_object�s"        �r]cCs tt|��S)z1Formatted details of methods, functions, or code.)�_format_code_infor]r\rrr r�srcCs�g}|�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|j�rH|�d�t |j�D]}|�d |��q2|j�rz|�d�t |j�D]}|�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:� )rR�co_name� co_filename� co_argcount�co_posonlyargcount�co_kwonlyargcount� co_nlocals� co_stacksizer[�co_flags� co_consts� enumerate�co_names� co_varnames� co_freevars� co_cellvarsrV)�co�linesZi_cZi_nrrr r^�s<     r^cCstt|�|d�dS)z}Print details of methods, functions, or code to *file*. If *file* is not provided, the output is printed on stdout. r%N)r3r)rnr#rrr r �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 False��c@seZdZdZddd�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 �FrIcCs�g}|r:|jdur,d|}|�||j�n|�d|�|rJ|�d�n |�d�|jrf|�d�n |�d�|�t|j��|��|�|j�t��|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%%%dd� z-->z z>>z �(�))� starts_linerR�is_jump_targetr�offset�rjust�opname�ljust� _OPNAME_WIDTH�arg� _OPARG_WIDTH�argreprrV�rstrip)�self� lineno_widthZmark_as_current� offset_width�fieldsZ lineno_fmtrrr � _disassemble�s&      zInstruction._disassembleN)rsFrI)r;� __module__� __qualname__�__doc__r�rrrr r �s r )� first_linecCsTt|�}|j|j}tt|��}|dur4||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) r]rmrl�dictr �co_firstlineno�_get_instructions_bytesr,rkrjrh)r<r�rn� cell_names� linestarts� line_offsetrrr r s    �r cCs |}|dur||}|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�r)Z const_indexZ const_list�argvalrrr �_get_const_infosr�cCs*|}|dur||}|}nt|�}||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(). Nr�)Z name_indexZ name_listr�r�rrr �_get_name_info's r�c #s�t|�}d}t|�D�]r\} } �|durD|�| d�}|durD||7}| |v} d} d} �du�rl�} | tvrzt�|�\} } n�| tvr�t�|�\} } n�| tvr�| d�} dt| �} n�| t vr�t�|�\} } n�| t vr�t �} | } n�| t vr�t�|�\} } nr| t k�rFt�d@\} } | t�d@�f} | d�rl| �r<| d7} | d 7} n&| tk�rld��fd d �tt�D��} 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. NrrHzto rsrIrGrPz with formatc3s"|]\}}�d|>@r|VqdS)rGNr)�.0rY�s�r~rr � gs  �z*_get_instructions_bytes..)r �_unpack_opargsrTZhasconstr�Zhasnamer��hasjrelrZhaslocalZ hascompareZcmp_opZhasfreer�FORMAT_VALUE_CONVERTERS�boolrrVri�MAKE_FUNCTION_FLAGSr r{)�code�varnamesrX� constants�cellsr�r��labelsrwry�oprxr�r�rr�r r�7sN         �r������c Cs<|j|j}tt|��}t|j||j|j|j|||d�dS)zDisassemble a code object.r%N) rmrlr�r r8r,rkrjrh)rn�lastir#r�r�rrr rms    �rcCspt||d�|dus|dkrl|dur,|d}|jD]8}t|d�r2t|d�td|f|d�t|||d�q2dS)Nr%rrGr,zDisassembly of %r:r")rrhr.r3r5)rnr#r$r<rrr r5ts    r5)r#r�c Cs�|du} | r8t|���|} | dkr2tt| ��} qN)r5r!)r�kwargsrrr r9�sr9ccsdd}tdt|�d�D]J}||}|tkrN||d|B}|tkrH|d>nd}nd}|||fVqdS)NrrHrGrJ)rQr�Z HAVE_ARGUMENTZ EXTENDED_ARG)r�Z extended_argrYr�r~rrr r��sr�cCs\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. NrH)r�r�ZhasjabsrR)r�r�ryr�r~Zlabelrrr r �s r c cs�|jddd�}|jddd�}t|j�}d}|j}d}t||�D]P\}}|rz||krd||fV|}||7}||krzdS|dkr�|d8}||7}qB||kr�||fVdS)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. rNrHrGrNrO)� co_lnotabr�r,r��zip) r�Zbyte_incrementsZline_incrementsZ bytecode_lenZ lastlineno�lineno�addrZ byte_incrZ line_incrrrr r �s&   r c@sLeZdZdZddd�dd�Zdd�Zdd �Zed d ��Zd d �Z dd�Z 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)r��current_offsetcCsdt|�|_}|dur&|j|_d|_n||_||j|_|j|j|_tt |��|_ ||_ ||_ dS)Nr) r]�codeobjr�r�� _line_offsetrmrl� _cell_namesr�r � _linestarts�_original_objectr�)r�r<r�r�rnrrr �__init__�s zBytecode.__init__c Cs*|j}t|j|j|j|j|j|j|jd�S)Nr�) r�r�r,rkrjrhr�r�r�)r�rnrrr �__iter__�s �zBytecode.__iter__cCsd�|jj|j�S)Nz{}({!r}))�format� __class__r;r��r�rrr �__repr__�s �zBytecode.__repr__cCs |jr|j}q||jj|jd�S)z/ Construct a Bytecode from the given traceback )r�)rBrCrDrE)�clsrFrrr �from_traceback�szBytecode.from_tracebackcCs t|j�S)z3Return formatted information about the code object.)r^r�r�rrr �infosz Bytecode.infoc Csx|j}|jdur|j}nd}t���@}t|j|j|j|j|j |j |j ||d� |� �Wd�S1sj0YdS)z3Return a formatted view of the bytecode operations.Nr�)r�rXr�r�r�r�r#r�) r�r��io�StringIOr8r,rkrjrhr�r�r��getvalue)r�rnry�outputrrr r s   �z Bytecode.dis) r;r�r�r�r�r�r�� classmethodr�r�rrrrr r�s  rcCszddl}|��}|jd|�d�ddd�|��}|j�}|��}Wd�n1sT0Yt||jjd�}t |�dS) z*Simple test program to disassemble a file.rN�infile�rb�?�-)r:�nargs�defaultr) �argparse�ArgumentParser� add_argument�FileType� parse_argsr��readrrr)r��parser�argsr�rr�rrr �_tests&r��__main__)N)N)NNNNNr)r�)r�NNNNN);r�r>�types� collectionsr�ZopcoderZ _opcodes_all� MethodType� FunctionType�CodeTyper�� staticmethodr:r2Zopmaprrrrr�rr�r!rrrSr[r]rr^r � namedtuplerpr{r~r�r�ryrwrxr}rr r r�r�r�rr5r8r9rr�r r rr�r;rrrr �s� � �� -�  �4 � 6 ��  =