HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/twisted/logger/test/__pycache__/test_global.cpython-38.pyc
U

�`�[/�@s�dZddlmZddlZddlmZddlmZddlm	Z	ddl
mZdd	lm
Z
dd
lmZddlmZddlmZdd
lmZdd�ZGdd�dej�ZdS)z+
Test cases for L{twisted.logger._global}.
�)�print_functionN)�unittest�)�textFileLogObserver)�LogPublisher)�Logger)�LogBeginner)�MORE_THAN_ONCE_WARNING)�LogLevel)�nextLine)�Failurecsjt|�t|�kr|�||�t��|D]}�t|���O�q&�fdd���fdd�|D�}|�||�dS)a�
    Compare two sequences of log events, examining only the the keys which are
    present in both.

    @param test: a test case doing the comparison
    @type test: L{unittest.TestCase}

    @param actualEvents: A list of log events that were emitted by a logger.
    @type actualEvents: L{list} of L{dict}

    @param expectedEvents: A list of log events that were expected by a test.
    @type expected: L{list} of L{dict}
    cs,|��}|��D]}|�kr|�|�q|S�N)�copy�keys�pop)�eventr�key)�
allMergedKeys��A/usr/lib/python3/dist-packages/twisted/logger/test/test_global.py�simplify.s
zcompareEvents.<locals>.simplifycsg|]}�|��qSrr��.0r)rrr�
<listcomp>5sz!compareEvents.<locals>.<listcomp>N)�len�assertEqual�setr)ZtestZactualEventsZexpectedEventsrZsimplifiedActualr)rrr�
compareEventssrc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�ZdS)�LogBeginnerTestsz#
    Tests for L{LogBeginner}.
    cCs^t�|_t��|_Gdd�dt�}Gdd�dt�}|�|_|�|_t|j|j|j|j�|_	dS)Nc@seZdZe�Ze�ZdS)z&LogBeginnerTests.setUp.<locals>.NotSysN)�__name__�
__module__�__qualname__�object�stdout�stderrrrrr�NotSysCsr%c@seZdZdd�Zddd�ZdS)z+LogBeginnerTests.setUp.<locals>.NotWarningscSs
g|_dSr
)�warnings)�selfrrr�__init__Hsz4LogBeginnerTests.setUp.<locals>.NotWarnings.__init__NcSs|j�||||||f�dS)a�
                Emulate warnings.showwarning.

                @param message: A warning message to emit.
                @type message: L{str}

                @param category: A warning category to associate with
                    C{message}.
                @type category: L{warnings.Warning}

                @param filename: A file name for the source code file issuing
                    the warning.
                @type warning: L{str}

                @param lineno: A line number in the source file where the
                    warning was issued.
                @type lineno: L{int}

                @param file: A file to write the warning message to.  If
                    L{None}, write to L{sys.stderr}.
                @type file: file-like object

                @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}.
                @type line: L{str}
                N)r&�append)r'�message�category�filename�lineno�file�linerrr�showwarningKs�z7LogBeginnerTests.setUp.<locals>.NotWarnings.showwarning)NN)rr r!r(r0rrrr�NotWarningsGs�r1)
r�	publisher�io�StringIO�errorStreamr"�	sysModule�warningsModuler�beginner)r'r%r1rrr�setUp?s
'�zLogBeginnerTests.setUpcsftddd�}g�g��fdd�}�fdd�}|j�||f�|�|�|�|g��|�|g��dS)z?
        Test that C{beginLoggingTo()} adds observers.
        �r�ZfooZbarcs
��|�Sr
�r)��e��events1rr�<lambda>�zBLogBeginnerTests.test_beginLoggingToAddObservers.<locals>.<lambda>cs
��|�Sr
r<r=��events2rrrA�rBN)�dictr8�beginLoggingTor2r�r'rZo1Zo2r�r@rDr�test_beginLoggingToAddObserversvs
z0LogBeginnerTests.test_beginLoggingToAddObserverscsftddd�}g�g��fdd�}�fdd�}|�|�|j�||f�|�|g��|�|g��dS)z\
        Test that events are buffered until C{beginLoggingTo()} is
        called.
        r:rr;cs
��|�Sr
r<r=r?rrrA�rBzDLogBeginnerTests.test_beginLoggingToBufferedEvents.<locals>.<lambda>cs
��|�Sr
r<r=rCrrrA�rBN)rEr2r8rFrrGrrHr�!test_beginLoggingToBufferedEvents�s
z2LogBeginnerTests.test_beginLoggingToBufferedEventscCs`t|d�D]}|�t|d��qg}|�|jg�|�ttd|d��tdd�|D���dS)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.
        @type limit: L{int}

        @param beginner: The L{LogBeginner} against which to verify.
        @type beginner: L{LogBeginner}

        @raise: C{self.failureException} if the wrong events are replayed by
            C{beginner}.

        @return: L{None}
        r:)�countcss|]}|dVqdS)rKNrrrrr�	<genexpr>�sz4LogBeginnerTests._bufferLimitTest.<locals>.<genexpr>N)�ranger2rErFr)r�list)r'�limitr8rKZeventsrrr�_bufferLimitTest�s�z!LogBeginnerTests._bufferLimitTestcCstj}|�||j�dS)z�
        Up to C{LogBeginner._DEFAULT_BUFFER_SIZE} log events are buffered for
        replay by L{LogBeginner.beginLoggingTo}.
        N)rZ_DEFAULT_BUFFER_SIZErPr8)r'rOrrr�test_defaultBufferLimit�sz(LogBeginnerTests.test_defaultBufferLimitcCs.d}t|j|j|j|j|d�}|�||�dS)z�
        The size of the L{LogBeginner} event buffer can be overridden with the
        C{initialBufferSize} initilizer argument.
        �)ZinitialBufferSizeN)rr2r5r6r7rP)r'rOr8rrr�test_overrideBufferLimit�s�z)LogBeginnerTests.test_overrideBufferLimitc	Csg}g}t��}t|�}|�tdd��t�\}}|j�|j|g�|�tdd��t�\}}|j�|j|g�|�tdd��tt	t
