ó
\K]c           @` s¢  d  Z  d d l m Z m Z m Z d d l m Z d d l Z d d l m	 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 y d d l Z e j ƒ  Z Wn e k
 rÏ d Z n Xe d k	 Z i  Z i  Z e ƒ  Z d „  Z d „  Z d „  Z d a d „  Z  e! d „ Z" d „  Z# e! d „ Z$ e j% ƒ  Z& e& j' d e j( f d „  ƒ  Yƒ Z) e& j* d e j+ f d „  ƒ  Yƒ Z, d „  Z- d „  Z. d S(   su   
Support for CFFI. Allows checking whether objects are CFFI functions and
obtaining the pointer and numba signature.
i    (   t   print_functiont   divisiont   absolute_import(   t   BuiltinFunctionTypeN(   t   partial(   t   types(   t   numpy_support(   t   TypingErrori   (   t	   templatesc         C` s9   y  |  t  k p t |  t j ƒ SWn t k
 r4 t SXd  S(   N(   t   _ffi_instancest
   isinstancet   cffit   FFIt	   TypeErrort   False(   t   obj(    (    s6   lib/python2.7/site-packages/numba/typing/cffi_utils.pyt   is_ffi_instance   s     c         C` sK   y t  j |  ƒ j d k SWn* t k
 rF y |  t k SWqG t SXn Xd S(   s(   Check whether the obj is a CFFI functiont   functionN(   t   ffit   typeoft   kindR   t   _ool_func_typesR   (   R   (    (    s6   lib/python2.7/site-packages/numba/typing/cffi_utils.pyt   is_cffi_func(   s    c         C` s*   |  t  k r t  |  St t j d |  ƒ ƒ S(   sY   
    Get a pointer to the underlying function for a CFFI function as an
    integer.
    t	   uintptr_t(   t   _ool_func_ptrt   intR   t   cast(   t	   cffi_func(    (    s6   lib/python2.7/site-packages/numba/typing/cffi_utils.pyt   get_pointer2   s    c           C` sá  t  d k rÝi t j t j d ƒ 6t j t j d ƒ 6t j t j d ƒ 6t j t j d ƒ 6t j	 t j d ƒ 6t j
 t j d ƒ 6t j t j d ƒ 6t j t j d ƒ 6t j t j d	 ƒ 6t j t j d
 ƒ 6t j t j d ƒ 6t j t j d ƒ 6t j t j d ƒ 6t j t j d ƒ 6t j t j d ƒ 6t j t j d ƒ 6t j t j d ƒ 6t j
 t j d ƒ 6t j t j d ƒ 6t j t j d ƒ 6t j t j d ƒ 6t j t j d ƒ 6t j t j d ƒ 6t j t j d ƒ 6a  n  t  S(   sc   
    Lazily compute type map, as calling ffi.typeof() involves costly
    parsing of C code...
    t   boolt   chart   shortR   t   longs	   long longs   unsigned chars   unsigned shorts   unsigned ints   unsigned longs   unsigned long longt   int8_tt   uint8_tt   int16_tt   uint16_tt   int32_tt   uint32_tt   int64_tt   uint64_tt   floatt   doublet   ssize_tt   size_tt   voidN(   t   _cached_type_mapt   NoneR   t   booleanR   R   R   R   t   intct   long_t   longlongt   uchart   ushortt   uintct   ulongt	   ulonglongt   float_R*   t   intpt   uintpR-   (    (    (    s6   lib/python2.7/site-packages/numba/typing/cffi_utils.pyt	   _type_map>   s6    c         C` ss  t  t d | ƒ} t |  d d ƒ } | d k r? t d ƒ ‚ n0| d k r¤ |  j rc t d ƒ ‚ n  | |  j ƒ } g  |  j D] } | | ƒ ^ q| } t j | | Œ S| d k rå |  j	 } | j
 d	 k rÏ t j St j | | ƒ ƒ SnŠ | d
 k r"| |  j	 ƒ } |  j }	 t j d | d |	 f ƒ S| d k r>| r>t |  ƒ St ƒ  j |  ƒ }
 |
 d k rkt |  ƒ ‚ n  |
 Sd S(   sì   
    Map CFFI type to numba type.

    Parameters
    ----------
    cffi_type:
        The CFFI type to be converted.
    use_record_dtype: bool (default: False)
        When True, struct types are mapped to a NumPy Record dtype.

    t   use_record_dtypeR   t    t   unions   No support for CFFI unionR   s    vararg function is not supportedt   pointerR-   t   arrayt   dtypet   shapet   structN(   R   t   map_typet   getattrR   t   ellipsist   resultt   argsR   t	   signaturet   itemR   R   t   voidptrt   CPointert   lengtht   NestedArrayt   map_struct_to_record_dtypeR<   t   getR/   (   t	   cffi_typeR=   t   primed_map_typeR   t   restypet   argt   argtypest   pointeeRB   t   nelemRH   (    (    s6   lib/python2.7/site-packages/numba/typing/cffi_utils.pyRE   b   s2    	"		
c         C` s\  i g  d 6g  d 6g  d 6t  j |  ƒ d 6} t } x|  j D]\ } } | j d k rt d } t | j | ƒ ƒ ‚ n  | j d k r¡ d } t | j | ƒ ƒ ‚ n  | j d k rÎ d	 } t | j | ƒ ƒ ‚ n  t	 j
 t | j d
 t ƒƒ } | d j | ƒ | d j | ƒ | d j | j ƒ | | j | j d k M} q; Wt	 j t j | d | ƒƒ S(   s2   Convert a cffi type into a NumPy Record dtype
    t   namest   formatst   offsetst   itemsizeiÿÿÿÿs.   field {!r} has bitshift, this is not supportedi    s+   field {!r} has flags, this is not supporteds-   field {!r} has bitsize, this is not supportedR=   t   align(   R   t   sizeoft   Truet   fieldst   bitshiftt
   ValueErrort   formatt   flagst   bitsizeR   t   as_dtypeRE   t   typet   appendt   offsett	   alignmentt
   from_dtypet   npRB   (   RR   R`   t
   is_alignedt   kt   vt   msgRB   (    (    s6   lib/python2.7/site-packages/numba/typing/cffi_utils.pyRP   ‹   s.    c         C` sj   t  j |  ƒ p t j |  ƒ } t | d d ƒ d k rE t d ƒ ‚ n  t | d | ƒ} t j | d t	 ƒS(   sB   
    Return a Numba type for the given CFFI function pointer.
    R   R>   RD   s!   No support for CFFI struct valuesR=   R   (
   R   RQ   R   R   RF   R   RE   R   t   ExternalFunctionPointerR   (   R   R=   RR   t   sig(    (    s6   lib/python2.7/site-packages/numba/typing/cffi_utils.pyt   make_function_type¬   s
    t   FFI_from_bufferc           B` s   e  Z d  Z d „  Z RS(   s   ffi.from_bufferc         C` sÈ   | s t  | ƒ d k r d  S| \ } t | t j ƒ sM t d | f ƒ ‚ n  | j d k rr t d | f ƒ ‚ n  | j d k r¦ | j d k r¦ t d | f ƒ ‚ n  t j | j ƒ } t	 j
 | | ƒ S(   Ni   s.   from_buffer() expected a buffer object, got %st   Ct   Fs<   from_buffer() unsupported on non-contiguous buffers (got %s)sJ   from_buffer() only supports multidimensional arrays with C layout (got %s)(   Ru   Rv   (   t   lenR
   R   t   BufferR   t   layoutt   ndimRM   RB   R   RJ   (   t   selfRI   t   kwst   aryt   ptr(    (    s6   lib/python2.7/site-packages/numba/typing/cffi_utils.pyt   generic½   s    	(   t   __name__t
   __module__t   keyR   (    (    (    s6   lib/python2.7/site-packages/numba/typing/cffi_utils.pyRt   ¹   s   t   FFIAttributec           B` s   e  Z e j Z d  „  Z RS(   c         C` s   t  j t t  j ƒ S(   N(   R   t   BoundFunctionRt   R   (   R{   R   (    (    s6   lib/python2.7/site-packages/numba/typing/cffi_utils.pyt   resolve_from_bufferÑ   s    (   R€   R   R   R   R‚   R…   (    (    (    s6   lib/python2.7/site-packages/numba/typing/cffi_utils.pyRƒ   Í   s   	c         C` s¢   x› t  |  j ƒ D]Š } t |  j | ƒ } t | t ƒ rŠ |  j j | ƒ t | <|  j j |  j | j	 ƒ } t
 |  j j d | ƒ ƒ t | <n  t j |  j ƒ q Wd S(   sS   
    Add typing for all functions in an out-of-line CFFI module to the typemap
    R   N(   t   dirt   libRF   R
   R   R   R   R   t	   addressofR€   R   R   R   R	   t   add(   t   modt   ft   addr(    (    s6   lib/python2.7/site-packages/numba/typing/cffi_utils.pyt   register_moduleÕ   s    "c         C` s   t  ƒ  } | | |  <d S(   s9   
    Add typing for a given CFFI type to the typemap
    N(   R<   (   RR   t
   numba_typet   tm(    (    s6   lib/python2.7/site-packages/numba/typing/cffi_utils.pyt   register_typeá   s    	(/   t   __doc__t
   __future__R    R   R   R   R   t   ctypest	   functoolsR   t   numpyRl   t   numbaR   t   numba.errorsR   R>   R   R   R   R   t   ImportErrorR/   t	   SUPPORTEDR   R   t   setR	   R   R   R   R.   R<   R   RE   RP   Rs   t   Registryt   registryt   registert   AbstractTemplateRt   t   register_attrt   AttributeTemplateRƒ   R   R   (    (    (    s6   lib/python2.7/site-packages/numba/typing/cffi_utils.pyt   <module>   sB   
		
	
	
	$)	!			