
    Vh!"                    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    O/home/dcms/DCMS/lib/python3.12/site-packages/twisted/logger/test/test_stdlib.pynextLiner!      s?     !_Fckk&"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*;*='DK""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) 	IIaLL	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UO	 !!))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#   
 *//NNJ,,MM:??MM:--NNJ,,z22
 *002 	!NE7E  %*k" #&g,E,MM% 	! ($--0Ws<'89 s7|$ 	JAWQZ//<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)WQZ^^,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)WQZ^^,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 ! 	 iG	 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 ! 	#iG  "	#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     YF&&H6GTBF(#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 !6Hu3X.. u3p $j(( $r"   