ó
\K]c           @` s¬   d  d l  m Z m Z m Z d  d l m Z m Z m Z d  d l j j	 Z
 d  d l m Z m Z m Z d e f d     YZ d e f d     YZ d	 e f d
     YZ d S(   i    (   t   print_functiont   divisiont   absolute_import(   t   Typet   Buildert   ConstantN(   t   typest   cgutilst   configt   _ArgManagerc           B` s)   e  Z d  Z d   Z d   Z d   Z RS(   sA   
    A utility class to handle argument unboxing and cleanup
    c         C` sC   | |  _  | |  _ | |  _ | |  _ d |  _ g  |  _ | |  _ d  S(   Ni    (   t   contextt   buildert   apit   env_managert	   arg_countt   cleanupst   nextblk(   t   selfR
   R   R   R   t   endblkt   nargs(    (    s0   lib/python2.7/site-packages/numba/callwrapper.pyt   __init__   s    						c         ` s×    j  j  |    t j  j   j    j j  j  Wd QX    f d   }  j j	 |   j j
 d  j  }  j j |   |    j j  j  Wd QX|  _  j d 7_   j S(   sk  
        Unbox argument and emit code that handles any error during unboxing.
        Args are cleaned up in reverse order of the parameter list, and
        cleanup begins as soon as unboxing of any argument fails. E.g. failure
        on arg2 will result in control flow going through:

            arg2.err -> arg1.err -> arg0.err -> arg.end (returns)
        Nc           ` sj    j  j    j  j    j d  k	 r8   j   n   j j rf  j j j	  j
    j  n  d  S(   N(   R   t   reflect_native_valuet   valueR   t   cleanupt   NoneR
   t
   enable_nrtt   nrtt   decrefR   (    (   t   nativeR   t   ty(    s0   lib/python2.7/site-packages/numba/callwrapper.pyt   cleanup_arg'   s
    s	   arg%d.erri   (   R   t   to_native_valueR   t   if_unlikelyR   t   is_errort   branchR   R   t   appendt   append_basic_blockR   t
   goto_blockR   (   R   t   objR   R   t
   cleanupblk(    (   R   R   R   s0   lib/python2.7/site-packages/numba/callwrapper.pyt   add_arg   s    
	c         C` s   x |  j  D] } |   q
 Wd S(   sR   
        Emit the cleanup code after returning from the wrapped function.
        N(   R   (   R   t   dtor(    (    s0   lib/python2.7/site-packages/numba/callwrapper.pyt   emit_cleanupB   s    (   t   __name__t
   __module__t   __doc__R   R(   R*   (    (    (    s0   lib/python2.7/site-packages/numba/callwrapper.pyR	   	   s   			,t   _GilManagerc           B` s    e  Z d  Z d   Z d   Z RS(   sY   
    A utility class to handle releasing the GIL and then re-acquiring it
    again.
    c         C` s.   | |  _  | |  _ | |  _ | j   |  _ d  S(   N(   R   R   t   argmant   save_threadt   thread_state(   R   R   R   R/   (    (    s0   lib/python2.7/site-packages/numba/callwrapper.pyR   P   s    			c         C` s$   |  j  j |  j  |  j j   d  S(   N(   R   t   restore_threadR1   R/   R*   (   R   (    (    s0   lib/python2.7/site-packages/numba/callwrapper.pyR*   V   s    (   R+   R,   R-   R   R*   (    (    (    s0   lib/python2.7/site-packages/numba/callwrapper.pyR.   J   s   	t   PyCallWrapperc           B` s>   e  Z d    Z d   Z d   Z d   Z d   Z d   Z RS(   c         C` s:   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   R
   t   modulet   funct   fndesct   envt   release_gil(   R   R
   R4   R5   R6   R7   t   call_helperR8   (    (    s0   lib/python2.7/site-packages/numba/callwrapper.pyR   \   s    					c   
      C` sĖ   |  j  j } |  j j t j  } t j | | | | g  } |  j j	 | d | } t
 | j d   } | j \ } } } d | _ d | _ d | _ |  j j |  }	 |  j |	 | | | |  | |	 f S(   Nt   namet   entryt
   py_closuret   py_argst   py_kws(   R6   t   llvm_cpython_wrapper_nameR
   t   get_argument_typeR   t   pyobjectR   t   functionR4   t   add_functionR   R$   t   argsR:   t   get_python_apit   build_wrapper(
   R   t   wrapnamet   pyobjt   wraptyt   wrapperR   t   closureRD   t   kwsR   (    (    s0   lib/python2.7/site-packages/numba/callwrapper.pyt   builde   s    			c         C` s¤  t  |  j j  } g  t |  D] } | j   ^ q } | j | |  j j | | |  }	 | j t j	 |	 t
 j |	 j   }
 t j | |
   | j | j    Wd  QX| j d  } | j |   | j | j    Wd  QX|  j | |  } t |  j | | | | |  } g  } xm t | |  j j  D]V \ } } t | t j  rZ| j d   q,| j | j |  |  } | j |  q,W|  j r¤t | | |  } n  |  j j  j! | |  j" |  j j# |  j j |  \ } } |  j$ | d  | j%   |  j$ | d  | j& | j' d t( V | j& | j)   | j*   Wd  QX|  j+   } | j, | | |  } | j |  Wd  QX|  j j  j- | | |  | j | j    d  S(   Ns   arg.ends   # callwrapper: emit_cleanups   # callwrapper: emit_cleanup endt   likely(.   t   lenR6   t   argtypest   ranget
   alloca_objt   unpack_tuplet   qualnamet   icmpt   lct   ICMP_EQR   t   nullt   typeR   R    t   rett   get_null_objectR$   R%   t   get_envR	   R
   t   zipt
   isinstanceR   t   OmittedR#   R   R(   t   loadR8   R.   t	   call_convt   call_functionR5   t   restypet   debug_printR*   t   if_thent   is_okt   Truet   is_nonet   return_nonet   _simplified_return_typet   from_native_returnt   raise_error(   R   R   R   RK   RD   RL   R   t   _t   objst   parseokt   predR   R   t   cleanup_managert	   innerargsR&   R   t   valt   statust   retvalt   retty(    (    s0   lib/python2.7/site-packages/numba/callwrapper.pyRF   ~   sF    %$"	
c         C` s   |  j  j |  j  } |  j  j | j |  } | j |  } |  j  j | |  } | j | d t | j	 |  j
 | |  } | S(   sp   Get the Environment object which is declared as a global
        in the module of the wrapped function.
        t   return_pyobject(   R
   t   get_env_nameR6   t   declare_env_globalR4   R`   t   get_env_bodyt   emit_environment_sentryRg   t   get_env_managerR7   (   R   R   R   t   envnamet   gvptrt   envptrt   env_bodyR   (    (    s0   lib/python2.7/site-packages/numba/callwrapper.pyR\   ·   s    c         C` s-   |  j  j } t | t j  r% | j S| Sd S(   s   
        The NPM callconv has already converted simplified optional types.
        We can simply use the value type from it.
        N(   R6   Rc   R^   R   t   OptionalRY   (   R   Rc   (    (    s0   lib/python2.7/site-packages/numba/callwrapper.pyRj   Å   s    c         C` s,   t  j r( |  j j | d j |   n  d  S(   Ns   DEBUGJIT: {0}(   R   t	   DEBUG_JITR
   Rd   t   format(   R   R   t   msg(    (    s0   lib/python2.7/site-packages/numba/callwrapper.pyRd   Ń   s    	(   R+   R,   R   RM   RF   R\   Rj   Rd   (    (    (    s0   lib/python2.7/site-packages/numba/callwrapper.pyR3   [   s   				9		(   t
   __future__R    R   R   t   llvmlite.llvmpy.coreR   R   R   t   llvmpyt   coreRV   t   numbaR   R   R   t   objectR	   R.   R3   (    (    (    s0   lib/python2.7/site-packages/numba/callwrapper.pyt   <module>   s   A