Ë
    oj!"  ã                  óH  — d Z ddlm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mZmZ ddl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 ddlmZ ddlmZmZ ddl m!Z! ddl"m#Z# dd„Z$ G d„ d«      Z% G d„ dejL                  «      Z'dd„Z( G d„ dejR                  «      Z*y)z+
Test cases for L{twisted.logger._format}.
é    )ÚannotationsN)Úgetsourcefile)ÚBytesIOÚTextIOWrapper)Ú	FormatterÚ	LogRecordÚStreamHandlerÚ	getLogger)ÚListÚOptionalÚTuple)ÚBrokenMethodImplementation)ÚverifyObject)Úcurrentframe)ÚFailure)Úunittesté   )ÚILogObserverÚLogEvent)ÚLogLevel)ÚSTDLibLogObserverc                 óˆ   — t        d«      } t        t        j                  | j                  d      «      | j
                  dz   fS )z�
    Retrive the file name and line number immediately after where this function
    is called.

    @return: the file name and line number
    é   Ú__name__)r   r   ÚsysÚmodulesÚ	f_globalsÚf_lineno)Úcallers    úA/usr/lib/python3/dist-packages/twisted/logger/test/test_stdlib.pyÚnextLiner!      s?   € ô ˜!‹_€Fä”c—k‘k &×"2Ñ"2°:Ñ">Ñ?Ó@Ø�‰˜!Ñðð ó    c                  ó(   — e Zd ZdZdd„Zdd„Zdd„Zy)ÚStdlibLoggingContainerzF
    Continer for a test configuration of stdlib logging objects.
    c                ó”  — t        d«      | _        | j                  j                  «       | _        | j                  j	                  t
        j                  «       t        «       | _        | j                  j                  | j                  «       t        «       \  | _        | _        | j                  j                  | j                  «       y )NÚ )r
   Ú
rootLoggerÚgetEffectiveLevelÚoriginalLevelÚsetLevelÚ
py_loggingÚDEBUGÚBufferedHandlerÚbufferedHandlerÚ
addHandlerÚhandlerAndBytesIOÚstreamHandlerÚoutput©Úselfs    r    Ú__init__zStdlibLoggingContainer.__init__.   s‡   € Ü# B›-ˆŒà!Ÿ_™_×>Ñ>Ó@ˆÔØ�‰× Ñ ¤×!1Ñ!1Ô2ä.Ó0ˆÔØ�‰×"Ñ" 4×#7Ñ#7Ô8ä*;Ó*=Ñ'ˆÔ˜DœKØ�‰×"Ñ" 4×#5Ñ#5Õ6r"   c                óJ  — | j                   j                  | j                  «       | j                   j                  | j                  «       | j                   j                  | j
                  «       | j
                  j                  «        | j                  j                  «        y)z#
        Close the logger.
        N)r'   r*   r)   ÚremoveHandlerr.   r1   Úcloser2   r3   s    r    r8   zStdlibLoggingContainer.close:   sn   € ð 	�‰× Ñ  ×!3Ñ!3Ô4Ø�‰×%Ñ% d×&:Ñ&:Ô;Ø�‰×%Ñ% d×&8Ñ&8Ô9Ø×Ñ× Ñ Ô"Ø�‰×ÑÕr"   c                óT   — | j                   j                  «       j                  d«      S )zd
        Get the output to the underlying stream as text.

        @return: the output text
        úutf-8)r2   ÚgetvalueÚdecoder3   s    r    ÚoutputAsTextz#StdlibLoggingContainer.outputAsTextD   s"   € ð �{‰{×#Ñ#Ó%×,Ñ,¨WÓ5Ð5r"   N©ÚreturnÚNone)r?   Ústr)r   Ú
