o
    i)                     @   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 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 urt |  d S d S N)_loggerappendargs r   r/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/numba/tests/test_ndarray_subclasses.py_do_log   s   r    c                  G   s4   t   t|   W d    d S 1 sw   Y  d S r   )r   r    r   r   r   r   log$   s   
"r!   c                    s   t   fdd}|S )Nc                     s   g a  | i |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r`d }g }|D ]E}t |tr|| q
t |t| tjfrLt |t| r5|t|j|j| n|| |d urH||jkrGtdq
|j}q
t	  S ||i |}| 
|j|j|S t	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 r   )NotImplementedError)r2   r   r"   r   r   r   copy\   s   zMyArrayType.copyc                 O   sJ   |dkr#|D ]}t |tjtjfst  S qtdd |D r!tS tS t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      zMyArrayType.box_type)FT)	r:   r;   r<   rC   rH   r9   propertyrM   __classcell__r   r   rF   r   r=   V   s    r=   c                 C   sV   zt | j}W n ty   td| jf w 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   

rU   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   rV   rC   )r2   ZdmmZfe_typerD   membersrF   r   r   rC      s   
zMyArrayTypeModel.__init__)r:   r;   r<   rC   rP   r   r   rF   r   rV   ~   s    rV   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   )contextrb   r   r   r   type_myarray   s   rd   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)rZ   r-   r[   rY   rW   )Znumba.np.arrayobjre   rf   r   return_typerZ   r-   r[   rY   rW   Z	_getvaluenrtZincref)rc   buildersigr   re   rf   Zsrcarytyr-   r.   r`   ZsrcaryZretaryr8   r   r   r   impl_myarray   s   

rm   c                 C   sT   |j jsJ t| j}|j|j|}|j	| ||}|j j
|j| | |S r   )rc   Z
enable_nrtr   Zas_dtyper.   Zenv_managerZ
read_constZ	add_constZpyapiZnrt_adapt_ndarray_to_pythonrj   Zdecrefrk   )typrS   rT   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rN| tj|}n	|j|ksWJ 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)rj   Z_require_nrtmoduler   ZIntTyper	   Z	voidptr_tFunctionTypeZget_or_insert_functioncallZintp_treturn_valueZadd_attributer)   builtinsintZget_constantr
   Zuint32r*   rA   )rc   rk   rl   r   sizert   modu32voidptrZget_alloc_fntyZget_alloc_fnZ	ext_allocZfntyr$   rz   r   r   r   ru      s*   

zallocator_MyArray.<locals>.impl)r
   r\   r   r   	signature)Z	typingctxrs   rt   ru   Zmiprl   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      rN   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   sr   t dd }tjdtjd}| t}|| W d    n1 s"w   Y  d}|D ]}| |t|j q+d 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   brT   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=   registerrU   r%   Z	datamodelmodelsZStructModelrV   rd   r]   ZDTyperm   ro   rv   rq   r   r:   mainr   r   r   r   <module>   sR    $
	#









!p