B
    dC              
   @   st  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m	Z
 ddlmZmZ ddlmZ ddd	d
ddddddg
ZejjZdd Zdd ZG dd dZdadaeddddddgaeg aedefdefd efd!efged"efgd#adg g i fd$dZd%d
 Z G d&d dej!Z!G d'd( d(e!Z"d1d)dZ#ej$ddd*gd+d,d Z%d-d Z&d.d Z'd2d0dZ(dS )3z
This module provides the tools used to internally run the astropy test suite
from the installed astropy.  It makes use of the `pytest`_ testing framework.
    N)allclose)AstropyDeprecationWarning AstropyPendingDeprecationWarning   )
TestRunnerraises!enable_deprecations_as_exceptionsremote_data treat_deprecations_as_exceptionscatch_warnings!assert_follows_unicode_guidelinesassert_quantity_allclosecheck_pickling_recoverypickle_protocolgeneric_recursive_equality_testc                s   dd   fdd| D S )Nc             S   s   | d krd S t | S )N)str)x r   a/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/tests/helper.pyto_str_or_none$   s    z)_fix_user_options.<locals>.to_str_or_nonec                s"   g | ]}t  fd d|D qS )c             3   s   | ]} |V  qd S )Nr   ).0r   )r   r   r   	<genexpr>)   s    z/_fix_user_options.<locals>.<listcomp>.<genexpr>)tuple)r   y)r   r   r   
<listcomp>)   s    z%_fix_user_options.<locals>.<listcomp>r   )optionsr   )r   r   _fix_user_options#   s    r   c             C   s   ddl m} |dkrdS y|   W n$ tk
rH   |   | jj}Y n
X | jj}xVt|	 D ]F}t
jt
j|t
j|}t
jt
j||}||||< q`W |dd |   |dd | jt
j|dd dS )	z
    This method is called after the tests have been run in coverage mode
    to cleanup and then save the coverage data and report.
    r   )color_printNz$Saving coverage data in .coverage...greenz)Saving HTML coverage report in htmlcov...Zhtmlcov)	directory)Zastropy.utils.consoler   get_dataAttributeErrorZ_harvest_datadatalines_lineslistkeysospathrelpathrealpathabspathjoinpopsaveZhtml_report)ZcovresultrootdirZtesting_pathr   r#   keynew_pathr   r   r   _save_coverage,   s(    


r3   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )r   a  
    A decorator to mark that a test should raise a given exception.
    Use as follows::

        @raises(ZeroDivisionError)
        def test_foo():
            x = 1/0

    This can also be used a context manager, in which case it is just
    an alias for the ``pytest.raises`` context manager (because the
    two have the same name this help avoid confusion by being
    flexible).

    .. note:: Usage of ``pytest.raises`` is preferred.

    c             C   s   || _ d | _d S )N)_exc_ctx)selfexcr   r   r   __init__i   s    zraises.__init__c                s   t   fdd}|S )Nc                 s   t jj f| | d S )N)pytestr   r4   )argskwargs)funcr6   r   r   run_raises_testn   s    z(raises.__call__.<locals>.run_raises_test)	functoolswraps)r6   r<   r=   r   )r<   r6   r   __call__m   s    zraises.__call__c             C   s   t | j| _| j S )N)r9   r   r4   r5   	__enter__)r6   r   r   r   rA   s   s    zraises.__enter__c             G   s   | j j| S )N)r5   __exit__)r6   exc_infor   r   r   rB   w   s    zraises.__exit__N)__name__
__module____qualname____doc__r8   r@   rA   rB   r   r   r   r   r   V   s
   FTcompilerZscipypygmentsZ	ipykernelZIPython
setuptoolsz]Importing from numpy\.testing\.decorators is deprecated, import from numpy\.testing instead\.z`inspect\.getargspec\(\) is deprecated, use inspect\.signature\(\) or inspect\.getfullargspec\(\)z,split\(\) requires a non-empty pattern matchzYcan't resolve package from __spec__ or __package__, falling back on __name__ and __path__z.Using or importing the ABCs from 'collections')N)      c             C   sZ   da | at| t| x8| D ],\}}|tkrFt| | q&t|t|< q&W dS )a  
    Turn on the feature that turns deprecations into exceptions.

    Parameters
    ----------
    include_astropy_deprecations : bool
        If set to `True`, ``AstropyDeprecationWarning`` and
        ``AstropyPendingDeprecationWarning`` are also turned into exceptions.

    modules_to_ignore_on_import : list of str
        List of additional modules that generate deprecation warnings
        on import, which are to be ignored. By default, these are already
        included: ``compiler``, ``scipy``, ``pygments``, ``ipykernel``, and
        ``setuptools``.

    warnings_to_ignore_entire_module : list of str
        List of modules with deprecation warnings to ignore completely,
        not just during import. If ``include_astropy_deprecations=True``
        is given, ``AstropyDeprecationWarning`` and
        ``AstropyPendingDeprecationWarning`` are also ignored for the modules.

    warnings_to_ignore_by_pyver : dict
        Dictionary mapping tuple of ``(major, minor)`` Python version to
        a list of ``(warning_message, warning_class)`` to ignore.
        Python version-agnostic warnings should be mapped to `None` key.
        This is in addition of those already ignored by default
        (see ``_warnings_to_ignore_by_pyver`` values).

    TN)_deprecations_as_exceptions_include_astropy_deprecations_modules_to_ignore_on_importupdate!_warnings_to_ignore_entire_moduleitems_warnings_to_ignore_by_pyverset)Zinclude_astropy_deprecationsZmodules_to_ignore_on_importZ warnings_to_ignore_entire_moduleZwarnings_to_ignore_by_pyverr1   valr   r   r   r      s    "

