o
    1ήcN                     @   sN  d dl Z d dl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mZmZmZ d dlmZmZmZmZ d dlmZ d dlZejje ddZd	d
 Zedd dd Zedd dd ZG dd dZG dd dZeG dd dZeG dd dZ dd Z!eG dd dZ"G dd dZ#G d d! d!Z$G d"d# d#Z%dS )$    NStringIO)mock)assert_assert_equalassert_raisesassert_raises_regex)_get_implementing_argsarray_function_dispatchverify_matching_signaturesARRAY_FUNCTION_ENABLED)picklez(__array_function__ dispatch not enabled.)reasonc                 O      t S NNotImplementedselfargskwargs r   F/tmp/pip-target-vg8gfxp4/lib/python/numpy/core/tests/test_overrides.py_return_not_implemented      r   c                 C      | fS r   r   arrayr   r   r   <lambda>       r   c                 C      dS 
Docstring.originalr   r   r   r   r   dispatched_one_arg      r$   c                 C      | |fS r   r   array1array2r   r   r   r   "       c                 C   r    r!   r   r'   r   r   r   dispatched_two_arg"   r%   r+   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )TestGetImplementingArgsc                 C   s|   t d}t|g}tt||g t||g}tt||g t|dg}tt||g td|g}tt||g d S )N   npr   r	   r   list)r   r   r   r   r   r   test_ndarray*   s   

z$TestGetImplementingArgs.test_ndarrayc                 C   s   G dd dt j}G dd dt j}t dt j}t d|}t d|}t||g}tt|||g t||g}tt|||g t||g}tt|||g d S )Nc                   @      e Zd ZeZdS )zDTestGetImplementingArgs.test_ndarray_subclasses.<locals>.OverrideSubN__name__
__module____qualname__r   __array_function__r   r   r   r   OverrideSub;       r8   c                   @      e Zd ZdS )zFTestGetImplementingArgs.test_ndarray_subclasses.<locals>.NoOverrideSubNr4   r5   r6   r   r   r   r   NoOverrideSub>       r<   r-   )r/   ndarrayr   viewr	   r   r0   )r   r8   r<   r   override_subno_override_subr   r   r   r   test_ndarray_subclasses9   s   z/TestGetImplementingArgs.test_ndarray_subclassesc                 C   s^   G dd d}t d}| }t||g}tt|||g t||g}tt|||g d S )Nc                   @   r2   )zBTestGetImplementingArgs.test_ndarray_and_duck_array.<locals>.OtherNr3   r   r   r   r   OtherQ   r9   rC   r-   r.   )r   rC   r   otherr   r   r   r   test_ndarray_and_duck_arrayO   s   
z3TestGetImplementingArgs.test_ndarray_and_duck_arrayc                 C   sx   G dd dt j}G dd d}t d}t d|}| }tt|||g|||g tt|||g|||g d S )Nc                   @   r2   )zQTestGetImplementingArgs.test_ndarray_subclass_and_duck_array.<locals>.OverrideSubNr3   r   r   r   r   r8   _   r9   r8   c                   @   r2   )zKTestGetImplementingArgs.test_ndarray_subclass_and_duck_array.<locals>.OtherNr3   r   r   r   r   rC   b   r9   rC   r-   )r/   r>   r   r?   r   r	   )r   r8   rC   r   subarrayrD   r   r   r   $test_ndarray_subclass_and_duck_array]   s   
z<TestGetImplementingArgs.test_ndarray_subclass_and_duck_arrayc           	      C   s  G dd d}G dd d|}G dd d|}G dd d}| }| }| }| }t td	gg  t t|g|g t t|d	g|g t t|||g|g t t|||g||g t t||g||g t t||g||g t t|||g|||g t t|||g|||g d S )
Nc                   @   r2   )z8TestGetImplementingArgs.test_many_duck_arrays.<locals>.ANr3   r   r   r   r   Ap   r9   rH   c                   @   r2   )z8TestGetImplementingArgs.test_many_duck_arrays.<locals>.BNr3   r   r   r   r   Bs   r9   rI   c                   @   r2   )z8TestGetImplementingArgs.test_many_duck_arrays.<locals>.CNr3   r   r   r   r   Cv   r9   rJ   c                   @   r2   )z8TestGetImplementingArgs.test_many_duck_arrays.<locals>.DNr3   r   r   r   r   Dy   r9   rK   r-   )r   r	   )	r   rH   rI   rJ   rK   abcdr   r   r   test_many_duck_arraysn   s"   z-TestGetImplementingArgs.test_many_duck_arraysc                    s   t td  fddtdD }dd |D }t|d d }t||d d  ttd t| W d    d S 1 s=w   Y  d S )N)r7   c                    s"   g | ]}t d t| tf qS )rH   )typestrobject).0i	namespacer   r   
<listcomp>   s   " zETestGetImplementingArgs.test_too_many_duck_arrays.<locals>.<listcomp>!   c                 S   s   g | ]}| qS r   r   rT   tr   r   r   rX      s        zdistinct argument types)dictr   ranger	   r   r   	TypeError)r   typesrelevant_argsactualr   rV   r   test_too_many_duck_arrays   s   

