o
    ¯bï!  ã                   @   s&  d 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eee" e#f fdd„Z$G dd„ dƒZ%G dd„ dej&ƒZ'deeef fdd„Z(G dd„ dej)ƒZ*dS )z+
Test cases for L{twisted.logger._format}.
é    N)Úgetsourcefile)ÚBytesIOÚTextIOWrapper)Ú	FormatterÚ	LogRecordÚStreamHandlerÚ	getLogger)ÚListÚOptionalÚTuple)ÚBrokenMethodImplementation)ÚverifyObject)Úcurrentframe)ÚFailure)Úunittesté   )ÚILogObserverÚLogEvent)ÚLogLevel)ÚSTDLibLogObserverÚreturnc                  C   s&   t dƒ} ttj| jd  ƒ| jd 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)Úcaller© r   úA/usr/lib/python3/dist-packages/twisted/logger/test/test_stdlib.pyÚnextLine   s   þr    c                   @   s2   e Zd ZdZd
dd„Zd
dd„Zdefdd	„ZdS )ÚStdlibLoggingContainerzF
    Continer for a test configuration of stdlib logging objects.
    r   Nc                 C   sZ   t dƒ| _| j ¡ | _| j tj¡ tƒ | _| j 	| j¡ t
ƒ \| _| _| j 	| j¡ d S )NÚ )r   Ú
rootLoggerÚgetEffectiveLevelÚoriginalLevelÚsetLevelÚ
py_loggingÚDEBUGÚBufferedHandlerÚbufferedHandlerÚ
addHandlerÚhandlerAndBytesIOÚstreamHandlerÚoutput©Úselfr   r   r   Ú__init__-   s   
zStdlibLoggingContainer.__init__c                 C   sB   | j  | j¡ | j  | j¡ | j  | j¡ | j ¡  | j ¡  dS )z#
        Close the logger.
        N)r#   r&   r%   ÚremoveHandlerr*   r-   Úcloser.   r/   r   r   r   r3   9   s
   
zStdlibLoggingContainer.closec                 C   s   | j  ¡  d¡S )zd
        Get the output to the underlying stream as text.

        @return: the output text
        úutf-8)r.   ÚgetvalueÚdecoder/   r   r   r   ÚoutputAsTextC   s   z#StdlibLoggingContainer.outputAsText©r   N)r   Ú
__module__Ú__qualname__Ú__doc__r1   r3   Ústrr7   r   r   r   r   r!   (   s
    


r!   c                   @   s–   e Zd ZdZddd„Zdefdd„Zdedee	e
 ef fd	d
„Zddd„Zddd„Zddd„Zddd„Zddd„Zddd„Zddd„Zddd„ZdS )ÚSTDLibLogObserverTestsz)
    Tests for L{STDLibLogObserver}.
    r   Nc              
   C   sH   t ƒ }ztt|ƒ W dS  ty# } z|  |¡ W Y d}~dS d}~ww )z=
        L{STDLibLogObserver} is an L{ILogObserver}.
        N)r   r   r   r   Úfail)r0   ÚobserverÚer   r   r   Útest_interfaceQ   s   €ÿz%STDLibLogObserverTests.test_interfacec                 C   s   t ƒ }|  |j¡ |S )zj
        Create a logging object we can use to test with.

        @return: a stdlib-style logger
        )r!   Ú
addCleanupr3   )r0   Úloggerr   r   r   Ú	py_logger[   s   z STDLibLogObserverTests.py_loggerÚeventsc                 G   s:   |   ¡ }ttjd d�}|D ]}||ƒ q|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)rD   r   ÚdefaultStackDepthr*   Úrecordsr7   )r0   rE   Úplr?   Úeventr   r   r   ÚlogEvente   s   	ÿý
zSTDLibLogObserverTests.logEventc                 C   s4   |   i ¡\}}|  t|ƒd¡ |  |d jd¡ dS )z
        Logger name.
        r   r   ÚtwistedN)rK   ÚassertEqualÚlenÚname©r0   rH   r.   r   r   r   Ú	test_name{   s   z STDLibLogObserverTests.test_namec           	      C   sÄ   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 }| ¡ D ]\}}i }|dur-||d< t|ƒ|d< | |¡ q| j|Ž \}}|  t|ƒt|ƒ¡ tt|ƒƒD ]}|  || j|| d ¡ qPdS )z
        Log levels.
        NÚ	log_levelÚ
