B
    q\;                 @   s  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m	Z	m
Z
 d dlmZmZ ejZejZy
e ZW n ek
r   dZY nX dd ZeZd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zej !e"ddd Z#ej $e"ddd Z%ej $e"dd d! Z&ej j'd"d#d$d% Z(ej )d&dd'd(d)d*gd+d, Z*d-d. Z+d/d0 Z,d1d2 Z-ej )d&dd'd(d)d*gd3d4 Z.d5d6 Z/d7d8 Z0d9d: Z1dS );    N   )catch_warnings)log)LoggingErrorconf)AstropyWarningAstropyUserWarningc             C   sL   t t t t d t_d t_t  t r8t	  t
 rHt  d S )N)	importlibreloadwarningssysr   _showwarning_origZ_excepthook_origZ_set_defaultsZwarnings_logging_enableddisable_warnings_loggingZexception_logging_enableddisable_exception_logging)Zfunction r   8lib/python3.7/site-packages/astropy/tests/test_logger.pysetup_function   s    

r   c           	   C   s6   t t} t  W d Q R X | jjd dks2td S )Nr   z%Warnings logging has not been enabled)pytestraisesr   r   r   valueargsAssertionError)er   r   r   'test_warnings_logging_disable_no_enable4   s    r   c           	   C   s>   t   tt} t   W d Q R X | jjd dks:td S )Nr   z)Warnings logging has already been enabled)r   enable_warnings_loggingr   r   r   r   r   r   )r   r   r   r   "test_warnings_logging_enable_twice:   s    r   c           	   C   sH   t   dd t_tt} t   W d Q R X | jj	d dksDt
d S )Nc               S   s   d S )Nr   r   r   r   r   <lambda>C   s    z2test_warnings_logging_overridden.<locals>.<lambda>r   zhCannot disable warnings logging: warnings.showwarning was not set by this logger, or has been overridden)r   r   r   showwarningr   r   r   r   r   r   r   )r   r   r   r    test_warnings_logging_overriddenA   s
    
r   c           
   C   sB  t  &} t }tdt W d Q R X W d Q R X t|dksBtt| dksRt| d jj	d dksjtt  6} t
  t }tdt W d Q R X t  W d Q R X t|dkstt| dkst|d jdkst|d jdst|d jdkstt  @} t
  t }tdt td W d Q R X t  W d Q R X t|dksdtt| dksvt|d jdkst|d jdst|d jdkst| d jj	d dkstt  &} t }tdt W d Q R X W d Q R X t|dkstt| dks$t| d jj	d dks>td S )NzThis is a warningr   r   WARNINGzastropy.tests.test_loggerz)This is another warning, not from Astropy)r   r   log_to_listr   warnr   lenr   messager   r   r   	levelname
startswithorigin)	warn_listlog_listr   r   r   test_warnings_loggingI   sD    
 


 r)   c           
   C   s   G dd dt } t 6}t  t }td|  W d Q R X t  W d Q R X t|dksbt	t|dksrt	|d j