"z1TestGetImplementingArgs.test_too_many_duck_arraysN)	r4   r5   r6   r1   rB   rE   rG   rP   rc   r   r   r   r   r,   (   s    r,   c                   @   s    e Zd Zedd Zdd ZdS )TestNDArrayArrayFunctionc           
      C   sv  G dd d}G dd dt j}G dd dt j}t dg}| }||}||}|jtt jf|dfi d	}t|d
 |jtt j|f||fi d	}t|tu  |jtt j|f||fi d	}t|d
 |jtt j|f||fi d	}t|d
 t	t
d t ||f W d    n1 sw   Y  t ||f}	t ||f}t||	| t ||f}t||	| d S )Nc                   @   r2   )z3TestNDArrayArrayFunction.test_method.<locals>.OtherNr3   r   r   r   r   rC      r9   rC   c                   @   r:   )z;TestNDArrayArrayFunction.test_method.<locals>.NoOverrideSubNr;   r   r   r   r   r<      r=   r<   c                   @   r2   )z9TestNDArrayArrayFunction.test_method.<locals>.OverrideSubNr3   r   r   r   r   r8      r9   r8   r-   g      ?funcr`   r   r   r#   no implementation found)r/   r>   r   r?   r7   r+   r   r   r   r   r_   concatenate)
r   rC   r<   r8   r   rD   rA   r@   resultexpectedr   r   r   test_method   sJ   




z$TestNDArrayArrayFunction.test_methodc                 C   sZ   t d}dd }ttd |j|t jf|fi d W d    d S 1 s&w   Y  d S )Nr-   c                 S      | S r   r   xr   r   r   r          z:TestNDArrayArrayFunction.test_no_wrapper.<locals>.<lambda>_implementationre   )r/   r   r   AttributeErrorr7   r>   )r   r   rf   r   r   r   test_no_wrapper   s   
"z(TestNDArrayArrayFunction.test_no_wrapperN)r4   r5   r6   requires_array_functionrk   rr   r   r   r   r   rd      s    
/rd   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestArrayFunctionDispatchc                 C   s:   t dtjd D ]}ttjt|d}t|tu  qd S )N   r-   )protocol)r^   r   HIGHEST_PROTOCOLloadsdumpsr$   r   )r   protoroundtrippedr   r   r   test_pickle   s   z%TestArrayFunctionDispatch.test_picklec                 C   s,   t tjd tjjdk rt tjd d S d S )Nr$   ru   r"   )r   r$   r4   sysflagsoptimize__doc__r   r   r   r   test_name_and_docstring   s   z1TestArrayFunctionDispatch.test_name_and_docstringc                 C   sj   G dd d}| }t |\}}}}}t||u  t|t u  tt||h t||fk t|i  d S )Nc                   @      e Zd Zdd ZdS )z9TestArrayFunctionDispatch.test_interface.<locals>.MyArrayc                 S   s   | ||||fS r   r   r   rf   r`   r   r   r   r   r   r7      s   zLTestArrayFunctionDispatch.test_interface.<locals>.MyArray.__array_function__Nr4   r5   r6   r7   r   r   r   r   MyArray       r   )r$   r   r   set)r   r   r#   objrf   r`   r   r   r   r   r   test_interface   s   z(TestArrayFunctionDispatch.test_interfacec                 C   sL   G dd d}| }t td t| W d    d S 1 sw   Y  d S )Nc                   @   r   )z?TestArrayFunctionDispatch.test_not_implemented.<locals>.MyArrayc                 S   r   r   r   r   r   r   r   r7      r   zRTestArrayFunctionDispatch.test_not_implemented.<locals>.MyArray.__array_function__Nr   r   r   r   r   r      r   r   rg   )r   r_   r$   r   r   r   r   r   r   test_not_implemented   s
   
