
[[\C                 @   s`  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Td d l m Z d d d d d	 d
 d d d d d g e Z [ e j e j	 e j
 e f Z d d   Z d d d d d Z d d d d d Z i d d 6d d 6d d 6d d 6d d 6d d  6d! d" 6Z d# d$   Z d% d&   Z d' d   Z d( d)   Z d d d* d  Z e j d+ d,  Z Gd- d   d e  Z d. d d/ d  Z d0 d1   Z d2 d3   Z d d d d d d d4 d5  Z dB d d d6 d Z dC d d d d d d d d7 d d8 d9 Z d d d: d;  Z e Z  d< d   Z! d= d
   Z" Gd> d   d  Z# d? d@   Z$ e% dA k r\e$   n  d S)Dz0Disassembler of Python byte code into mnemonics.    N)*)__all__	code_infodisdisassembledistbdiscofindlinestarts
findlabels	show_codeget_instructionsInstructionBytecodec             C   sA   y t  |  | d  } Wn$ t k
 r< t  |  | d  } Yn X| 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
    evalexec)compileSyntaxError)sourcenamec r   /usr/lib/python3.4/dis.py_try_compile   s
    r   filec            C   s  |  d k r t  d |  d St |  d  r8 |  j }  n  t |  d  rS |  j }  n  t |  d  rt |  j j    } x| D] \ } } t | t  r~ t	 d | d | y t
 | d | Wn8 t k
 r } z t	 d | d | WYd d } ~ Xn Xt	 d |  q~ q~ Wn t |  d  r5t |  d | nc t |  t t f  r]t |  d | n; t |  t  rt |  d | n t d	 t |   j   d S)
znDisassemble classes, methods, functions, or code.

    With no argument, disassemble the last traceback.

    Nr   __func____code____dict__zDisassembly of %s:zSorry:co_codez(don't know how to disassemble %s objects)r   hasattrr   r   sortedr   items
isinstance
_have_codeprintr   	TypeErrorr   bytes	bytearray_disassemble_bytesstr_disassemble_strtype__name__)xr   r    r   Zx1msgr   r   r   r      s2    &c            C   sv   |  d k rV y t  j }  Wn t k
 r9 t d   Yn Xx |  j rR |  j }  q= Wn  t |  j j |  j d | d S)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   r   r   r   r   @   s     Z	OPTIMIZED   Z	NEWLOCALS   ZVARARGS   ZVARKEYWORDS   ZNESTED   Z	GENERATOR    ZNOFREE@   c             C   s   g  } xq t  d  D]P } d | >} |  | @r | j t j | t |    |  | N}  |  sc Pqc q q W| j t |    d j |  S)z+Return pretty representation of code flags.r<   r7   z, )rangeappendCOMPILER_FLAG_NAMESgethexjoin)flagsnamesiZflagr   r   r   pretty_flagsW   s    


rG   c             C   s   t  |  d  r |  j }  n  t  |  d  r6 |  j }  n  t |  t  rW t |  d  }  n  t  |  d  rj |  St d t |   j   d S)zAHelper to handle methods, functions, strings and raw code objectsr   r   z<disassembly>r   z(don't know how to disassemble %s objectsN)	r   r   r   r!   r(   r   r$   r*   r+   )r,   r   r   r   _get_code_objecte   s    rH   c             C   s   t  t |    S)z1Formatted details of methods, functions, or code.)_format_code_inforH   )r,   r   r   r   r   r   s    c             C   s  g  } | j  d |  j  | j  d |  j  | j  d |  j  | j  d |  j  | j  d |  j  | j  d |  j  | j  d t |  j   |  j	 r | j  d  x+ t
 |  j	  D] } | j  d	 |  q Wn  |  j r | j  d
  x+ t
 |  j  D] } | j  d |  qWn  |  j rd| j  d  x+ t
 |  j  D] } | j  d |  qFWn  |  j r| j  d  x+ t
 |  j  D] } | j  d |  qWn  |  j r| j  d  x+ t
 |  j  D] } | j  d |  qWn  d j |  S)NzName:              %szFilename:          %szArgument count:    %szKw-only arguments: %szNumber of locals:  %szStack size:        %szFlags:             %sz
Constants:z%4d: %rzNames:z%4d: %szVariable names:zFree variables:zCell 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linesZi_cZi_nr   r   r   rI   v   s:    					rI   c            C   s   t  t |   d | d S)z}Print details of methods, functions, or code to *file*.

    If *file* is not provided, the output is printed on stdout.
    r   N)r#   r   )rX   r   r   r   r   r      s    _InstructionzBopname opcode arg argval argrepr offset starts_line is_jump_targetc               @   s(   e  Z d  Z d Z d d d d  Z d S)r   aK  Details 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
       Fc             C   s9  g  } | rP |  j  d k	 r< d | } | j | |  j   qP | j d |  n  | rf | j d  n | j d  |  j r | j d  n | j d  | j t |  j  j d   | j |  j j d	   |  j d k	 r&| j t |  j  j d
   |  j	 r&| j d |  j	 d  q&n  d j
 |  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  r9         ())starts_liner?   is_jump_targetreproffsetrjustopnameljustargargreprrC   rstrip)selflineno_widthZmark_as_currentZfieldsZ
lineno_fmtr   r   r   _disassemble   s&    
		zInstruction._disassembleN)r+   
__module____qualname____doc__rm   r   r   r   r   r      s   
first_linec            C   sx   t  |   } | j | j } t t |   } | d k	 rJ | | j } n d } 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   )rH   rW   rV   dictr	   co_firstlineno_get_instructions_bytesr   rU   rT   rR   )r,   rq   rX   
cell_names
linestartsline_offsetr   r   r   r      s    c             C   s/   |  } | d k	 r | |  } n  | t  |  f S)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)rc   )Zconst_indexZ
const_listargvalr   r   r   _get_const_info   s    ry   c             C   s;   |  } | d k	 r% | |  } | } n t  |  } | | f S)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)rc   )Z
name_indexZ	name_listrx   ri   r   r   r   _get_name_info   s    
	rz   c          	   c   s@  t  |   } d } d }	 d }
 t |   } d } x	| | k  r;|  | } | } | d k	 r | j | d  }	 |	 d k	 r |	 | 7}	 q n  | | k } | d } d } d } d } | t k r|  | |  | d d | } d } | d } | t k r| d } n  | } | t k r.t | |  \ } } q| t k rRt | |  \ } } q| t	 k r{| | } d t
 |  } q| t k rt | |  \ } } q| t k rt | } | } q| t k rt | |  \ } } q| t k rd	 |  | d |  | d f } qn  t t | | | | | | |	 |  Vq3 Wd S)
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.

    r   Nr7       r8   i   zto z%d positional, %d keyword pair)r
   lenrA   HAVE_ARGUMENTZEXTENDED_ARGZhasconstry   Zhasnamerz   hasjrelrc   ZhaslocalZ
hascompareZcmp_opZhasfreeZhasnargsr   rf   )codevarnamesrE   	constantscellsrv   rw   labelsZextended_argra   ZfreenrF   oprd   rb   rh   rx   ri   r   r   r   rt     sX    





	&	rt   c         
   C   sT   |  j  |  j } t t |    } t |  j | |  j |  j |  j | | d | d S)zDisassemble a code object.r   N)	rW   rV   rr   r	   r'   r   rU   rT   rR   )rX   lastir   ru   rv   r   r   r   r   A  s    rw   c         	   C   s   | d  k	 }	 |	 r d n d }
 x t  |  | | | | | d | D]k } |	 og | j d  k	 og | j d k } | r t d |  n  | j | k } t | j |
 |  d | q@ Wd  S)Nr[   r   rw   r   )rt   ra   rd   r#   rm   )r   r   r   rE   r   r   rv   r   rw   Zshow_linenorl   ZinstrZnew_source_lineZis_current_instrr   r   r   r'   H  s    r'   c            C   s   t  t |  d  d | d S)z<Compile the source string, then disassemble the code object.z<dis>r   N)r   r   )r   r   r   r   r   r)   Z  s    r)   c             C   s   g  } t  |   } d } x | | k  r |  | } | d } | t k r |  | |  | d d } | d } d } | t k r | | } n | t k r | } n  | d k r | | k r | j |  q q q q W| S)z`Detect all offsets in a byte code which are jump targets.

    Return the list of offsets.

    r   r7   r|   r8   )r}   r~   r   Zhasjabsr?   )r   r   r   rF   r   rh   Zlabelr   r   r   r
   `  s$    


	c             c   s   t  |  j d d d   } t  |  j d d d   } d } |  j } d } xZ t | |  D]I \ } } | r | | k r | | f V| } n  | | 7} n  | | 7} q] W| | k r | | f Vn  d S)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.

    r   Nr8   r7   )list	co_lnotabrs   zip)r   Zbyte_incrementsZline_incrementsZ
lastlinenolinenoZaddrZ	byte_incrZ	line_incrr   r   r   r	   {  s    		c               @   sp   e  Z d  Z d Z d d d d d d  Z d d   Z d	 d
   Z e d d    Z d d   Z	 d d   Z
 d S)r   zThe 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.
    rq   Ncurrent_offsetc            C   s   t  |  |  _ } | d  k r7 | j |  _ d |  _ n | |  _ | | j |  _ | j | j |  _ t t	 |   |  _
 | |  _ | |  _ d  S)Nr   )rH   codeobjrs   rq   _line_offsetrW   rV   _cell_namesrr   r	   _linestarts_original_objectr   )rk   r,   rq   r   rX   r   r   r   __init__  s    		zBytecode.__init__c          	   C   s=   |  j  } t | j | j | j | j |  j |  j d |  j S)Nrw   )	r   rt   r   rU   rT   rR   r   r   r   )rk   rX   r   r   r   __iter__  s
    		zBytecode.__iter__c             C   s   d j  |  j j |  j  S)Nz{}({!r}))format	__class__r+   r   )rk   r   r   r   __repr__  s    zBytecode.__repr__c             C   s2   x | j  r | j  } q W|  | j j d | j S)z/ Construct a Bytecode from the given traceback r   )r2   r3   r4   r5   )clsr6   r   r   r   from_traceback  s    zBytecode.from_tracebackc             C   s   t  |  j  S)z3Return formatted information about the code object.)rI   r   )rk   r   r   r   info  s    zBytecode.infoc             C   s   |  j  } |  j d k	 r$ |  j } n d } t j   ` } t | j d | j d | j d | j d |  j	 d |  j
 d |  j d	 | d
 | | j   SWd QXd S)z3Return a formatted view of the bytecode operations.Nr7   r   rE   r   r   rv   rw   r   r   r   )r   r   ioStringIOr'   r   rU   rT   rR   r   r   r   getvalue)rk   rX   rd   outputr   r   r   r     s    				zBytecode.dis)r+   rn   ro   rp   r   r   r   classmethodr   r   r   r   r   r   r   r     s   c           
   C   s   d d l  }  |  j   } | j d d |  j   d d d d | j   } | j  } | j   } Wd QXt | | j j d	  } t	 |  d S)
z*Simple test program to disassemble a file.r   Ninfiler*   nargs?default-r   )
argparseArgumentParseradd_argumentZFileType
parse_argsr   readr   r   r   )r   parserargsr   r   r   r   r   r   _test  s    %r   __main__r   r   )&rp   r.   typescollectionsr   Zopcoder   Z_opcodes_all
MethodTypeFunctionTypeCodeTyper*   r"   r   r   r   r@   rG   rH   r   rI   r   
namedtuplerZ   r   r   ry   rz   rt   r   r'   r)   r   r
   r	   r   r   r+   r   r   r   r   <module>   s^   
	!
		3	<	=