U
    9%e)                     @   s  d Z ddlZddlZddl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mZmZ ddl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mZmZmZmZm Z  ddl!m"Z" ddl#m$Z$m%Z% da&dd Z'e dd Z(dd Z)G dd dej*Z+G dd dej,Z-e.e+dd Z/ee-G dd dej0j1j2j3Z4ee+dd Z5ee+ej6ej7ej,dd Z8ee-d d! Z9ee-d"d#d$ Z:ed%d& Z;G d'd( d(e%e$Z<e=d)kre>  dS )*z!
Test NumPy Subclassing features
    N)Numberwraps)ir)njittypeofobjmode)cgutilstypestyping)box)TypingError)
cpu_target)	intrinsiclower_builtinoverload_classmethodregister_modeltype_callabletypeof_implregister_jitable)numpy_support)TestCaseMemoryLeakMixinc                  G   s   t d k	rt |  d S N)_loggerappendargs r   b/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/numba/tests/test_ndarray_subclasses.py_do_log   s    r    c               	   G   s   t   t|   W 5 Q R X d S r   )r   r    r   r   r   r   log$   s    r!   c                    s   t   fdd}|S )Nc                     s   g a  | |S r   )r   )r   kwargsfnr   r   core+   s    zuse_logger.<locals>.corer   )r$   r%   r   r#   r   
use_logger*   s    r&   c                   @   s   e Zd ZdZdd ZdS )MyArrayTc           	      O   s   |dkrd }g }|D ]}t |tr.|| qt |t| tjfrt |t| rj|t|j|j| n
|| |d k	r||jkrtdq|j}qt	  S q|||}| 
|j|j|S t	S d S )N__call__zinconsistent sizes)
isinstancer   r   typenpndarrayshapedtype	TypeErrorNotImplemented	__class__)	selfufuncmethodinputsr"   NZscalarsinpretr   r   r   __array_ufunc__=   s$    





zMyArray.__array_ufunc__N)__name__
__module____qualname__Z __numba_array_subtype_dispatch__r9   r   r   r   r   r'   3   s   r'   c                       s:   e Zd Zd fdd	Zdd Zdd Zed	d
 Z  ZS )MyArrayTypeFTc                    s4   d| d| d| d}t  j||||||d d S )NzMyArray(z, ))readonlyalignedname)super__init__)r2   r.   ndimlayoutr?   r@   rA   r1   r   r   rC   W   s
     zMyArrayType.__init__c                 O   s   t d S r   )NotImplementedError)r2   r   r"   r   r   r   copy\   s    zMyArrayType.copyc                 O   sN   |dkrF|D ]}t |tjtjfst  S qtdd |D rBtS tS tS d S )Nr(   c                 s   s   | ]}t |tV  qd S r   )r)   r=   ).0r7   r   r   r   	<genexpr>h   s     z.MyArrayType.__array_ufunc__.<locals>.<genexpr>)r)   r
   Arrayr   r0   allr=   )r2   r3   r4   r5   r"   r7   r   r   r   r9   b   s    
zMyArrayType.__array_ufunc__c                 C   s   t S r   )r'   )r2   r   r   r   box_typen   s    zMyArrayType.box_type)FT)	r:   r;   r<   rC   rH   r9   propertyrM   __classcell__r   r   rF   r   r=   V   s
   r=   c                 C   s\   zt | j}W n$ tk
r4   td| jf Y nX t | }| jj }t|| j	||dS )NzUnsupported array dtype: %s)r?   )
r   Z
from_dtyper.   rG   
ValueErrorZ
map_layoutflagsZ	writeabler=   rD   )valcr.   rE   r?   r   r   r   typeof_ta_ndarrays   s    

rT   c                       s   e Zd Z fddZ  ZS )MyArrayTypeModelc                    s~   |j }dt|jfdtjfdtjfdtjfdt|jfdttj|fdttj|fdtjfg}tt	| 
||| d S )	NmeminfoparentZnitemsitemsizedatar-   stridesZextra_field)rD   r
   MemInfoPointerr.   ZpyobjectZintpZCPointerUniTuplerB   rU   rC   )r2   ZdmmZfe_typerD   membersrF   r   r   rC      s    
