
    Vh1                        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!y)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        |      k7  r| j                  ||       t               |D ]  }t        |j                               z    dt        dt        ffd}|D cg c]
  } ||       }}| j                  ||       yc c}w )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                 ~    | j                         }| j                         D ]  }|vs|j                  |        |S N)copykeyspop)r   r   keyallMergedKeyss      O/home/dcms/DCMS/lib/python3.12/site-packages/twisted/logger/test/test_global.pysimplifyzcompareEvents.<locals>.simplify*   s=    zz|::< 	C-'	     N)lenassertEqualsetr    r   )r   r   r   r   r%   simplifiedActualr#   s         @r$   compareEventsr+      s     <C//~6EM +UZZ\**+ X  6BBEBB%~6 Cs   .B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y)LogBeginnerTestsz#
    Tests for L{LogBeginner}.
    r   Nc                 "   t               | _        t        j                         | _         G d d      } G d d      } |       | _         |       | _        t        | j                  | j                  | j
                  | j                        | _        y )Nc                   (    e Zd Z e       Z e       Zy)&LogBeginnerTests.setUp.<locals>.NotSysN)__name__
__module____qualname__objectstdoutstderr r&   r$   NotSysr0   >   s    XFXFr&   r8   c                   V    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y)+LogBeginnerTests.setUp.<locals>.NotWarningsr   Nc                     g | _         y r   )warnings)selfs    r$   __init__z4LogBeginnerTests.setUp.<locals>.NotWarnings.__init__C   s    
 	 r&   messagecategoryfilenamelinenofilelinec                 F    | j                   j                  ||||||f       y)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=   r?   r@   rA   rB   rC   rD   s          r$   showwarningz7LogBeginnerTests.setUp.<locals>.NotWarnings.showwarningJ   s$    2 $$gx64QU%VWr&   r   N)NN)r1   r2   r3   r>   strr	   Warningintr   r   r   rG   r7   r&   r$   NotWarningsr:   B   sq     +/&*XX w-X 	X
 X r#w'X smX Xr&   rL   )	r   	publisherioStringIOerrorStream	sysModulewarningsModuler   beginner)r=   r8   rL   s      r$   setUpzLogBeginnerTests.setUp:   sn    %;;=	 	!	X !	XF  )m#NND,,dnnd>Q>Q
r&   c                    t        dd      }g g t        t        fd      }t        t        fd      }| j                  j	                  ||f       | j                  |       | j                  |g       | j                  |g       y)z?
        Test that C{beginLoggingTo()} adds observers.
           r   foobarc                 &    j                  |       S r   rF   eevents1s    r$   <lambda>zBLogBeginnerTests.test_beginLoggingToAddObservers.<locals>.<lambda>t       '..*; r&   c                 &    j                  |       S r   r[   r]   events2s    r$   r_   zBLogBeginnerTests.test_beginLoggingToAddObservers.<locals>.<lambda>u   r`   r&   N)dictr
   r   rS   beginLoggingTorM   r(   r=   r   o1o2r^   rc   s       @@r$   test_beginLoggingToAddObserversz0LogBeginnerTests.test_beginLoggingToAddObserversk   s{     ""$"$, ;<, ;<$$b"X.u%'*%'*r&   c                    t        dd      }g g t        t        fd      }t        t        fd      }| j                  |       | j                  j                  ||f       | j                  |g       | j                  |g       y)z\
        Test that events are buffered until C{beginLoggingTo()} is
        called.
        rV   r   rW   c                 &    j                  |       S r   r[   r\   s    r$   r_   zDLogBeginnerTests.test_beginLoggingToBufferedEvents.<locals>.<lambda>   r`   r&   c                 &    j                  |       S r   r[   rb   s    r$   r_   zDLogBeginnerTests.test_beginLoggingToBufferedEvents.<locals>.<lambda>   r`   r&   N)rd   r
   r   rM   rS   re   r(   rf   s       @@r$   !test_beginLoggingToBufferedEventsz2LogBeginnerTests.test_beginLoggingToBufferedEvents}   s{    
 ""$"$, ;<, ;<u$$b"X.%'*%'*r&   limitrS   c           	      (   t        |dz         D ]  }| j                  t        |              g }|j                  t	        t
        |j                        g       | j                  t        t        d|dz               t        d |D                     y)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}.
        rV   )countc              3   &   K   | ]	  }|d      yw)rp   Nr7   ).0r   s     r$   	<genexpr>z4LogBeginnerTests._bufferLimitTest.<locals>.<genexpr>   s     4Ew4s   N)	rangerM   rd   re   r
   r   rF   r(   list)r=   rn   rS   rp   eventss        r$   _bufferLimitTestz!LogBeginnerTests._bufferLimitTest   s}     519% 	.ENN4e,-	.!#lFMM!B CDq%!)$%4V44	
r&   c                 \    t         j                  }| j                  || j                         y)z
        Up to C{LogBeginner._DEFAULT_BUFFER_SIZE} log events are buffered for
        replay by L{LogBeginner.beginLoggingTo}.
        N)r   _DEFAULT_BUFFER_SIZErw   rS   )r=   rn   s     r$   test_defaultBufferLimitz(LogBeginnerTests.test_defaultBufferLimit   s#    
 00eT]]3r&   c                     d}t        | j                  | j                  | j                  | j                  |      }| j                  ||       y)z
        The size of the L{LogBeginner} event buffer can be overridden with the
        C{initialBufferSize} initilizer argument.
           )initialBufferSizeN)r   rM   rP   rQ   rR   rw   )r=   rn   rS   s      r$   test_overrideBufferLimitz)LogBeginnerTests.test_overrideBufferLimit   sH    
 NNNN#
 	eX.r&   c           
      \   g }g }t        j                         }t        |      }| j                  t	        d             t               \  }}| j                  j                  t        t        |j                        |g       | j                  t	        d             t               \  }}| j                  j                  t        t        |j                        |g       | j                  t	        d             t	        t        t        j                  ||||      }	d| _        t        | |t	        d      t	        d      |	t	        d      g       t        | ||	t	        d      g       |j!                         }
