3
^#                 @   s  d Z ddlZddlmZ ddlmZ ddlmZ ddl	m
Z ddlmZ ddlmZmZmZmZ d	ZerlendZG d
d dZejefddZe ZejZejZejZej Z ej!Z!ej"Z"ej#Z#ej$Z$ejZej%Z%ejZej&Z&ejZejZej'Z'ej(Z(ej)Z)ej
Z
ej*Z*ej+Z+ej,Z,dS )a  
Provide the same API as Python 3.x so xdis can be used as a drop in
replacement for dis. This will provide a dis module with support for the
Python version being run.

Why would you want this? The main reason is if you want a compatibility shim
for supporting the more advanced Python 3 dis API (being able to iterate through
opcodes, supplying a custom file to dump the dis to) across python versions, for
example:

    import xdis.std as dis

    # works in Python 2 and 3
    for op in dis.Bytecode('for i in range(10): pass'):
        print(op)

There is also the ability to generate an std api for a specific version, for example:

    from xdis.std import make_std_api
    dis = make_std_api(2.4)
    # dis can now disassemble code objects from python 2.4

Version 'variants' are also supported, for example:

    from xdis.std import make_std_api
    dis = make_std_api(2.6, 'pypy')
    # dis can now disassemble pypy code objects from python 2.6

    N)IS_PYPY)Bytecode)_Instruction)disco)get_opcode_module)	code_infopretty_flags	show_codexstack_effectZpypyc               @   s   e Zd ZejefddZdddZdd Zddd	Z	dd
dZ
dd Zd ddZd!ddZd#ddZd%ddZd&ddZdd Zdd ZdS )'_StdApic                s   |dkrdd l j}n
dd lj}|| _t|| | _j| _|t	k| _
j| _j| _j| _j| _j| _j| _G  fdddt  | _G fdddt}|| _d S )	N      r   c                   s&   e Zd ZdZd fdd	Z  ZS )z"_StdApi.__init__.<locals>.Bytecodea  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.
            Nc                s   t  | j|||d d S )N)opc
first_linecurrent_offset)super__init__)selfxr   r   )r   	__class__r    E/home/psgendb/BIRCHDEV/python/lib/python3.6/site-packages/xdis/std.pyr   [   s    z+_StdApi.__init__.<locals>.Bytecode.__init__)NN)__name__
__module____qualname____doc__r   __classcell__r   )r   r   )r   r   r   S   s   r   c                   s   e Zd ZdZ fddZdS )z%_StdApi.__init__.<locals>.Instructiona  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
            c                s   t ||  | _d S )N)r   r   )r   argskwargs)r   r   r   r   n   s    
z._StdApi.__init__.<locals>.Instruction.__init__N)r   r   r   r   r   r   )r   r   r   Instruction`   s   r   )r   r   )Zxdis.wordcodeZwordcodexdis.bytecodebytecodexcoder   r   versionpython_versionPYPYis_pypyhasconsthasnameopmapopnameEXTENDED_ARGHAVE_ARGUMENT	_Bytecoder   r   r   )r   r$   variantr"   r   r   )r   r   r   r   A   s"    

z_StdApi.__init__Nc             C   s(   |d krt | n|jt|d  d S )N
)printwritestr)r   r   filer   r   r   _prints   s    
z_StdApi._printc             C   s   t || jS )z1Formatted details of methods, functions, or code.)
_code_infor$   )r   r   r   r   r   r   y   s    z_StdApi.code_infoc             C   s   t || jj|| jdS )zPrint details of methods, functions, or code to *file*.

        If *file* is not provided, the output is printed on stdout.
        )r&   )
_show_coder   r#   r&   )r   r   r3   r   r   r   r	   }   s    z_StdApi.show_codec             C   s   t t| j||S )zDCompute the stack effect of *opcode* with argument *oparg*.
        )_stack_effectr   r   )r   ZopargZjumpr   r   r   stack_effect   s    z_StdApi.stack_effectc             C   s   t |S )z+Return pretty representation of code flags.)_pretty_flags)r   flagsr   r   r   r      s    z_StdApi.pretty_flagsc             C   s   | j | j|j | dS )zDisassemble classes, methods, functions, generators, or code.

        With no argument, disassemble the last traceback.

        N)r4   r   dis)r   r   r3   r   r   r   r;      s    z_StdApi.disc             C   s\   |dkrBy
t j}W n tk
r.   tdY nX x|jr@|j}q2W | j|jj|j|d dS )z2Disassemble a traceback (default: last traceback).Nz no last traceback to disassemble)r3   )	syslast_tracebackAttributeErrorRuntimeErrortb_nextdisassembletb_framef_codetb_lasti)r   tbr3   r   r   r   distb   s    
 
z_StdApi.distb   c             C   s   | j |||S )zDisassemble a code object.)r   )r   codelastir3   r   r   r   rA      s    z_StdApi.disassemblec             C   s   t | j|d|| jddS )zDisassemble a code object.NF)	timestampoutr&   header)_discor$   r&   )r   rH   rI   r3   r   r   r   r      s    
z_StdApi.discoc             C   s   | 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.
        )r   get_instructions)r   r   r   r   r   r   rN      s    z_StdApi.get_instructionsc             C   s   | j j|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   findlinestarts)r   rH   r   r   r   rO      s    z_StdApi.findlinestartsc             C   s   | j j|| j S )zhDetect all offsets in a byte code which are jump targets.

        Return the list of offsets.

        )r   
findlabels)r   rH   r   r   r   rP      s    z_StdApi.findlabels)N)N)NN)NN)NN)rQ   NrQ   )rQ   N)N)r   r   r   r<   version_infoVARIANTr   r4   r   r	   r8   r   r;   rF   rA   r   rN   rO   rP   r   r   r   r   r   ?   s   2








r   c             C   s8   t | tr.t| }t| | d d }||f} t| |S )a  
    Generate an object which can be used in the same way as the python
    standard dis module. The difference is that the generated 'module' can be
    used to disassemble byte / word code from a different python version than
    the version of the interpreter under which we are running.

    :param python_version: Generate a dis module for this version of python
                           (defaults to the currently running python version.)
    :param variant:        The string denoting the variant of the python version
                           being run, for example 'pypy' or 'alpha0', 'rc1' etc,
                           or None to auto detect based on the currently running
                           interpreter.

    :return: object which can be used like the std dis module.
    g?
   )
isinstancefloatintr   )r$   r.   majorminorr   r   r   make_std_api   s
    
rZ   )-r   r<   xdisr   r    r   r-   Zxdis.instructionr   Z	xdis.mainr   rM   Zxdis.op_importsr   Zxdis.cross_disr   r5   r   r9   r	   r6   r
   r7   r%   rS   r   rR   rZ   Z_std_apir'   r(   r)   r*   r+   r,   r"   r   r   r4   r;   rF   rA   rN   rO   rP   r   r   r   r   <module>.   sD    