"z.TestArrayFunctionDispatch.test_not_implementedN)r4   r5   r6   r|   r   r   r   r   r   r   r   rt      s
    rt   c                   @   s   e Zd Zdd Zdd ZdS )TestVerifyMatchingSignaturesc                 C   s6  t dd dd  t dddddd t dddddd tt t dd dd  W d    n1 s7w   Y  tt t dd ddd W d    n1 sUw   Y  tt t dd	ddd
d W d    n1 stw   Y  tt t dddddd W d    d S 1 sw   Y  d S )Nc                 S   r    Nr   r   rm   r   r   r   r     ro   zNTestVerifyMatchingSignatures.test_verify_matching_signatures.<locals>.<lambda>c                 S   r    r   r   rm   r   r   r   r     ro   r-   c                 S   r    r   r   rm   r   r   r   r     ro   c                 S   r    r   r   )rL   r   r   r   r     ro   c                 S   r    r   r   )rM   r   r   r   r     ro   c                 S   r    r   r   rm   r   r   r   r     ro   c                 S   r    r   r   rm   r   r   r   r   
  ro   c                 S   r    r   r   yr   r   r   r   
  ro   c                 S   r    r   r   rm   r   r   r   r     ro   c                 S   r    r   r   r   r   r   r   r     ro   r   r-   )r   r   RuntimeErrorr   r   r   r   test_verify_matching_signatures   s   



"z<TestVerifyMatchingSignatures.test_verify_matching_signaturesc                 C   sX   t t tdd dd }W d    n1 sw   Y  tdd dddd }d S )	Nc                 S   r   r   r   rm   r   r   r   r     r   zKTestVerifyMatchingSignatures.test_array_function_dispatch.<locals>.<lambda>c                 S      d S r   r   r   r   r   r   f     zDTestVerifyMatchingSignatures.test_array_function_dispatch.<locals>.fc                 S   r   r   r   rm   r   r   r   r     r   F)verifyc                 S   r   r   r   r   r   r   r   r     r   )r   r   r
   )r   r   r   r   r   test_array_function_dispatch  s   

z9TestVerifyMatchingSignatures.test_array_function_dispatchN)r4   r5   r6   r   r   r   r   r   r   r      s    r   c                     s,   i  G  fddd fdd} | fS )z2Create a duck array type and implements functions.c                       s   e Zd Z fddZdS )z._new_duck_type_and_implements.<locals>.MyArrayc                    s8   | vrt S tfdd|D st S  | |i |S )Nc                 3   s    | ]}t | V  qd S r   )
issubclassrZ   )r   r   r   	<genexpr>#  s    zT_new_duck_type_and_implements.<locals>.MyArray.__array_function__.<locals>.<genexpr>)r   allr   HANDLED_FUNCTIONSr   r   r   r7      s
   zA_new_duck_type_and_implements.<locals>.MyArray.__array_function__Nr   r   r   r   r   r     s    r   c                    s    fdd}|S )z/Register an __array_function__ implementations.c                    s   |  < | S r   r   )rf   )r   numpy_functionr   r   	decorator)  s   zD_new_duck_type_and_implements.<locals>.implements.<locals>.decoratorr   )r   r   )r   )r   r   
