3
h#              	   @   s  d dl mZmZmZmZmZmZmZmZ d dl	m
Z
 d dl mZ d dlmZ d dlmZmZ d dlmZmZmZ ddd	d
dddddg	Zdd eD Zee dddddg Zdd ZG dd deZdd ZG dd deZG dd  d eZd!d" Zd#d$ Zd%d& Zd'S )(   )Nodes	ExprNodes	FusedNodeTreeFragmentPipelineParseTreeTransformsNaming	UtilNodes)error)
PyrexTypes)CythonUtilityCode)TempitaUtilityCodeUtilityCode)	PrintTreeTreeVisitorVisitorTransformZNPY_BYTEZNPY_INT8Z	NPY_SHORTZ	NPY_INT16ZNPY_INTZ	NPY_INT32ZNPY_LONGZNPY_LONGLONGZ	NPY_INT64c             C   s   g | ]}|j d dqS )ZNPY_ZNPY_U)replace).0tp r   h/var/www/html/CrowdFlow/Picklecon/ble_analysis_env/lib/python3.6/site-packages/Cython/Compiler/UFuncs.py
<listcomp>   s    r   Z	NPY_FLOATZNPY_FLOAT32Z
NPY_DOUBLEZNPY_FLOAT64ZNPY_LONGDOUBLEc             C   s~   |j r2|tjkrdS |tjkr"dS |tjkrldS n:|jrb|j j jdd}d| }|t	krl|S n
|j
rldS t| d|  d S )	NZ
NPY_CFLOATZNPY_CDOUBLEZNPY_CLONGDOUBLE  zNPY_%sZ
NPY_OBJECTz,Type '%s' cannot be used as a ufunc argument)Z
is_complexr   Zc_float_complex_typeZc_double_complex_typeZc_longdouble_complex_typeZ
is_numericZempty_declaration_codeupperr   numpy_numeric_typesis_pyobjectr
   )postype_Zpostfixtypenamer   r   r   _get_type_constant+   s    


r    c               @   s,   e Zd ZdZdZdd Zdd Zdd ZdS )	_FindCFuncDefNodezf
    Finds the CFuncDefNode in the tree

    The assumption is that there's only one CFuncDefNode
    Nc             C   s   | j r
d S | j| d S )N)
found_nodeZvisitchildren)selfnoder   r   r   
visit_NodeI   s    z_FindCFuncDefNode.visit_Nodec             C   s
   || _ d S )N)r"   )r#   r$   r   r   r   visit_CFuncDefNodeO   s    z$_FindCFuncDefNode.visit_CFuncDefNodec             C   s   | j | | jS )N)visitr"   )r#   treer   r   r   __call__R   s    
z_FindCFuncDefNode.__call__)__name__
__module____qualname____doc__r"   r%   r&   r)   r   r   r   r   r!   @   s
   r!   c             C   s
   t  | S )N)r!   )r(   r   r   r   get_cfunc_from_treeW   s    r.   c               @   s   e Zd ZdZdd ZdS )_ArgumentInfoz
    Everything related to defining an input/output argument for a ufunc

    type  - PyrexType
    type_constant  - str such as "NPY_INT8" representing numpy dtype constants
    c             C   s   || _ || _d S )N)typetype_constant)r#   r0   r1   r   r   r   __init__c   s    z_ArgumentInfo.__init__N)r*   r+   r,   r-   r2   r   r   r   r   r/   [   s   r/   c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )UFuncConversionc             C   s*   || _ |jj | _| j | _| j | _d S )N)r$   local_scopeglobal_scopeget_in_type_infoin_definitionsget_out_type_infoout_definitions)r#   r$   r   r   r   r2   i   s    
zUFuncConversion.__init__c             C   sD   g }x:t | jjD ]*\}}t| jj|j}|jt|j| qW |S )N)	enumerater$   argsr    r   r0   appendr/   )r#   definitionsnargZ
type_constr   r   r   r6   p   s
    z UFuncConversion.get_in_type_infoc             C   sX   | j jjr| j jj}n
| j jg}g }x.t|D ]"\}}|jt|t| j j| q.W |S )N)	r$   return_typeZ	is_ctuple
componentsr:   r<   r/   r    r   )r#   rA   r=   r>   r0   r   r   r   r8   w   s    

