o
    ¯béŒ  ã                   @   sŽ  d Z 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m	Z	 ddl
mZmZmZmZ ddlmZ ddlmZmZ ddlmZ ddlmZ G d	d
„ d
eƒZG dd„ dejƒZG dd„ dejƒZG dd„ deƒZe  e¡ G dd„ dƒZ!G dd„ dƒZ"G dd„ de!e"ƒZ#G dd„ dƒZ$G dd„ de$ejƒZ%G dd„ de$ejƒZ&G dd„ dejƒZ'G dd „ d ejƒZ(G d!d"„ d"ejƒZ)G d#d$„ d$ejƒZ*dS )%z"
Tests for L{twisted.python.log}.
é    N)ÚIOBaseÚStringIO)ÚLogBeginnerÚLoggingFileÚLogLevelÚLogPublisher)ÚhandlerAndBytesIO)ÚfailureÚlog)r   )Úunittestc                   @   s   e Zd ZdZdS )ÚFakeWarningz
    A unique L{Warning} subclass used by tests for interactions of
    L{twisted.python.log} with the L{warnings} module.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú7/usr/lib/python3/dist-packages/twisted/test/test_log.pyr      s    r   c                   @   ó@   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S )ÚTextFromEventDictTestsz)
    Tests for L{textFromEventDict}.
    c                 C   s$   t dd}t |¡}|  |d¡ dS )zj
        The C{"message"} value, when specified, is concatenated to generate the
        message.
        )ÚaÚbÚc)Úmessageza b cN©Údictr
   ÚtextFromEventDictÚassertEqual©ÚselfÚ	eventDictÚtextr   r   r   Útest_message)   s   

z#TextFromEventDictTests.test_messagec                 C   s*   t ddddd}t |¡}|  |d¡ dS )zW
        The C{"format"} value, when specified, is used to format the message.
        r   r   zHello, %(foo)s!Údude)r   ÚisErrorÚformatÚfoozHello, dude!Nr   r   r   r   r   Útest_format2   s   
z"TextFromEventDictTests.test_formatc                 C   s$   t ddd}t |¡}|  |¡ dS )zb
        If C{"format"} is unspecified and C{"message"} is empty, return
        L{None}.
        r   r   ©r   r#   N)r   r
   r   ÚassertIsNoner   r   r   r   Útest_noMessageNoFormat:   s   
z-TextFromEventDictTests.test_noMessageNoFormatc              	   C   sJ   zt ƒ ‚ ty$   tddt ¡ dd}t |¡}|  | d¡¡ Y dS w )zO
        The C{"why"} value, when specified, is first part of message.
        r   é   r%   )r   r#   r	   Úwhyzfoo
N©	ÚRuntimeErrorÚBaseExceptionr   r	   ÚFailurer
   r   Ú
assertTrueÚ
startswithr   r   r   r   Útest_whySpecifiedC   s   ÿ
ûz(TextFromEventDictTests.test_whySpecifiedc                 C   sH   zt ƒ ‚ ty#   tddt ¡ d}t |¡}|  | d¡¡ Y dS w )zY
        The C{"why"} value, when unspecified, defaults to C{"Unhandled Error"}.
        r   r*   ©r   r#   r	   zUnhandled Error
Nr,   r   r   r   r   Útest_whyDefaultP   s   
ýz&TextFromEventDictTests.test_whyDefaultc                 C   s*   t ddtƒ d}t |¡}|  d|¡ dS )zk
        If unable to obtain a traceback due to an exception, catch it and note
        the error.
        r   r*   r3   z
(unable to obtain traceback)N)r   Úobjectr
   r   ÚassertInr   r   r   r   Útest_noTracebackForYou[   s   
z-TextFromEventDictTests.test_noTracebackForYouN)
r   r   r   r   r!   r&   r)   r2   r4   r7   r   r   r   r   r   $   s    		r   c                   @   sT   e 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S )ÚLogTestsc                 C   s0   g | _ | j j| _t | j¡ |  tj| j¡ d S ©N)ÚcatcherÚappendÚobserverr
   ÚaddObserverÚ
addCleanupÚremoveObserver©r   r   r   r   ÚsetUpg   s   
zLogTests.setUpc                 C   s^   | j }tjddd | ¡ }|  |d d d¡ |  |d ¡ |  d|¡ |  t|ƒd¡ d S )NÚtestT)ÚtestShouldCatchr   r   rC   Útime)r:   r
   ÚmsgÚpopr   r0   r6   Úlen©r   r:   Úir   r   r   ÚtestObservationm   s   zLogTests.testObservationc                 C   s|   | j }tjddddœtjdditjddd	 | ¡ }|  |d d¡ |  |d
 d¡ |  |d d¡ |  |d d d¡ d S )Nznot the defaultr   r   )Ú	subsystemÚsubsubsystemÚotherrL   r   r%   Úd)rM   rK   rM   r   r   )r:   r
   ÚcallWithContextrE   rF   r   rH   r   r   r   ÚtestContextv   s   
