
    Yf1                        d Z ddlZddlmZmZmZmZmZmZm	Z	m
Z
 ddlm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 dd
lmZ ddlmZ ddlmZ ddlmZ dej        dee         dee         ddfdZ  G d dej                  Z!dS )z+
Test cases for L{twisted.logger._global}.
    N)IOAnyListOptionalTextIOTupleTypecast)Failure)unittest   )textFileLogObserver)MORE_THAN_ONCE_WARNINGLogBeginner)ILogObserverLogEvent)LogLevel)Logger)LogPublisher)nextLinetestactualEventsexpectedEventsreturnc                 T   t          |          t          |          k    r|                     ||           t                      |D ]&}t          |                                          z  'dt          dt          ffdfd|D             }|                     ||           dS )a7  
    Compare two sequences of log events, examining only the the keys which are
    present in both.

    @param test: a test case doing the comparison
    @param actualEvents: A list of log events that were emitted by a logger.
    @param expectedEvents: A list of log events that were expected by a test.
    eventr   c                     |                                  }|                                 D ]}|vr|                    |           |S N)copykeyspop)r   r   keyallMergedKeyss      Q/var/www/html/env/lib/python3.11/site-packages/twisted/logger/test/test_global.pysimplifyzcompareEvents.<locals>.simplify*   sG    zz||::<< 	 	C-''    c                 &    g | ]} |          S  r(   ).0r   r%   s     r$   
<listcomp>z!compareEvents.<locals>.<listcomp>1   s!    BBBEBBBr&   N)lenassertEqualsetr    r   )r   r   r   r   simplifiedActualr#   r%   s        @@r$   compareEventsr/      s     <C////~666EEM + +UZZ\\*** X       CBBB\BBB%~66666r&   c                       e Zd ZdZddZddZddZdededdf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ddZddZdS )LogBeginnerTestsz#
    Tests for L{LogBeginner}.
    r   Nc                 "   t                      | _        t          j                    | _         G d d          } G d d          } |            | _         |            | _        t          | j        | j        | j        | j                  | _        d S )Nc                   6    e Zd Z e            Z e            ZdS )&LogBeginnerTests.setUp.<locals>.NotSysN)__name__
