ó
î&]\c           @   s§   d  d l  m Z d d l Z e j e j ƒ j d Z d a d e	 f d „  ƒ  Ya
 d „  Z d e f d	 „  ƒ  YZ d d
 „ Z d „  Z d „  Z d d „ Z d „  Z d S(   i   (   t   _ccallback_ciÿÿÿÿNi    t   CDatac           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyR   	   s   c          C   sS   t  d  k	 r d  Sy% d d  l }  |  j ƒ  a  t  j a Wn t k
 rN t a  n Xd  S(   Niÿÿÿÿ(   t   ffit   Nonet   cffit   FFIR   t   ImportErrort   False(   R   (    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyt   _import_cffi   s    t   LowLevelCallablec           B   sŒ   e  Z d  Z d	 Z d
 d
 d „ Z d „  Z e d „  ƒ Z e d „  ƒ Z	 e d „  ƒ Z
 d „  Z e d
 d
 d „ ƒ Z e d
 d
 d „ ƒ Z RS(   s¤  
    Low-level callback function.

    Parameters
    ----------
    function : {PyCapsule, ctypes function pointer, cffi function pointer}
        Low-level callback function.
    user_data : {PyCapsule, ctypes void pointer, cffi void pointer}
        User data to pass on to the callback function.
    signature : str, optional
        Signature of the function. If omitted, determined from *function*,
        if possible.

    Attributes
    ----------
    function
        Callback function given
    user_data
        User data given
    signature
        Signature of the function.

    Methods
    -------
    from_cython
        Class method for constructing callables from Cython C-exported
        functions.

    Notes
    -----
    The argument ``function`` can be one of:

    - PyCapsule, whose name contains the C function signature
    - ctypes function pointer
    - cffi function pointer

    The signature of the low-level callback must match one of  those expected 
    by the routine it is passed to.

    If constructing low-level functions from a PyCapsule, the name of the 
    capsule must be the corresponding signature, in the format::

        return_type (arg1_type, arg2_type, ...)

    For example::

        "void (double)"
        "double (double, int *, void *)"

    The context of a PyCapsule passed in as ``function`` is used as ``user_data``, 
    if an explicit value for `user_data` was not given.

    c         C   s.   |  j  | | | ƒ } t j |  | | | f ƒ S(   N(   t   _parse_callbackt   tuplet   __new__(   t   clst   functiont	   user_datat	   signaturet   item(    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyR   T   s    c         C   s   d j  |  j |  j ƒ S(   Ns   LowLevelCallable({!r}, {!r})(   t   formatR   R   (   t   self(    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyt   __repr__Z   s    c         C   s   t  j |  d ƒ S(   Ni   (   R   t   __getitem__(   R   (    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyR   ]   s    c         C   s   t  j |  d ƒ S(   Ni   (   R   R   (   R   (    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyR   a   s    c         C   s   t  j t j |  d ƒ ƒ S(   Ni    (   R    t   get_capsule_signatureR   R   (   R   (    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyR   e   s    c         C   s   t  ƒ  ‚ d  S(   N(   t
   ValueError(   R   t   idx(    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyR   i   s    c         C   sf   y | j  | } WnB t k
 r0 t d ƒ ‚ n& t k
 rU t d j | ƒ ƒ ‚ n X|  | | | ƒ S(   s  
        Create a low-level callback function from an exported Cython function.

        Parameters
        ----------
        module : module
            Cython module where the exported function resides
        name : str
            Name of the exported function
        user_data : {PyCapsule, ctypes void pointer, cffi void pointer}, optional
            User data to pass on to the callback function.
        signature : str, optional
            Signature of the function. If omitted, determined from *function*.

        s?   Given module is not a Cython module with __pyx_capi__ attributes4   No function {!r} found in __pyx_capi__ of the module(   t   __pyx_capi__t   AttributeErrorR   t   KeyErrorR   (   R   t   modulet   nameR   R   R   (    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyt   from_cythonl   s    c         C   s(  t  ƒ  t | t ƒ r+ t j | d ƒ } nr t | t ƒ rR t | | ƒ \ } } nK t | t ƒ ry t | | ƒ \ } } n$ t	 j
 | ƒ r‘ | } n t d ƒ ‚ t | t j ƒ r¾ t | ƒ } nW t | t ƒ rÜ t | ƒ } n9 | d  k rñ d } n$ t	 j
 | ƒ r	| } n t d ƒ ‚ t	 j | | | ƒ S(   Ni    sM   Given input is not a callable or a low-level callable (pycapsule/ctypes/cffi)sN   Given user data is not a valid low-level void* pointer (pycapsule/ctypes/cffi)(   R
   t
   isinstanceR   R   R   t
   PyCFuncPtrt   _get_ctypes_funcR   t   _get_cffi_funcR    t   check_capsuleR   t   ctypest   c_void_pt   _get_ctypes_datat   _get_cffi_dataR   t   get_raw_capsule(   R   t   objR   R   t   funct   context(    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyR   …   s(    			(    N(   R   R   t   __doc__t	   __slots__R   R   R   t   propertyR   R   R   R   t   classmethodR    R   (    (    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyR      s   5		c         C   s¡   t  j |  t  j ƒ j } | d  k r— t |  j ƒ d } xP t |  j ƒ D]? \ } } | d k rr | t | ƒ 7} qG | d t | ƒ 7} qG W| d 7} n  | | f S(   Ns    (i    s   , t   )(	   R&   t   castR'   t   valueR   t   _typename_from_ctypest   restypet	   enumeratet   argtypes(   R,   R   t   func_ptrt   jt   arg(    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyR#   ¦   s    c         C   s   |  d  k r d S|  t j k r# d S|  j } d } x' | j d ƒ r[ | d 7} | d } q5 W| j d ƒ rx | d } n  | d k r™ | d	 d
 | 7} n  | S(   Nt   voids   void *i    t   LP_i   i   t   c_i   t    t   *(   R   R&   R'   R   t
   startswith(   R   R   t   pointer_level(    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyR5   ·   s    	
c         C   s   t  j |  t  j ƒ j S(   N(   R&   R3   R'   R4   (   t   data(    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyR(   Í   s    c         C   sO   t  j d |  ƒ } | d  k rE t  j t  j |  ƒ ƒ j d d ƒ } n  | | f S(   Nt	   uintptr_ts   (*)R?   (   R   R3   R   t   getctypet   typeoft   replace(   R,   R   R9   (    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyR$   Ö   s    'c         C   s   t  j d |  ƒ S(   NRD   (   R   R3   (   RC   (    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyR)   á   s    (   t    R    R&   t	   CFUNCTYPER'   t	   __bases__R"   R   R   t   objectR   R
   R   R   R#   R5   R(   R$   R)   (    (    (    s4   lib/python2.7/site-packages/scipy/_lib/_ccallback.pyt   <module>   s   	Œ			