úzLogTests.testContextc                 C   sX   dt ƒ t ft tƒ ¡tffD ]\}}t |¡ | j ¡ }|  |d d¡ |  	|¡ qd S )N©úhello worldrR   r#   r*   ©
ÚKeyErrorr	   r/   r-   r
   Úerrr:   rF   r   ÚflushLoggedErrors©r   ÚeÚigrI   r   r   r   Ú
testErrors†   s   ý

øzLogTests.testErrorsc                 C   sj   dt ƒ t ft tƒ ¡tffD ]$\}}t |d¡ | j ¡ }|  |d d¡ |  |d d¡ |  	|¡ qd S )NrQ   Úfoobarr#   r*   r+   rS   rW   r   r   r   ÚtestErrorsWithWhy‘   s   ý
÷zLogTests.testErrorsWithWhyc                 C   sâ   g }g }dd„ }|j ||j fD ]}t |¡ |  tj|¡ qtdƒD ]L}g |dd…< g |dd…< tjdg d |  t¡}| j	dd…= |  
t|ƒd¡ |  
t|ƒd¡ |  
t|ƒd¡ |  
|d	 d
 d¡ |  
|d	 d
 d¡ q"dS )a  
        Exceptions raised by log observers are logged but the observer which
        raised the exception remains registered with the publisher.  These
        exceptions do not prevent the event from being sent to other observers
        registered with the publisher.
        c                 S   s   dd  d S ©Nr*   r   r   ©Úeventr   r   r   Úbroken§   ó   z-LogTests.test_erroneousErrors.<locals>.brokené   NúHowdy, y'all.)Ú	log_tracer*   é   r   r   )rc   )r;   r
   r=   r>   r?   ÚrangerE   rV   ÚZeroDivisionErrorr:   r   rG   )r   ÚL1ÚL2r`   r<   rI   Úexcsr   r   r   Útest_erroneousErrors   s$   

ìzLogTests.test_erroneousErrorsc                 C   sž   t  ¡ }| | j¡ | tdƒtdd¡ | j ¡ }|  |d | d¡ |  | jg ¡ |jtdƒtddt	ƒ d | j ¡ }|  |d | d¡ |  | jg ¡ dS )zy
        L{twisted.python.log.showwarning} emits the warning as a message
        to the Twisted logging system.
        zunique warning messageúwarning-filename.pyé   r$   zQwarning-filename.py:27: twisted.test.test_log.FakeWarning: unique warning message)ÚlineN)
r
   r   r=   r<   Úshowwarningr   r:   rF   r   r5   )r   Ú	publisherr_   r   r   r   Útest_showwarningÄ   s6   ü

þû

þzLogTests.test_showwarningc              
   C   sˆ   d}t }d}d}tƒ }tj|||||d |  | ¡ t ||||¡¡ d}tƒ }tj||||||d |  | ¡ t |||||¡¡ dS )z”
        L{twisted.python.log.showwarning} passes warnings with an explicit file
        target on to the underlying Python warning system.
        zanother unique messagerl   é   ©ÚfilerR   )rt   rn   N)r   r   r
   ro   r   ÚgetvalueÚwarningsÚformatwarning)r   r   ÚcategoryÚfilenameÚlinenoÚoutputrn   r   r   r   Útest_warningToFileì   s"   þþzLogTests.test_warningToFilec                    sx   g ‰ ‡ fdd„}dd„ }t  ¡ }| |¡ | |¡ | d¡ |  t|jƒ||h¡ |  tˆ ƒd¡ |  ˆ d t	¡ dS )	zk
        Log publisher does not use the global L{log.err} when reporting broken
        observers.
        c                    s"   |   d¡rˆ  | d j¡ d S d S ©Nr#   r	   )Úgetr;   Úvalue©r   ©Úerrorsr   r   ÚlogError  s   
ÿzHLogTests.test_publisherReportsBrokenObserversPrivately.<locals>.logErrorc                 S   s   t dƒ‚)NÚ*test_publisherLocalyReportsBrokenObservers)r-   r€   r   r   r   Úfail  s   zDLogTests.test_publisherReportsBrokenObserversPrivately.<locals>.failúHello!r*   r   N)
r
   r   r=   rE   r   ÚsetÚ	observersrG   ÚassertIsInstancer-   )r   rƒ   r…   rp   r   r   r   Ú-test_publisherReportsBrokenObserversPrivately
  s   


z6LogTests.test_publisherReportsBrokenObserversPrivatelyN)r   r   r   rA   rJ   rP   rZ   r\   rk   rq   r|   rŠ   r   r   r   r   r8   f   s    	'(r8   c                   @   ó   e Zd Zdd„ Zdd„ ZdS )ÚFakeFilec                 C   s   |   |¡ d S r9   ©r;   )r   Úbytesr   r   r   Úwrite#  ó   zFakeFile.writec                 C   s   d S r9   r   r@   r   r   r   Úflush&  ó   zFakeFile.flushN)r   r   r   r   r‘   r   r   r   r   rŒ   "  s    rŒ   c                   @   ó   e Zd Zdefdd„ZdS )ÚEvilStrÚreturnc                 C   ó   t dd ƒS r]   ©Ústrr@   r   r   r   Ú__str__.  ra   zEvilStr.__str__N)r   r   r   r˜   r™   r   r   r   r   r”   -  ó    r”   c                   @   s(   e Zd Zdefdd„Zdefdd„ZdS )ÚEvilReprr•   c                 C   ó   dS )NzHappy Evil Reprr   r@   r   r   r   r™   3  r’   zEvilRepr.__str__c                 C   r–   r]   r—   r@   r   r   r   Ú__repr__6  ra   zEvilRepr.__repr__N)r   r   r   r˜   r™   r   r   r   r   r   r›   2  s    r›   c                   @   s   e Zd ZdS )ÚEvilReprStrN)r   r   r   r   r   r   r   rž   :  s    rž   c                   @   r‹   )ÚLogPublisherTestCaseMixinc                 C   sR   t ƒ | _t ¡ | _t | j¡| _| j | jj¡ zW n	 t	y#   Y nw d| _
dS )z÷
        Add a log observer which records log events in C{self.out}.  Also,
        make sure the default string encoding is ASCII so that
        L{testSingleUnicode} can test the behavior of logging unencodable
        unicode messages.
        N)rŒ   Úoutr
   r   ÚlpÚFileLogObserverÚflor=   ÚemitÚUnicodeEncodeErrorÚ_origEncodingr@   r   r   r   rA   ?  s   
ÿ
zLogPublisherTestCaseMixin.setUpc                 C   sB   | j D ]}|  |t|›d¡ q| jdurt | j¡ t`dS dS )zÏ
        Verify that everything written to the fake file C{self.out} was a
        C{str}.  Also, restore the default string encoding to its previous
        setting, if it was modified by L{setUp}.
        z was not a stringN)r    r‰   r˜   r¦   ÚsysÚsetdefaultencoding)r   Úchunkr   r   r   ÚtearDownR  s   