| j#                  d| d| d	|
       | j#                  d| d| d	|
       y)
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<:>)rN   rO   r   rM   rd   r   rS   re   r
   r   rF   r   r   warnmaxDiffr+   getvalueassertIn)r=   r^   rc   
fileHandletextObserverfirstFilename	firstLinesecondFilename
secondLinewarningoutputs              r$   test_beginLoggingToTwicez)LogBeginnerTests.test_beginLoggingToTwice   sj    #%"$[[]
*:6t+./#+: y$$d<&H,%WXt,/0%-Z"
$$d<&H,%WXt*-.-mm""
 ;'<(:&			
 	dGgt*/E%FG$$&-)A6?.):,a8&Ar&   c                     t        | j                        }|j                  d       |j                  dd       | j	                  | j
                  j                         d       y)zP
        Critical messages will be written as text to the error stream.
        observerzignore thisza critical {message}r?   )r?   za critical message
N)r   rM   infocriticalr(   rP   r   r=   logs     r$   test_criticalLoggingz%LogBeginnerTests.test_criticalLogging   sO     dnn-+Y?))2246LMr&   c                     t        | j                        }| j                  j                  d       |j	                  d       | j                  | j                  j                         d       y)z
        Once logging has begun with C{beginLoggingTo}, critical messages are no
        longer written to the output stream.
        r   r7   zanother critical message N)r   rM   rS   re   r   r(   rP   r   r   s     r$   test_criticalLoggingStopsz*LogBeginnerTests.test_criticalLoggingStops   sP    
 dnn-$$R(/0))224b9r&   c                    g }| j                   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       y)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.rC   r5   )log_namespacelog_ioNzError, world.r6   )rS   re   r
   r   rF   printr   rQ   r5   r+   rd   r6   )r=   rv   s     r$   %test_beginLoggingToRedirectStandardIOz6LogBeginnerTests.test_beginLoggingToRedirectStandardIO   s     "$$$d<&G%HIoD1F1F$GH&4hOP	
 1IoD1F1F$GH&4hOP	
r&   c                 .   | j                   j                  }| j                   j                  }| j                  j	                  dd       | j                  | j                   j                  |       | j                  | j                   j                  |       y)z
        L{LogBeginner.beginLoggingTo} will leave the existing stdout/stderr in
        place if it has been told not to replace them.
        r7   F)redirectStandardION)rQ   r5   r6   rS   re   assertIs)r=   oldOutoldErrs      r$   test_beginLoggingToDontRedirectz0LogBeginnerTests.test_beginLoggingToDontRedirect  si    
 &&&&$$RE$Bdnn++V4dnn++V4r&   c                    t        j                  t        j                         d      }t        j                  t        j                         d      }|| j                  _        || j                  _        g }| j                  j                  t        t        |j                        g       t        t        | j                  j                        }t        t        | j                  j
                        }| j                  |j                  d       | j                  |j                  d       |j                  d       |j                  d       t        | |t!        d      t!        d      g       y)	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)rN   TextIOWrapperBytesIOrQ   r5   r6   rS   re   r
   r   rF   r   r(   encodingwriter+   rd   )r=   weirdweirderrrv   r5   r6   s         r$   $test_beginLoggingToPreservesEncodingz5LogBeginnerTests.test_beginLoggingToPreservesEncoding  s       {;##BJJL&9 % (!#$$d<&G%HIfdnn334fdnn334+6&1]#]#dFT%:D<Q$RSr&   c           
         | j                   j                  dt        t        d       g }| j                  j                  t        t        |j                        g       | j                   j                  dt        t        d       t        j                         }| j                   j                  dt        t        d|       | j                  | 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       y)z
        L{LogBeginner.beginLoggingTo} will redirect the warnings of its
        warnings module into the logging system.
        z	a messagerV   zanother messager   zyet anotherr|   r   N.)r   r@   rA   rB   )rR   rG   DeprecationWarning__file__rS   re   r
   r   rF   rN   rO   r(   r<   r+   rd   r2   r1   )r=   rv   fs      r$   test_warningsModulez$LogBeginnerTests.test_warningsModule/  s"   
 	''5GSTU!#$$d<&G%HI''18Q	
 KKM''-x 	( 	
 	((0(AtTJ 2HaDI	
 	-*55,556 &		
r&   c                    t        t        d            }t        | j                        }|j	                  d|       | j
                  j                         }| j                  d|       | j                  d|       | j                  d|       y)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   rM   r   rP   r   r   )r=   r   r   msgs       r$   test_failuresAppendTracebacksz.LogBeginnerTests.test_failuresAppendTracebacksV  sq     ILMNdnn-K+'')k3'DcJk3'r&   rH   )r1   r2   r3   __doc__rT   ri   rm   rK   r   rw   rz   r~   r   r   r   r   r   r   r   r   r7   r&   r$   r-   r-   5   sh    /
b+$+&
c 
[ 
T 
*4/)BVN:
$	5T0%
N
(r&   r-   )"r   rN   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+   r-   r7   r&   r$   <module>r      s~    
 E E E * " ' 9 0   $ '7


7x.7 N7 
	7>k(x(( k(r&   