o
    Ö­j#/  ã                   @   sP  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
mZ ddlZddlZzddlZddlmZ W n eyI   dZY nw ddlmZ dZe e¡e e¡k r^dZg d¢ZdZe d	¡pjd
Zdev rqdZdejd v ZG dd„ deƒZ G dd„ dƒZ!G dd„ dƒZ"dd„ Z#dd„ Z$dd„ Z%dd„ Z&dd„ Z'dd„ Z(dS ) z
Generic test utilities.

é    N)Úmodule_from_specÚspec_from_file_location)Úversion)Ú_pep440z3.0.8)ÚPytestTesterÚcheck_free_memoryÚ_TestPythranFuncÚIS_MUSLFÚHOST_GNU_TYPEÚ ÚmuslTÚeditablec                   @   s   e Zd ZdZdS )ÚFPUModeChangeWarningzWarning about FPU mode changeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úR/var/www/html/CropPilot/venv/lib/python3.10/site-packages/scipy/_lib/_testutils.pyr   3   s    r   c                   @   s&   e Zd ZdZdd„ Z		d
dd	„ZdS )r   a"  
    Run tests for this namespace

    ``scipy.test()`` runs tests for all of SciPy, with the default settings.
    When used from a submodule (e.g., ``scipy.cluster.test()``, only the tests
    for that namespace are run.

    Parameters
    ----------
    label : {'fast', 'full'}, optional
        Whether to run only the fast tests, or also those marked as slow.
        Default is 'fast'.
    verbose : int, optional
        Test output verbosity. Default is 1.
    extra_argv : list, optional
        Arguments to pass through to Pytest.
    doctests : bool, optional
        Whether to run doctests or not. Default is False.
    coverage : bool, optional
        Whether to run tests with code coverage measurements enabled.
        Default is False.
    tests : list of str, optional
        List of module names to run tests for. By default, uses the module
        from which the ``test`` function is called.
    parallel : int, optional
        Run tests in parallel with pytest-xdist, if number given is larger than
        1. Default is 1.

    c                 C   s
   || _ d S ©N)Úmodule_name)Úselfr   r   r   r   Ú__init__V   s   
zPytestTester.__init__Úfasté   NFc              
   C   sJ  dd l }tj| j }	tj |	jd ¡}
ddg}|r|t|ƒ7 }|r4t	|ƒdkr4|ddt	|ƒd   g7 }|r=|d|
 g7 }|dkrH|d	d
g7 }n
|dkrR|d	|g7 }|d u rZ| jg}|d ury|dkryt
ƒ rn|dt|ƒg7 }ndd l}|jddd� |dgt|ƒ 7 }z
| |¡}W |dkS  ty¤ } z|j}W Y d }~|dkS d }~ww )Nr   z--showlocalsz
--tb=shortr   ú-Úvz--cov=r   z-mznot slowÚfullz-nzMCould not run tests in parallel because pytest-xdist plugin is not available.é   )Ú
stacklevelz--pyargs)ÚpytestÚsysÚmodulesr   ÚosÚpathÚabspathÚ__path__ÚlistÚintÚ_pytest_has_xdistÚstrÚwarningsÚwarnÚmainÚ
SystemExitÚcode)r   ÚlabelÚverboseÚ
extra_argvÚdoctestsÚcoverageÚtestsÚparallelr    ÚmoduleÚmodule_pathÚpytest_argsr+   r/   Úexcr   r   r   Ú__call__Y   s@   þý€ýzPytestTester.__call__)r   r   NFFNN)r   r   r   r   r   r;   r   r   r   r   r   8   s    ÿr   c                   @   s|   e Zd ZdZejejejejej	ej
gZejejgZejejgZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ ZdS )r   aL  
    These are situations that can be tested in our pythran tests:
    - A function with multiple array arguments and then
      other positional and keyword arguments.
    - A function with array-like keywords (e.g. `def somefunc(x0, x1=None)`.
    Note: list/tuple input is not yet tested!

    `self.arguments`: A dictionary which key is the index of the argument,
                      value is tuple(array value, all supported dtypes)
    `self.partialfunc`: A function used to freeze some non-array argument
                        that of no interests in the original function
    c                 C   s   i | _ d | _d | _d S r   )Ú	argumentsÚpartialfuncÚexpected)r   r   r   r   Úsetup_method—   s   
