
    IR-e>                        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mZ ej        Zej        Z	  e            Zn# e$ r dZY nw xY wd ZeZd Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zej         !                    edud          d             Z"ej         #                    d          d             Z$ej         #                    d          d             Z%ej         &                    d          d             Z'ej         (                    dg d          d             Z)d Z*d Z+d Z,ej         (                    dg d          d             Z-d Z.d Z/d  Z0ej         (                    d!g d"          d#             Z1dS )$    N)log)LoggingErrorconf)AstropyUserWarningAstropyWarningc                 \   t          j        t                     t          j        t                     d t          _        d t          _        t	          j                     t	          j                    rt	          j	                     t	          j
                    rt	          j                     d S d S N)	importlibreloadwarningssysr   _showwarning_orig_excepthook_orig_set_defaultswarnings_logging_enableddisable_warnings_loggingexception_logging_enableddisable_exception_logging)functions    9lib/python3.11/site-packages/astropy/tests/test_logger.pysetup_functionr      s    XS !CC  #%% '$&&&
$&& (%'''''( (    c                      t          j        t          d          5  t          j                     d d d            d S # 1 swxY w Y   d S )Nz%Warnings logging has not been enabledmatch)pytestraisesr   r   r    r   r   'test_warnings_logging_disable_no_enabler   1   s    	|+S	T	T	T ' '$&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '   =AAc                      t          j                     t          j        t          d          5  t          j                     d d d            d S # 1 swxY w Y   d S )Nz)Warnings logging has already been enabledr   )r   enable_warnings_loggingr   r   r   r   r   r   "test_warnings_logging_enable_twicer#   6   s    !!!	H
 
 
 & & 	#%%%& & & & & & & & & & & & & & & & & &   AAAc                      t          j                     d t          _        t	          j        t          d          5  t          j                     d d d            d S # 1 swxY w Y   d S )Nc                      d S r	   r   r   r   r   <lambda>z2test_warnings_logging_overridden.<locals>.<lambda>@   s    4 r   ziCannot disable warnings logging: warnings\.showwarning was not set by this logger, or has been overriddenr   )r   r"   r   showwarningr   r   r   r   r   r   r    test_warnings_logging_overriddenr)   >   s    !!!'<H	T
 
 
 ' '
 	$&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '   AA!$A!c                     t          j        t          d          5 } t          j                    5 }t          j        dt                     d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   t          |          dk    sJ t          |           dk    sJ t          j        d          5 } t          j	                     t          j                    5 }t          j        dt                     d d d            n# 1 swxY w Y   t          j
                     d d d            n# 1 swxY w Y   t          |          dk    sJ t          |           dk    sJ |d         j        dk    sJ |d         j                            d          sJ |d         j        dk    sJ t          j        t          d	          5 } t          j	                     t          j                    5 }t          j        dt                     t          j        d	           d d d            n# 1 swxY w Y   t          j
                     d d d            n# 1 swxY w Y   t          |          dk    sJ t          |           dk    sJ |d         j        dk    sJ |d         j                            d          sJ |d         j        dk    sJ t          j        t          d          5 } t          j                    5 }t          j        dt                     d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   t          |          dk    sJ t          |           dk    sJ d S )
NThis is a warningr   r      TrecordWARNINGastropy.tests.test_loggerz)This is another warning, not from Astropy)r   warnsr   r   log_to_listr   warnlencatch_warningsr"   r   	levelnamemessage
startswithoriginUserWarning)	warn_listlog_lists     r   test_warnings_loggingr>   I   s   	(0C	D	D	D C	_ 	C(M-/ABBB	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	CC C C C C C C C C C C C C C C x==Ay>>Q 
	 	-	-	- '#%%%_ 	C(M-/ABBB	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C$&&&	' ' ' ' ' ' ' ' ' ' ' ' ' ' '
 x==Ay>>QA; I----A;))*=>>>>>A;!<<<<< 
F
 
 
 '	#%%%_ 	G(M-/ABBBMEFFF	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	$&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' x==Ay>>QA; I----A;))*=>>>>>A;!<<<<< 
(0C	D	D	D C	_ 	C(M-/ABBB	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	CC C C C C C C C C C C C C C C x==Ay>>Qs   A.AA.A	A.A	A..A25A28'D0D:D0D
	
D0D
	D00D47D4'I//H*I*H.	.I1H.	2III,L> L'L>'L+	+L>.L+	/L>>MMc                  6    G d dt                     } t          j        d          5 }t          j                     t          j                    5 }t          j        d|            d d d            n# 1 swxY w Y   t          j                     d d d            n# 1 swxY w Y   t          |          dk    sJ t          |          dk    sJ |d         j	        dk    sJ |d         j
                            d	          sJ |d         j        d
k    sJ d S )Nc                       e Zd ZdS )Jtest_warnings_logging_with_custom_class.<locals>.CustomAstropyWarningClassN)__name__
__module____qualname__r   r   r   CustomAstropyWarningClassrA   u   s        r   rE   Tr.   r,   r-   r   r0   z,CustomAstropyWarningClass: This is a warningr1   )r   r   r6   r   r"   r3   r4   r   r5   r7   r8   r9   r:   )rE   r<   r=   s      r   'test_warnings_logging_with_custom_classrF   t   s       N    
	 	-	-	- '#%%%_ 	J(M-/HIII	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J$&&&	' ' ' ' ' ' ' ' ' ' ' ' ' ' '
 x==Ay>>QA; I----A;))6     A;!<<<<<<<s5   'BA2&B2A6	6B9A6	:BB #B c                     ddl m} m} t          j        d          5 }t          j                     t          j                    5 } || d           d d d            n# 1 swxY w Y   t          j                     d d d            n# 1 swxY w Y   t          |          dk    sJ t          |          dk    sJ |d         j
        dk    sJ |d         j                            d          }|sJ |d         j        d	k    sJ d S )
Nr   )W02vo_warnTr.   )abr-   r0   zGW02: ?:?:?: W02: a attribute 'b' is invalid.  Must be a standard XML idr1   )astropy.io.votable.exceptionsrH   rI   r   r6   r   r"   r3   r   r5   r7   r8   r9   r:   )rH   rI   r<   r=   xs        r   ,test_warning_logging_with_io_votable_warningrN      s   ::::::::		 	-	-	- '#%%%_ 	%(GC$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%$&&&	' ' ' ' ' ' ' ' ' ' ' ' ' ' '
 x==Ay>>QA; I----&&Q	 	A HHHA;!<<<<<<<s5   'BABA"	"B%A"	&BBBc                      G d d          } t          j                      |             t          j        d<   	 t	          j        t          d          t          dd           t          j        d= dS # t          j        d= w xY w)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                       e Zd Zd ZdS )8test_import_error_in_warning_logging.<locals>.FakeModulec                      t          d          )Nz._showwarning should ignore any exceptions here)ImportError)selfattrs     r   __getattr__zDtest_import_error_in_warning_logging.<locals>.FakeModule.__getattr__   s    NOOOr   N)rB   rC   rD   rV   r   r   r   
FakeModulerQ      s(        	P 	P 	P 	P 	Pr   rW   z<test fake module>zRegression test for #2671z<this is only a test>r-   N)r   r"   r   modulesr   r(   r   )rW   s    r   $test_import_error_in_warning_loggingrY      s    P P P P P P P P !!!(2
CK$%.677#		
 	
 	
 K,---CK,-----s   )A1 1B c                      t          j        t          d          5  t          j                     d d d            d S # 1 swxY w Y   d S )Nz&Exception logging has not been enabledr   )r   r   r   r   r   r   r   r   (test_exception_logging_disable_no_enabler[      s    	|+T	U	U	U ( (%'''( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (r    c                      t          j                     t          j        t          d          5  t          j                     d d d            d S # 1 swxY w Y   d S )Nz*Exception logging has already been enabledr   )r   enable_exception_loggingr   r   r   r   r   r   #test_exception_logging_enable_twicer^      s     """	I
 
 
 ' ' 	$&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 'r$   z,Cannot override exception handler in IPython)reasonc                      t          j                     d t          _        t	          j        t          d          5  t          j                     d d d            d S # 1 swxY w Y   d S )Nc                     d S r	   r   )etypeevaluetbs      r   r'   z3test_exception_logging_overridden.<locals>.<lambda>   s    t r   zcCannot disable exception logging: sys.excepthook was not set by this logger, or has been overriddenr   )r   r]   r   
excepthookr   r   r   r   r   r   r   !test_exception_logging_overriddenrf      s      """33CN	P
 
 
 ( ( 	%'''( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (r*   zip is not Nonec                  B   	 t          j                    5 } t          d          # 1 swxY w Y   	 J # t          $ r<}t          j        t          j                      |j        d         dk    sJ Y d }~nd }~ww xY wt          |           dk    sJ 	 t          j                     t          j                    5 } t          d          # 1 swxY w Y   	 J # t          $ r<}t          j        t          j                      |j        d         dk    sJ Y d }~nd }~ww xY wt          |           dk    sJ | d         j	        dk    sJ | d         j
                            d          sJ | d         j        dk    sJ t          j                     	 t          j                    5 } t          d          # 1 swxY w Y   	 J # t          $ r<}t          j        t          j                      |j        d         dk    sJ Y d }~nd }~ww xY wt          |           dk    sJ d S )NzThis is an ExceptionFr   r-   ERRORzException: This is an Exceptionr1   )r   r3   	Exceptionr   re   exc_infoargsr5   r]   r7   r8   r9   r:   r   )r=   excs     r   test_exception_loggingrm      s   _ 	4(2333	4 	4 	4 	4 	4 	4 	4 	4 	4 		  5 5 5''x{44444444445
 x==A$&&&_ 	4(2333	4 	4 	4 	4 	4 	4 	4 	4 	4 		  5 5 5''x{44444444445
 x==AA; G++++A;))*KLLLLLA;!<<<<< !###_ 	4(2333	4 	4 	4 	4 	4 	4 	4 	4 	4 		  5 5 5''x{44444444445
 x==As   3 %)3 )3 
A92A44A9&C 8CCC CC 
D 2DDG #F33F77G :F7;G 
H2HHc                  H   ddl m}   | t                    }	 t          j                     t          j                    5 }|                    d           d d d            n# 1 swxY w Y   J # t          $ rK}t          j	        t          j
                      |j        d                             d          sJ Y d }~nd }~ww xY wt          |          dk    sJ |d         j        dk    sJ |d         j                            d          sJ |d         j        d	k    sJ d S )
Nr   )HomogeneousListfooFz3homogeneous list must contain only objects of type r-   rh   z>TypeError: homogeneous list must contain only objects of type zastropy.utils.collections)astropy.utils.collectionsro   intr   r]   r3   append	TypeErrorr   re   rj   rk   r9   r5   r7   r8   r:   )ro   lstr=   rl   s       r   test_exception_logging_originrv      s   
 :99999
/#

C
$&&&_ 	(JJu	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  
 
 
''x{%%A
 
 	
 	
 	
 	
 	
 	
 	
 	

 x==AA; G++++A;))H     A;!<<<<<<<s;   &A- A A-  A$$A- 'A$(A- -
C7AB==Cz8Infinite recursion on Python 3.5+, probably a real issuec                     	 t          j                     t          j                    5 } t                      # 1 swxY w Y   	 J # t          $ r" t	          j        t	          j                      Y nw xY wt          |           dk    sJ | d         j        dk    sJ | d         j	        dk    sJ | d         j
        dk    sJ 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   rh   z%Exception [astropy.tests.test_logger]r1   )r   r]   r3   ri   r   re   rj   r5   r7   r8   r:   r=   s    r   (test_exception_logging_argless_exceptionry     s   $&&&_ 	(++	 	 	 	 	 	 	 	 	
 	  ( ( (''''( x==AA; G++++A;"IIIIIA;!<<<<<<<s'   &A 7;A ;A )A10A1level)NDEBUGINFOWARNrh   c                 N   t           j        }	 | t          j        |            t          j                    5 }t          j        d           t          j        d           t          j        d           t          j        d           d d d            n# 1 swxY w Y   t          j        |           n# t          j        |           w xY w| t          j	        } | dk    rt          |          dk    sJ nS| dk    rt          |          dk    sJ n7| d	k    rt          |          d
k    sJ n| dk    rt          |          dk    sJ |d         j        dk    sJ |d         j                            d          sJ |d         j        dk    sJ t          |          d
k    rH|d         j        dk    sJ |d         j                            d          sJ |d         j        dk    sJ t          |          dk    rH|d
         j        dk    sJ |d
         j                            d          sJ |d
         j        dk    sJ t          |          dk    rH|d         j        dk    sJ |d         j                            d          sJ |d         j        dk    sJ d S d S )NError messageWarning messageInformation messageDebug messager{      r|      r}      rh   r-   r   r1   r0   )r   rz   setLevelr3   errorwarninginfodebugr   	log_levelr5   r7   r8   r9   r:   )rz   
orig_levelr=   s      r   test_log_to_listr   ,  s   J
!L_ 	'(Io&&&K)***H*+++Io&&&		' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	Z    Z    } 8}}!!!!!	&8}}!!!!!	&8}}!!!!!	'		8}}!!!! A; G++++A;))/:::::A;!<<<<<
8}}{$	1111{"--.?@@@@@{!%@@@@@
8}}{$....{"--.CDDDDD{!%@@@@@
8}}{$////{"--o>>>>>{!%@@@@@  A@s/   )B4 ABB4 BB4 BB4 4C
c                      t          j        d          5 } t          j        d           t          j        d           d d d            n# 1 swxY w Y   t	          |           dk    r| d         j        dk    sJ d S )Nrh   filter_levelr   r   r-   r   )r   r3   r   r   r5   r7   rx   s    r   test_log_to_list_levelr   `  s    	g	.	.	. '(	/"""%&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' x==A(1+"77"B"B"B"B"B"B   )AAAc                      t          j        d          5 } t          j        d           t          j        d           d d d            n# 1 swxY w Y   t	          |           dk    sJ d S )Nastropy.testsfilter_originr   r   r   r   r3   r   r   r5   rx   s    r   test_log_to_list_origin1r   h  s    		7	7	7 '8	/"""%&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' x==Ar   c                      t          j        d          5 } t          j        d           t          j        d           d d d            n# 1 swxY w Y   t	          |           dk    sJ d S )Nastropy.wcsr   r   r   r   r   rx   s    r   test_log_to_list_origin2r   p  s    	}	5	5	5 '	/"""%&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' x==Ar   c                    | dz  }|                     d          }t          |                                          }t          j        }	 |t          j        |           t          j        |          5  t          j        d           t          j        d           t          j	        d           t          j
        d           d d d            n# 1 swxY w Y   |                                 t          j        |           n# t          j        |           w xY w|                     d          }|                                }|                                 |t          j        }|dk    rt          |          d	k    sJ nS|d
k    rt          |          dk    sJ n7|dk    rt          |          dk    sJ n|dk    rt          |          dk    sJ t!          |d                                                   dd          dk    sJ t          |          dk    r5t!          |d                                                   dd          dk    sJ t          |          dk    r5t!          |d                                                   dd          dk    sJ t          |          d	k    r5t!          |d                                                   dd          dk    sJ d S d S )Ntest.logwbr   r   r   r   rbr{   r   r|   r   r}   r   rh   r-   r   )r1   rh   r   )r1   r0   r   )r1   r|   r   )r1   r{   r   )openstrresolver   rz   r   log_to_filer   r   r   r   close	readlinesr   r   r5   evalstrip)tmp_pathrz   
local_pathlog_filelog_pathr   log_entriess          r   test_log_to_filer   x  sV   J&Jt$$H:%%''((HJ!L_X&& 	' 	'Io&&&K)***H*+++Io&&&		' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	Z    Z    t$$H$$&&KNN} ;1$$$$$	&;1$$$$$	&;1$$$$$	'		;1$$$$ A$$&&'', 1     ;1KN((**++BCC0 5
 
 
 
 
 ;1KN((**++BCC0 5
 
 
 
 
 ;1KN((**++BCC0 5
 
 
 
 
 
 
s1   	*D 3ACD CD CD Dc                 ^   | dz  }|                     d          }t          |                                          }t          j        |d          5  t          j        d           t          j        d           d d d            n# 1 swxY w Y   |                                 |                     d          }|                                }|                                 t          |          dk    sJ t          |d	                                                   d
d          dk    sJ d S )Nr   r   rh   r   r   r   r   r-   r   )rh   r   )r   r   r   r   r   r   r   r   r   r5   r   r   r   r   r   r   r   s        r   test_log_to_file_levelr     sW   J&Jt$$H:%%''((H		8	8	8 ' '	/"""%&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' NNt$$H$$&&KNN{q    A$$&&'',0JJJJJJJ   )BBBc                    | dz  }|                     d          }t          |                                          }t          j        |d          5  t          j        d           t          j        d           d d d            n# 1 swxY w Y   |                                 |                     d          }|                                }|                                 t          |          dk    sJ d S )	Nr   r   r   r   r   r   r   r   
r   r   r   r   r   r   r   r   r   r5   r   s        r   test_log_to_file_origin1r     s$   J&Jt$$H:%%''((H		A	A	A ' '	/"""%&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' NNt$$H$$&&KNN{q      r   c                    | dz  }|                     d          }t          |                                          }t          j        |d          5  t          j        d           t          j        d           d d d            n# 1 swxY w Y   |                                 |                     d          }|                                }|                                 t          |          dk    sJ d S )	Nr   r   r   r   r   r   r   r   r   r   s        r   test_log_to_file_origin2r     s$   J&Jt$$H:%%''((H		?	?	? ' '	/"""%&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' NNt$$H$$&&KNN{q      r   encoding) zutf-8cp1252c                    | dz  }t          |                                          }t          j        }|t          _        t	          j        |          5  t          j        D ]T}t          |t          j	                  r8|r|j
        j        |k    sJ 1|j
        j        t          j                    k    sJ U	 d d d            n# 1 swxY w Y   |t          _        d S )Nr   )r   r   r   log_file_encodingr   r   handlers
