Ë
    ojß1  ã                   ó  — 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      €úA/usr/lib/python3/dist-packages/twisted/logger/test/test_global.pyÚsimplifyzcompareEvents.<locals>.simplify*   s=   ø€ Ø�z‰z‹|ˆØ—:‘:“<ò 	ˆCØ˜-Ò'Ø—‘˜•ð	ð ˆó    N)ÚlenÚassertEqualÚsetr    r   )r   r   r   r   r%   ÚsimplifiedActualr#   s         @r$   ÚcompareEventsr+      s—   ø€ ô ˆ<ÓœC Ó/Ò/Ø×Ñ˜ ~Ô6Ü“E€Màò +ˆØœ˜UŸZ™Z›\Ó*Ñ*‰ð+ðœð ¤Xõ ð 6BÖB¨E™ �Ð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   „ Ù“XˆFÙ“X‰Fr&   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 —‘×$Ñ$ g¨x¸À6È4ÐQUÐ%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ð ˜s‘mð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ˆÔÜ#Ø�N‰N˜D×,Ñ,¨d¯n©n¸d×>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   è ø€ Ò4 E��w•Ñ4ùs   ‚N)	ÚrangerM   rd   re   r
   r   rF   r(   Úlist)r=   rn   rS   rp   Úeventss        r$   Ú_bufferLimitTestz!LogBeginnerTests._bufferLimitTest�   s}   € ô ˜5 1™9Ó%ò 	.ˆEØ�N‰Nœ4 eÔ,Õ-ð	.à!#ˆØ×Ñ¤¤l°F·M±MÓ!BÐ CÔDØ×ÑÜ”�q˜% !™)Ó$Ó%ÜÑ4¨VÔ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ˆØ×Ñ˜e T§]¡]Õ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   € ð
 ˆÜØ�N‰NØ×ÑØ�N‰NØ×ÑØ#ô
ˆð 	×Ñ˜e XÕ.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 *Ô-Ô.ÜÜ-Ü—m‘mØ"ØØ"Øô
ˆð ˆŒÜØØä˜;Ô'Ü˜<Ô(ØÜ˜:Ô&ð	ô		
ô 	�d˜G g¬t¸*Ô/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   € ô ˜dŸn™nÔ-ˆØ�‰�ÔØ�‰Ð+°YˆÔ?Ø×Ñ˜×)Ñ)×2Ñ2Ó4Ð6LÕ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   € ô
 ˜dŸn™nÔ-ˆØ�‰×$Ñ$ RÔ(Ø�‰Ð/Ô0Ø×Ñ˜×)Ñ)×2Ñ2Ó4°bÕ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Üˆo¤D¬°·±×1FÑ1FÓ$GÕHÜØ�&œ4¨h¸ÔOÐPô	
ð ’1ˆIÜˆo¤D¬°·±×1FÑ1FÓ$GÕHÜØ�&œ4¨h¸Ô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   € ð
 —‘×&Ñ&ˆØ—‘×&Ñ&ˆØ�‰×$Ñ$ R¸EÐ$ÔBØ�‰�d—n‘n×+Ñ+¨VÔ4Ø�‰�d—n‘n×+Ñ+¨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ø   € ô × Ñ ¤§¡£¨{Ó;ˆÜ×#Ñ#¤B§J¡J£L°&Ó9ˆà %ˆ�‰ÔØ (ˆ�‰Ôà!#ˆØ�‰×$Ñ$¤d¬<¸¿¹Ó&GÐ%HÔIÜ”f˜dŸn™n×3Ñ3Ó4ˆÜ”f˜dŸn™n×3Ñ3Ó4ˆØ×Ñ˜Ÿ™¨+Ô6Ø×Ñ˜Ÿ™¨&Ô1à�‰�]Ô#Ø�‰�]Ô#Ü�d˜F¤T°Ô%:¼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Ø×Ñ×'Ñ'ØÔ1´8¸Qô	
ô �K‰K‹MˆØ×Ñ×'Ñ'ØÔ-¬x¸Àð 	(ô 	
ð 	×ÑØ×Ñ×(Ñ(àÔ0´(¸A¸tÀTÐJØÔ 2´H¸aÀÀ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ˆÜ˜dŸn™nÔ-ˆØ�‰�K¨ˆÔ+Ø×Ñ×'Ñ'Ó)ˆØ�‰�k 3Ô'Ø�‰ÐDÀcÔJØ�‰�k 3Õ'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&   