zMyArrayTypeModel.__init__)r:   r;   r<   rC   rO   r   r   rF   r   rU   ~   s   rU   c                 C   s   dd }|S )Nc                 S   s   t |jt| |jd}|S )N)r.   rD   rE   )r=   r.   lenrE   )r-   r.   bufoutr   r   r   typer   s      ztype_myarray.<locals>.typerr   )contextra   r   r   r   type_myarray   s    rc   c                 C   s   ddl m}m} |jd }|\}}}	||| ||	d}
||j| |}|||
j|
j|
j|
j|
j	d |
 }| j||j| |S )Nr   )
make_arraypopulate_array)value)rY   r-   rZ   rX   rV   )Znumba.np.arrayobjrd   re   r   return_typerY   r-   rZ   rX   rV   Z	_getvaluenrtZincref)rb   buildersigr   rd   re   Zsrcarytyr-   r.   r_   ZsrcaryZretaryr8   r   r   r   impl_myarray   s    

rl   c                 C   sT   |j jstt| j}|j|j|}|j	
| ||}|j j|j| | |S r   )rb   Z
enable_nrtAssertionErrorr   Zas_dtyper.   Zenv_managerZ
read_constZ	add_constZpyapiZnrt_adapt_ndarray_to_pythonri   Zdecrefrj   )typrR   rS   Znp_dtypeZdtypeptrZnewaryr   r   r   	box_array   s    ro   Z	_allocatec                 C   s   dd }|S )z;Implements a Numba-only classmethod on the array type.
    c                 S   s   t d|| t||S )NLOG _ol_array_allocate)r!   allocator_MyArray)cls	allocsizealignr   r   r   impl   s    z _ol_array_allocate.<locals>.implr   )rr   rs   rt   ru   r   r   r   _ol_array_allocate   s    rv   c                 C   s*   dd }t t j}t|||}||fS )Nc                 S   s   | j   |\}}|j}td}tj}t|d}	tj||	dd}
|	|
d}t|tj
||g}tj||dd}|jd t|tjr| tj|}n|j|kstd|	||||g}d|_|S )	N    r   Z"_nrt_get_sample_external_allocator)rA   Z'NRT_MemInfo_alloc_safe_aligned_externalZnoaliaszalign must be a uint32Zallocate_MyArray)ri   Z_require_nrtmoduler   ZIntTyper	   Z	voidptr_tFunctionTypeZget_or_insert_functioncallZintp_tZreturn_valueZadd_attributer)   builtinsintZget_constantr
   Zuint32r*   rm   rA   )rb   rj   rk   r   sizert   modu32voidptrZget_alloc_fntyZget_alloc_fnZ	ext_allocZfntyr$   rz   r   r   r   ru      s2    

    zallocator_MyArray.<locals>.impl)r
   r[   r   r   	signature)Z	typingctxrs   rt   ru   Zmiprk   r   r   r   rq      s    rq   c                   @   sN   e Zd Zdd Zdd Zdd Zdd Zejd	d
 Z	dd Z
edd ZdS )TestNdarraySubclassesc                 C   sh   t dd }td}t|j|j|}||}||}| |t | t	|t	| | 
|| dS )z6This tests the path to `MyArrayType.box_type`
        c                 S   s   | d S )N   r   ar   r   r   foo   s    z6TestNdarraySubclasses.test_myarray_return.<locals>.foo   Nr   r+   aranger'   r-   r.   py_funcassertIsInstanceassertIsr*   assertPreciseEqualr2   r   r_   r   expectedgotr   r   r   test_myarray_return   s    