__module____qualname__objectstdoutstderrr(   r&   r$   NotSysr4   >   s$        VXXFVXXFFFr&   r;   c                   p    e Zd ZddZ	 	 ddedee         dededee	e
                  d	ee         ddfd
ZdS )+LogBeginnerTests.setUp.<locals>.NotWarningsr   Nc                     g | _         d S r   )warnings)selfs    r$   __init__z4LogBeginnerTests.setUp.<locals>.NotWarnings.__init__C   s    
 	 r&   messagecategoryfilenamelinenofilelinec                 F    | j                             ||||||f           dS )a  
                Emulate warnings.showwarning.

                @param message: A warning message to emit.
                @param category: A warning category to associate with
                    C{message}.
                @param filename: A file name for the source code file issuing
                    the warning.
                @param lineno: A line number in the source file where the
                    warning was issued.
                @param file: A file to write the warning message to.  If
                    L{None}, write to L{sys.stderr}.
                @param line: A line of source code to include with the warning
                    message. If L{None}, attempt to read the line from
                    C{filename} and C{lineno}.
                N)r?   append)r@   rB   rC   rD   rE   rF   rG   s          r$   showwarningz7LogBeginnerTests.setUp.<locals>.NotWarnings.showwarningJ   s.    2 $$gx64QU%VWWWWWr&   r   N)NN)r5   r6   r7   rA   strr	   Warningintr   r   r   rJ   r(   r&   r$   NotWarningsr=   B   s            +/&*X XX w-X 	X
 X r#w'X smX X X X X X Xr&   rO   )	r   	publisherioStringIOerrorStream	sysModulewarningsModuler   beginner)r@   r;   rO   s      r$   setUpzLogBeginnerTests.setUp:   s    %;==	 	 	 	 	 	 	 	!	X !	X !	X !	X !	X !	X !	X !	XF  )kmm#ND,dnd>Q
 
r&   c                 R   t          dd          }g g t          t          fd          }t          t          fd          }| j                            ||f           |                     |           |                     |g           |                     |g           dS )z?
        Test that C{beginLoggingTo()} adds observers.
           r   foobarc                 .                         |           S r   rI   eevents1s    r$   <lambda>zBLogBeginnerTests.test_beginLoggingToAddObservers.<locals>.<lambda>t       '..*;*; r&   c                 .                         |           S r   r^   r`   events2s    r$   rb   zBLogBeginnerTests.test_beginLoggingToAddObservers.<locals>.<lambda>u   rc   r&   N)dictr
   r   rV   beginLoggingTorP   r,   r@   r   o1o2ra   rf   s       @@r$   test_beginLoggingToAddObserversz0LogBeginnerTests.test_beginLoggingToAddObserversk   s     """"$"$, ; ; ; ;<<, ; ; ; ;<<$$b"X...u%'***%'*****r&   c                 R   t          dd          }g g t          t          fd          }t          t          fd          }|                     |           | j                            ||f           |                     |g           |                     |g           dS )z\
        Test that events are buffered until C{beginLoggingTo()} is
        called.
        rY   r   rZ   c                 .                         |           S r   r^   r_   s    r$   rb   zDLogBeginnerTests.test_beginLoggingToBufferedEvents.<locals>.<lambda>   rc   r&   c                 .                         |           S r   r^   re   s    r$   rb   zDLogBeginnerTests.test_beginLoggingToBufferedEvents.<locals>.<lambda>   rc   r&   N)rg   r
   r   rP   rV   rh   r,   ri   s       @@r$   !test_beginLoggingToBufferedEventsz2LogBeginnerTests.test_beginLoggingToBufferedEvents}   s    
 """"$"$, ; ; ; ;<<, ; ; ; ;<<u$$b"X...%'***%'*****r&   limitrV   c           	      l   t          |dz             D ]%}|                     t          |                     &g }|                    t	          t
          |j                  g           |                     t          t          d|dz                       t          d |D                                  dS )a  
        Verify that when more than C{limit} events are logged to L{LogBeginner},
        only the last C{limit} are replayed by L{LogBeginner.beginLoggingTo}.

        @param limit: The maximum number of events the log beginner should
            buffer.
        @param beginner: The L{LogBeginner} against which to verify.

        @raise: C{self.failureException} if the wrong events are replayed by
            C{beginner}.
        rY   )countc              3   &   K   | ]}|d          V  dS )rs   Nr(   )r)   r   s     r$   	<genexpr>z4LogBeginnerTests._bufferLimitTest.<locals>.<genexpr>   s&      44Ew444444r&   N)	rangerP   rg   rh   r
   r   rI   r,   list)r@   rq   rV   rs   eventss        r$   _bufferLimitTestz!LogBeginnerTests._bufferLimitTest   s     519%% 	. 	.ENN4e,,,----!#lFM!B!B CDDDq%!)$$%%44V44444	
 	
 	
 	
 	
r&   c                 T    t           j        }|                     || j                   dS )z
        Up to C{LogBeginner._DEFAULT_BUFFER_SIZE} log events are buffered for
        replay by L{LogBeginner.beginLoggingTo}.
        N)r   _DEFAULT_BUFFER_SIZEry   rV   )r@   rq   s     r$   test_defaultBufferLimitz(LogBeginnerTests.test_defaultBufferLimit   s*    
 0eT]33333r&   c                     d}t          | j        | j        | j        | j        |          }|                     ||           dS )z
        The size of the L{LogBeginner} event buffer can be overridden with the
        C{initialBufferSize} initilizer argument.
           )initialBufferSizeN)r   rP   rS   rT   rU   ry   )r@   rq   rV   s      r$   test_overrideBufferLimitz)LogBeginnerTests.test_overrideBufferLimit   sT    
 NN#
 
 
 	eX.....r&   c           
         g }g }t          j                    }t          |          }|                     t	          d                     t                      \  }}| j                            t          t          |j
                  |g           |                     t	          d                     t                      \  }}| j                            t          t          |j
                  |g           |                     t	          d                     t	          t          t          j        ||||          }	d| _        t          | |t	          d          t	          d          |	t	          d          g           t          | ||	t	          d          g           |                                }