z!UFuncConversion.get_out_type_infoc       	   	   C   s   dd | j D }dd | jD }| jjjj| jjjdd}d| jj_| jj	| jjj
d }tdd |D p|td	d |D  }t|||| jjj|| jjjj|d
}tjdd|| jd}|jdd}|S )Nc             S   s   g | ]
}|j qS r   )r0   )r   ar   r   r   r      s    z<UFuncConversion.generate_cy_utility_code.<locals>.<listcomp>c             S   s   g | ]
}|j qS r   )r0   )r   rB   r   r   r   r      s    T)ZpyrexZ
_ufunc_defc             s   s   | ]}|j V  qd S )N)r   )r   tr   r   r   	<genexpr>   s    z;UFuncConversion.generate_cy_utility_code.<locals>.<genexpr>c             s   s   | ]}|j V  qd S )N)r   )r   rC   r   r   r   rD      s    )Z
func_cnameZin_types	out_typesZinline_func_callZinline_func_declarationnogilwill_be_called_without_gilZUFuncDefinitionz
UFuncs.pyx)contextZouter_module_scope)Zentries_only)r7   r9   r$   entryr0   Zdeclaration_codecnameusedr5   next_idnameanydictrF   r   loadZget_tree)	r#   Z	arg_typesrE   Zinline_func_declZufunc_cnamerG   rH   coder(   r   r   r   generate_cy_utility_code   s.    



z(UFuncConversion.generate_cy_utility_codec             C   s,   | j jtjdd | j jtjdd d S )NZ
UFuncsInitz
UFuncs_C.cZNumpyImportUFunczNumpyImportArray.c)r5   use_utility_coder   Zload_cached)r#   r   r   r   use_generic_utility_code   s    z(UFuncConversion.use_generic_utility_codeN)r*   r+   r,   r2   r6   r8   rR   rT   r   r   r   r   r3   h   s
   !r3   c             C   s   t | tjr6| jjjr&t| jd | S t| g}| }n^t | t	j
rt | jtjr| jjjjrlt| jd | S dd | jD }| j}nt| jd | S |sd S |d jj|jj= |d j  | gt|| S )Nz0Methods cannot currently be converted to a ufuncc             S   s   g | ]}t |qS r   )r3   )r   r>   r   r   r   r      s    z$convert_to_ufunc.<locals>.<listcomp>z,Only C functions can be converted to a ufunc    )
isinstancer   ZCFuncDefNoder4   Zparent_scopeZis_c_class_scoper
   r   r3   r   ZFusedCFuncDefNoder$   Znodesr5   entriesrI   rM   rT   _generate_stats_from_converters)r$   
convertersoriginal_noder   r   r   convert_to_ufunc   s(    

r[   c          	   C   s  | d j }|jtjd }|jtjd }|jtjd }g }d }d }	x| D ]|}
dd |
jD }|d k	rz|t|kstnt|}|j| dd |
jD }|	d k	r|	t|kstnt|}	|j| qLW dd |D }t	|||||d	}|j
tjd
d|d |j}|jj}|j}d|||t|||	||r8|j ndf }tj|dtjtjtjdtjd gtj|tj|dgd}|j|tj|}tj|tj||tj|d|d}|S )NrU   funcstypesdatac             S   s   g | ]
}|j qS r   )r1   )r   dr   r   r   r      s    z1generate_ufunc_initialization.<locals>.<listcomp>c             S   s   g | ]
}|j qS r   )r1   )r   r_   r   r   r   r      s    c             S   s   g | ]}|j jqS r   )rI   rJ   )r   Zcfnoder   r   r   r      s    )ufunc_funcs_namefunc_cnamesufunc_types_nametype_constantsufunc_data_nameZUFuncConstsz
UFuncs_C.c)rH   z5%s(), %s, %s(), %s, %s, %s, PyUFunc_None, "%s", %s, 0ZNULLZPyUFunc_FromFuncAndDatadummy)r0   value)Zfunction_nameZ	func_typer;   )rM   r0   rI   )lhsrhs)r5   rL   r   Zpyrex_prefixr7   lenAssertionErrorextendr9   rO   rS   r   rP   r   rI   rM   docZas_c_string_literalr   ZPythonCapiCallNoder   Z	CFuncTypeZpy_object_typeZCFuncTypeArgZc_void_ptr_typeZ	ConstNodeZdeclare_varr   ZSingleAssignmentNodeZNameNode)rY   Zcfunc_nodesrZ   r5   r`   rb   rd   rc   Znarg_inZnarg_outcZin_constZ	out_constra   rH   r   	func_nameZdocstrZargs_to_funcZ	call_nodeZ	lhs_entryZ
assgn_noder   r   r   generate_ufunc_initialization   sl    


ro   c             C   sT   g }x8| D ]0}|j  }t|}|jjj|jj |j| q
W |jt| || |S )N)rR   r.   r5   Zutility_code_listrk   Zscoper<   ro   )rY   r$   stats	converterr(   Z
ufunc_noder   r   r   rX     s    
rX   N) r   r   r   r   r   r   r   r   r	   ZErrorsr
   r   r   r   ZCoder   ZVisitorr   r   r   Znumpy_int_typesZnumpy_uint_typesr   r    r!   r.   objectr/   r3   r[   ro   rX   r   r   r   r   <module>   s:   (
FJ