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: //proc/self/root/lib/python3/dist-packages/twisted/logger/test/__pycache__/test_json.cpython-38.pyc
U

��W[�H�@s�dZddlmZmZddlmZmZddlmZddl	m
Z
ddlmZddl
mZmZdd	lmZdd
lmZddlmZddlmZdd
lmZmZmZmZmZddlm Z dd�Z!Gdd�de
�Z"Gdd�de
�Z#Gdd�de
�Z$dS)z$
Tests for L{twisted.logger._json}.
�)�StringIO�BytesIO)�verifyObject�BrokenMethodImplementation)�unicode)�TestCase)�Failure�)�ILogObserver�LogPublisher)�formatEvent)�LogLevel)�extractField)�globalLogPublisher)�eventAsJSON�
eventFromJSON�jsonFileLogObserver�eventsFromJSONLogFile�log)�LoggercCs"|�|t�|�|�d�d�|S)a�
    Assert a few things about the result of L{eventAsJSON}, then return it.

    @param testCase: The L{TestCase} with which to perform the assertions.
    @type testCase: L{TestCase}

    @param savedJSON: The result of L{eventAsJSON}.
    @type savedJSON: L{unicode} (we hope)

    @return: C{savedJSON}
    @rtype: L{unicode}

    @raise AssertionError: If any of the preconditions fail.
    �
r)�assertIsInstancer�assertEqual�count)ZtestCaseZ	savedJSON�r�?/usr/lib/python3/dist-packages/twisted/logger/test/test_json.py�savedJSONInvariantssrc@sheZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�ZdS)�
SaveLoadTestsz2
    Tests for loading and saving log events.
    cCst|t|��S)z�
        Serialize some an events, assert some things about it, and return the
        JSON.

        @param event: An event.
        @type event: L{dict}

        @return: JSON.
        )rr)�self�eventrrr�savedEventJSON8s
zSaveLoadTests.savedEventJSONcCs|�t|�i��i�dS)zX
        Saving and loading an empty dictionary results in an empty dictionary.
        N�rrr �rrrr�test_simpleSaveLoadEsz!SaveLoadTests.test_simpleSaveLoadcCs&|�t|�ddd���ddd��dS)a
        Saving and loading a dictionary with some simple values in it results
        in those same simple values in the output; according to JSON's rules,
        though, all dictionary keys must be L{unicode} and any non-L{unicode}
        keys will be converted.
        r	�4)��3��1r&Nr!r"rrr�
test_saveLoadLs�zSaveLoadTests.test_saveLoadcCs,|�t|�dt�d���dddid��dS)zx
        Saving and loading an object which cannot be represented in JSON will
        result in a placeholder.
        r	r'�
unpersistableTN�rrr �objectr"rrr�test_saveUnPersistableYs�z$SaveLoadTests.test_saveUnPersistablecCs,|�t|�dt�d���dddid��dS)zD
        Non-ASCII keys and values can be saved and loaded.
        u䌡)uሴr&r*TNr+r"rrr�test_saveNonASCIIds
��zSaveLoadTests.test_saveNonASCIIcCsZdd�}d|td��i}ttk	r.|�ddi�|�t|�|��d|td���d�i�dS)	zx
        Any L{bytes} objects will be saved as if they are latin-1 so they can
        be faithfully re-loaded.
        cSs$ttkrd�tt|��St|�SdS)N�)�bytes�str�join�map�chr��xrrr�asbytesusz-SaveLoadTests.test_saveBytes.<locals>.asbytesZhello�sskippedZokay�charmapN)�ranger0r1�updaterrr �decode)rr7�
inputEventrrr�test_saveBytesps�zSaveLoadTests.test_saveBytescCs@Gdd�dt�}d|d�d�}t|�|��}|�t|�d�dS)a

        Saving and loading an object which cannot be represented in JSON, but
        has a string representation which I{can} be saved as JSON, will result
        in the same string formatting; any extractable fields will retain their
        data types.
        c@seZdZdd�Zdd�ZdS)z@SaveLoadTests.test_saveUnPersistableThenFormat.<locals>.ReprablecSs