j||||d�}	t||tdd�tdd�|	tdd�g�t|||	tdd�g�|�
�}
|�d�||�|
�|�d�||�|
�dS)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.
        Z	prebuffer)rZ
postbufferZpostwarn)Z
log_formatZ	log_levelZfileNowZlineNowZfileThenZlineThenz	<{0}:{1}>N)r3r4rr2rErr8rFr)r	r
�warnr�getvalue�assertIn�format)r'r@rDZ
fileHandleZtextObserverZ
firstFilenameZ	firstLineZsecondFilenameZ
secondLine�warning�outputrrr�test_beginLoggingToTwice�sH

���	��z)LogBeginnerTests.test_beginLoggingToTwicecCs:t|jd�}|�d�|jddd�|�|j��d�dS)zP
        Critical messages will be written as text to the error stream.
        �Zobserverzignore thisza critical {message}r*)r*za critical message
N)rr2�info�criticalrr5rU�r'�logrrr�test_criticalLogging�s
z%LogBeginnerTests.test_criticalLoggingcCs8t|jd�}|j�d�|�d�|�|j��d�dS)z�
        Once logging has begun with C{beginLoggingTo}, critical messages are no
        longer written to the output stream.
        r[rzanother critical message�N)rr2r8rFr]rr5rUr^rrr�test_criticalLoggingStopss
z*LogBeginnerTests.test_criticalLoggingStopscCsng}|j�|jg�td|jjd�t||tddd�g�|dd�=td|jjd�t||tddd�g�dS)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.�r.r#)Z
log_namespace�log_ioNz
Error, world.r$)	r8rFr)�printr6r#rrEr$)r'�xrrr�%test_beginLoggingToRedirectStandardIO
s�
�z6LogBeginnerTests.test_beginLoggingToRedirectStandardIOcCsD|jj}|jj}|jjddd�|�|jj|�|�|jj|�dS)z�
        L{LogBeginner.beginLoggingTo} will leave the existing stdout/stderr in
        place if it has been told not to replace them.
        rF)ZredirectStandardION)r6r#r$r8rFZassertIs)r'ZoldOutZoldErrrrr�test_beginLoggingToDontRedirect s
z0LogBeginnerTests.test_beginLoggingToDontRedirectcCs�t�t��d�}t�t��d�}||j_||j_g}|j�|jg�|�	|jjj
d�|�	|jjj
d�|jj�d�|jj�d�t||t
dd�t
dd�g�dS)	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李)rdu瑩N)r3�
TextIOWrapper�BytesIOr6r#r$r8rFr)r�encoding�writerrE)r'ZweirdZweirderrrfrrr�$test_beginLoggingToPreservesEncoding,s�z5LogBeginnerTests.test_beginLoggingToPreservesEncodingc
Cs�|j�dttd�g}|j�|jg�|j�dttd�t��}|jjdttd|d�|�	|jj
dttdddfdttd|dfg�t||tdtj
d	tjtdd
�g�dS)z�
        L{LogBeginner.beginLoggingTo} will redirect the warnings of its
        warnings module into the logging system.
        z	a messager:zanother messagerzyet anotherrRrcN�.)rXr+r,r-)r7r0�DeprecationWarning�__file__r8rFr)r3r4rr&rrEr r)r'rf�frrr�test_warningsModuleEsP��������z$LogBeginnerTests.test_warningsModulecCsXttd��}t|jd�}|jd|d�|j��}|�d|�|�d|�|�d|�dS)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�	Exceptionrr2rtr5rUrV)r'rrr_�msgrrr�test_failuresAppendTracebacksjs
z.LogBeginnerTests.test_failuresAppendTracebacksN)rr r!�__doc__r9rIrJrPrQrSrZr`rbrgrhrnrsrxrrrrr:s7	
*
%r)ryZ
__future__rr3Z
twisted.trialrZ_filerZ	_observerrZ_loggerrZ_globalrr	Z_levelsr
Ztest.test_stdlibrZtwisted.python.failurerrZTestCaserrrrr�<module>s!