ó
\K]c           @   sW  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l m Z m Z d d l m Z d d l	 m
 Z
 m Z d d l m Z m Z m Z m Z m Z m Z d d l m Z m Z d d l m Z m Z m Z m Z d d	 l m Z d
 „  Z i e d 6Z  i  e d „ Z! d „  Z" d „  Z# d „  Z$ d „  Z% d e& f d „  ƒ  YZ' d „  Z( d „  Z) d S(   iÿÿÿÿN(   t   typest   configi   (   t   typeof_impl(   t   infert   infer_getattr(   t   lower_builtint   lower_getattrt   lower_getattr_generict   lower_setattrt   lower_setattr_generict
   lower_cast(   t   modelst   register_default(   t   boxt   unboxt   reflectt   NativeValue(   t   _import_cython_functionc            s”   d d l  m ‰  m ‰ m ‰ t ˆ ƒ rH t ˆ t ƒ rH t d ƒ ‚ n  y ˆ j ‰ Wn t	 k
 rt t ˆ ƒ ‰ n X‡  ‡ ‡ ‡ ‡ f d †  } | S(   sØ   
    Decorate a function as implementing typing for the callable *func*.
    *func* can be a callable object (probably a global) or a string
    denoting a built-in operation (such 'getitem' or '__array_wrap__')
    i   (   t   CallableTemplateR   t   infer_globals%   `func` should be a function or stringc            s   ‡  f d †  } d ˆ f } ˆ f } t  d ˆ d | ƒ } t | | | ƒ } ˆ | ƒ t ˆ ƒ r{ ˆ ˆ t j | ƒ ƒ n  d  S(   Nc            s   ˆ  |  j  ƒ S(   N(   t   context(   t   self(   t   typing_func(    s.   lib/python2.7/site-packages/numba/extending.pyt   generic"   s    s   %s_CallableTemplatet   keyR   (   t   dictt   typet   callableR    t   Function(   R   R   t   namet   basest
   class_dictt   template(   R   t   funct	   func_nameR   R   (   R   s.   lib/python2.7/site-packages/numba/extending.pyt   decorate!   s    	
(
   t   typing.templatesR   R   R   R   t
   isinstancet   strt	   TypeErrort   __name__t   AttributeError(   R!   R#   (    (   R   R!   R"   R   R   s.   lib/python2.7/site-packages/numba/extending.pyt   type_callable   s    t   no_cpython_wrapperc            sN   d d l  m ‰ m ‰ t j ƒ  ‰ ˆ j | ƒ ‡  ‡ ‡ ‡ ‡ f d †  } | S(   s·  
    A decorator marking the decorated function as typing and implementing
    *func* in nopython mode.

    The decorated function will have the same formal parameters as *func*
    and be passed the Numba types of those parameters.  It should return
    a function implementing *func* for the given types.

    Here is an example implementing len() for tuple types::

        @overload(len)
        def tuple_len(seq):
            if isinstance(seq, types.BaseTuple):
                n = len(seq)
                def len_impl(seq):
                    return n
                return len_impl

    Compiler options can be passed as an dictionary using the **jit_options**
    argument.

    Overloading strictness (that the typing and implementing signatures match)
    is enforced by the **strict** keyword argument, it is recommended that this
    is set to True (default).

    To handle a function that accepts imprecise types, an overload
    definition can return 2-tuple of ``(signature, impl_function)``, where
    the ``signature`` is a ``typing.Signature`` specifying the precise
    signature to be used; and ``impl_function`` is the same implementation
    function as in the simple case.
    i   (   t   make_overload_templateR   c            sH   ˆ ˆ  |  ˆ ˆ ƒ } t  | ƒ t ˆ  ƒ rD ˆ ˆ  t j | ƒ ƒ n  |  S(   N(   R   R   R    R   (   t   overload_funcR    (   R!   R   R,   t   optst   strict(    s.   lib/python2.7/site-packages/numba/extending.pyR#   [   s
    
(   R$   R,   R   t   _overload_default_jit_optionst   copyt   update(   R!   t   jit_optionsR/   R#   (    (   R!   R   R,   R.   R/   s.   lib/python2.7/site-packages/numba/extending.pyt   overload5   s
     c             s'   ‡  f d †  } ˆ  r | S| |  Œ  Sd S(   s®  
    Register a regular python function that can be executed by the python
    interpreter and can be compiled into a nopython function when referenced
    by other jit'ed functions.  Can be used as::

        @register_jitable
        def foo(x, y):
            return x + y

    Or, with compiler options::

        @register_jitable(_nrt=False) # disable runtime allocation
        def foo(x, y):
            return x + y

    c            s+   t  ˆ  d ˆ d t ƒ‡  f d †  ƒ } ˆ  S(   NR3   R/   c             s   ˆ  S(   N(    (   t   argst   kwargs(   t   fn(    s.   lib/python2.7/site-packages/numba/extending.pyt   ov_wrapx   s    (   R4   t   False(   R7   R8   (   R6   (   R7   s.   lib/python2.7/site-packages/numba/extending.pyt   wrapv   s    'N(    (   R5   R6   R:   (    (   R6   s.   lib/python2.7/site-packages/numba/extending.pyt   register_jitablee   s    c            s)   d d l  m ‰ ‡  ‡ ‡ f d †  } | S(   s  
    A decorator marking the decorated function as typing and implementing
    attribute *attr* for the given Numba type in nopython mode.

    Here is an example implementing .nbytes for array types::

        @overload_attribute(types.Array, 'nbytes')
        def array_nbytes(arr):
            def get(arr):
                return arr.size * arr.itemsize
            return get
    i   (   t    make_overload_attribute_templatec            s    ˆ ˆ ˆ  |  ƒ } t  | ƒ |  S(   N(   R   (   R-   R    (   t   attrR<   t   typ(    s.   lib/python2.7/site-packages/numba/extending.pyR#   “   s    
(   R$   R<   (   R>   R=   R#   (    (   R=   R<   R>   s.   lib/python2.7/site-packages/numba/extending.pyt   overload_attributeƒ   s    c            s)   d d l  m ‰ ‡  ‡ ‡ f d †  } | S(   ss  
    A decorator marking the decorated function as typing and implementing
    attribute *attr* for the given Numba type in nopython mode.

    Here is an example implementing .take() for array types::

        @overload_method(types.Array, 'take')
        def array_take(arr, indices):
            if isinstance(indices, types.Array):
                def take_impl(arr, indices):
                    n = indices.shape[0]
                    res = np.empty(n, arr.dtype)
                    for i in range(n):
                        res[i] = arr[indices[i]]
                    return res
                return take_impl
    i   (   t   make_overload_method_templatec            s    ˆ ˆ ˆ  |  ƒ } t  | ƒ |  S(   N(   R   (   R-   R    (   R=   R@   R>   (    s.   lib/python2.7/site-packages/numba/extending.pyR#   ¯   s    
(   R$   R@   (   R>   R=   R#   (    (   R=   R@   R>   s.   lib/python2.7/site-packages/numba/extending.pyt   overload_method›   s    c            sñ   d d l  m } d d l m ‰ d d l m ‰  d d l m ‰ d d l m	 ‰ t
 ˆ t ƒ ss t ˆ t j ƒ r‰ t d ˆ f ƒ ‚ n  ‡  ‡ ‡ f d †  ‰ t d	 | f ‡ ‡ ‡ f d
 †  ƒ  Yƒ } t ˆ ˆ ƒ ‡ ‡ ‡ ‡ f d †  ƒ } d S(   sÅ   
    Make an automatic attribute wrapper exposing member named *struct_attr*
    as a read-only attribute named *python_attr*.
    The given *typeclass*'s model must be a StructModel subclass.
    i   (   t   AttributeTemplate(   t   default_manager(   t   StructModel(   t   impl_ret_borrowed(   t   cgutilss+   typeclass should be a Type subclass, got %sc            sA   ˆ j  |  ƒ } t | ˆ  ƒ s4 t d | f ƒ ‚ n  | j ˆ ƒ S(   sF   
        Get the Numba type of member *struct_attr* in *typ*.
        sK   make_struct_attribute_wrapper() needs a type with a StructModel, but got %s(   t   lookupR%   R'   t   get_member_fe_type(   R>   t   model(   RD   RC   t   struct_attr(    s.   lib/python2.7/site-packages/numba/extending.pyt   get_attr_fe_typeÇ   s
    t   StructAttributec              s    e  Z ˆ Z ‡  ‡ f d  †  Z RS(   c            s   | ˆ k r ˆ  | ƒ Sd  S(   N(    (   R   R>   R=   (   RK   t   python_attr(    s.   lib/python2.7/site-packages/numba/extending.pyt   generic_resolveÕ   s    (   R(   t
   __module__R   RN   (    (   RK   RM   t	   typeclass(    s.   lib/python2.7/site-packages/numba/extending.pyRL   Ñ   s   c            sL   ˆ  j  | ƒ |  | d | ƒ} ˆ | ƒ } t | ˆ ƒ } ˆ |  | | | ƒ S(   Nt   value(   t   create_struct_proxyt   getattr(   R   t   builderR>   t   valt   attrtyt   attrval(   RF   RK   RE   RJ   (    s.   lib/python2.7/site-packages/numba/extending.pyt   struct_getattr_implÙ   s    N(   R$   RB   t	   datamodelRC   t   datamodel.modelsRD   t   targets.imputilsRE   t    RF   R%   R   t
   issubclassR    t   TypeR'   R   R   (   RP   RJ   RM   RB   RL   RX   (    (   RD   RF   RC   RK   RE   RM   RJ   RP   s.   lib/python2.7/site-packages/numba/extending.pyt   make_attribute_wrapper·   s    #
"t
   _Intrinsicc           B   s‰   e  Z d  Z e j ƒ  Z e j d e j	 ƒ Z
 d
 Z d „  Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z e d	 „  ƒ Z RS(   s&   
    Dummy callable for intrinsic
    t   maxlenc         C   s   | |  _  | |  _ d  S(   N(   t   _namet   _defn(   R   R   t   defn(    (    s.   lib/python2.7/site-packages/numba/extending.pyt   __init__ì   s    	c         C   s;   |  j  } | d k r7 t t j ƒ  ƒ } |  j | ƒ n  | S(   s­   
        An instance-specific UUID, to avoid multiple deserializations of
        a given instance.

        Note this is lazily-generated, for performance reasons.
        N(   t   _Intrinsic__uuidt   NoneR&   t   uuidt   uuid1t	   _set_uuid(   R   t   u(    (    s.   lib/python2.7/site-packages/numba/extending.pyt   _uuidð   s
    	c         C   s?   |  j  d  k s t ‚ | |  _  |  |  j | <|  j j |  ƒ d  S(   N(   Rf   Rg   t   AssertionErrort   _memot   _recentt   append(   R   Rk   (    (    s.   lib/python2.7/site-packages/numba/extending.pyRj   þ   s    	c         C   sR   d d l  m } m } | |  |  j |  j ƒ } t | ƒ | |  t j | ƒ ƒ d  S(   Ni   (   t   make_intrinsic_templateR   (   R$   Rq   R   Rc   Rb   R   R    R   (   R   Rq   R   R    (    (    s.   lib/python2.7/site-packages/numba/extending.pyt	   _register  s    
c         O   s   d j  |  ƒ } t | ƒ ‚ d S(   sP   
        This is only defined to pretend to be a callable from CPython.
        s    {0} is not usable in pure-pythonN(   t   formatt   NotImplementedError(   R   R5   R6   t   msg(    (    s.   lib/python2.7/site-packages/numba/extending.pyt   __call__  s    c         C   s   d j  |  j ƒ S(   Ns   <intrinsic {0}>(   Rs   Rb   (   R   (    (    s.   lib/python2.7/site-packages/numba/extending.pyt   __repr__  s    c            sP   d d l  m ‰  ‡  f d †  } ˆ  j |  j t |  j ƒ |  j | |  j ƒ f f S(   Niÿÿÿÿ(   t	   serializec            s   ˆ  j  |  ƒ } ˆ  j |  | ƒ S(   N(   t#   _get_function_globals_for_reductiont   _reduce_function(   R7   t   gs(   Rx   (    s.   lib/python2.7/site-packages/numba/extending.pyt   reduce_func  s    (   t   numbaRx   t   _rebuild_reductiont	   __class__R&   Rl   Rb   Rc   (   R   R|   (    (   Rx   s.   lib/python2.7/site-packages/numba/extending.pyt
   __reduce__  s
    c         C   ss   d d l  m } y |  j | SWnM t k
 rn | j | Œ  } |  d | d | ƒ } | j ƒ  | j | ƒ | SXd  S(   Niÿÿÿÿ(   Rx   R   Rd   (   R}   Rx   Rn   t   KeyErrort   _rebuild_functionRr   Rj   (   t   clsRh   R   t   defn_reducedRx   Rd   t   llc(    (    s.   lib/python2.7/site-packages/numba/extending.pyt   _rebuild   s    
N(   R(   RO   t   __doc__t   weakreft   WeakValueDictionaryRn   t   collectionst   dequeR   t   FUNCTION_CACHE_SIZERo   Rg   Rf   Re   t   propertyRl   Rj   Rr   Rv   Rw   R€   t   classmethodR†   (    (    (    s.   lib/python2.7/site-packages/numba/extending.pyR`   á   s   						c             s6   ‡ f d †  ‰  ˆ s ˆ  |  Œ  S‡  f d †  } | Sd S(   su  
    A decorator marking the decorated function as typing and implementing
    *func* in nopython mode using the llvmlite IRBuilder API.  This is an escape
    hatch for expert users to build custom LLVM IR that will be inlined to
    the caller.

    The first argument to *func* is the typing context.  The rest of the
    arguments corresponds to the type of arguments of the decorated function.
    These arguments are also used as the formal argument of the decorated
    function.  If *func* has the signature ``foo(typing_context, arg0, arg1)``,
    the decorated function will have the signature ``foo(arg0, arg1)``.

    The return values of *func* should be a 2-tuple of expected type signature,
    and a code-generation function that will passed to ``lower_builtin``.
    For unsupported operation, return None.

    Here is an example implementing a ``cast_int_to_byte_ptr`` that cast
    any integer to a byte pointer::

        @intrinsic
        def cast_int_to_byte_ptr(typingctx, src):
            # check for accepted types
            if isinstance(src, types.Integer):
                # create the expected type signature
                result_type = types.CPointer(types.uint8)
                sig = result_type(types.uintp)
                # defines the custom code generation
                def codegen(context, builder, signature, args):
                    # llvm IRBuilder code here
                    [src] = args
                    rtype = signature.return_type
                    llrtype = context.get_value_type(rtype)
                    return builder.inttoptr(src, llrtype)
                return sig, codegen
    c            s8   t  |  d t |  ƒ ƒ } t | |  ˆ   } | j ƒ  | S(   NR(   (   RS   R&   R`   Rr   (   R!   R   R…   (   R6   (    s.   lib/python2.7/site-packages/numba/extending.pyt
   _intrinsicT  s    
c            s
   ˆ  |  ƒ S(   N(    (   R!   (   R   (    s.   lib/python2.7/site-packages/numba/extending.pyt   wrapper`  s    N(    (   R5   R6   R   (    (   R   R6   s.   lib/python2.7/site-packages/numba/extending.pyt	   intrinsic/  s
    %
c         C   s   t  |  | ƒ S(   sþ   
    Get the address of a Cython function.

    Args
    ----
    module_name:
        Name of the Cython module
    function_name:
        Name of the Cython function

    Returns
    -------
    A Python int containing the address of the function

    (   R   (   t   module_namet   function_name(    (    s.   lib/python2.7/site-packages/numba/extending.pyt   get_cython_function_addresse  s    (*   t   inspectRh   Rˆ   RŠ   R}   R    R   t   typing.typeofR   R$   R   R   R[   R   R   R   R   R	   R
   RY   R   R   t   register_modelt	   pythonapiR   R   R   R   t
   _helperlibR   R*   t   TrueR0   R4   R;   R?   RA   R_   t   objectR`   R‘   R”   (    (    (    s.   lib/python2.7/site-packages/numba/extending.pyt   <module>   s(   ."	0				*N	6