||_dS�N��value)rrArrr�__init__�szISaveLoadTests.test_saveUnPersistableThenFormat.<locals>.Reprable.__init__cSsdS)NZreprablerr"rrr�__repr__�szISaveLoadTests.test_saveUnPersistableThenFormat.<locals>.Reprable.__repr__N)�__name__�
__module__�__qualname__rBrCrrrr�Reprable�srGz{object} {object.value}���
log_formatr,z
reprable 7N)r,rr rr)rrGr=ZoutputEventrrr� test_saveUnPersistableThenFormat�s�z.SaveLoadTests.test_saveUnPersistableThenFormatcCsbGdd�dt�}td|�d�}t|�|��}|�td|�d�|�ttd|�|�ttd|�dS)	zs
        L{extractField} can extract fields from an object that's been saved and
        loaded from JSON.
        c@seZdZdd�ZdS)z8SaveLoadTests.test_extractingFieldsPostLoad.<locals>.ObjcSs
d|_dS)N�Yr@r"rrrrB�szASaveLoadTests.test_extractingFieldsPostLoad.<locals>.Obj.__init__N)rDrErFrBrrrr�Obj�srMz{object.value}rIzobject.valuerLr,N)r,�dictrr rr�assertRaises�KeyError)rrMr=�loadedEventrrr�test_extractingFieldsPostLoad�sz+SaveLoadTests.test_extractingFieldsPostLoadcCs�g}t|jd�}zddWn&tk
rBt�}|�d|�YnXddl}|��ddk	rd|��|�t	|�d�t
|�|d��d}|�|t�|�
|�t��|�|��t�dS)zl
        Round-tripping a failure through L{eventAsJSON} preserves its class and
        structure.
        ��observerr%rza message about failureN�log_failure)r�append�ZeroDivisionErrorr�failure�sys�exc_infoZ	exc_clearr�lenrr rZ
assertTrueZcheckZgetTracebackr1)r�eventsr�frYZloadedrrr�test_failureStructurePreserved�sz,SaveLoadTests.test_failureStructurePreservedcCs0ttjd�}t|�|��}|�|dtj�dS)zo
        It's important that the C{log_level} key remain a
        L{constantly.NamedConstant} object.
        ��	log_levelr`N)rNr
�warnrr ZassertIs)rr=rQrrr�test_saveLoadLevel�sz SaveLoadTests.test_saveLoadLevelcCstd�}|�|tdd��dS)z�
        If a saved bit of JSON (let's say, from a future version of Twisted)
        were to persist a different log_level, it will resolve as None.
        zZ{"log_level": {"name": "other", "__class_uuid__": "02E59486-F24D-46AD-8224-3ACDF2A5732A"}}Nr_)rrrN)rrQrrr�test_saveLoadUnknownLevel�s�z'SaveLoadTests.test_saveLoadUnknownLevelN)rDrErF�__doc__r r#r)r-r.r>rKrRr^rbrcrrrrr3s


rc@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�FileLogObserverTestsz+
    Tests for L{jsonFileLogObserver}.
    cCsZt��J}t|�}ztt|�Wn,tk
rJ}z|�|�W5d}~XYnXW5QRXdS)zh
        A L{FileLogObserver} returned by L{jsonFileLogObserver} is an
        L{ILogObserver}.
        N)rrrr
rZfail)r�
fileHandlerT�errr�test_interface�sz#FileLogObserverTests.test_interfacec	KsV|�dd�}t��:}t|f|�}tdd�}||�|�|��d�|��W5QRXdS)a�
        Asserts that an observer created by L{jsonFileLogObserver} with the
        given arguments writes events serialized as JSON text, using the given
        record separator.

        @param recordSeparator: A record separator.
        @type recordSeparator: L{unicode}

        @param kwargs: Keyword arguments to pass to L{jsonFileLogObserver}.
        @type kwargs: L{dict}
        �recordSeparator�r%r5z{0}{{"x": 1}}
N)�getrrrNr�getvalue�format)r�kwargsrirfrTrrrr�assertObserverWritesJSON�s
�z-FileLogObserverTests.assertObserverWritesJSONcCs|��dS)z�
        A L{FileLogObserver} created by L{jsonFileLogObserver} writes events
        serialzed as JSON text to a file when it observes events.
        By default, the record separator is C{u"\x1e"}.
        N�ror"rrr�(test_observeWritesDefaultRecordSeparator	sz=FileLogObserverTests.test_observeWritesDefaultRecordSeparatorcCs|jdd�dS)z�
        A L{FileLogObserver} created by L{jsonFileLogObserver} writes events
        serialzed as JSON text to a file when it observes events.
        This test sets the record separator to C{u""}.
        ��riNrpr"rrr�&test_observeWritesEmptyRecordSeparatorsz;FileLogObserverTests.test_observeWritesEmptyRecordSeparatorcs�t�}t�}g}|�|j�|�t|��t|d�}zddWn|�d�YnXt|���}tt	|��}�fdd�}||�||�dS)z�
        A L{FileLogObserver} created by L{jsonFileLogObserver} writes failures
        serialized as JSON text to a file when it observes events.
        rSr%rzfailed as expectedcs`��t|�d�|\}��d|�|d}��|t�|��}��|jjj�	d�t
