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_legacy.cpython-38.pyc
U


W[;8�@s�dZddlmZddlZddlmZmZddlmZddl	m
Z
ddl	mZddl
mZd	d
lmZd	dlmZd	dlmZd	d
lmZd	dlmZGdd�dej�ZGdd�dej�ZdS)z+
Test cases for L{twisted.logger._legacy}.
�)�timeN)�verifyObject�BrokenMethodImplementation)�unittest)�context)�log)�Failure�)�LogLevel)�ILogObserver)�formatEvent)�LegacyLogObserverWrapper)�publishToNewObserverc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0S)1�LegacyLogObserverWrapperTestsz0
    Tests for L{LegacyLogObserverWrapper}.
    c
CsPdd�}t|�}ztt|�Wn,tk
rJ}z|�|�W5d}~XYnXdS)zD
        L{LegacyLogObserverWrapper} is an L{ILogObserver}.
        cSsdS�N���err�A/usr/lib/python3/dist-packages/twisted/logger/test/test_legacy.py�<lambda>$�z>LegacyLogObserverWrapperTests.test_interface.<locals>.<lambda>N)r
rrrZfail)�self�legacyObserver�observerrrrr�test_interface sz,LegacyLogObserverWrapperTests.test_interfacecCs.Gdd�dt�}t|��}|�t|�d�dS)zJ
        L{LegacyLogObserverWrapper} returns the expected string.
        c@seZdZdd�Zdd�ZdS)z?LegacyLogObserverWrapperTests.test_repr.<locals>.LegacyObservercSsdS)Nz<Legacy Observer>r�rrrr�__repr__1szHLegacyLogObserverWrapperTests.test_repr.<locals>.LegacyObserver.__repr__cSsdSrrrrrr�__call__4szHLegacyLogObserverWrapperTests.test_repr.<locals>.LegacyObserver.__call__N)�__name__�
__module__�__qualname__rrrrrr�LegacyObserver0sr!z+LegacyLogObserverWrapper(<Legacy Observer>)N)�objectr
�assertEqual�repr)rr!rrrr�	test_repr,s
�z'LegacyLogObserverWrapperTests.test_reprcs8g��fdd�}t|�}||�|�t��d��dS)z�
        Send an event to a wrapped legacy observer and capture the event as
        seen by that observer.

        @param event: an event
        @type event: L{dict}

        @return: the event as observed by the legacy wrapper
        cs
��|�Sr)�appendr��eventsrrrKrz7LegacyLogObserverWrapperTests.observe.<locals>.<lambda>�r)r
r#�len)r�eventrrrr'r�observe?s
z%LegacyLogObserverWrapperTests.observecCsX|�dt��|�dd�|�dtj�|�t|��}|��D]\}}|�||�q>|S)z�
        Send an event to a wrapped legacy observer and verify that its data is
        preserved.

        @param event: an event
        @type event: L{dict}

        @return: the event as observed by the legacy wrapper
        �log_time�
log_system�-�	log_level)�
setdefaultrr
�infor,�dict�itemsZassertIn)rr+�observed�key�valuerrr�forwardAndVerifySsz.LegacyLogObserverWrapperTests.forwardAndVerifycCs|�tddd��dS)ze
        Basic forwarding: event keys as observed by a legacy observer are the
        same.
        r)r	��foo�barN)r8r3rrrr�test_forwardlsz*LegacyLogObserverWrapperTests.test_forwardcCs*t�}|�t|d��}|�|d|�dS)zc
        The new-style C{"log_time"} key is copied to the old-style C{"time"}
        key.
        )r-rN�rr8r3r#�rZstampr+rrr�	test_timetsz'LegacyLogObserverWrapperTests.test_timecCs0t�}|�t|d|d��}|�|d|�dS)zr
        The new-style C{"log_time"} key does not step on a pre-existing
        old-style C{"time"} key.
        r))r-rrNr=r>rrr�test_timeAlreadySet~sz1LegacyLogObserverWrapperTests.test_timeAlreadySetcCs$|�tdd��}|�|dd�dS)zg
        The new-style C{"log_system"} key is copied to the old-style
        C{"system"} key.
        r:)r.�systemN�r8r3r#�rr+rrr�test_system�sz)LegacyLogObserverWrapperTests.test_systemcCs&|�tddd��}|�|dd�dS)zv
        The new-style C{"log_system"} key does not step on a pre-existing
        old-style C{"system"} key.
        r:r;)r.rArANrBrCrrr�test_systemAlreadySet�sz3LegacyLogObserverWrapperTests.test_systemAlreadySetcCs2tt�tjd�}|�t|��}|�|dd�dS)zy
        If the new-style C{"log_system"} key is absent, the old-style
        C{"system"} key is set to C{"-"}.
        )r-r0rAr/N)r3rr