implements'  s   z1_new_duck_type_and_implements.<locals>.implementsr   )r   r   r   r   _new_duck_type_and_implements  s   r   c                   @   $   e Zd Zdd Zdd Zdd ZdS )TestArrayFunctionImplementationc                 C   s<   t  \}}|tdd }ttdd tt| d d S )Nc                 S   r    )Nmyarrayr   r   r   r   r   _7  r   z7TestArrayFunctionImplementation.test_one_arg.<locals>._r-   r#   r   )r   r$   r   r   r   r   r   r   r   r   test_one_arg4  s
   

z,TestArrayFunctionImplementation.test_one_argc                 C   s   t  \}}tdddddd}||ddd}t|d	d t|d	d
dd
 t|| d tt || d
d W d    n1 sHw   Y  || dd}t|d tt || dd W d    d S 1 spw   Y  d S )Nc                 S   r   r   r   r   optionr   r   r   r   A  r   zDTestArrayFunctionImplementation.test_optional_args.<locals>.<lambda>defaultc                 S      |S r   r   r   r   r   r   func_with_optionA  r   zLTestArrayFunctionImplementation.test_optional_args.<locals>.func_with_optionr   c                 S   r   r   r   )r   
new_optionr   r   r   my_array_func_with_optionE  r   zUTestArrayFunctionImplementation.test_optional_args.<locals>.my_array_func_with_optionr-   extra)r   yes)r   nor   )r   )r   )r   r
   r   r   r_   )r   r   r   r   r   ri   r   r   r   test_optional_args>  s    



"z2TestArrayFunctionImplementation.test_optional_argsc                 C   s   t  \}}tdd dddd }td}t|||u  t|jd ttd ||  W d    d S 1 s:w   Y  d S )	Nc                 S   r   r   r   r   r   r   r   r   Z  r   zFTestArrayFunctionImplementation.test_not_implemented.<locals>.<lambda>my)modulec                 S   rl   r   r   r   r   r   r   rf   Z  r   zBTestArrayFunctionImplementation.test_not_implemented.<locals>.funcr-   z%no implementation found for 'my.func')	r   r
   r/   r   r   r   r5   r   r_   )r   r   r   rf   r   r   r   r   r   W  s   


"z4TestArrayFunctionImplementation.test_not_implementedN)r4   r5   r6   r   r   r   r   r   r   r   r   1  s    
r   c                   @   r   )TestNDArrayMethodsc                 C   sB   G dd dt j}t d|}tt|d tt|d d S )Nc                   @   r   )z-TestNDArrayMethods.test_repr.<locals>.MyArrayc                  _   r   r   r   r   r   r   r   r   r7   n  r   z@TestNDArrayMethods.test_repr.<locals>.MyArray.__array_function__Nr   r   r   r   r   r   m  r   r   r-   z
MyArray(1)1)r/   r>   r   r?   r   reprrR   r   r   r   r   	test_repri  s   zTestNDArrayMethods.test_reprN)r4   r5   r6   r   r   r   r   r   r   g  s    r   c                   @   s@   e Zd Zdd Zdd Zedd Zedd Zed	d
 ZdS )TestNumPyFunctionsc                 C   sB   t tjjd t tjjjd t tjjjd t tjjjd d S )Nnumpyz
numpy.charz	numpy.fftznumpy.linalg)	r   r/   sumr5   charequalfftlinalgsolver   r   r   r   test_set_modulex  s   z"TestNumPyFunctions.test_set_modulec                 C   s   t tj}td|jv  d S )Naxis)inspect	signaturer/   r   r   
parameters)r   r   r   r   r   test_inspect_sum~  s   z#TestNumPyFunctions.test_inspect_sumc                 C   s2   t  \}}|tjdd }tt| d d S )Nc                 S   r    )Nr   r   r   r   r   r   r     r   z/TestNumPyFunctions.test_override_sum.<locals>._r   )r   r/   r   r   r   r   r   r   test_override_sum  s   