py_levelno)r'   ÚINFOr   Údebugr(   ÚinfoÚwarnÚWARNINGÚerrorÚERRORÚcriticalÚCRITICALÚitemsÚintÚappendrK   rM   rN   ÚrangeÚlevelno)	r0   ÚlevelMappingrE   ÚlevelÚpyLevelrJ   rH   r.   Úir   r   r   Útest_levels„   s&   ú
ÿz"STDLibLogObserverTests.test_levelsc                 C   s`   t ƒ \}}|  i ¡\}}|  t|ƒd¡ |  |d j|¡ |  |d j|¡ |  |d j¡ dS )zb
        C{pathname}, C{lineno}, C{exc_info}, C{func} is set properly on
        records.
        r   r   N)r    rK   rM   rN   ÚpathnameÚlinenoÚassertIsNoneÚexc_info)r0   ÚfilenameÚlogLinerH   r.   r   r   r   Útest_callerInfo§   s   
z&STDLibLogObserverTests.test_callerInfoc                 C   sV   t ddd�}|  |¡\}}|  t|ƒd¡ |  t|d jƒd¡ |  |d jd¡ dS )	zL
        Basic formattable event passes the format along correctly.
        úHello, {who}!Údude©Ú
log_formatÚwhor   r   zHello, dude!r   N)ÚdictrK   rM   rN   r<   ÚmsgÚargs©r0   rJ   rH   r.   r   r   r   Útest_basicFormat¸   s
   z'STDLibLogObserverTests.test_basicFormatc                 C   sD   t ddd�}|  |¡\}}|  t|ƒd¡ |  | d¡t|ƒ¡ dS )z<
        Basic formattable event renders correctly.
        rn   ro   rp   r   z:Hello, dude!
N)rs   rK   rM   rN   Ú
assertTrueÚendswithÚreprrv   r   r   r   Útest_basicFormatRenderedÃ   s   z/STDLibLogObserverTests.test_basicFormatRenderedc                 C   s8   |   i ¡\}}|  t|ƒd¡ |  t|d jƒd¡ dS )z'
        Event with no format.
        r   r   r"   N)rK   rM   rN   r<   rt   rP   r   r   r   Útest_noFormatÍ   s   z$STDLibLogObserverTests.test_noFormatc                 C   s‚   ddd„}z|ƒ  W n t y   tƒ }Y nw tdd|d�}|  |¡\}}|  t|ƒd¡ |  d|¡ |  d	|¡ |  d
|¡ dS )zK
        An event with a failure logs the failure details as well.
        r   Nc                   S   ó   dd  d S ©Nr   r   r   r   r   r   r   Úfailing_funcÛ   ó   z9STDLibLogObserverTests.test_failure.<locals>.failing_funcúHi momÚme©rq   rr   Úlog_failurer   úin failing_funcÚZeroDivisionErrorr8   )r†   r   rs   rK   rM   rN   ÚassertIn©r0   r   ÚfailurerJ   rH   r.   r   r   r   Útest_failureÖ   s   


ÿz#STDLibLogObserverTests.test_failurec                 C   sŠ   ddd„}z|ƒ  W n t y   tƒ }| ¡  Y nw tdd|d�}|  |¡\}}|  t|ƒd¡ |  d|¡ |  d	|¡ |  d
|¡ dS )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.
        r   Nc                   S   r}   r~   r   r   r   r   r   r   ò   r€   z@STDLibLogObserverTests.test_cleanedFailure.<locals>.failing_funcr�   r‚   rƒ   r   r…   r†   r8   )r†   r   ÚcleanFailurers   rK   rM   rN   r‡   rˆ   r   r   r   Útest_cleanedFailureë   s   

þz*STDLibLogObserverTests.test_cleanedFailurer8   )r   r9   r:   r;   rA   r!   rD   r   r   r	   r   r<   rK   rQ   rf   rm   rw   r{   r|   rŠ   rŒ   r   r   r   r   r=   L   s    




	
#




	r=   c                  C   s<   t ƒ } tj}t| ddd�}t|ƒ}t|ƒ}| |¡ || fS )z—
    Construct a 2-tuple of C{(StreamHandler, BytesIO)} for testing interaction
    with the 'logging' module.

    @return: handler and io object
    r4   Ú
)ÚencodingÚnewline)r   r'   ÚBASIC_FORMATr   r   r   ÚsetFormatter)r.   ÚtemplateÚstreamÚ	formatterÚhandlerr   r   r   r,     s   
r,   c                   @   s,   e Zd ZdZd	dd„Zdeddfdd„ZdS )
r)   zN
    A L{py_logging.Handler} that remembers all logged records in a list.
    r   Nc                 C   s   t j | ¡ g | _dS )z5
        Initialize this L{BufferedHandler}.
        N)r'   ÚHandlerr1   rH   r/   r   r   r   r1     s   
zBufferedHandler.__init__Úrecordc                 C   s   | j  |¡ dS )z&
        Remember the record.
        N)rH   r_   )r0   r—   r   r   r   Úemit   s   zBufferedHandler.emitr8   )r   r9   r:   r;   r1   r   r˜   r   r   r   r   r)     s    
r)   )+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^   r    r!   ÚTestCaser=   r,   r–   r)   r   r   r   r   Ú<module>   s*   $ 9