r2r,r#)rr+r5rrr�
test_noSystem�sz+LegacyLogObserverWrapperTests.test_noSystemcCsT|�ttjdd��|�ttjdd��|�ttjdd��|�ttjdd��dS)z�
        If explicitly set, the C{isError} key will be preserved when forwarding
        from a new-style logging emitter to a legacy logging observer,
        regardless of log level.
        r))r0�isErrorrN)r8r3r
r2�warn�error�criticalrrrr�test_levelNotChange�sz1LegacyLogObserverWrapperTests.test_levelNotChangecCs"|�ttjd��}|�d|�dS)a)
        The new-style C{"log_level"} key is not translated to the old-style
        C{"logLevel"} key.

        Events are forwarded from the old module from to new module and are
        then seen by old-style observers.
        We don't want to add unexpected keys to old-style events.
        )r0�logLevelN)r8r3r
r2�assertNotInrCrrr�test_pythonLogLevelNotSet�s	z7LegacyLogObserverWrapperTests.test_pythonLogLevelNotSetcCs$|�tdd��}|�|dd�dS)a
        If a stdlib log level was provided as a string (eg. C{"WARNING"}) in
        the legacy "logLevel" key, it does not get converted to a number.
        The documentation suggested that numerical values should be used but
        this was not a requirement.
        �WARNING�rLrLNrBrCrrr�test_stringPythonLogLevel�s�z7LegacyLogObserverWrapperTests.test_stringPythonLogLevelcCs |�t��}|�|dd�dS)z�
        The old-style C{"message"} key is added, even if no new-style
        C{"log_format"} is given, as it is required, but may be empty.
        �messagerNrBrCrrr�test_message�sz*LegacyLogObserverWrapperTests.test_messagecCs$|�tdd��}|�|dd�dS)zV
        The old-style C{"message"} key is not modified if it already exists.
        r9)rRrRNrBrCrrr�test_messageAlreadySet�sz4LegacyLogObserverWrapperTests.test_messageAlreadySetcCs(|�tddd��}|�t�|�d�dS)z�
        Formatting is translated such that text is rendered correctly, even
        though old-style logging doesn't use PEP 3101 formatting.
        z
Hello, {who}!�world)�
log_formatZwho�
Hello, world!N�r8r3r#�	legacyLog�textFromEventDictrCrrr�test_format�s
��z)LegacyLogObserverWrapperTests.test_formatcCs(|�tddd��}|�t�|�d�dS)zo
        Using the message key, which is special in old-style, works for
        new-style formatting.
        zHello, {message}!rU)rVrRrWNrXrCrrr�test_formatMessage�s
��z0LegacyLogObserverWrapperTests.test_formatMessagecCs(|�tddd��}|�t�|�d�dS)zd
        Formatting is not altered if the old-style C{"format"} key already
        exists.
        zHello!zHowdy!)rV�formatNrXrCrrr�test_formatAlreadySet�s
�z3LegacyLogObserverWrapperTests.test_formatAlreadySetcKs&ttd��}|�tf|dd�|���S)z�
        Create a new-style event with a captured failure.

        @param values: Additional values to include in the event.
        @type values: L{dict}

        @return: the new event
        @rtype: L{dict}
        znyargh!�	oopsie...)�log_failurerV)r�RuntimeErrorr8r3)r�values�failurerrr�eventWithFailures