z$TestNumPyFunctions.test_override_sumc                 C   sf   G dd d}|t j|d}d|jj_t|}t|d |jjtj|f|fi  |jj	
  d S )Nc                   @   r   )z=TestNumPyFunctions.test_sum_on_mock_array.<locals>.ArrayProxyc                 S   
   || _ d S r   )value)r   r   r   r   r   __init__     
zFTestNumPyFunctions.test_sum_on_mock_array.<locals>.ArrayProxy.__init__c                 _      | j j|i |S r   )r   r7   r   r   r   r   r7        zPTestNumPyFunctions.test_sum_on_mock_array.<locals>.ArrayProxy.__array_function__c                 _   r   r   )r   	__array__r   r   r   r   r     r   zGTestNumPyFunctions.test_sum_on_mock_array.<locals>.ArrayProxy.__array__N)r4   r5   r6   r   r7   r   r   r   r   r   
ArrayProxy  s    r   )specr-   )r   Mockr   r7   return_valuer/   r   r   assert_called_once_withr   assert_not_called)r   r   proxyri   r   r   r   test_sum_on_mock_array  s   


z)TestNumPyFunctions.test_sum_on_mock_arrayc                 C   s6   G dd dt j}t d|}tt |d d S )Nc                       s$   e Zd Zdd Z fddZ  ZS )zFTestNumPyFunctions.test_sum_forwarding_implementation.<locals>.MyArrayc                 S   r    )Nsummedr   )r   r   outr   r   r   r     r   zJTestNumPyFunctions.test_sum_forwarding_implementation.<locals>.MyArray.sumc                    s   t  ||||S r   )superr7   r   	__class__r   r   r7     r   zYTestNumPyFunctions.test_sum_forwarding_implementation.<locals>.MyArray.__array_function__)r4   r5   r6   r   r7   __classcell__r   r   r   r   r     s    r   r-   r   )r/   r>   r   r?   r   r   r   r   r   r   "test_sum_forwarding_implementation  s   	z5TestNumPyFunctions.test_sum_forwarding_implementationN)	r4   r5   r6   r   r   rs   r   r   r   r   r   r   r   r   v  s    
	