�	d��dS)Nr%rU�co)rr[ZassertInrrZgetTracebackObject�tb_frame�f_code�co_filename�rstrip�__file__)Z	logEventsZfailureEventZ
failureObjectZtracebackObjectr"rr�checkEvents,s�z@FileLogObserverTests.test_failureFormatting.<locals>.checkEventsN)
rr�addObserverrVrrrXrl�listr)r�ioZ	publisherZloggedZlogger�readerZdeserializedr{rr"r�test_failureFormattings
z+FileLogObserverTests.test_failureFormattingN)	rDrErFrdrhrorqrtr�rrrrre�s
		rec@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd S)!�LogFileReaderTestsz-
    Tests for L{eventsFromJSONLogFile}.
    cs&g�_�fdd�}|�_t�|�dS)Ncs&|dtjkr"d|kr"�j�|�dS)NZ
log_namespace�record)�jsonLog�	namespace�errorEventsrV)rr"rrrTDs
��z*LogFileReaderTests.setUp.<locals>.observer)r��logObserverrr|)rrTrr"r�setUpAszLogFileReaderTests.setUpcCst�|j�dSr?)rZremoveObserverr�r"rrr�tearDownPszLogFileReaderTests.tearDowncKsFt|f|�}|�t|�ddi�|�t|�ddi�|�tt|�dS)a
        Test that L{eventsFromJSONLogFile} reads two pre-defined events from a
        file: C{{u"x": 1}} and C{{u"y": 2}}.

        @param fileHandle: The file to read from.

        @param kwargs: Keyword arguments to pass to L{eventsFromJSONLogFile}.
        r6r%�yr	N)rr�nextrO�
StopIteration)rrfrnr\rrr�_readEventsTs	zLogFileReaderTests._readEventsc	Cs4td��"}|�|�|�t|j�d�W5QRXdS)z�
        L{eventsFromJSONLogFile} reads events from a file and automatically
        detects use of C{u"\x1e"} as the record separator.
        �{"x": 1}
{"y": 2}
rN�rr�rr[r��rrfrrr�&test_readEventsAutoWithRecordSeparatords�
z9LogFileReaderTests.test_readEventsAutoWithRecordSeparatorc	Cs4td��"}|�|�|�t|j�d�W5QRXdS)z�
        L{eventsFromJSONLogFile} reads events from a file and automatically
        detects use of C{u""} as the record separator.
        z{"x": 1}
{"y": 2}
rNr�r�rrr�'test_readEventsAutoEmptyRecordSeparatorqs�
z:LogFileReaderTests.test_readEventsAutoEmptyRecordSeparatorc	Cs8td��&}|j|dd�|�t|j�d�W5QRXdS)z{
        L{eventsFromJSONLogFile} reads events from a file and is told to use
        a specific record separator.
        z{"x": 1}
{"y": 2}
�rsrNr�r�rrr�&test_readEventsExplicitRecordSeparator~s�z9LogFileReaderTests.test_readEventsExplicitRecordSeparatorc	Cs8td��&}|j|dd�|�t|j�d�W5QRXdS)zM
        L{eventsFromJSONLogFile} handles buffering a partial event.
        r�r%�Z