__module__Ú__qualname__Ú__doc__r5   r8   r=   © r"   r    r$   r$   )   s   „ ñó
7óô6r"   r$   c                  óh   — e Zd Z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d„Zy)ÚSTDLibLogObserverTestsz)
    Tests for L{STDLibLogObserver}.
    c                óˆ   — t        «       }	 t        t        |«       y# t        $ r}| j	                  |«       Y d}~yd}~ww xY w)z=
        L{STDLibLogObserver} is an L{ILogObserver}.
        N)r   r   r   r   Úfail)r4   ÚobserverÚes      r    Útest_interfacez%STDLibLogObserverTests.test_interfaceR   s8   € ô %Ó&ˆð	Üœ xÕ0øÜ)ò 	Ø�I‰I�a�L‰Lûð	ús   Œ �	A¦<¼Ac                óP   — t        «       }| j                  |j                  «       |S )zj
        Create a logging object we can use to test with.

        @return: a stdlib-style logger
        )r$   Ú
addCleanupr8   )r4   Úloggers     r    Ú	py_loggerz STDLibLogObserverTests.py_logger\   s!   € ô (Ó)ˆØ�‰˜Ÿ™Ô%Øˆr"   c                óÆ   — | j                  «       }t        t        j                  dz   ¬«      }|D ]
  } ||«       Œ |j                  j                  |j                  «       fS )zÞ
        Send one or more events to Python's logging module, and capture the
        emitted L{LogRecord}s and output stream as a string.

        @param events: events

        @return: a tuple: (records, output)
        r   )Ú
stackDepth)rP   r   ÚdefaultStackDepthr.   Úrecordsr=   )r4   ÚeventsÚplrJ   Úevents        r    ÚlogEventzSTDLibLogObserverTests.logEventf   se   € ð �^‰^ÓˆÜ$ô )×:Ñ:Øñô
ˆð ò 	ˆEÙ�U�Oð	ð ×!Ñ!×)Ñ)¨2¯?©?Ó+<Ð<Ð<r"   c                ó    — | j                  i «      \  }}| j                  t        |«      d«       | j                  |d   j                  d«       y)z
        Logger name.
        r   r   ÚtwistedN)rX   ÚassertEqualÚlenÚname©r4   rT   r2   s      r    Ú	test_namez STDLibLogObserverTests.test_name|   sB   € ð Ÿ-™-¨Ó+‰ˆ�à×Ñœ˜W› qÔ)Ø×Ñ˜ ™Ÿ™¨)Õ4r"   c                óÀ  — dt         j                  t        j                  t         j                  t        j
                  t         j                  t        j                  t         j                  t        j                  t         j                  t        j                  t         j                  i}g }|j                  «       D ]-  \  }}i }|�||d<   t        |«      |d<   |j                  |«       Œ/  | j                  |Ž \  }}| j!                  t#        |«      t#        |«      «       t%        t#        |«      «      D ]'  }| j!                  ||   j&                  ||   d   «       Œ) y)z
        Log levels.
        NÚ	log_levelÚ
py_levelno)r+   ÚINFOr   Údebugr,   ÚinfoÚwarnÚWARNINGÚerrorÚERRORÚcriticalÚCRITICALÚitemsÚintÚappendrX   r[   r\   ÚrangeÚlevelno)	r4   ÚlevelMappingrU   ÚlevelÚpyLevelrW   rT   r2   Úis	            r    Útest_levelsz"STDLibLogObserverTests.test_levels…   s#  € ð
 ”*—/‘/Ü�N‰NœJ×,Ñ,Ü�M‰Mœ:Ÿ?™?Ü�M‰Mœ:×-Ñ-Ü�N‰NœJ×,Ñ,Ü×Ñœz×2Ñ2ð