��z.LegacyLogObserverWrapperTests.eventWithFailurecCs>|��}|�|d|d�|�|d�|�|dd�dS)z}
        Captured failures in the new style set the old-style C{"failure"},
        C{"isError"}, and C{"why"} keys.
        rcr`rG�whyr_N)rd�assertIsZ
assertTruer#rCrrr�test_failuresz*LegacyLogObserverWrapperTests.test_failurecCs,ttd��}|j|d�}|�|d|�dS)�v
        Captured failures in the new style do not step on a pre-existing
        old-style C{"failure"} key.
        zWeak salsa!)rcrcN)rrardrf)rrcr+rrr�test_failureAlreadySet sz4LegacyLogObserverWrapperTests.test_failureAlreadySetcCs |jdd�}|�|dd�dS)zv
        Captured failures in the new style do not step on a pre-existing
        old-style C{"isError"} key.
        r�rGrGN�rdr#rCrrr�test_isErrorAlreadySet*sz4LegacyLogObserverWrapperTests.test_isErrorAlreadySetcCs |jdd�}|�|dd�dS)rhZblah)rereNrkrCrrr�test_whyAlreadySet3sz0LegacyLogObserverWrapperTests.test_whyAlreadySetN)rrr �__doc__rr%r,r8r<r?r@rDrErFrKrNrQrSrTr[r\r^rdrgrirlrmrrrrrs0

		

	
	rc@speZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�ZdS)�PublishToNewObserverTestsz,
    Tests for L{publishToNewObserver}.
    cCsg|_|jj|_dSr)r(r&rrrrr�setUpBszPublishToNewObserverTests.setUpcOsDt�tj�pi��}|�|�||d<t�|d<d|kr@d|d<|S)aX
        Return a basic old-style event as would be created by L{legacyLog.msg}.

        @param message: a message event value in the legacy event format
        @type message: L{tuple} of L{bytes}

        @param values: additional event values in the legacy event format
        @type event: L{dict}

        @return: a legacy event
        rRrrGr)r�getrYZILogContext�copy�updater)rrRrbr+rrr�legacyEventGs

z%PublishToNewObserverTests.legacyEventcCs*t|j|��tj�|�t|j�d�dS)z6
        The observer is called exactly once.
        r)N)rrrtrYrZr#r*r(rrrr�
test_observed\s�z'PublishToNewObserverTests.test_observedcCs8t|j|��tj�|�|jdd|jdd�dS)zc
        The old-style C{"time"} key is copied to the new-style C{"log_time"}
        key.
        rr-rN�rrrtrYrZr#r(rrrrr?fs��z#PublishToNewObserverTests.test_timecCsDdd�}|�dd�}||�}t|j||�|�t|jd�|�dS)z�
        A published old-style event should format as text in the same way as
        the given C{textFromEventDict} callable would format it.
        cSsd�td�|d���S)N�� rR)�join�reversed)r+rrrrZxszAPublishToNewObserverTests.test_message.<locals>.textFromEventDictzHello,zworld!rN)rtrrr#rr()rrZr+�textrrrrSss
z&PublishToNewObserverTests.test_messagecCs0t|j|��tj�|�|jddtj�dS)zL
        Published event should have log level of L{LogLevel.info}.
        rr0N)	rrrtrYrZr#r(r
r2rrrr�test_defaultLogLevel�s�z.PublishToNewObserverTests.test_defaultLogLevelcCs4t|j|jdd�tj�|�|jddtj�dS)z�
        If C{"isError"} is set to C{1} (true) on the legacy event, the
        C{"log_level"} key should get set to L{LogLevel.critical}.
        r)rjrr0N)	rrrtrYrZr#r(r
rJrrrr�test_isError�s
�z&PublishToNewObserverTests.test_isErrorcCs6t|j|jtjd�tj�|�|jddt	j
�dS)z�
        If the old-style C{"logLevel"} key is set to a standard library logging
        level, using a predefined (L{int}) constant, the new-style
        C{"log_level"} key should get set to the corresponding log level.
        rPrr0N)rrrt�
py_loggingrOrYrZr#r(r
rHrrrr�test_stdlibLogLevel�s�z-PublishToNewObserverTests.test_stdlibLogLevelcCs4t|j|jdd�tj�|�|jddtj�dS)z�
        If the old-style C{"logLevel"} key is set to a standard library logging
        level, using a string value, the new-style C{"log_level"} key should
        get set to the corresponding log level.
        rOrPrr0N)	rrrtrYrZr#r(r
rHrrrr�test_stdlibLogLevelWithString�s
�z7PublishToNewObserverTests.test_stdlibLogLevelWithStringcCs.t|j|jdd�tj�|�d|jd�dS)z�
        If the old-style C{"logLevel"} key is set to a standard library logging
        level, using an unknown value, the new-style C{"log_level"} key should
        not get set.
        zFoo!!!!!rPr0rN)rrrtrYrZrMr(rrrr�test_stdlibLogLevelWithGarbage�s
�z8PublishToNewObserverTests.test_stdlibLogLevelWithGarbagecCs.t|j|��tj�|�|jddd�dS)z�
        Published event should have a namespace of C{"log_legacy"} to indicate
        that it was forwarded from legacy logging.
        rZ
log_namespaceZ
log_legacyNrvrrrr�test_defaultNamespace�s�z/PublishToNewObserverTests.test_defaultNamespacecCs8t|j|��tj�|�|jdd|jdd�dS)zg
        The old-style C{"system"} key is copied to the new-style
        C{"log_system"} key.
        rr.rANrvrrrrrD�s��z%PublishToNewObserverTests.test_systemN)rrr rnrprtrur?rSr|r}rr�r�r�rDrrrrro=s



ro)rnrZloggingr~Zzope.interface.verifyrrZ
twisted.trialrZtwisted.pythonrrrYZtwisted.python.failurerZ_levelsr
Z	_observerrZ_formatrZ_legacyr
rZTestCaserrorrrr�<module>s $