U
    ?hE                     @   s   d dl mZ ddlZeejjd ZdaG dd dadd Z	G dd	 d	e
Zdd
dZdd Zdd ZdddZdd ZdS )   )_ccallback_c    Nc                   @   s   e Zd ZdS )CDataN)__name__
__module____qualname__ r   r   G/var/www/html/venv/lib/python3.8/site-packages/scipy/_lib/_ccallback.pyr   	   s   r   c                  C   sD   t d k	rd S zdd l} |  a t jaW n tk
r>   da Y nX d S )Nr   F)fficffiZFFIr   ImportError)r   r   r   r	   _import_cffi   s    
r   c                   @   sn   e Zd ZdZdZdddZdd Zedd	 Zed
d Z	edd Z
dd ZedddZedddZdS )LowLevelCallablea  
    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.

    r   Nc                 C   s    |  |||}t| |||fS N)_parse_callbacktuple__new__)clsfunction	user_data	signatureitemr   r   r	   r   T   s    zLowLevelCallable.__new__c                 C   s   d | j| jS )NzLowLevelCallable({!r}, {!r}))formatr   r   selfr   r   r	   __repr__Z   s    zLowLevelCallable.__repr__c                 C   s   t | dS )Nr   r   __getitem__r   r   r   r	   r   ]   s    zLowLevelCallable.functionc                 C   s   t | dS )N   r   r   r   r   r	   r   a   s    zLowLevelCallable.user_datac                 C   s   t t| dS )Nr   )r   Zget_capsule_signaturer   r   r   r   r   r	   r   e   s    zLowLevelCallable.signaturec                 C   s
   t  d S r   )
ValueError)r   idxr   r   r	   r   i   s    zLowLevelCallable.__getitem__c              
   C   sx   z|j | }W n\ tk
r: } ztd|W 5 d}~X Y n2 tk
rj } ztd||W 5 d}~X Y nX | |||S )a  
        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*.

        z?Given module is not a Cython module with __pyx_capi__ attributeNz4No function {!r} found in __pyx_capi__ of the module)Z__pyx_capi__AttributeErrorr   KeyErrorr   )r   modulenamer   r   r   er   r   r	   from_cythonl   s    "zLowLevelCallable.from_cythonc                 C   s   t   t|trt|d}nLt|tr8t||\}}n2t|trRt||\}}nt	
|rb|}ntdt|tjrt|}n:t|trt|}n&|d krd}nt	
|r|}ntdt	|||S )Nr   zMGiven input is not a callable or a low-level callable (pycapsule/ctypes/cffi)zNGiven user data is not a valid low-level void* pointer (pycapsule/ctypes/cffi))r   
isinstancer   r   r   
PyCFuncPtr_get_ctypes_funcr   _get_cffi_funcr   Zcheck_capsuler   ctypesc_void_p_get_ctypes_data_get_cffi_dataZget_raw_capsule)r   objr   r   funccontextr   r   r	   r      s(    







z LowLevelCallable._parse_callback)NN)NN)NN)r   r   r   __doc__	__slots__r   r   propertyr   r   r   r   classmethodr&   r   r   r   r   r	   r      s   7



r   c                 C   sp   t | t jj}|d krht| jd }t| jD ].\}}|dkrN|t|7 }q0|dt| 7 }q0|d7 }||fS )Nz (r   z, ))r+   castr,   value_typename_from_ctypesrestype	enumerateZargtypes)r0   r   func_ptrjargr   r   r	   r)      s    r)   c                 C   sv   | d krdS | t jkrdS | j}d}|drD|d7 }|dd  }q$|drZ|dd  }|dkrr|d	d
|  7 }|S )Nvoidzvoid *r   ZLP_r      Zc_r    *)r+   r,   r   
startswith)r   r$   Zpointer_levelr   r   r	   r9      s    


r9   c                 C   s   t | t jjS r   )r+   r7   r,   r8   datar   r   r	   r-      s    r-   c                 C   s4   t d| }|d kr,t t | dd}||fS )N	uintptr_tz(*)rA   )r
   r7   ZgetctypeZtypeofreplace)r0   r   r<   r   r   r	   r*      s    r*   c                 C   s   t d| S )NrF   )r
   r7   rD   r   r   r	   r.      s    r.   )N)N) r   r+   Z	CFUNCTYPEr,   	__bases__r(   r
   r   r   r   r   r)   r9   r-   r*   r.   r   r   r   r	   <module>   s    
	