þz"LogPublisherTestCaseMixin.tearDownN)r   r   r   rA   rª   r   r   r   r   rŸ   >  s    rŸ   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚLogPublisherTestsc                 C   s"   | j  d¡ |  t| jƒd¡ d S )NúHello, world.r*   ©r¡   rE   r   rG   r    r@   r   r   r   ÚtestSingleStringa  s   z"LogPublisherTests.testSingleStringc                 C   s$   | j  dd¡ |  t| jƒd¡ d S )NzHello, zworld.r*   r­   r@   r   r   r   ÚtestMultipleStringe  s   z$LogPublisherTests.testMultipleStringc                 C   s8   d}| j  |¡ |  t| jƒd¡ |  || jd ¡ dS )zß
        L{log.LogPublisher.msg} does not accept non-ASCII Unicode on Python 2,
        logging an error instead.

        On Python 3, where Unicode is default message type, the message is
        logged normally.
        õ   Hello, Â½ world.r*   r   N)r¡   rE   r   rG   r    r6   )r   r   r   r   r   Útest_singleUnicodel  s   z$LogPublisherTests.test_singleUnicodeN)r   r   r   r®   r¯   r±   r   r   r   r   r«   `  s    r«   c                   @   sÜ   e Zd ZdZdZdZdZdZd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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/d0„ Zd1d2„ Zd3d4„ Z d5S )6ÚFileObserverTestsz(
    Tests for L{log.FileObserver}.
    zInvalid format stringzUNFORMATTABLE OBJECTz<Invalid format string or unformattable object in log messagezPATHOLOGICAL ERRORzUnable to format eventz[UNFORMATTABLE]z)MESSAGE LOST: unformattable object loggedc           	   	   C   sô   t tddƒdu rt d¡‚tj dd¡}zR|tjd< t ¡  d}t |¡}d}zt |¡}W n t	y?   ||kr<|}n‚ Y nw |  
| j |¡| j |¡f||f¡ W |du r\tjd= n|tjd< t ¡  dS |du rptjd= n|tjd< t ¡  w )ay  
        Verify that L{getTimezoneOffset} produces the expected offset for a
        certain timezone both when daylight saving time is in effect and when
        it is not.

        @param tzname: The name of a timezone to exercise.
        @type tzname: L{bytes}

        @param daylightOffset: The number of seconds west of UTC the timezone
            should be when daylight saving time is in effect.
        @type daylightOffset: L{int}

        @param standardOffset: The number of seconds west of UTC the timezone
            should be when daylight saving time is not in effect.
        @type standardOffset: L{int}
        ÚtzsetNzWPlatform cannot change timezone, cannot verify correct offsets in well-known timezones.ÚTZ)	i×  r*   rr   r   r   r   re   rr   r   )	iÖ  é   é   r   r   r   é   éµ   r*   )ÚgetattrrD   r   ÚSkipTestÚosÚenvironr~   r³   ÚmktimeÚOverflowErrorr   r£   ÚgetTimezoneOffset)	r   ÚtznameÚdaylightOffsetÚstandardOffsetÚoriginalTimezoneÚlocalStandardTupleÚstandardÚlocalDaylightTupleÚdaylightr   r   r   Ú_getTimezoneOffsetTestˆ  sB   ÿ

þø

þû

ü


z(FileObserverTests._getTimezoneOffsetTestc                 C   ó   |   ddd¡ dS )a!  
        Attempt to verify that L{FileLogObserver.getTimezoneOffset} returns
        correct values for the current C{TZ} environment setting for at least
        some cases.  This test method exercises a timezone that is west of UTC
        (and should produce positive results).
        zAmerica/New_Yorki@8  éPF  N©rÈ   r@   r   r   r   Útest_getTimezoneOffsetWestOfUTCÌ  ó   z1FileObserverTests.test_getTimezoneOffsetWestOfUTCc                 C   rÉ   )a!  
        Attempt to verify that L{FileLogObserver.getTimezoneOffset} returns
        correct values for the current C{TZ} environment setting for at least
        some cases.  This test method exercises a timezone that is east of UTC
        (and should produce negative results).
        zEurope/BerlinéàãÿÿéðñÿÿNrË   r@   r   r   r   Útest_getTimezoneOffsetEastOfUTCÕ  rÍ   z1FileObserverTests.test_getTimezoneOffsetEastOfUTCc                 C   s   |   ddd¡ dS )af  
        Attempt to verify that L{FileLogObserver.getTimezoneOffset} returns
        correct values for the current C{TZ} environment setting for at least
        some cases.  This test method exercises a timezone that does not use
        daylight saving time at all (so both summer and winter time test values
        should have the same offset).
        zAfrica/JohannesburgrÎ   NrË   r@   r   r   r   Ú/test_getTimezoneOffsetWithoutDaylightSavingTimeÞ  s   zAFileObserverTests.test_getTimezoneOffsetWithoutDaylightSavingTimec                 C   s
  t  d¡}dd„ | j_|  | j |¡d¡ dd„ | j_|  | j |¡d¡ dd„ | j_|  | j |¡d¡ d	d„ | j_|  | j |¡d
¡ dd„ | j_|  | j |¡d¡ dd„ | j_|  | j |¡d¡ dd„ | j_|  | j |¡d¡ d| j_|  | j |¡d¡ dS )zu
        Test the method of L{FileLogObserver} which turns a timestamp into a
        human-readable string.
        )	iÑ  re   rb   r·   é   rµ   é   é   r   c                 S   rœ   )NrÊ   r   ©Úwhenr   r   r   Ú<lambda>ô  ó    z7FileObserverTests.test_timeFormatting.<locals>.<lambda>z2001-02-02 23:05:06-0500c                 S   rœ   )NrÏ   r   rÕ   r   r   r   r×   ø  rØ   z2001-02-03 05:05:06+0100c                 S   rœ   )NiPeÿÿr   rÕ   r   r   r   r×   ü  rØ   z2001-02-03 15:05:06+1100c                 S   rœ   )Ni  r   rÕ   r   r   r   r×      rØ   z2001-02-03 02:35:06-0130c                 S   rœ   )Nièêÿÿr   rÕ   r   r   r   r×     rØ   z2001-02-03 05:35:06+0130c                 S   rœ   )Ni  r   rÕ   r   r   r   r×   	  rØ   z2001-02-03 03:35:06-0030c                 S   rœ   )Niøøÿÿr   rÕ   r   r   r   r×     rØ   z2001-02-03 04:35:06+0030z%Y %mz2001 02N)ÚcalendarÚtimegmr£   r¿   r   Ú
formatTimeÚ
timeFormat)r   rÖ   r   r   r   Útest_timeFormattingì  s"   
z%FileObserverTests.test_timeFormattingc                 C   s    d| j _|  d| j  d¡¡ dS )z§
        L{FileLogObserver.formatTime} supports a value of C{timeFormat} which
        includes C{"%f"}, a L{datetime}-only format specifier for microseconds.
        z%fÚ600000gš™™™™mû@N)r£   rÜ   r   rÛ   r@   r   r   r   Ú#test_microsecondTimestampFormatting  s   z5FileObserverTests.test_microsecondTimestampFormattingc                 C   s8   | j  tƒ ¡ |  t| jƒd¡ |  | j| jd ¡ d S r]   )r¡   rE   r”   r   rG   r    ÚassertNotInÚERROR_UNFORMATTABLE_OBJECTr@   r   r   r   Ú%test_loggingAnObjectWithBroken__str__  s   z7FileObserverTests.test_loggingAnObjectWithBroken__str__c                 C   ó<   | j jdtƒ d |  t| jƒd¡ |  | j| jd ¡ d S ©Nz%(blat)s©r$   Úblatr*   r   )r¡   rE   r”   r   rG   r    r6   ÚERROR_INVALID_FORMATr@   r   r   r   Ú(test_formattingAnObjectWithBroken__str__&  ó   z:FileObserverTests.test_formattingAnObjectWithBroken__str__c                 C   rã   ©NÚhuh)Úsystemr*   r   )r¡   rE   r”   r   rG   r    r6   ÚERROR_FORMATr@   r   r   r   Útest_brokenSystem__str__+  ré   z*FileObserverTests.test_brokenSystem__str__c                 C   ó>   | j jdtƒ gd |  t| jƒd¡ |  | j| jd ¡ d S rä   ©r¡   rE   r›   r   rG   r    r6   rá   r@   r   r   r   Ú1test_formattingAnObjectWithBroken__repr__Indirect0  ó   zCFileObserverTests.test_formattingAnObjectWithBroken__repr__Indirectc                 C   rï   rê   rð   r@   r   r   r   Ú%test_systemWithBroker__repr__Indirect5  rò   z7FileObserverTests.test_systemWithBroker__repr__Indirectc                 C   ó:   | j jddd |  t| jƒd¡ |  | j| jd ¡ d S )Nz
hooj %s %sr*   rå   r   ©r¡   rE   r   rG   r    r6   rç   r@   r   r   r   Útest_simpleBrokenFormat:  ó   z)FileObserverTests.test_simpleBrokenFormatc                 C   rô   )Né*   r*   rå   r   rõ   r@   r   r   r   Útest_ridiculousFormat?  r÷   z'FileObserverTests.test_ridiculousFormatc                 C   s<   | j jtƒ dd |  t| jƒd¡ |  | j| jd ¡ d S )Nr*   rå   r   )r¡   rE   rž   r   rG   r    r6   ÚERROR_PATHOLOGICALr@   r   r   r   Ú!test_evilFormat__repr__And__str__D  ré   z3FileObserverTests.test_evilFormat__repr__And__str__c                 C   s&   | j jddd |  t| jƒd¡ dS )zP
        This kind of eventDict used to fail silently, so test it does.
        Ú Fr'   r   Nr­   r@   r   r   r   Útest_strangeEventDictI  s   z'FileObserverTests.test_strangeEventDictc                 C   s6   |   tjj¡ |   ttdtj¡ |   ttdtj¡ dS )zm
        Cleanup after a startLogging() call that mutates the hell out of some
        global state.
        ÚstdoutÚstderrN)r>   r
   ÚtheLogPublisherÚ_stopLoggingÚsetattrr§   rþ   rÿ   r@   r   r   r   Ú_startLoggingCleanupP  s   z&FileObserverTests._startLoggingCleanupc                    sX   |   ¡  tƒ }t |¡ ‡ fdd„}g ‰ t |¡ tdtjd |  ˆ d d d¡ dS )	zs
        startLogging()'s overridden sys.stderr should consider everything
        written to it an error.
        c                    s   ˆ   | ¡ d S r9   r   r^   ©Úobservedr   r   Úobserveb  r   z@FileObserverTests.test_printToStderrSetsIsError.<locals>.observer¬   rs   r   r#   r*   N)	r  r   r
   ÚstartLoggingr=   Úprintr§   rÿ   r   )r   ÚfakeFiler  r   r  r   Útest_printToStderrSetsIsErrorY  s   

z/FileObserverTests.test_printToStderrSetsIsErrorc                 C   sð   t jt j}}|  ¡  tƒ }t |¡}|  |j¡ t 	d¡ |  
d| ¡ ¡ |  t jt¡ |  t jjtj¡ t|ddƒ}|sBt  ¡ }|  t jj ¡ | ¡ ¡ |  t jt¡ |  t jjtj¡ t|ddƒ}|sjt  ¡ }|  t jj ¡ | ¡ ¡ dS )zj
        startLogging() installs FileLogObserver and overrides sys.stdout and
        sys.stderr.
        r†   ÚencodingN)r§   rþ   rÿ   r  r   r
   r  r>   ÚstoprE   r6   ru   r‰   r   r   ÚlevelÚNewLogLevelÚinfor¹   Úgetdefaultencodingr  ÚupperÚerror)r   Ú
origStdoutÚ
origStderrr	  r<   r  r   r   r   Útest_startLoggingk  s&   

z#FileObserverTests.test_startLoggingc                 C   sz   |   ¡  tƒ t_dd„ }t |¡ |  tj|¡ t tj¡}|  |j	¡ |  
tjt¡ tj}t tj¡}|  tj|¡ dS )z»
        There are some obscure error conditions that can occur when logging is
        started twice. See http://twistedmatrix.com/trac/ticket/3289 for more
        information.
        c                 S   s$   | d rt j | d  ¡ ¡ d S d S r}   )r§   Ú
__stdout__r   ÚgetTracebackr€   r   r   r   Ú	showError‘  s   ÿz;FileObserverTests.test_startLoggingTwice.<locals>.showErrorN)r  r   r§   rþ   r
   r=   r>   r?   r  r  r‰   r   ÚassertIs)r   r  r<   Ú
fakeStdoutr   r   r   Útest_startLoggingTwice…  s   
z(FileObserverTests.test_startLoggingTwicec                    sÂ   |   ¡  tƒ }G dd„ dƒ}t||t|tƒ |tƒd}|  td|¡ dt_tƒ }ddi}g ‰ ‡ fdd	„}||ƒ | 	|¡ tj
|d
d |  |j¡ |  ˆ g ¡ t d¡ | ¡ }|  d|¡ dS )z
        startLogging() overrides global C{warnings.showwarning} such that
        warnings go to Twisted log observers.
        c                   @   s   e Zd Zeƒ Zeƒ ZdS )zFFileObserverTests.test_startLoggingOverridesWarning.<locals>.SysModuleN)r   r   r   r5   rþ   rÿ   r   r   r   r   Ú	SysModule§  s    
r  )ÚlogBeginnerr   Nú	pre-startr_   c                    s   d|   ¡ v rˆ  | ¡ d S d S )Nr  )Úkeysr;   )Úx©Úreceivedr   r   ÚpreStartObserverÄ  s   ÿzMFileObserverTests.test_startLoggingOverridesWarning.<locals>.preStartObserverF)Ú	setStdoutzhello!zUserWarning: hello!)r  ÚNewLogPublisherr   r   r   rv   Úpatchr
   Ú_oldshowwarningr=   r  r>   r  r   Úwarnru   r6   )r   ÚnewPublisherr  ÚtempLogPublisherr	  Úevtr#  r{   r   r!  r   Ú!test_startLoggingOverridesWarningŸ  s,   ý

z3FileObserverTests.test_startLoggingOverridesWarningc                    sš   t ƒ }t |¡‰g ‰ ‡ ‡fdd„}t ¡ }| |¡ | d¡ |  tˆ ƒd¡ ˆ d }| ¡ }dj	ˆ 
|d ¡|d d	}|  | |¡|›d
|›¡ dS )zk
        FileLogObserver.emit() will add a timestamp and system prefix to its
        file output.
        c                    s   ˆ   | ¡ ˆ | ¡ d S r9   )r;   r¤   r^   ©Úeventsr£   r   r   r<   Ú  s   
z3FileObserverTests.test_emitPrefix.<locals>.observerr†   r*   r   z{time} [{system}] rD   rì   )rD   rì   z does not start with N)r   r
   r¢   r   r=   rE   r   rG   ru   r$   rÛ   r0   r1   )r   r{   r<   rp   r_   ÚresultÚprefixr   r-  r   Útest_emitPrefixÑ  s$   


þþz!FileObserverTests.test_emitPrefixc                 C   sZ   t ƒ }t |¡}t ¡ }| |j¡ | d¡ | ¡ }d}|  | 	|¡|›d|›¡ dS )zR
        FileLogObserver.emit() will append a newline to its file output.
        r†   zHello!
z does not end with N)
r   r
   r¢   r   r=   r¤   rE   ru   r0   Úendswith)r   r{   r£   rp   r/  Úsuffixr   r   r   Útest_emitNewlineñ  s   

þz"FileObserverTests.test_emitNewlineN)!r   r   r   r   rç   rá   rí   rú   ÚERROR_NO_FORMATÚERROR_UNFORMATTABLE_SYSTEMÚERROR_MESSAGE_LOSTrÈ   rÌ   rÐ   rÑ   rÝ   rß   râ   rè   rî   rñ   ró   rö   rù   rû   rý   r  r
  r  r  r,  r1  r4  r   r   r   r   r²   z  s>    D		+	2 r²   c                   @   r   )ÚPythonLoggingObserverTestsz5
    Test the bridge with python logging module.
    c                    sˆ   t  d¡‰ˆ ¡ ‰ ˆ t j¡ ˆj‡ ‡fdd„ƒ}tƒ \ˆ_ˆ_ˆ 	ˆj¡ ˆj‡‡fdd„ƒ}t
 ¡ ˆ_t
 ¡ ˆ_ˆj ˆjj¡ d S )Nrü   c                      s   ˆ  ˆ ¡ d S r9   )ÚsetLevelr   )ÚoriginalLevelÚ
rootLoggerr   r   ÚrestoreLevel  s   z6PythonLoggingObserverTests.setUp.<locals>.restoreLevelc                      s   ˆ   ˆj¡ ˆj ¡  d S r9   )ÚremoveHandlerÚhdlrÚcloser   )r;  r   r   r   ÚremoveLogger  s   z6PythonLoggingObserverTests.setUp.<locals>.removeLogger)ÚloggingÚ	getLoggerÚgetEffectiveLevelr9  ÚDEBUGr>   r   r>  r    Ú
addHandlerr
   r   r¡   ÚPythonLoggingObserverÚobsr=   r¤   )r   r<  r@  r   )r:  r;  r   r   rA     s   


z PythonLoggingObserverTests.setUpc                 C   s4   | j  d¡ |  d| j ¡ ¡ |  d| j ¡ ¡ dS )z<
        Test simple output, and default log level.
        r¬   s   Hello, world.s   INFON©r¡   rE   r6   r    ru   r@   r   r   r   Útest_singleString   s   z,PythonLoggingObserverTests.test_singleStringc                 C   sN   t  tdƒ¡}| jj|dd d}| j ¡ }|  | |¡d|›d|›¡ dS )z$
        Test error output.
        zThat is bad.T)r	   r#   s	   CRITICAL:zDoes not start with z: N)	r	   r/   Ú
ValueErrorr¡   rE   r    ru   r0   r1   )r   Úfr0  r{   r   r   r   Útest_errorString(  s   
þz+PythonLoggingObserverTests.test_errorStringc                 C   s(   | j jdddd |  d| j ¡ ¡ dS )z-
        Test logging with a format.
        z%(bar)s oo %(foo)sÚHelloÚworld)r$   Úbarr%   s   Hello oo worldNrH  r@   r   r   r   Útest_formatString5  s   z,PythonLoggingObserverTests.test_formatStringc                 C   sˆ   | j jdtjd |  d| j ¡ ¡ |  d| j ¡ ¡ | j dd¡ | j ¡  | j jdtj	d |  d| j ¡ ¡ |  d| j ¡ ¡ d	S )
zG
        Test the logLevel keyword for customizing level used.
        z	Spam egg.)ÚlogLevels	   Spam egg.s   ERRORr   zFoo bar.s   Foo bar.s   WARNINGN)
r¡   rE   rA  ÚERRORr6   r    ru   ÚseekÚtruncateÚWARNINGr@   r   r   r   Útest_customLevel<  s   
z+PythonLoggingObserverTests.test_customLevelc                 C   s&   | j jddd |  | j ¡ d¡ dS )zx
        Verify that an event dictionary which is not an error and has an empty
        message isn't recorded.
        rü   Fr'   ó    N)r¡   rE   r   r    ru   r@   r   r   r   rý   I  s   z0PythonLoggingObserverTests.test_strangeEventDictN)
r   r   r   r   rA   rI  rL  rP  rV  rý   r   r   r   r   r8    s    r8  c                   @   ó    e Zd ZdZdd„ Zdd„ ZdS )ÚPythonLoggingIntegrationTestsz4
    Test integration of python logging bridge.
    c                 C   s|   t j}t j}g }z.|jt _|jt _t  ¡ }| ¡  |  |d |j¡ | 	¡  |  t
|ƒd¡ W |t _|t _dS |t _|t _w )z~
        Test that start and stop methods of the observer actually register
        and unregister to the log system.
        r   N)r
   r=   r?   r;   ÚremoverF  Ústartr   r¤   r  rG   )r   ÚoldAddObserverÚoldRemoveObserverÚlrG  r   r   r   Útest_startStopObserverW  s   
ÿz4PythonLoggingIntegrationTests.test_startStopObserverc                 C   sb   G dd„ dt jƒ}|ƒ }g }t jj}z|jt j_| d¡ |  t|ƒd¡ W |t j_dS |t j_w )zº
        Test that we can inherit L{log.PythonLoggingObserver} and use super:
        that's basically a validation that L{log.PythonLoggingObserver} is
        new-style class.
        c                       s   e Zd Z‡ fdd„Z‡  ZS )zBPythonLoggingIntegrationTests.test_inheritance.<locals>.MyObserverc                    s   t ƒ  |¡ d S r9   )Úsuperr¤   )r   r   ©Ú	__class__r   r   r¤   s  ó   zGPythonLoggingIntegrationTests.test_inheritance.<locals>.MyObserver.emit)r   r   r   r¤   Ú__classcell__r   r   ra  r   Ú
MyObserverr  s    re  r%   r*   N)r
   rF  r¤   r;   r   rG   )r   re  rG  r^  ÚoldEmitr   r   r   Útest_inheritancek  s   

z.PythonLoggingIntegrationTests.test_inheritanceN)r   r   r   r   r_  rg  r   r   r   r   rY  R  s    rY  c                   @   rX  )ÚDefaultObserverTestsz$
    Test the default observer.
    c                 C   s   g | _ | j j| _t | j¡ |  tj| j¡ t ¡ }tƒ |_	| 
¡  |  |j¡ d}t tƒ |¡ |  ¡ }|  ||j	 ¡ ¡ |  t|ƒd¡ dS )zu
        The reason argument passed to log.err() appears in the report
        generated by DefaultObserver.
        zThe reason.r*   N)r:   r;   r<   r
   r=   r>   r?   ÚDefaultObserverr   rÿ   r[  r  rU   Ú	ExceptionrV   r6   ru   r   rG   )r   rG  Úreasonr‚   r   r   r   Útest_failureLogger†  s   
z'DefaultObserverTests.test_failureLoggerc                 C   s|   G dd„ dƒ}d|ƒ f}t |dd}t ¡ }tƒ }||_| |¡ |  | ¡  d¡¡ W d  ƒ dS 1 s7w   Y  dS )zŒ
        DefaultObserver.emit() does not raise when it observes an error event
        with a message that causes L{repr} to raise.
        c                   @   r“   )z?DefaultObserverTests.test_emitEventWithBrokenRepr.<locals>.Ouchr•   c                 S   s   t dd ƒS r]   r—   r@   r   r   r   r   £  ra   zHDefaultObserverTests.test_emitEventWithBrokenRepr.<locals>.Ouch.__repr__N)r   r   r   r˜   r   r   r   r   r   ÚOuch¢  rš   rm  r%   r*   r'   zfoo <Ouch instanceN)	r   r
   ri  r   rÿ   r¤   r0   ru   r1   )r   rm  r   r_   r<   r{   r   r   r   Útest_emitEventWithBrokenReprœ  s   

"ýz1DefaultObserverTests.test_emitEventWithBrokenReprN)r   r   r   r   rl  rn  r   r   r   r   rh    s    rh  c                   @   sX   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S )ÚStdioOnnaStickTestszF
    StdioOnnaStick should act like the normal sys.stdout object.
    c                 C   s   g | _ t | j j¡ d S r9   )Ú
resultLogsr
   r=   r;   r@   r   r   r   rA   µ  s   zStdioOnnaStickTests.setUpc                 C   s   t  | jj¡ d S r9   )r
   r?   rp  r;   r@   r   r   r   rª   ¹  s   zStdioOnnaStickTests.tearDownc                 C   s   dd„ | j D ƒS )Nc                 S   s   g | ]	}d   |d ¡‘qS )rü   r   )Újoin)Ú.0rN   r   r   r   Ú
<listcomp>½  s    z6StdioOnnaStickTests.getLogMessages.<locals>.<listcomp>)rp  r@   r   r   r   ÚgetLogMessages¼  rc  z"StdioOnnaStickTests.getLogMessagesc                 C   sF   t  ¡ }| d¡ |  |  ¡ dg¡ | d¡ |  |  ¡ ddg¡ dS )z
        Writing to a StdioOnnaStick instance results in Twisted log messages.

        Log messages are generated every time a '\n' is encountered.
        zHello there
This is a testzHello therez!
zThis is a test!N)r
   ÚStdioOnnaStickr   r   rt  ©r   Ústdior   r   r   Ú
test_write¿  s
   

zStdioOnnaStickTests.test_writec                 C   s>   t  ¡ }| d¡ |  | jd d ¡ |  | jd d ¡ dS )z{
        The log messages written by StdioOnnaStick have printed=1 keyword, and
        by default are not errors.
        zhello
r   r#   ÚprintedN)r
   ru  r   ÚassertFalserp  r0   rv  r   r   r   Útest_metadataË  s   
z!StdioOnnaStickTests.test_metadatac                 C   s.   t  ¡ }| ddg¡ |  |  ¡ ddg¡ dS )zT
        Writing lines to a StdioOnnaStick results in Twisted log messages.
        zlog 1zlog 2N)r
   ru  Ú
writelinesr   rt  rv  r   r   r   Útest_writeLinesÕ  s   z#StdioOnnaStickTests.test_writeLinesc                 C   sJ   t j}t ¡ t _|  tt d|¡ tddd tdƒ |  |  ¡ dg¡ dS )zW
        When StdioOnnaStick is set as sys.stdout, prints become log messages.
        rþ   ÚThisú )Úendz	is a testzThis is a testN)	r§   rþ   r
   ru  r>   r  r  r   rt  )r   Ú	oldStdoutr   r   r   Ú
test_printÝ  s   
zStdioOnnaStickTests.test_printc                 C   s.   t jdd}| d¡ |  | jd d ¡ dS )zR
        StdioOnnaStick created with isError=True log messages as errors.
        T)r#   zlog 1
r   r#   N)r
   ru  r   r0   rp  rv  r   r   r   Ú
test_errorè  s   
zStdioOnnaStickTests.test_errorc                 C   s~   d}t jdd}|  |jd¡ | |d ¡ | d| g¡ tj}|t_|  t	td|¡ t
|ƒ |  |  ¡ |d| |g¡ dS )zÔ
        StdioOnnaStick converts unicode prints to byte strings on Python 2, in
        order to be compatible with the normal stdout/stderr objects.

        On Python 3, the prints are left unmodified.
        r°   zutf-8)r  Ú
zAlso, rþ   N)r
   ru  r   r  r   r|  r§   rþ   r>   r  r  rt  )r   ÚunicodeStringrw  r  r   r   r   Útest_unicodeð  s   þz StdioOnnaStickTests.test_unicodeN)r   r   r   r   rA   rª   rt  rx  r{  r}  r‚  rƒ  r†  r   r   r   r   ro  °  s    
ro  )+r   rÙ   rA  r»   r§   rD   rv   Úior   r   Útwisted.loggerr   r   r   r  r   r%  Útwisted.logger.test.test_stdlibr   Útwisted.pythonr	   r
   Útwisted.python.logÚtwisted.trialr   ÚWarningr   ÚSynchronousTestCaser   r8   ÚlistrŒ   Úregisterr”   r›   rž   rŸ   r«   r²   r8  rY  rh  ro  r   r   r   r   Ú<module>   s@   B =
"   L//