dkst	|d jdst	|d jdkst	d S )	Nc               @   s   e Zd ZdS )zJtest_warnings_logging_with_custom_class.<locals>.CustomAstropyWarningClassN)__name__
__module____qualname__r   r   r   r   CustomAstropyWarningClassw   s   r-   zThis is a warningr   r   r   z,CustomAstropyWarningClass: This is a warningzastropy.tests.test_logger)r   r   r   r   r    r   r!   r   r"   r   r$   r#   r%   r&   )r-   r'   r(   r   r   r   'test_warnings_logging_with_custom_classv   s    
r.   c           
   C   s   ddl m} m} t 4}t  t }|| d W d Q R X t  W d Q R X t|dks`t	t|dkspt	|d j
dkst	|d jd}|st	|d jdkst	d S )Nr   )W02vo_warn)abr   r   zGW02: ?:?:?: W02: a attribute 'b' is invalid.  Must be a standard XML idzastropy.tests.test_logger)Zastropy.io.votable.exceptionsr/   r0   r   r   r   r    r   r"   r   r$   r#   r%   r&   )r/   r0   r'   r(   xr   r   r   ,test_warning_logging_with_io_votable_warning   s    
r4   c              C   sJ   G dd d} t   |  tjd< zttdtdd W dtjd= X dS )z
    Regression test for https://github.com/astropy/astropy/issues/2671

    This test actually puts a goofy fake module into ``sys.modules`` to test
    this problem.
    c               @   s   e Zd Zdd ZdS )z8test_import_error_in_warning_logging.<locals>.FakeModulec             S   s   t dd S )Nz._showwarning should ignore any exceptions here)ImportError)selfattrr   r   r   __getattr__   s    zDtest_import_error_in_warning_logging.<locals>.FakeModule.__getattr__N)r*   r+   r,   r8   r   r   r   r   
FakeModule   s   r9   z<test fake module>zRegression test for #2671z<this is only a test>r   N)r   r   r   modulesr   r   r   )r9   r   r   r   $test_import_error_in_warning_logging   s    
r;   c           	   C   s6   t t} t  W d Q R X | jjd dks2td S )Nr   z&Exception logging has not been enabled)r   r   r   r   r   r   r   r   )r   r   r   r   (test_exception_logging_disable_no_enable   s    r<   c           	   C   s>   t   tt} t   W d Q R X | jjd dks:td S )Nr   z*Exception logging has already been enabled)r   enable_exception_loggingr   r   r   r   r   r   )r   r   r   r   #test_exception_logging_enable_twice   s    r>   zip is not Nonec           	   C   sH   t   dd t_tt} t   W d Q R X | jj	d dksDt
d S )Nc             S   s   d S )Nr   )etypeZevaluetbr   r   r   r      s    z3test_exception_logging_overridden.<locals>.<lambda>r   zcCannot disable exception logging: sys.excepthook was not set by this logger, or has been overridden)r   r=   r   
excepthookr   r   r   r   r   r   r   )r   r   r   r   !test_exception_logging_overridden   s
    
rB   c           
   C   s  y t  } tdW d Q R X W nB tk
rb } z$tjt   |jd dksRtW d d }~X Y n
X dsltt| dks|ty(t 	  t  } tdW d Q R X W nB tk
r } z$tjt   |jd dkstW d d }~X Y n
X dstt| dkst| d j
dkst| d jds,t| d jdks@tt   y t  } tdW d Q R X W nF tk
r } z&tjt   |jd dkstW d d }~X Y nX dstt| dkstd S )NzThis is an Exceptionr   Fr   ERRORzException: This is an Exceptionzastropy.tests.test_logger)r   r    	Exceptionr   rA   exc_infor   r   r"   r=   r$   r#   r%   r&   r   )r(   excr   r   r   test_exception_logging   s:    
$
$
&
rG   c           
   C   s   ddl m}  | t}y*t  t }|d W d Q R X W nD tk
r } z&tj	t
   |jd dsrtW d d }~X Y n
X dstt|dkst|d jdkst|d jdst|d jd	kstd S )
Nr   )HomogeneousListZfooz3homogeneous list must contain only objects of type Fr   rC   z>TypeError: homogeneous list must contain only objects of type zastropy.utils.collections)Zastropy.utils.collectionsrH   intr   r=   r    append	TypeErrorr   rA   rE   r   r%   r   r"   r$   r#   r&   )rH   lr(   rF   r   r   r   test_exception_logging_origin   s     



rM   z8Infinite recursion on Python 3.5+, probably a real issue)reasonc           
   C   s   y&t   t  } t W dQ R X W n0 tk
rV } ztjt   W dd}~X Y n
X ds`tt| dkspt| d j	dkst| d j
dkst| d jdkstdS )z
    Regression test for a crash that occurred on Python 3 when logging an
    exception that was instantiated with no arguments (no message, etc.)

    Regression test for https://github.com/astropy/astropy/pull/4056
    NFr   r   rC   z%Exception [astropy.tests.test_logger]zastropy.tests.test_logger)r   r=   r    rD   r   rA   rE   r   r"   r$   r#   r&   )r(   rF   r   r   r   (test_exception_logging_argless_exception
  s    

 rO   levelDEBUGINFOWARNrC   c          	   C   s  t j}zR| d k	rt |  t  .}t d t d t d t d W d Q R X W d t | X | d krttj	} | dkrt
