ó
\K]c           @   s  d  d l  m Z m Z m Z m Z m Z d  d l m Z d d l m	 Z	 d e
 f d „  ƒ  YZ e j e ƒ d „  ƒ Z d „  Z d	 „  Z d
 e f d „  ƒ  YZ e ƒ  Z d e f d „  ƒ  YZ e ƒ  Z d e f d „  ƒ  YZ e ƒ  Z d „  Z d „  Z d „  Z d S(   iÿÿÿÿ(   t   irt   ir_utilst   typest   errorst   sigutils(   t   typeof_impli   (   t   find_region_inout_varst   WithContextc           B   s/   e  Z d  Z e Z d „  Z d „  Z d „  Z RS(   sX   A dummy object for use as contextmanager.
    This can be used as a contextmanager.
    c         C   s   d  S(   N(    (   t   self(    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyt	   __enter__   s    c         C   s   d  S(   N(    (   R   t   typt   valt   tb(    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyt   __exit__   s    c         C   s
   t  ‚ d S(   sé  Mutate the *blocks* to implement this contextmanager.

        Parameters
        ----------
        func_ir : FunctionIR
        blocks : dict[ir.Block]
        blk_start, blk_end : int
            labels of the starting and ending block of the context-maanger.
        body_block: sequence[int]
            A sequence of int's representing labels of the with-body
        dispatcher_factory : callable
            A callable that takes a `FunctionIR` and returns a `Dispatcher`.
        N(   t   NotImplementedError(   R   t   func_irt   blockst	   blk_startt   blk_endt   body_blockst   dispatcher_factoryt   extra(    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyt   mutate_with_body   s    (   t   __name__t
   __module__t   __doc__t   Falset   is_callableR	   R   R   (    (    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyR      s
   		c         C   s   t  j |  ƒ S(   N(   R   t   ContextManager(   R   t   c(    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyt   typeof_contextmanager$   s    c         C   s$   |  j  d ƒ s  |  j d ƒ d Sd S(   s.   Get parent of the variable given its name
    t   $t   .i    N(   t
   startswitht   split(   t   name(    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyt   _get_var_parent)   s    c         C   s   x | D] } |  | =q Wd S(   s-   Remove keys in *to_clear* from *blocks*.
    N(    (   R   t   to_cleart   b(    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyt   _clear_blocks2   s    t   _ByPassContextTypec           B   s   e  Z d  Z d „  Z RS(   s_   A simple context-manager that tells the compiler to bypass the body
    of the with-block.
    c   
         s   | d  k s t ‚ | j } d „  | j | Dƒ ‰  d „  | j | Dƒ ‰ ‡  ‡ f d †  t t ˆ ƒ Dƒ }	 t | | | |	 ƒ t | | ƒ d  S(   Nc         S   s   i  |  ] } | t  | ƒ “ q S(    (   R$   (   t   .0t   k(    (    s1   lib/python2.7/site-packages/numba/withcontexts.pys
   <dictcomp>B   s   	 c         S   s   i  |  ] } | t  | ƒ “ q S(    (   R$   (   R)   R*   (    (    s1   lib/python2.7/site-packages/numba/withcontexts.pys
   <dictcomp>C   s   	 c            s!   i  |  ] } ˆ | ˆ  | “ q S(    (    (   R)   R*   (   t   inmapt   outmap(    s1   lib/python2.7/site-packages/numba/withcontexts.pys
   <dictcomp>D   s   	 (   t   Nonet   AssertionErrort   variable_lifetimet   livemapt   filtert   boolt   _bypass_with_contextR'   (
   R   R   R   R   R   R   R   R   t   vltt   forwardvars(    (   R+   R,   s1   lib/python2.7/site-packages/numba/withcontexts.pyR   =   s    	"(   R   R   R   R   (    (    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyR(   9   s   t   _CallContextTypec           B   s   e  Z d  Z d „  Z RS(   sq   A simple context-manager that tells the compiler to lift the body of the
    with-block as another function.
    c            sï   | d  k s t ‚ | j } t d ˆ  d | j d | d | d t | ƒ ƒ \ }	 }
 ‡  f d †  | Dƒ } t | | | |	 |
 ƒ | j d | d t |	 ƒ d t	 |	 ƒ d	 t
 ƒ } | | ƒ } t | ˆ  | | |	 |
 ƒ } | ˆ  | <t ˆ  | ƒ | S(
   NR   R0   t   callfromt   returntot   body_block_idsc            s   i  |  ] } ˆ  | | “ q S(    (    (   R)   R*   (   R   (    s1   lib/python2.7/site-packages/numba/withcontexts.pys
   <dictcomp>^   s   	 t	   arg_namest	   arg_countt   force_non_generator(   R-   R.   R/   R   R0   t   sett   _mutate_with_block_calleet   derivet   tuplet   lent   Truet   _mutate_with_block_callerR'   (   R   R   R   R   R   R   R   R   R4   t   inputst   outputst   lifted_blkst	   lifted_irt
   dispatchert   newblk(    (   R   s1   lib/python2.7/site-packages/numba/withcontexts.pyR   Q   s,    		
		
(   R   R   R   R   (    (    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyR6   M   s   t   _ObjModeContextTypec           B   s/   e  Z d  Z e Z d „  Z d „  Z d „  Z RS(   s  Creates a contextmanager to be used inside jitted functions to enter
    *object-mode* for using interpreter features.  The body of the with-context
    is lifted into a function that is compiled in *object-mode*.  This
    transformation process is limited and cannot process all possible
    Python code.  However, users can wrap complicated logic in another
    Python function, which will then be executed by the interpreter.

    Use this as a function that takes keyword arguments only.
    The argument names must correspond to the output variables from the
    with-block.  Their respective values are strings representing the expected
    types.  When exiting the with-context, the output variables are cast
    to the expected nopython types according to the annotation.  This process
    is the same as passing Python objects into arguments of a nopython
    function.

    Example::

        import numpy as np
        from numba import njit, objmode

        def bar(x):
            # This code is executed by the interpreter.
            return np.asarray(list(reversed(x.tolist())))

        @njit
        def foo():
            x = np.arange(5)
            y = np.zeros_like(x)
            with objmode(y='intp[:]'):  # annotate return type
                # this region is executed by object-mode.
                y += bar(x)
            return y

    .. note:: Known limitations:

        - with-block cannot use incoming list objects.
        - with-block cannot use incoming function objects.
        - with-block cannot ``yield``, ``break``, ``return`` or ``raise``           such that the execution will leave the with-block immediately.
        - with-block cannot contain `with` statements.
        - random number generator states do not synchronize; i.e.           nopython-mode and object-mode uses different RNG states.

    .. note:: When used outside of no-python mode, the context-manager has no
        effect.

    .. warning:: This feature is experimental.  The supported features may
        change with or without notice.

    c         C   s·   | d k r i  St | d ƒ d k r8 t j d ƒ ‚ n  | d } i  } xh | j ƒ  D]Z \ } } t | t j ƒ s‡ t | j t	 ƒ r™ t j d ƒ ‚ n  t
 j | j ƒ | | <qU W| S(   s;   
        Legalize arguments to the context-manager
        t   argsi    s8   objectmode context doesn't take any positional argumentst   kwargss@   objectmode context requires constants string for type annotationN(   R-   RA   R   t   CompilerErrort   itemst
   isinstanceR    t   Constt   valuet   strR   t   _parse_signature_string(   R   R   t   loct
   callkwargst   typeannsR*   t   v(    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyt   _legalize_args­   s    
&c            sÃ  |  j  | d ˆ  | j ƒ} | j }	 t d ˆ  d |	 j d | d | d t | ƒ ƒ \ }
 } d „  } t t | | ƒ ƒ } t | ƒ t | ƒ } | r¶ d } t j	 | j
 | ƒ ƒ ‚ n  t | ƒ t | ƒ } | ró d	 } t j	 | j
 | ƒ ƒ ‚ n  t j g  | D] } | | ^ q ƒ } ‡  f d
 †  | Dƒ } t | | | |
 | ƒ | j d | d t |
 ƒ d t |
 ƒ d t ƒ } | | d t d | ƒ} t | ˆ  | | |
 | ƒ } | ˆ  | <t ˆ  | ƒ | S(   NRT   R   R0   R7   R8   R9   c         S   s   |  j  d d ƒ d S(   NR    i   i    (   R"   (   t   x(    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyt   strip_var_verÓ   s    sk   Invalid type annotation on non-outgoing variables: {}.Suggestion: remove annotation of the listed variabless1   missing type annotation on outgoing variables: {}c            s   i  |  ] } ˆ  | | “ q S(    (    (   R)   R*   (   R   (    s1   lib/python2.7/site-packages/numba/withcontexts.pys
   <dictcomp>ê   s   	 R:   R;   R<   t
   objectmodet   output_types(   RX   RT   R/   R   R0   R=   t   listt   mapR   t   TypingErrort   formatR   t   TupleR>   R?   R@   RA   RB   RC   R'   (   R   R   R   R   R   R   R   R   RV   R4   RD   RE   RZ   t   stripped_outst   extra_annotatedt   msgt   not_annotatedRW   t   outtupRF   RG   RH   RI   (    (   R   s1   lib/python2.7/site-packages/numba/withcontexts.pyR   Å   sD    			&
			
c         O   s   |  S(   N(    (   R   RK   RL   (    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyt   __call__   s    (   R   R   R   RB   R   RX   R   Rg   (    (    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyRJ   x   s
   2		;c   
   	   C   sµ   |  | } | j  } | j } t j d | d | ƒ } xQ | j ƒ  D]C \ } }	 | j t j d | j | ƒ d | j |	 ƒ d | ƒ ƒ qA W| j t j d | d | ƒ ƒ | |  | <d S(   s¤   Given the starting and ending block of the with-context,
    replaces the head block with a new block that jumps to the end.

    *blocks* is modified inplace.
    t   scopeRT   RQ   t   targetN(	   Rh   RT   R    t   BlockRN   t   appendt   Assignt	   get_exactt   Jump(
   R   R   R   R5   t   sblkRh   RT   RI   R*   RW   (    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyR3     s    
		c   
      C   s`   | | } | j  } | j } t j d | d | ƒ }	 t j d |	 d |  d | d | d | ƒ |	 S(   sk  Make a new block that calls into the lifeted with-context.

    Parameters
    ----------
    dispatcher : Dispatcher
    blocks : dict[ir.Block]
    blk_start, blk_end : int
        labels of the starting and ending block of the context-manager.
    inputs: sequence[str]
        Input variable names
    outputs: sequence[str]
        Output variable names
    Rh   RT   t   newblockt   calleet
   label_nextRD   RE   (   Rh   RT   R    Rj   R   t   fill_block_with_call(
   RH   R   R   R   RD   RE   Ro   Rh   RT   Rp   (    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyRC     s    
			c   	      C   sŽ   t  |  ƒ } |  | } | j } | j } t j d t j d | d | ƒ d | d | ƒ |  | <t j d t j d | d | ƒ d | ƒ |  | <d S(   sF  Mutate *blocks* for the callee of a with-context.

    Parameters
    ----------
    blocks : dict[ir.Block]
    blk_start, blk_end : int
        labels of the starting and ending block of the context-manager.
    inputs: sequence[str]
        Input variable names
    outputs: sequence[str]
        Output variable names
    t   blockRh   RT   RD   Rr   RE   N(   t   minRh   RT   R   t   fill_callee_prologueR    Rj   t   fill_callee_epilogue(	   R   R   R   RD   RE   t   head_blkt   temp_blkRh   RT   (    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyR>   8  s    
				N(   t   numbaR    R   R   R   R   t   numba.typing.typeofR   t
   transformsR   t   objectR   t   registerR   R$   R'   R(   t   bypass_contextR6   t   call_contextRJ   t   objmode_contextR3   RC   R>   (    (    (    s1   lib/python2.7/site-packages/numba/withcontexts.pyt   <module>   s   (				(				