bufferSizerNr�r�rrr�test_readEventsPartialBuffer�s�z/LogFileReaderTests.test_readEventsPartialBufferc	Cs�td��n}t|�}|�t|�ddi�|�tt|�|�t|j�d�|�|jddd�|�|jddd	�W5QRXd
S)zF
        If the JSON text for a record is truncated, skip it.
        z{"x": 1{"y": 2}
r�r	r%rrJz0Unable to read truncated JSON record: {record!r}r�s{"x": 1N�rrrr�rOr�r[r��rrfr\rrr�test_readTruncated�s��z%LogFileReaderTests.test_readTruncatedc	CsTtd��B}t|�}|�t|�ddi�|�tt|�|�t|j�d�W5QRXdS)zg
        If the file being read from vends L{unicode}, strings decode from JSON
        as-is.
        u{"currency": "€"}
�currency�€rNr�r�rrr�test_readUnicode�s

z#LogFileReaderTests.test_readUnicodec	CsTtd��B}t|�}|�t|�ddi�|�tt|�|�t|j�d�W5QRXdS)zh
        If the file being read from vends L{bytes}, strings decode from JSON as
        UTF-8.
        s{"currency": "€"}
r�r�rN�rrrr�rOr�r[r�r�rrr�test_readUTF8Bytes�s

z%LogFileReaderTests.test_readUTF8Bytesc	CsXtd��F}t|dd�}|�t|�ddi�|�tt|�|�t|j�d�W5QRXdS)z�
        If the JSON text for a record is truncated in the middle of a two-byte
        Unicode codepoint, we don't want to see a codec exception and the
        stream is read properly when the additional data arrives.
        s{"x": "€"}
�r�r6r�rNr�r�rrr�test_readTruncatedUTF8Bytes�s

z.LogFileReaderTests.test_readTruncatedUTF8Bytesc	Cs�td��n}t|�}|�t|�ddi�|�tt|�|�t|j�d�|�|jddd�|�|jddd	�W5QRXd
S)zh
        If the JSON text for a record contains invalid UTF-8 text, ignore that
        record.
        s{"x": "�"}
{"y": 2}
r�r	r%rrJz2Unable to decode UTF-8 for JSON record: {record!r}r�s{"x": "�"}
Nr�r�rrr�test_readInvalidUTF8Bytes�s ���z,LogFileReaderTests.test_readInvalidUTF8Bytesc	Cs�td��n}t|�}|�t|�ddi�|�tt|�|�t|j�d�|�|jddd�|�|jddd	�W5QRXd
S)zD
        If the JSON text for a record is invalid, skip it.
        z{"x": }
{"y": 2}
r�r	r%rrJ�&Unable to read JSON record: {record!r}r�s{"x": }
Nr�r�rrr�test_readInvalidJSON�s��z'LogFileReaderTests.test_readInvalidJSONc	Csltd��Z}t|�}|�tt|�|�t|j�d�|�|jddd�|�|jddd�W5QRXdS)	zI
        Multiple events without a record separator are skipped.
        z{"x": 1}
{"y": 2}
r%rrJr�r�s{"x": 1}
{"y": 2}
N)rrrOr�r�rr[r�r�rrr�test_readUnseparated
s���z'LogFileReaderTests.test_readUnseparatedc	Csftdd�}t��L}t|�}||�|�d�t|�}|�t|�|f�|�t|j�d�W5QRXdS)z�
        Data written by a L{FileLogObserver} returned by L{jsonFileLogObserver}
        and read by L{eventsFromJSONLogFile} is reconstructed properly.
        r%r5rN)	rNrr�seekrr�tupler[r�)rrrfrTr\rrr�test_roundTrip%s

z!LogFileReaderTests.test_roundTripN)rDrErFrdr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr�<s 


r�N)%rdr~rrZzope.interface.verifyrrZtwisted.python.compatrZtwisted.trial.unittestrZtwisted.python.failurerZ	_observerr
rZ_formatrZ_levelsr
Z_flattenrZ_globalrZ_jsonrrrrrr�Z_loggerrrrrer�rrrr�<module>s"-]