ˆð ˆØ*×0Ñ0Ó2ò 	!‰NˆE�7ØˆEð Ð Ø%*��kÑ"ô #& g£,ˆE�,Ñà�M‰M˜%Õ ð	!ð (˜$Ÿ-™-¨Ð0‰ˆ�Ø×Ñœ˜W›¤s¨<Ó'8Ô9ô ”s˜7“|Ó$ò 	JˆAØ×Ñ˜W Q™Z×/Ñ/°¸±¸<Ñ1HÕIñ	Jr"   c                ó4  — t        «       \  }}| j                  i «      \  }}| j                  t        |«      d«       | j                  |d   j                  |«       | j                  |d   j
                  |«       | j                  |d   j                  «       y)zb
        C{pathname}, C{lineno}, C{exc_info}, C{func} is set properly on
        records.
        r   r   N)r!   rX   r[   r\   ÚpathnameÚlinenoÚassertIsNoneÚexc_info)r4   ÚfilenameÚlogLinerT   r2   s        r    Útest_callerInfoz&STDLibLogObserverTests.test_callerInfo¨   s‚   € ô
 %›JÑˆ�'ØŸ-™-¨Ó+‰ˆ�à×Ñœ˜W› qÔ)Ø×Ñ˜ ™×,Ñ,¨hÔ7Ø×Ñ˜ ™×*Ñ*¨GÔ4Ø×Ñ˜' !™*×-Ñ-Õ.r"   c                ó
  — t        dd¬«      }| j                  |«      \  }}| j                  t        |«      d«       | j                  t	        |d   j
                  «      d«       | j                  |d   j                  d«       y)	zL
        Basic formattable event passes the format along correctly.
        úHello, {who}!Údude©Ú
log_formatÚwhor   r   zHello, dude!rE   N)ÚdictrX   r[   r\   rA   ÚmsgÚargs©r4   rW   rT   r2   s       r    Útest_basicFormatz'STDLibLogObserverTests.test_basicFormat¹   sk   € ô  °VÔ<ˆØŸ-™-¨Ó.‰ˆ�à×Ñœ˜W› qÔ)Ø×Ñœ˜W Q™ZŸ^™^Ó,¨nÔ=Ø×Ñ˜ ™Ÿ™¨"Õ-r"   c                óÐ   — t        dd¬«      }| j                  |«      \  }}| j                  t        |«      d«       | j	                  |j                  d«      t        |«      «       y)z<
        Basic formattable event renders correctly.
        r   r€   r�   r   z:Hello, dude!
N)r„   rX   r[   r\   Ú
assertTrueÚendswithÚreprr‡   s       r    Útest_basicFormatRenderedz/STDLibLogObserverTests.test_basicFormatRenderedÄ   sR   € ô  °VÔ<ˆØŸ-™-¨Ó.‰ˆ�à×Ñœ˜W› qÔ)Ø�‰˜Ÿ™Ð(9Ó:¼DÀ»LÕIr"   c                ó²   — | j                  i «      \  }}| j                  t        |«      d«       | j                  t        |d   j                  «      d«       y)z'
        Event with no format.
        r   r   r&   N)rX   r[   r\   rA   r…   r^   s      r    Útest_noFormatz$STDLibLogObserverTests.test_noFormatÎ   sG   € ð Ÿ-™-¨Ó+‰ˆ�à×Ñœ˜W› qÔ)Ø×Ñœ˜W Q™ZŸ^™^Ó,¨bÕ1r"   c                ó4  — d	d„}	  |«        t        dd¬«      }| j                  |«      \  }}| j	                  t        |«      d«       | j                  d|«       | j                  d|«       | j                  d|«       y# t         $ r t        «       }Y Œ‰w xY w)
zK
        An event with a failure logs the failure details as well.
        c                 ó   — ddz   y ©Nr   r   rE   rE   r"   r    Úfailing_funcz9STDLibLogObserverTests.test_failure.<locals>.failing_funcÜ   ó	   € Ø�‹Er"   úHi momÚme©r‚   rƒ   Úlog_failurer   úin failing_funcÚZeroDivisionErrorNr>   )rš   r   r„   rX   r[   r\   ÚassertIn©r4   r“   ÚfailurerW   rT   r2   s         r    Útest_failurez#STDLibLogObserverTests.test_failure×   s‹   € ó
	ð	 ÙŒNô  ¨dÀÔHˆØŸ-™-¨Ó.‰ˆ�à×Ñœ˜W› qÔ)Ø�‰�h Ô'Ø�‰Ð'¨Ô0Ø�‰Ð)¨6Õ2øô !ò 	 Ü“iŠGð	 ús   †B ÂBÂBc                óT  — d	d„}	  |«        t        dd¬«      }| j	                  |«      \  }}| j                  t        |«      d«       | j                  d|«       | j                  d|«       | j                  d|«       y# t         $ r t        «       }|j                  «        Y Œ™w xY w)