c           	   C   s  x0t tj D ]} t| tjrt| dr| `qW t	s:dS t
  t
dt x.tD ]&}yt| W qT tk
rx   Y qTX qTW t
  tttg}tr|ttg7 }x|D ]}t
dd| qW x*tD ]"}x|D ]}t
jd||d qW qW xRtD ]J}|dkstjdd |krx(t| D ]}t
d|d |d	  qW qW yddl}W n tk
r^   Y n"X |jd d
k rt
jdtdd dS )aE  
    Turn all DeprecationWarnings (which indicate deprecated uses of
    Python itself or Numpy, but not within Astropy, where we use our
    own deprecation warning class) into exceptions so that we find
    out about them early.

    This completely resets the warning filters and any "already seen"
    warning state.
    __warningregistry__Nignoreerrorz.*)categorymodule   r   r   3znumpy.lib.type_check)r%   sysmodulesvalues
isinstancetypes
ModuleTypehasattrrV   rM   warningsresetwarningssimplefilterDeprecationWarningrO   
__import__ImportErrorFutureWarningImportWarningrN   r   r   filterwarningsrQ   rS   version_info
matplotlib__version__)rZ   mZ
_all_warnswvsrn   r   r   r   r
      sD    







 c                   s4   e Zd ZdZ fddZ fddZdd Z  ZS )r   ax  
    A high-powered version of warnings.catch_warnings to use for testing
    and to make sure that there is no dependence on the order in which
    the tests are run.

    This completely blitzes any memory of any warnings that have
    appeared before so that all warnings will be caught and displayed.

    ``*args`` is a set of warning classes to collect.  If no arguments are
    provided, all warnings are collected.

    Use as follows::

        with catch_warnings(MyCustomWarning) as w:
            do.something.bad()
        assert len(w) > 0

    .. note:: Usage of :ref:`pytest.warns <pytest:warns>` is preferred.

    c                s   t  jdd || _d S )NT)record)superr8   classes)r6   rv   )	__class__r   r   r8   4  s    zcatch_warnings.__init__c                sT   t   }t  t| jdkr*td n&td x| jD ]}td| q<W |S )Nr   alwaysrW   )ru   rA   r
   lenrv   rd   rf   )r6   Zwarning_listcls)rw   r   r   rA   8  s    

zcatch_warnings.__enter__c             C   s
   t   d S )N)r
   )r6   typevalue	tracebackr   r   r   rB   C  s    zcatch_warnings.__exit__)rD   rE   rF   rG   r8   rA   rB   __classcell__r   r   )rw   r   r     s   c                   s6   e Zd ZdZd	 fdd	Zdd Z fddZ  ZS )
ignore_warningsa   
    This can be used either as a context manager or function decorator to
    ignore all warnings that occur within a function or block of code.

    An optional category option can be supplied to only ignore warnings of a
    certain category or categories (if a list is provided).
    Nc                s2   t    t|tr(t|tr(|g| _n|| _d S )N)ru   r8   r`   r{   
issubclassWarningrY   )r6   rY   )rw   r   r   r8   P  s    