z_TestPythranFunc.setup_methodc                 C   s>   t  |¡}i }|j ¡ D ]\}}|jt jjur|j||< q|S r   )ÚinspectÚ	signatureÚ
parametersÚitemsÚdefaultÚ	ParameterÚempty)r   ÚfuncrA   Úoptional_argsÚkr   r   r   r   Úget_optional_argsœ   s   

€z"_TestPythranFunc.get_optional_argsc                 C   s2   d}| j D ]}t| j | d ƒ}||kr|}q|S )Nr   r   )r<   Úlen)r   Úmax_lenÚarg_idxÚcur_lenr   r   r   Úget_max_dtype_list_length¦   s   
€z*_TestPythranFunc.get_max_dtype_list_lengthc                 C   s    |t |ƒd kr|d S || S )Nr   éÿÿÿÿ)rK   )r   Ú
dtype_listÚ	dtype_idxr   r   r   Ú	get_dtype¯   s   z_TestPythranFunc.get_dtypec                 C   s`   t |  ¡ ƒD ]'}g }| jD ]}|  | j| d |¡}| | j| d  |¡¡ q| j|Ž  qd S )Nr   r   )ÚrangerO   r<   rS   ÚappendÚastypeÚpythranfunc)r   Útype_idxÚ
args_arrayrM   Ú	new_dtyper   r   r   Útest_all_dtypes·   s   
ÿúz _TestPythranFunc.test_all_dtypesc                 C   sF   g }| j D ]}| | j | d d d d… d d d… ¡ q| j|Ž  d S )Nr   rP   )r<   rU   rW   ©r   rY   rM   r   r   r   Ú
test_viewsÀ   s   
*z_TestPythranFunc.test_viewsc                 C   sH   g }| j D ]}| tj| j | d ddd�d d d… ¡ q| j|Ž  d S )Nr   r   )Úaxis)r<   rU   ÚnpÚrepeatrW   r\   r   r   r   Útest_stridedÆ   s   
ÿÿz_TestPythranFunc.test_stridedN)r   r   r   r   r_   Úint8Úint16Úint32Úint64ÚintcÚintpÚALL_INTEGERÚfloat32Úfloat64Ú	ALL_FLOATÚ	complex64Ú
complex128ÚALL_COMPLEXr?   rJ   rO   rS   r[   r]   ra   r   r   r   r   r   †   s    
		r   c                  C   s   ddl m}  | dƒduS )zQ
    Check if the pytest-xdist plugin is installed, providing parallel tests
    r   ©Ú	find_specÚxdistN)Úimportlib.utilrp   ro   r   r   r   r)   Î   s   r)   c                 C   sŠ   ddl }zttjd ƒ}d | tjd ¡}W n ty5   tƒ }|du r)| d¡ | › d|d › d�}Y nw || d k rC| |¡ dS dS )	zJ
    Check *free_mb* of memory is available, otherwise do pytest.skip
    r   NÚSCIPY_AVAILABLE_MEMz={} MB memory required, but environment SCIPY_AVAILABLE_MEM={}zlCould not determine available memory; set SCIPY_AVAILABLE_MEM variable to free memory in MB to run the test.z MB memory required, but ç    €„.Az MB available)r    Ú_parse_sizer#   ÚenvironÚformatÚKeyErrorÚ_get_mem_availableÚskip)Úfree_mbr    Úmem_freeÚmsgr   r   r   r   ×   s   
ÿ
ûÿr   c                 C   sx   dddddddddddddd	d
œ}t  d d | ¡ ¡¡| t j¡}|r*| d¡|vr.tdƒ‚t| d¡ƒ|| d¡  S )Nrt   g      ð?ç     @�@g    eÍÍAg   ¢”mBg      �@g      0Ag      ÐAg      pB)r   ÚbrI   ÚMÚGÚTÚkbÚMbÚGbÚTbÚkibÚMibÚGibÚTibz^\s*(\d+)\s*({})\s*$ú|r   zInvalid size stringr   )	ÚreÚmatchrw   ÚjoinÚkeysÚIÚgroupÚ
ValueErrorÚfloat)Úsize_strÚsuffixesÚmr   r   r   ru   ì   s   üþru   c               	   C   s¼   z
ddl } |  ¡ jW S  ttfy   Y nw tj d¡r\i }tdƒ�"}|D ]}| 	¡ }t
|d ƒd ||d  d¡ ¡ < q%W d  ƒ n1 sGw   Y  d|v rT|d S |d	 |d
  S dS )zD
    Get information about memory available, not counting swap.
    r   NÚlinuxz/proc/meminfor   r~   ú:ÚmemavailableÚmemfreeÚcached)ÚpsutilÚvirtual_memoryÚ	availableÚImportErrorÚAttributeErrorr!   ÚplatformÚ
startswithÚopenÚsplitr“   ÚstripÚlower)rœ   ÚinfoÚfÚlineÚpr   r   r   ry   û   s$   ÿ
$þÿry   c              	      sd  ddl }z	t ddg¡ W n ty   | d¡ Y nw | tt ¡ ƒ } tj	 
|¡d }t || | ¡ | | d d }|d	 ‰tjˆd
d� t|d ƒ}t|dƒ�}| d¡ | dtj› d�¡ W d  ƒ n1 slw   Y  tjdkr‡tjdddd|dt|ƒgˆd� ntjddd|t|ƒgˆd� tjg d¢ˆd� t d¡‰ ‡ ‡fdd„}|dƒ|dƒfS )z•
    Helper function to test building and importing Cython modules that
    make use of the Cython APIs for BLAS, LAPACK, optimize, and special.
    r   NÚmesonz	--versionzNo usable 'meson' foundr   r5   Ú_cython_examplesÚbuildT)Úexist_okzinterpreter-native-file.iniÚwz[binaries]
z
python = 'ú'Úwin32Úsetupz--buildtype=releasez--native-filez--vsenv)Úcwd)r«   Úcompilez-vvÚ
EXT_SUFFIXc                    s0   ˆ|    ˆ ¡}t| |ƒ}t|ƒ}|j |¡ |S r   )Úwith_suffixr   r   ÚloaderÚexec_module)ÚmodnameÚsoÚspecÚmod©ÚsuffixÚ
target_dirr   r   ÚloadA  s
   
z$_test_cython_extension.<locals>.loadÚ	extendingÚextending_cpp)r    Ú
subprocessÚ
check_callÚFileNotFoundErrorrz   r*   Ú	threadingÚ	get_identr#   r$   r¤   ÚshutilÚcopytreeÚmakedirsr£   Úwriter!   Ú
executabler¡   Ú	sysconfigÚget_config_var)Útmp_pathÚsrcdirr    Úmod_nameÚ	build_dirÚnative_filer¨   rÀ   r   r½   r   Ú_test_cython_extension  sD   ÿ
þ
ýü
ÿþ
rÔ   c                    sp   t  | ¡‰ ‡ ‡fdd„}g }td| ƒD ]}| t j||f| |d�¡ q|D ]}| ¡  q&|D ]}| ¡  q/dS )aÍ  
    Run a given function concurrently across a given number of threads.

    This is equivalent to using a ThreadPoolExecutor, but using the threading
    primitives instead. This function ensures that the closure passed by
    parameter gets called concurrently by setting up a barrier before it gets
    called before any of the threads.

    Arguments
    ---------
    n_workers: int
        Number of concurrent threads to spawn.
    fn: callable
        Function closure to execute concurrently. Its first argument will
        be the thread id.
    *args: tuple
        Variable number of positional arguments to pass to the function.
    **kwargs: dict
        Keyword arguments to pass to the function.
    c                    s"   ˆ   ¡  ˆ| g|¢R i |¤Ž d S r   )Úwait)ÚiÚargsÚkwargs©ÚbarrierÚfnr   r   Úclosurec  s   z(_run_concurrent_barrier.<locals>.closurer   )Útargetr×   rØ   N)rÆ   ÚBarrierrT   rU   ÚThreadÚstartrŽ   )Ú	n_workersrÛ   r×   rØ   rÜ   ÚworkersrÖ   Úworkerr   rÙ   r   Ú_run_concurrent_barrierL  s   


þ

ÿrä   ))r   r@   r#   rŒ   rÈ   rÃ   r!   rÍ   rÆ   rr   r   r   Únumpyr_   ÚscipyÚcythonÚCython.Compiler.Versionr   Úcython_versionrŸ   Ú
scipy._libr   Úrequired_versionÚparseÚVersionÚ__all__r	   rÎ   Ú_vr&   ÚIS_EDITABLEÚRuntimeWarningr   r   r   r)   r   ru   ry   rÔ   rä   r   r   r   r   Ú<module>   sJ    ÿNH	8