r   c                   @   s  e Zd Zdd Zd<ddZdd Zedd	 Zd
gedR dgedR dgedR dgedR dgedR dgee	d
ddfddgdR dgedR dgeddR dgedR dgedR dgedR dgededR d geededR d!ged"ed#d$R d%ged&d' R d(ged)d' d*d+gd#d,R gZejd-eejd.d/dged0d1 Zejd-eejd2d3d3gd4ged5d6 Zejd.d/dgd7d8 Zed9d: Zd;S )=TestArrayLikec                 C   s,   G dd d}|| _ G dd d}|| _d S )Nc                   @   s   e Zd ZdddZdd ZdS )z+TestArrayLike.setup_method.<locals>.MyArrayNc                 S   r   r   functionr   r   r   r   r   r     r   z4TestArrayLike.setup_method.<locals>.MyArray.__init__c                 S   sJ   |t t|ju s
J zt | |j}W n ty   t Y S w ||i |S r   )getattrr/   r4   rq   r   )r   rf   r`   r   r   my_funcr   r   r   r7     s   z>TestArrayLike.setup_method.<locals>.MyArray.__array_function__r   )r4   r5   r6   r   r7   r   r   r   r   r     s    
r   c                   @   s   e Zd ZdddZdS )z:TestArrayLike.setup_method.<locals>.MyNoArrayFunctionArrayNc                 S   r   r   r   r   r   r   r   r     r   zCTestArrayLike.setup_method.<locals>.MyNoArrayFunctionArray.__init__r   )r4   r5   r6   r   r   r   r   r   MyNoArrayFunctionArray  s    r   )r   r   )r   r   r   r   r   r   setup_method  s   
zTestArrayLike.setup_methodFc                    s     fdd}t  | d S )Nc                     s*   d|vsJ rd|v rt  t S )Nlikevalue_error)
ValueErrorr   r   	arr_classenable_value_errornamer   r   _definition  s   z-TestArrayLike.add_method.<locals>._definition)setattr)r   r   r   r   r   r   r   r   
add_method  s   zTestArrayLike.add_methodc                  O   r&   r   r   r   r   r   r   	func_args  s   zTestArrayLike.func_argsc                 C   sV   |  d| j | j }ttd tjd|d}W d    d S 1 s$w   Y  d S )Nr   rg   r-   r   )r   r   r   r   r_   r/   asarray)r   ref
array_liker   r   r   test_array_like_not_implemented  s
   
"z-TestArrayLike.test_array_like_not_implementedr   r   r   
asanyarrayascontiguousarray)ru      asfortranarrayrequire   ru   r   rH   F)requirementsemptyfulloneszerosarange
frombuffers           )dtypefromiter
fromstringz1,2,)r  seploadtxtc                   C      t dS )Nz0 1
2 3r   r   r   r   r   r     r*   zTestArrayLike.<lambda>
genfromtxtc                   C   r  )Nz1,2.1r   r   r   r   r   r     r*   )inti8)floatf8)r  	delimiterzfunction, args, kwargs	numpy_refTc                 C   s   |  d| j |  || j tt|}t| j|}|du r#td}n| j }tdd |D }||i |d|i}	|du rnt|	tju sIJ tdd |D }
||
i |}|dkrg|d |	d t	|	| d S t|	| ju swJ |	j
|u s~J d S )	Nr   Tr-   c                 s   "    | ]}t |r| n|V  qd S r   callablerT   rL   r   r   r   r          z0TestArrayLike.test_array_like.<locals>.<genexpr>r   c                 s   r  r   r  r  r   r   r   r     r  r   )r   r   r   r/   r   tuplerQ   r>   fillr   r   )r   r   r   r   r  np_funcr   r   	like_argsr   np_argsnp_arrr   r   r   test_array_like  s&   



zTestArrayLike.test_array_liker   r-   r   c                 C   s   |  d| j |  || j tt|}|dkr| j }tdd |D }ttd ||i |d|i W d    d S 1 sAw   Y  d S )Nr   r   c                 s   r  r   r  r  r   r   r   r   !  r  z<TestArrayLike.test_no_array_function_like.<locals>.<genexpr>z9The `like` argument must be an array-like that implementsr   )r   r   r   r/   r   r  r   r_   )r   r   r   r   r   r  r  r   r   r   test_no_array_function_like  s   

"z)TestArrayLike.test_no_array_function_likec                 C   s  |  d| j |  d| j |du rtd}n| j }tjd}t U}tj	|d}|
| tj||d}|du rZt|tju sHJ tj||d}t|| t|| nt|| ju scJ |j| jju stJ W d    d S W d    d S 1 sw   Y  d S )Nr   fromfileTr-      testfiler   )r   r   r/   r   randomtempfileTemporaryDirectoryospathjointofiler   rQ   r>   r   r   )r   r  r   datatmpdirfnamer   np_resr   r   r   test_array_like_fromfile'  s*   



"z&TestArrayLike.test_array_like_fromfilec                 C   sZ   | j d| jdd | j }tt tjdd|d W d    d S 1 s&w   Y  d S )Nr   T)r   r-   )r   r   )r   r   r   r   r_   r/   )r   r   r   r   r   test_exception_handlingA  s
   

"z%TestArrayLike.test_exception_handlingN)F)r4   r5   r6   r   r   r   rs   r   r/   r  reshaper  r^   _array_testspytestmarkparametrizer  r  r.  r/  r   r   r   r   r     sP    

	
r   )&r   r}   r&  r$  ior   unittestr   r   r/   numpy.testingr   r   r   r   numpy.core.overridesr	   r
   r   r   numpy.compatr   r2  r3  skipifrs   r   r$   r+   r,   rd   rt   r   r   r   r   r   r   r   r   r   r   <module>   s@    



o='5;