zignore_warnings.__init__c                s   t   fdd}|S )Nc           	      s$   j jd  | |S Q R X d S )N)rY   )rw   rY   )r:   r;   )r<   r6   r   r   wrapperY  s    z)ignore_warnings.__call__.<locals>.wrapper)r>   r?   )r6   r<   r   r   )r<   r6   r   r@   X  s    zignore_warnings.__call__c                s@   t   }| jd k	r2x&| jD ]}td| qW n
td |S )NrW   )ru   rA   rY   rd   rf   )r6   retvalrY   )rw   r   r   rA   c  s    


zignore_warnings.__enter__)N)rD   rE   rF   rG   r8   r@   rA   r~   r   r   )rw   r   r   G  s   r   c          	   C   s  ddl m} |dd t| }t| }t| }t|ts@t|d t|tsXt|	d t|tsptt|tr|d n
|	d |dk	r| 
|| kst| 
|| kstt||| kstW dQ R X |dd t| }t| }t| }t|tst|d t|ts(tt|ts8tt|trP|d n
|	d |dk	r| 
|| ksxt| 
|| kstt||| kstW dQ R X dS )a  
    Test that an object follows our Unicode policy.  See
    "Unicode guidelines" in the coding guidelines.

    Parameters
    ----------
    x : object
        The instance to test

    roundtrip : module, optional
        When provided, this namespace will be used to evaluate
        ``repr(x)`` and ensure that it roundtrips.  It will also
        ensure that ``__bytes__(x)`` roundtrip.
        If not provided, no roundtrip testing will be performed.
    r   )confZunicode_outputFasciiNT)Zastropyr   Zset_tempbytesr   reprr`   AssertionErrordecodeencoderw   eval)r   Z	roundtripr   Zbytes_xZ	unicode_xZrepr_xr   r   r   r   m  s@    






)paramsc             C   s   | j S )z
    Fixture to run all the tests for protocols 0 and 1, and -1 (most advanced).
    (Originally from astropy.table.tests.test_pickle)
    )param)requestr   r   r   r     s    c             C   s   t | dr|  n| j}|j}x|D ]}||ks>td| t || dr|| || k}dt|krpd|k}|std| dt || dr$|| j|krq$|| jg}|| t|| || | q$W d	S )
zv
    Check if the attributes of a and b are equal. Then,
    check if the attributes of the attributes are equal.
    __getstate__zDid not pickle __eq____iter__Fz	Value of z changed by pickling__dict__N)rc   r   r   r   dirrw   extendr   )abclass_historyZdict_aZdict_br1   eqZnew_class_historyr   r   r   r     s$    


c             C   s0   t j| |d}t |}| jg}t| || dS )zy
    Try to pickle an object. If successful, make sure
    the object's attributes survived pickling and unpickling.
    )protocolN)pickledumpsloadsrw   r   )originalr   fZ	unpickledr   r   r   r   r     s
    
Hz>c             K   s0   ddl }ddlm} |jj|| |||| dS )z
    Raise an assertion if two objects are not equal up to desired tolerance.

    This is a :class:`~astropy.units.Quantity`-aware version of
    :func:`numpy.testing.assert_allclose`.
    r   N)_unquantify_allclose_arguments)numpyZastropy.units.quantityr   testingZassert_allclose)actualZdesiredZrtolZatolr;   npr   r   r   r   r     s    )N)r   N))rG   r'   r]   ra   r   rd   r>   r9   Zastropy.unitsr   Zquantity_allcloseZastropy.utils.exceptionsr   r   runnerr   __all__markr	   r   r3   r   rM   rN   rT   rO   rQ   rg   rj   rk   rS   r   r
   r   r   r   Zfixturer   r   r   r   r   r   r   r   <module>   sh   	*%2M)'
:	