isinstanceloggingFileHandlerstreamr   localegetpreferredencoding)r   r   r   r   orig_encodinghandlers         r   test_log_to_file_encodingr     s,   J&J:%%''((H*M%D		"	" T T| 	T 	TG'7#677 T T">2h>>>>>">2f6Q6S6SSSSS	TT T T T T T T T T T T T T T T +Ds   A"CC	C)2r
   r   r   r   r   r   astropyr   astropy.loggerr   r   astropy.utils.exceptionsr   r   __excepthook___excepthookr(   _showwarningget_ipythonip	NameErrorr   teardown_moduler   r#   r)   r>   rF   rN   rY   r[   r^   markskipifrf   xfailrm   rv   skipry   parametrizer   r   r   r   r   r   r   r   r   r   r   r   <module>r      sF         



         - - - - - - - - G G G G G G G G
  #	BB   	BBB( ( (& !' ' '
& & &' ' '( ( (V= = =&= = =$. . .4( ( (
' ' ' dNI   
( 
( 
( #$$% % %$%P #$$= = %$=4 STT= = UT=, "J"J"JKK0A 0A LK0AfC C C     "J"J"JKKA
 A
 LKA
HK K K&! ! !$! ! !$ %<%<%<==+ + >=+ + +s   
A	 	AA