o 6aN@sNddlZddlZddlZddlZddlmZddlmZddlZ ddl m Z m Z m Z mZddlmZmZmZmZddlmZddlZejje ddZd d Zed d d dZedd ddZGdddZGdddZeGdddZeGdddZ ddZ!eGdddZ"GdddZ#Gd d!d!Z$Gd"d#d#Z%dS)$NStringIO)mock)assert_ assert_equal assert_raisesassert_raises_regex)_get_implementing_argsarray_function_dispatchverify_matching_signaturesARRAY_FUNCTION_ENABLED)picklez(__array_function__ dispatch not enabled.)reasoncOtSNNotImplementedselfargskwargsrA/usr/lib/python3/dist-packages/numpy/core/tests/test_overrides.py_return_not_implementedrcC|fSrrarrayrrrrcCdS Docstring.originalrrrrrdispatched_one_argr$cC||fSrrarray1array2rrrr"cCr r!rr'rrrdispatched_two_arg"r%r+c@s<eZdZddZddZddZddZd d Zd d Zd S)TestGetImplementingArgscCs|td}t|g}tt||gt||g}tt||gt|dg}tt||gtd|g}tt||gdS)Nnprr rlist)rrrrrr test_ndarray*s     z$TestGetImplementingArgs.test_ndarraycCsGdddtj}Gdddtj}tdtj}td|}td|}t||g}tt|||gt||g}tt|||gt||g}tt|||gdS)Nc@eZdZeZdS)zDTestGetImplementingArgs.test_ndarray_subclasses..OverrideSubN__name__ __module__ __qualname__r__array_function__rrrr OverrideSub;r8c@ eZdZdS)zFTestGetImplementingArgs.test_ndarray_subclasses..NoOverrideSubNr4r5r6rrrr NoOverrideSub>r<r-)r/ndarrayrviewr rr0)rr8r<r override_subno_override_subrrrrtest_ndarray_subclasses9s  z/TestGetImplementingArgs.test_ndarray_subclassescCs^Gddd}td}|}t||g}tt|||gt||g}tt|||gdS)Nc@r2)zBTestGetImplementingArgs.test_ndarray_and_duck_array..OtherNr3rrrrOtherQr9rCr-r.)rrCrotherrrrrtest_ndarray_and_duck_arrayOs   z3TestGetImplementingArgs.test_ndarray_and_duck_arraycCsxGdddtj}Gddd}td}td|}|}tt|||g|||gtt|||g|||gdS)Nc@r2)zQTestGetImplementingArgs.test_ndarray_subclass_and_duck_array..OverrideSubNr3rrrrr8_r9r8c@r2)zKTestGetImplementingArgs.test_ndarray_subclass_and_duck_array..OtherNr3rrrrrCbr9rCr-)r/r>rr?rr )rr8rCrsubarrayrDrrr$test_ndarray_subclass_and_duck_array]s z.ANr3rrrrApr9rHc@r2)z8TestGetImplementingArgs.test_many_duck_arrays..BNr3rrrrBsr9rIc@r2)z8TestGetImplementingArgs.test_many_duck_arrays..CNr3rrrrCvr9rJc@r2)z8TestGetImplementingArgs.test_many_duck_arrays..DNr3rrrrDyr9rKr-)rr ) rrHrIrJrKabcdrrrtest_many_duck_arraysns"z-TestGetImplementingArgs.test_many_duck_arrayscsttdfddtdD}dd|D}t|dd}t||ddttd t|WddS1s=wYdS)N)r7cs"g|] }tdt|tfqS)rH)typestrobject).0i namespacerr s"zETestGetImplementingArgs.test_too_many_duck_arrays..!cSsg|]}|qSrrrTtrrrrXs zdistinct argument types)dictrranger rr TypeError)rtypes relevant_argsactualrrVrtest_too_many_duck_arrayss   "z1TestGetImplementingArgs.test_too_many_duck_arraysN) r4r5r6r1rBrErGrPrcrrrrr,(s r,c@s eZdZeddZddZdS)TestNDArrayArrayFunctionc CsvGddd}Gdddtj}Gdddtj}tdg}|}||}||}|jttjf|dfid }t|d |jttj|f||fid }t|tu|jttj|f||fid }t|d |jttj|f||fid }t|d t t d t ||fWdn1swYt ||f} t ||f}t|| |t ||f}t|| |dS) Nc@r2)z3TestNDArrayArrayFunction.test_method..OtherNr3rrrrrCr9rCc@r:)z;TestNDArrayArrayFunction.test_method..NoOverrideSubNr;rrrrr<r=r<c@r2)z9TestNDArrayArrayFunction.test_method..OverrideSubNr3rrrrr8r9r8r-g?funcr`rrr#no implementation found) r/r>rr?r7r+rrrrr_ concatenate) rrCr<r8rrDrAr@resultexpectedrrr test_methodsJ        z$TestNDArrayArrayFunction.test_methodcCsZtd}dd}ttd|j|tjf|fidWddS1s&wYdS)Nr-cS|Srrxrrrrz:TestNDArrayArrayFunction.test_no_wrapper.._implementationre)r/rrAttributeErrorr7r>)rrrfrrrtest_no_wrappers   "z(TestNDArrayArrayFunction.test_no_wrapperN)r4r5r6requires_array_functionrkrrrrrrrds  /rdc@s,eZdZddZddZddZddZd S) TestArrayFunctionDispatchcCs:tdtjdD]}ttjt|d}t|tuqdS)Nr-)protocol)r^r HIGHEST_PROTOCOLloadsdumpsr$r)rproto roundtrippedrrr test_pickles  z%TestArrayFunctionDispatch.test_picklecCs,ttjdtjjdkrttjddSdS)Nr$rur")rr$r4sysflagsoptimize__doc__rrrrtest_name_and_docstrings  z1TestArrayFunctionDispatch.test_name_and_docstringcCsjGddd}|}t|\}}}}}t||ut|tutt||ht||fkt|idS)Nc@eZdZddZdS)z9TestArrayFunctionDispatch.test_interface..MyArraycSs|||||fSrrrrfr`rrrrrr7szLTestArrayFunctionDispatch.test_interface..MyArray.__array_function__Nr4r5r6r7rrrrMyArray r)r$rrset)rrr#objrfr`rrrrrtest_interfaces  z(TestArrayFunctionDispatch.test_interfacecCsLGddd}|}ttd t|WddS1swYdS)Nc@r)z?TestArrayFunctionDispatch.test_not_implemented..MyArraycSrrrrrrrr7rzRTestArrayFunctionDispatch.test_not_implemented..MyArray.__array_function__Nrrrrrrrrrg)rr_r$rrrrrrtest_not_implementeds   "z.TestArrayFunctionDispatch.test_not_implementedN)r4r5r6r|rrrrrrrrts  rtc@seZdZddZddZdS)TestVerifyMatchingSignaturescCs6tddddtd ddd ddtdddd ddtttddddWdn1s7wYtttddd ddWdn1sUwYtttd d dd d dWdn1stwYtttdd ddd dWddS1swYdS)NcSr NrrrmrrrrrozNTestVerifyMatchingSignatures.test_verify_matching_signatures..cSr rrrmrrrrror-cSr rrrmrrrrrocSr rr)rLrrrrrocSr rr)rMrrrrrocSr rrrmrrrrrocSr rrrmrrrr rocSr rryrrrr rocSr rrrmrrrr rocSr rrrrrrr rorr-)r r RuntimeErrorrrrrtest_verify_matching_signaturess    "z.cSdSrrrrrrfzDTestVerifyMatchingSignatures.test_array_function_dispatch..fcSrrrrmrrrrrF)verifycSrrrrrrrrr)rrr )rrrrrtest_array_function_dispatchs   z9TestVerifyMatchingSignatures.test_array_function_dispatchN)r4r5r6rrrrrrrs rcs,iGfdddfdd}|fS)z2Create a duck array type and implements functions.cseZdZfddZdS)z._new_duck_type_and_implements..MyArraycs8|vrtStfdd|DstS||i|S)Nc3s|]}t|VqdSr) issubclassrZ)rrr #szT_new_duck_type_and_implements..MyArray.__array_function__..)rallrHANDLED_FUNCTIONSrrrr7 s zA_new_duck_type_and_implements..MyArray.__array_function__Nrrrrrrsrcsfdd}|S)z/Register an __array_function__ implementations.cs |<|Srr)rf)rnumpy_functionrr decorator)szD_new_duck_type_and_implements..implements..decoratorr)rr)r)rr implements'sz1_new_duck_type_and_implements..implementsr)rrrr_new_duck_type_and_implementss rc@$eZdZddZddZddZdS)TestArrayFunctionImplementationcCs<t\}}|tdd}ttddtt|ddS)NcSr )Nmyarrayrrrrr_7rz7TestArrayFunctionImplementation.test_one_arg.._r-r#r)rr$rrrrrrrr test_one_arg4s  z,TestArrayFunctionImplementation.test_one_argcCst\}}tdddddd}||ddd}t|d dt|d d d d t||dtt||d d Wdn1sHwY||d d }t|d tt||dd WddS1spwYdS)NcSrrrroptionrrrrArzDTestArrayFunctionImplementation.test_optional_args..defaultcS|Srrrrrrfunc_with_optionArzLTestArrayFunctionImplementation.test_optional_args..func_with_optionrcSrrr)r new_optionrrrmy_array_func_with_optionErzUTestArrayFunctionImplementation.test_optional_args..my_array_func_with_optionr-extra)ryes)rnor)r)r)rr rrr_)rrrrrrirrrtest_optional_args>s       "z2TestArrayFunctionImplementation.test_optional_argscCst\}}tdddddd}td}t|||ut|jdttd||WddS1s:wYdS) NcSrrrrrrrrZrzFTestArrayFunctionImplementation.test_not_implemented..my)modulecSrlrrrrrrrfZrzBTestArrayFunctionImplementation.test_not_implemented..funcr-z%no implementation found for 'my.func') rr r/rrrr5rr_)rrrrfrrrrrWs     "z4TestArrayFunctionImplementation.test_not_implementedN)r4r5r6rrrrrrrr1s rc@r)TestNDArrayMethodscCsBGdddtj}td|}tt|dtt|ddS)Nc@r)z-TestNDArrayMethods.test_repr..MyArrayc_rrrrrrrrr7nrz@TestNDArrayMethods.test_repr..MyArray.__array_function__Nrrrrrrmrrr-z MyArray(1)1)r/r>rr?rreprrRrrrr test_repriszTestNDArrayMethods.test_reprN)r4r5r6rrrrrrgs rc@s@eZdZddZddZeddZeddZed d Zd S) TestNumPyFunctionscCsBttjjdttjjjdttjjjdttjjjddS)Nnumpyz numpy.charz numpy.fftz numpy.linalg) rr/sumr5charequalfftlinalgsolverrrrtest_set_modulexsz"TestNumPyFunctions.test_set_modulecCsttj}td|jvdS)Naxis)inspect signaturer/rr parameters)rrrrrtest_inspect_sum~s z#TestNumPyFunctions.test_inspect_sumcCs2t\}}|tjdd}tt|ddS)NcSr )Nrrrrrrrrz/TestNumPyFunctions.test_override_sum.._r)rr/rrrrrrtest_override_sums  z$TestNumPyFunctions.test_override_sumcCsfGddd}|tj|d}d|jj_t|}t|d|jjtj|f|fi|jj dS)Nc@r)z=TestNumPyFunctions.test_sum_on_mock_array..ArrayProxycS ||_dSr)value)rrrrr__init__ zFTestNumPyFunctions.test_sum_on_mock_array..ArrayProxy.__init__c_|jj|i|Sr)rr7rrrrr7zPTestNumPyFunctions.test_sum_on_mock_array..ArrayProxy.__array_function__c_rr)r __array__rrrrrrzGTestNumPyFunctions.test_sum_on_mock_array..ArrayProxy.__array__N)r4r5r6rr7rrrrr ArrayProxys r)specr-) rMockrr7 return_valuer/rrassert_called_once_withrassert_not_called)rrproxyrirrrtest_sum_on_mock_arrays   z)TestNumPyFunctions.test_sum_on_mock_arraycCs6Gdddtj}td|}tt|ddS)Ncs$eZdZddZfddZZS)zFTestNumPyFunctions.test_sum_forwarding_implementation..MyArraycSr )Nsummedr)rroutrrrrrzJTestNumPyFunctions.test_sum_forwarding_implementation..MyArray.sumcst||||Sr)superr7r __class__rrr7rzYTestNumPyFunctions.test_sum_forwarding_implementation..MyArray.__array_function__)r4r5r6rr7 __classcell__rrrrrsrr-r)r/r>rr?rrrrrr"test_sum_forwarding_implementations z5TestNumPyFunctions.test_sum_forwarding_implementationN) r4r5r6rrrsrrrrrrrrvs  rc@seZdZddZd.MyArrayNcSrrfunctionrrrrrrrz-TestArrayLike.setup..MyArray.__init__cSs6zt||j}Wn tytYSw||i|Sr)getattrr4rqr)rrfr`rrmy_funcrrrr7s  z7TestArrayLike.setup..MyArray.__array_function__r)r4r5r6rr7rrrrrs  rc@seZdZdddZdS)z3TestArrayLike.setup..MyNoArrayFunctionArrayNcSrrrrrrrrrz.MyNoArrayFunctionArray.__init__r)r4r5r6rrrrrMyNoArrayFunctionArraysr)rr)rrrrrrsetups  zTestArrayLike.setupFcs fdd}t|dS)Ncs*d|vsJrd|vrttS)Nlike value_error) ValueErrorrr arr_classenable_value_errornamerr _definitions  z-TestArrayLike.add_method.._definition)setattr)rrrrrrrr add_methodszTestArrayLike.add_methodcOr&rrrrrr func_argsszTestArrayLike.func_argscCsV|d|j|j}ttdtjd|d}WddS1s$wYdS)Nrrgr-r)rrrrr_r/asarray)rref array_likerrrtest_array_like_not_implementeds   "z-TestArrayLike.test_array_like_not_implementedrrr asanyarrayascontiguousarray)ruasfortranarrayrequirerurrHF) requirementsemptyfulloneszerosarange frombuffers)dtypefromiter fromstringz1,2,)rseploadtxtcCtdS)Nz0 1 2 3rrrrrrr*zTestArrayLike. genfromtxtcCr )Nz1,2.1rrrrrrr*)inti8)floatf8)r delimiterzfunction, args, kwargs numpy_refTc Cs|d|j|||jtt|}t|j|}|dur#td}n|j}tdd|D}||i|d|i} |durnt| tjusIJtdd|D} || i|} |dkrg| d| dt | | dSt| |juswJ| j |us~JdS) NrTr-cs"|] }t|r |n|VqdSrcallablerTrLrrrr z0TestArrayLike.test_array_like..rcsrrrrrrrrrr) rrrr/rtuplerQr>fillrr) rrrrrnp_funcrr like_argsrnp_argsnp_arrrrrtest_array_likes&      zTestArrayLike.test_array_likerr-rcCs|d|j|||jtt|}|dkr|j}tdd|D}ttd||i|d|iWddS1sAwYdS)Nrrcsrrrrrrrr rz.z9The `like` argument must be an array-like that implementsr)rrrr/rrrr_)rrrrrrrrrrtest_no_array_function_likes  "z)TestArrayLike.test_no_array_function_likecCs |d|j|d|j|durtd}n|j}tjd}tU}tj |d}| |tj ||d}|durZt |tj usHJtj ||d}t||t||nt ||juscJ|j|jj ustJWddSWddS1swYdS)NrfromfileTr-testfiler)rrr/rrandomtempfileTemporaryDirectoryospathjointofiler rQr>rr)rrrdatatmpdirfnamernp_resrrrtest_array_like_fromfile&s*        "z&TestArrayLike.test_array_like_fromfilecCsZ|jd|jdd|j}tttjdd|dWddS1s&wYdS)NrT)rr-)rr)rrrrr_r/)rrrrrtest_exception_handling@s   "z%TestArrayLike.test_exception_handlingN)F)r4r5r6rrrrsrr/rreshaper r^ _array_testspytestmark parametrizerrr.r/rrrrrsP        r)&rr}r&r$iorunittestrrr/ numpy.testingrrrrnumpy.core.overridesr r r r numpy.compatr r2r3skipifrsrr$r+r,rdrtrrrrrrrrrrs@       o='5;