|                     d| d| d	|
           |                     d| d| d	|
           dS )
z
        When invoked twice, L{LogBeginner.beginLoggingTo} will emit a log
        message warning the user that they previously began logging, and add
        the new log observers.
        	prebuffer)r   
postbufferpostwarn)
log_format	log_levelfileNowlineNowfileThenlineThenN<:>)rQ   rR   r   rP   rg   r   rV   rh   r
   r   rI   r   r   warnmaxDiffr/   getvalueassertIn)r@   ra   rf   
fileHandletextObserverfirstFilename	firstLinesecondFilename
secondLinewarningoutputs              r$   test_beginLoggingToTwicez)LogBeginnerTests.test_beginLoggingToTwice   s    #%"$[]]
*:66t+...///#+:: y$$d<&H&H,%WXXXt,///000%-ZZ"
$$d<&H&H,%WXXXt*---...-m""
 
 
 ;'''<(((:&&&			
 		
 		
 	dGgt*/E/E/E%FGGG$$&&6-66)666???8.88:888&AAAAAr&   c                     t          | j                  }|                    d           |                    dd           |                     | j                                        d           dS )zP
        Critical messages will be written as text to the error stream.
        observerzignore thisza critical {message}rB   )rB   za critical message
N)r   rP   infocriticalr,   rS   r   r@   logs     r$   test_criticalLoggingz%LogBeginnerTests.test_criticalLogging   sk     dn---+Y???)22446LMMMMMr&   c                     t          | j                  }| j                            d           |                    d           |                     | j                                        d           dS )z
        Once logging has begun with C{beginLoggingTo}, critical messages are no
        longer written to the output stream.
        r   r(   zanother critical message N)r   rP   rV   rh   r   r,   rS   r   r   s     r$   test_criticalLoggingStopsz*LogBeginnerTests.test_criticalLoggingStops   sj    
 dn---$$R(((/000)2244b99999r&   c                    g }| j                             t          t          |j                  g           t          dt          t          | j        j                             t          | |t          dd          g           |dd= t          dt          t          | j        j                             t          | |t          dd          g           dS )z
        L{LogBeginner.beginLoggingTo} will re-direct the standard output and
        error streams by setting the C{stdio} and C{stderr} attributes on its
        sys module object.
        zHello, world.rF   r9   )log_namespacelog_ioNzError, world.r:   )rV   rh   r
   r   rI   printr   rT   r9   r/   rg   r:   )r@   rx   s     r$   %test_beginLoggingToRedirectStandardIOz6LogBeginnerTests.test_beginLoggingToRedirectStandardIO   s     "$$$d<&G&G%HIIIoD1F$G$GHHHH&4hOOOP	
 	
 	
 111IoD1F$G$GHHHH&4hOOOP	
 	
 	
 	
 	
r&   c                     | j         j        }| j         j        }| j                            dd           |                     | j         j        |           |                     | j         j        |           dS )z
        L{LogBeginner.beginLoggingTo} will leave the existing stdout/stderr in
        place if it has been told not to replace them.
        r(   F)redirectStandardION)rT   r9   r:   rV   rh   assertIs)r@   oldOutoldErrs      r$   test_beginLoggingToDontRedirectz0LogBeginnerTests.test_beginLoggingToDontRedirect  sk    
 &&$$RE$BBBdn+V444dn+V44444r&   c                    t          j        t          j                    d          }t          j        t          j                    d          }|| j        _        || j        _        g }| j                            t          t          |j
                  g           t          t          | j        j                  }t          t          | j        j                  }|                     |j        d           |                     |j        d           |                    d           |                    d           t          | |t!          d          t!          d          g           dS )	z
        When L{LogBeginner.beginLoggingTo} redirects stdout/stderr streams, the
        replacement streams will preserve the encoding of the replaced streams,
        to minimally disrupt any application relying on a specific encoding.
        z	shift-JISbig5s   
s   
u   李)r   u   瑩N)rQ   TextIOWrapperBytesIOrT   r9   r:   rV   rh   r
   r   rI   r   r,   encodingwriter/   rg   )r@   weirdweirderrrx   r9   r:   s         r$   $test_beginLoggingToPreservesEncodingz5LogBeginnerTests.test_beginLoggingToPreservesEncoding  s!     {;;#BJLL&99 % (!#$$d<&G&G%HIIIfdn344fdn344+666&111]###]###dFT%:%:%:D<Q<Q<Q$RSSSSSr&   c           
         | j                             dt          t          d           g }| j                            t          t          |j                  g           | j                             dt          t          d           t          j
                    }| j                             dt          t          d|           |                     | j         j        dt          t          dddfdt          t          d|dfg           t          | |t          dt          j        d	z   t          j        z   t          d
          g           dS )z
        L{LogBeginner.beginLoggingTo} will redirect the warnings of its
        warnings module into the logging system.
        z	a messagerY   zanother messager   zyet anotherr~   r   N.)r   rC   rD   rE   )rU   rJ   DeprecationWarning__file__rV   rh   r
   r   rI   rQ   rR   r,   r?   r/   rg   r6   r5   )r@   rx   fs      r$   test_warningsModulez$LogBeginnerTests.test_warningsModule/  sT   
 	''5GSTUUU!#$$d<&G&G%HIII''18Q	
 	
 	
 KMM''-x 	( 	
 	
 	
 	(0(AtTJ 2HaDI	
 	
 	
 	-*5,56 &	 	 		
 	
 	
 	
 	
r&   c                 L   t          t          d                    }t          | j                  }|                    d|           | j                                        }|                     d|           |                     d|           |                     d|           dS )zR
        The string resulting from a logged failure contains a traceback.
        z,this is not the behavior you are looking forr   z	a failure)failure	TracebackN)r   	Exceptionr   rP   r   rS   r   r   )r@   r   r   msgs       r$   test_failuresAppendTracebacksz.LogBeginnerTests.test_failuresAppendTracebacksV  s     ILMMNNdn---K+++''))k3'''DcJJJk3'''''r&   rK   )r5   r6   r7   __doc__rW   rl   rp   rN   r   ry   r|   r   r   r   r   r   r   r   r   r   r(   r&   r$   r1   r1   5   sZ        /
 /
 /
 /
b+ + + +$+ + + +&
c 
[ 
T 
 
 
 
*4 4 4 4/ / / /)B )B )B )BVN N N N: : : :
 
 
 
$	5 	5 	5 	5T T T T0%
 %
 %
 %
N
( 
( 
( 
( 
( 
(r&   r1   )"r   rQ   typingr   r   r   r   r   r   r	   r
   twisted.python.failurer   twisted.trialr   _filer   _globalr   r   _interfacesr   r   _levelsr   _loggerr   	_observerr   test.test_stdlibr   TestCaser/   r1   r(   r&   r$   <module>r      s    
			 E E E E E E E E E E E E E E E E E E E E * * * * * * " " " " " " ' ' ' ' ' ' 9 9 9 9 9 9 9 9 0 0 0 0 0 0 0 0             $ $ $ $ $ $ ' ' ' ' ' '7

7x.7 N7 
	7 7 7 7>k( k( k( k( k(x( k( k( k( k( k(r&   