z)TestNdarraySubclasses.test_myarray_returnc                 C   sh   t dd }td}t|j|j|}||}||}| |t | t	|t	| | 
|| d S )Nc                 S   s   | S r   r   r   r   r   r   r      s    z8TestNdarraySubclasses.test_myarray_passthru.<locals>.foor   r   r   r   r   r   test_myarray_passthru   s    


z+TestNdarraySubclasses.test_myarray_passthruc                 C   sX   t dd }td}||}||}| |t | t|t| | || d S )Nc                 S   s   t | j| j| S r   r'   r-   r.   )r_   r   r   r   r     s    z7TestNdarraySubclasses.test_myarray_convert.<locals>.foor   )	r   r+   r   r   r   r'   r   r*   r   )r2   r   r_   r   r   r   r   r   test_myarray_convert  s    


z*TestNdarraySubclasses.test_myarray_convertc                 C   s@   dd }t d}||}| t|t j | |||  d S )Nc                 S   s   t | j| j| }t||  S r   r'   r-   r.   r+   Zasarrayr_   Z	convertedr   r   r   r     s    z?TestNdarraySubclasses.test_myarray_asarray_non_jit.<locals>.foor   )r+   r   r   r*   r,   r   r2   r   r_   r   r   r   r   test_myarray_asarray_non_jit  s
    
z2TestNdarraySubclasses.test_myarray_asarray_non_jitc                 C   s<   |    tdd }td}||}| t|tj d S )Nc                 S   s   t | j| j| }t|S r   r   r   r   r   r   r   !  s    z7TestNdarraySubclasses.test_myarray_asarray.<locals>.foor   )Zdisable_leak_checkr   r+   r   r   r*   r,   r   r   r   r   test_myarray_asarray  s    

z*TestNdarraySubclasses.test_myarray_asarrayc              	   C   s^   t dd }tjdtjd}| t}|| W 5 Q R X d}|D ]}| |t|j qBd S )Nc                 S   s   t | j| j| }|| S r   r   r   r   r   r   r   -  s    zATestNdarraySubclasses.test_myarray_ufunc_unsupported.<locals>.foor   r.   )zNo implementation of functionz3add(MyArray(1, float32, C), MyArray(1, float32, C)))	r   r+   r   Zfloat32assertRaisesr   ZassertInstr	exception)r2   r   r_   Zraisesmsgmr   r   r   test_myarray_ufunc_unsupported,  s    
z4TestNdarraySubclasses.test_myarray_ufunc_unsupportedc           
      C   s   t dd }tjdtjd}t|j|j|}||}||}| || t	}t
j}t|j}||}	| |d|d j|	fd|d j|	fg dS )	z:
        Checks that our custom allocator is used
        c                 S   s&   | t j| jt jd }| d }||fS )Nr   y              ?)r+   r   r}   float64)r   brS   r   r   r   r   @  s    zBTestNdarraySubclasses.test_myarray_allocator_override.<locals>.foor   r   rp   r   r   N)r   r+   r   r   r'   r-   r.   r   r   r   r   Ztarget_contextr   Zget_preferred_array_alignmentassertEqualnbytes)
r2   r   r_   r   r   r   Zlogged_linesZ	targetctxZnb_dtypert   r   r   r   test_myarray_allocator_override;  s    



z5TestNdarraySubclasses.test_myarray_allocator_overrideN)r:   r;   r<   r   r   r   r   unittestZexpectedFailurer   r   r&   r   r   r   r   r   r      s   

r   __main__)?__doc__r{   r   numbersr   	functoolsr   numpyr+   Zllvmliter   Znumbar   r   r   Z
numba.corer	   r
   r   Znumba.core.pythonapir   Znumba.core.errorsr   Znumba.core.registryr   Znumba.extendingr   r   r   r   r   r   r   Znumba.npr   Znumba.tests.supportr   r   r   r    r!   r&   r,   r'   rK   r=   registerrT   r%   Z	datamodelmodelsZStructModelrU   rc   r\   ZDTyperl   ro   rv   rq   r   r:   mainr   r   r   r   <module>   sN   $
	#









!p