|dkstnL| dkrt
|dkstn2| d	krt
|d
kstn| dkrt
|dkst|d jdkst|d jdst|d jdkstt
|d
krb|d jdks8t|d jdsNt|d jdksbtt
|dkr|d
 jdkst|d
 jdst|d
 jdkstt
|dkr|d jdkst|d jdst|d jdkstd S )NzError messagezWarning messagezInformation messagezDebug messagerQ      rR      rS      rC   r   r   zastropy.tests.test_loggerr   )r   rP   setLevelr    errorwarninginfodebugr   	log_levelr"   r   r$   r#   r%   r&   )rP   
orig_levelr(   r   r   r   test_log_to_list"  sF    




r^   c           	   C   sN   t jdd} t d t d W d Q R X t| dkrF| d jdksJtd S )NrC   )filter_levelzError messagezWarning messager   r   )r   r    rX   rY   r"   r$   r   )r(   r   r   r   test_log_to_list_levelW  s    
r`   c           	   C   s@   t jdd} t d t d W d Q R X t| dks<td S )Nzastropy.tests)filter_originzError messagezWarning messagerV   )r   r    rX   rY   r"   r   )r(   r   r   r   test_log_to_list_origin1`  s    
rb   c           	   C   s@   t jdd} t d t d W d Q R X t| dks<td S )Nzastropy.wcs)ra   zError messagezWarning messager   )r   r    rX   rY   r"   r   )r(   r   r   r   test_log_to_list_origin2i  s    
rc   c          	   C   s  |  d}|d}t| }tj}z\|d k	r:t| t|. td t	d t
d td W d Q R X |  W d t| X |d}| }|  |d krtj}|dkrt|d	kstnT|d
krt|dkstn:|dkr
t|dks&tn|dkr&t|dks&tt|d  dd  dksHtt|dkrxt|d  dd  dksxtt|dkrt|d  dd  dkstt|d	krt|d  dd  dkstd S )Nztest.logwbzError messagezWarning messagezInformation messagezDebug messagerbrQ   rT   rR   rU   rS   rV   rC   r   r   )zastropy.tests.test_loggerrC   zError message)zastropy.tests.test_loggerr   zWarning message)zastropy.tests.test_loggerrR   zInformation message)zastropy.tests.test_loggerrQ   zDebug message)joinopenstrrealpathr   rP   rW   log_to_filerX   rY   rZ   r[   close	readlinesr   r\   r"   r   evalstrip)tmpdirrP   
local_pathlog_filelog_pathr]   log_entriesr   r   r   test_log_to_filer  sL    








ru   c          	   C   s   |  d}|d}t| }tj|dd td td W d Q R X |  |d}|	 }|  t
|dkstt|d	  d
d  dkstd S )Nztest.logrd   rC   )r_   zError messagezWarning messagere   r   r   )rC   zError message)rg   rh   ri   rj   r   rk   rX   rY   rl   rm   r"   r   rn   ro   )rp   rq   rr   rs   rt   r   r   r   test_log_to_file_level  s    



rw   c          	   C   s   |  d}|d}t| }tj|dd td td W d Q R X |  |d}|	 }|  t
|dkstd S )	Nztest.logrd   zastropy.tests)ra   zError messagezWarning messagere   rV   )rg   rh   ri   rj   r   rk   rX   rY   rl   rm   r"   r   )rp   rq   rr   rs   rt   r   r   r   test_log_to_file_origin1  s    



rx   c          	   C   s   |  d}|d}t| }tj|dd td td W d Q R X |  |d}|	 }|  t
|dkstd S )	Nztest.logrd   zastropy.wcs)ra   zError messagezWarning messagere   r   )rg   rh   ri   rj   r   rk   rX   rY   rl   rm   r"   r   )rp   rq   rr   rs   rt   r   r   r   test_log_to_file_origin2  s    



ry   )2r	   r   r   r   helperr   Zastropyr   Zastropy.loggerr   r   Zastropy.utils.exceptionsr   r   __excepthook__Z_excepthookr   _showwarningZget_ipythonZip	NameErrorr   Zteardown_moduler   r   r   r)   r.   r4   r;   r<   r>   ZmarkZskipifri   rB   ZxfailrG   rM   skiprO   Zparametrizer^   r`   rb   rc   ru   rw   rx   ry   r   r   r   r   <module>   sH   

-		* 5			 :