z²
        A cleaned Failure object has a fake traceback object; make sure that
        logging such a failure still results in the exception details being
        logged.
        c                 ó   — ddz   y r’   rE   rE   r"   r    r“   z@STDLibLogObserverTests.test_cleanedFailure.<locals>.failing_funcó   r”   r"   r•   r–   r—   r   r™   rš   Nr>   )rš   r   ÚcleanFailurer„   rX   r[   r\   r›   rœ   s         r    Útest_cleanedFailurez*STDLibLogObserverTests.test_cleanedFailureì   s—   € ó	ð	#ÙŒNô
  ¨dÀÔHˆØŸ-™-¨Ó.‰ˆ�à×Ñœ˜W› qÔ)Ø�‰�h Ô'Ø�‰Ð'¨Ô0Ø�‰Ð)¨6Õ2øô !ò 	#Ü“iˆGØ× Ñ Ö"ð	#ús   †B Â#B'Â&B'Nr>   )r?   r$   )rU   r   r?   zTuple[List[LogRecord], str])r   rB   rC   rD   rL   rP   rX   r_   ru   r}   rˆ   r�   r�   rž   r¢   rE   r"   r    rG   rG   M   sA   „ ñóóó=ó,5ó!JóF/ó"	.óJó2ó3ô*3r"   rG   c                 ó¨   — t        «       } t        j                  }t        | dd¬«      }t	        |«      }t        |«      }|j                  |«       || fS )z—
    Construct a 2-tuple of C{(StreamHandler, BytesIO)} for testing interaction
    with the 'logging' module.

    @return: handler and io object
    r:   ú
)ÚencodingÚnewline)r   r+   ÚBASIC_FORMATr   r   r	   ÚsetFormatter)r2   ÚtemplateÚstreamÚ	formatterÚhandlers        r    r0   r0     sP   € ô ‹Y€FÜ×&Ñ&€HÜ˜6¨G¸TÔB€FÜ˜(Ó#€IÜ˜FÓ#€GØ×Ñ˜Ô#Ø�Fˆ?Ðr"   c                  ó    — e Zd ZdZdd„Zdd„Zy)r-   zN
    A L{py_logging.Handler} that remembers all logged records in a list.
    c                óP   — t         j                  j                  | «       g | _        y)z5
        Initialize this L{BufferedHandler}.
        N)r+   ÚHandlerr5   rT   r3   s    r    r5   zBufferedHandler.__init__  s   € ô 	×Ñ×#Ñ# DÔ)Ø(*ˆ�r"   c                ó:   — | j                   j                  |«       y)z&
        Remember the record.
        N)rT   rn   )r4   Úrecords     r    ÚemitzBufferedHandler.emit!  s   € ð 	�‰×Ñ˜FÕ#r"   Nr>   )r±   r   r?   r@   )r   rB   rC   rD   r5   r²   rE   r"   r    r-   r-     s   „ ñó+ô$r"   r-   )r?   zTuple[Optional[str], int])r?   z,tuple[StreamHandler[TextIOWrapper], BytesIO])+rD   Ú
__future__r   Úloggingr+   r   Úinspectr   Úior   r   r   r   r	   r
   Útypingr   r   r   Úzope.interface.exceptionsr   Úzope.interface.verifyr   Útwisted.python.compatr   Útwisted.python.failurer   Útwisted.trialr   Ú_interfacesr   r   Ú_levelsr   Ú_stdlibr   r!   r$   ÚTestCaserG   r0   r¯   r-   rE   r"   r    ú<module>rÁ      s{   ðñõ #ã Û 
Ý !ß %ß BÓ Bß (Ñ (å @Ý .å .Ý *Ý "ß 0Ý Ý 'ó÷!6ñ !6ôHu3˜X×.Ñ.ô u3ópô $�j×(Ñ(õ $r"   