Ϫf{YdZddlmZmZmZmZ ddlmZeddl m Z ddl m Z m Z mZddlmZddlmZd d lmZmZmZmZmZmZd d lmZd d lmZGd dej<ZGddej<Z Gddej<Z!Gddej<Z"GddZ#Gdde$Z%Gddej<Z&y#e$rdZYwxYw)z+ Test cases for L{twisted.logger._format}. )AnyStrDictOptionalcast)tzsetN)Failure) addTZCleanupmktimesetTZ)unittest)SkipTest) eventAsText formatEventformatEventAsClassicLogText formatTimeformatUnformattableEventformatWithCall)LogEvent)LogLevelcleZdZdZdededefdZddZddZ dd Z dd Z dd Z dd Z dd ZddZddZy)FormattingTestsz5 Tests for basic event formatting functions. logFormateventreturnc d||d<t|}|jt|t|S)z Create a Twisted log event dictionary from C{event} with the given C{logFormat} format string, format it with L{formatEvent}, ensure that its type is L{str}, and return its result. log_format)rassertIstypestr)selfrrresults A/usr/lib/python3/dist-packages/twisted/logger/test/test_format.pyformatzFormattingTests.format's/ (lU# d6lC( Nc`|jd|jd|jd|jd|jd|jdd|jd|jddd |jd |jd |jd |jd|jdd}|jd|tt d}|jd|zdz|jddy)ac L{formatEvent} will format an event according to several rules: - A string with no formatting instructions will be passed straight through. - PEP 3101 strings will be formatted using the keys and values of the event as named fields. - PEP 3101 keys ending with C{()} will be treated as instructions to call that key (which ought to be a callable) before formatting. L{formatEvent} will always return L{str}, and if given bytes, will always treat its format string as UTF-8 encoded. r%abcz{x})xzno, yes.z{not_called}, {called()}.nocy)Nyesr-r%r#z2FormattingTests.test_formatEvent..Ir%) not_calledcalleduSánchezsSánchezUnable to format eventsSnchezs S{a!s}nchez)az Sb'\xe1'nchezSnchezs S{a!r}nchezN) assertEqualr$assertInr repr)r! maybeResultxe1s r#test_formatEventz FormattingTests.test_formatEvent2s" T[[-. T[[_-  EU ;<   KK+]   t{{3D'EF . M0JKkk.Gk<  & 4$w-  cCi')4;;~;+QRr%chGdd}|jd|jd|y)zq L{formatEvent} will format PEP 3101 keys containing C{.}s ending with C{()} as methods. ceZdZdefdZy)0FormattingTests.test_formatMethod..Worldrcy)Nworldr-r!s r#wherez6FormattingTests.test_formatMethod..World.where[sr%N)__name__ __module__ __qualname__r rCr-r%r#Worldr?Zs s r%rGz hello worldzhello {what.where()})whatN)r7r$)r!rGs r#test_formatMethodz!FormattingTests.test_formatMethodTs1     4;;'=EG;L r%crGddt}|jd|jd|y)zS L{formatEvent} will format subscripts of attributes per PEP 3101. c6eZdZUeddZeeefed<y)>FormattingTests.test_formatAttributeSubscript..Examplebarqux)foobazconfigN)rDrErFdictrQrr __annotations__r-r%r#ExamplerLgs%)e%?FDcN ?r%rTzbar quxz+{example.config[foo]} {example.config[baz]})exampleN)objectr7r$)r!rTs r#test_formatAttributeSubscriptz-FormattingTests.test_formatAttributeSubscriptbs<  @f @   KK=    r%cXtdd}t|}|jd|y)z5 Formatting an event with no format. r)rOrMr'N)rRrr7r!rr"s r#test_formatEventNoFormatz(FormattingTests.test_formatEventNoFormatrs*"U# V$r%cttdd}t|}|jd||jt ||y)z: Formatting an event with a bogus format. rYr)rrOrMzLog format must be strN)rRrVrr8r9rZs r#test_formatEventWeirdFormatz+FormattingTests.test_formatEventWeirdFormat{s?aQ7U# .7 d5k6*r%ctdd}t|}|jd||jt||y)zF Formatting an event that's just plain out to get us. {evil()}c ddz SNrYrr-r-r%r#r.z?FormattingTests.test_formatUnformattableEvent..s Qr%revilr2N)rRrr8r9rZs r#test_formatUnformattableEventz-FormattingTests.test_formatUnformattableEvents; ?U# .7 d5k6*r%c ddddtttdi}t|}|j d||j d||j d|y ) zR Formatting an unformattable event that has an unformattable key. rr_rcc ddz Srar-r-r%r#r.zSFormattingTests.test_formatUnformattableEventWithUnformattableKey.. AEr%gurk*MESSAGE LOST: unformattable object logged:Recoverable data:Exception during formatting:N)rr Unformattablerr8rZs r#1test_formatUnformattableEventWithUnformattableKeyzAFormattingTests.test_formatUnformattableEventWithUnformattableKeys\ * M mo &  U# BFK )62 4f=r%ctddt}t|}|jd||jd||jd|y)T Formatting an unformattable event that has an unformattable value. r_c ddz Srar-r-r%r#r.zUFormattingTests.test_formatUnformattableEventWithUnformattableValue.. Qr%)rrcrhrirjrkN)rRrlrr8rZs r#3test_formatUnformattableEventWithUnformattableValuezCFormattingTests.test_formatUnformattableEventWithUnformattableValuesR!  U# BFK )62 4f=r%ctddd}t|ttt }|j d||j t ddzt dz|y) ror_c ddz Srar-r-r%r#r.zbFormattingTests.test_formatUnformattableEventWithUnformattableErrorOMGWillItStop..rqr%okay)rrc recoverablerirvz = N)rRrr BaseExceptionrlr8r9rZs r#@test_formatUnformattableEventWithUnformattableErrorOMGWillItStopzPFormattingTests.test_formatUnformattableEventWithUnformattableErrorOMGWillItStops^!  *%m]_1UV BFK d=)E1DL@&Ir%rN)rDrErF__doc__rrVr r$r<rIrWr[r]rdrmrrrxr-r%r#rr"sQ   C  SD   %++ > > Jr%rc@eZdZdZd dZd dZd dZd dZd dZd dZ y) TimeFormattingTestsz. Tests for time formatting functions. Nct|y)N)r rBs r#setUpzTimeFormattingTests.setUps Tr%ct tddtdttdtddffd }|ddd |d d d |d dd|dddy)z Default time stamp format is RFC 3339 and offset respects the timezone as set by the standard C{TZ} environment variable and L{tzset} API. N:Platform cannot change timezone; unable to verify offsets.name expectedDST expectedSTDrct|td}jt|||r'td}jt||yy)N) irYrrrrrr) irrrrY)r r r7r)rrrlocalSTDlocalDSTr!s r#testForTimeZonezMTimeFormattingTests.test_formatTimeWithDefaultFormat..testForTimeZonesT $K>?H   Z1; ?!"CD  H!5{Cr%UTC+00z2007-01-31T00:00:00+0000zEST+05EDT,M4.1.0,M10.5.0z2006-06-30T00:00:00-0400z2007-01-31T00:00:00-0500zCEST-01CEDT,M4.1.0,M10.5.0z2006-06-30T00:00:00+0200z2007-01-31T00:00:00+0100zCST+06z2007-01-31T00:00:00-0600)rr r r)r!rs` r# test_formatTimeWithDefaultFormatz4TimeFormattingTests.test_formatTimeWithDefaultFormats =WX X D D$,SM D@C D  D    &   & & &   ( & &     & r%ct|jtdd|jtdddy)zL If C{when} argument is L{None}, we get the default output. N-!)defaultr7rrBs r#test_formatTimeWithNoTimez-TimeFormattingTests.test_formatTimeWithNoTimes0 D)3/ D#6|jtdddy)z0 "%f" supported in time format. g?x.Az%fr234560NrrBs r#test_formatTimePercentFz+TimeFormattingTests.test_formatTimePercentFs MdCXNr%ry) rDrErFrzr~rrrrrr-r%r#r|r|s). `=KGOr%r|cheZdZdZddZddZddZddZddZddZ dd Z dd Z dd Z dd Z dd Zy)ClassicLogFormattingTestsz@ Tests for classic text log event formatting functions. Nct tdt|tdt d}t d|}|j t|dy)z Time is first field. Default time stamp format is RFC 3339 and offset respects the timezone as set by the standard C{TZ} environment variable and L{tzset} API. NrrrXYZZYrlog_timez%2013-09-24T11:40:47+0000 [-#-] XYZZY )rr r r r rRr7r)r!rrs r#test_formatTimeDefaultz0ClassicLogFormattingTests.test_formatTimeDefaultsT =WX XT h 8 9!4  ' . 7 r%cdttdtfd}tdd}|j t ||dy ) zc Time is first field. Custom formatting function is an optional argument. rrcd|dS)N__r-)rs r#rzCClassicLogFormattingTests.test_formatTimeCustom..formatTime-ss": r%ri90r)rz__12345__ [-#-] XYZZY N)rfloatr rRr7r)r!rrs r#test_formatTimeCustomz/ClassicLogFormattingTests.test_formatTimeCustom'sB  (5/ c %8  '* E ( r%cTtdd}|jt|dy)z: Namespace is first part of second field. r my.namespace)r log_namespacez- [my.namespace#-] XYZZY NrRr7rr!rs r#test_formatNamespacez.ClassicLogFormattingTests.test_formatNamespace6s)~F  ' . + r%cptdtj}|jt |dy)z7 Level is second part of second field. r)r log_levelz- [-#warn] XYZZY NrRrwarnr7rrs r#test_formatLevelz*ClassicLogFormattingTests.test_formatLevel@s-8==A  ' . # r%cTtdd}|jt|dy)z) System is second field. r S.Y.S.T.E.M.r log_system- [S.Y.S.T.E.M.] XYZZY Nrrs r#test_formatSystemz+ClassicLogFormattingTests.test_formatSystemJs)NC  ' . & r%cttddtjd}|jt |dy)B System is not supplanted by namespace and level. rrr)rrrrrNrrs r#test_formatSystemRulzz/ClassicLogFormattingTests.test_formatSystemRulzTs9(mm%    ' . & r%cdtdt}|jt|dy)rrrz- [UNFORMATTABLE] XYZZY N)rRrlr7rrs r#test_formatSystemUnformattablez8ClassicLogFormattingTests.test_formatSystemUnformattablecs+MOD  ' . ' r%cTtdd}|jt|dy)z0 Formatted event is last field. zid:{id}123ridz- [-#-] id:123 Nrrs r#test_formatFormatz+ClassicLogFormattingTests.test_formatFormatms) e4  ' . ! r%cRtd}|jt|dy)z# No format string. r)rNrRrrrs r#test_formatNoFormatz-ClassicLogFormattingTests.test_formatNoFormatws! 1%8$?r%cTtdd}|jt|dy)z& Empty format string. r'rrNrrs r#test_formatEmptyFormatz0ClassicLogFormattingTests.test_formatEmptyFormat~s$u- 1%8$?r%cRtd}|jt|dy)zO If the formatted event has newlines, indent additional lines. z"XYZZY A hollow voice says: "Plugh")rz-- [-#-] XYZZY A hollow voice says: "Plugh" Nrrs r#test_formatFormatMultiLinez4ClassicLogFormattingTests.test_formatFormatMultiLines( FG  ' . C r%ry)rDrErFrzrrrrrrrrrrrr-r%r#rrs@ &         @@ r%rceZdZdZddZy)FormatFieldTestsz+ Tests for format field functions. Nc |jtdtddd|jtdtdd y ) z L{formatWithCall} is an extended version of L{str.format} that will interpret a set of parentheses "C{()}" at the end of a format key to mean that the format key ought to be I{called} rather than stringified. zHello, {world}. {callme()}.earthcy)Nmayber-r-r%r#r.z6FormatFieldTests.test_formatWithCall..r/r%)rAcallmezHello, earth. maybe.zHello, {repr()!r}.cy)Nr9r-r-r%r#r.z6FormatFieldTests.test_formatWithCall..r/r%)r9zHello, 'repr'.N)r7rrRrBs r#test_formatWithCallz$FormatFieldTests.test_formatWithCallsO  -7?;  #    />1J K  r%ry)rDrErFrzrr-r%r#rrs  r%rceZdZdZdefdZy)rlz> An object that raises an exception from C{__repr__}. rctddz Sra)r rBs r#__repr__zUnformattable.__repr__s1q5zr%N)rDrErFrzr rr-r%r#rlrls#r%rlceZdZdZy) CapturedErrorz3 A captured error for use in format tests. N)rDrErFrzr-r%r#rrsr%rcxeZdZdZddZddZddZddZddZddZ dd Z dd Z dd Z dd Z dd ZddZddZy)EventAsTextTestszi Tests for L{eventAsText}, all of which ensure that the returned type is UTF-8 decoded text. Nc td#t$r t}YnwxYwddi}||d<t|dd}|jt |j ||jd|y) zT An event with a C{log_failure} key will have a traceback appended. This is a fake errorrThis is a test log message log_failureTFincludeTimestamp includeSystemNrrrr8r getTracebackr!fr eventTexts r#test_eventWithTracebackz(EventAsTextTests.test_eventWithTracebacksw  67 7  A ()EF mER  c!..*+Y7 2I> ##c td#t$r t}YnwxYwddi}||d<t|dd}|jt |j ||jd|y) zu An event with an empty C{log_format} key appends a traceback from the accompanying failure. rrr'rTFrNrrs r#"test_formatEmptyEventWithTracebackz3EventAsTextTests.test_formatEmptyEventWithTracebackst   67 7  A ', mER  c!..*+Y7 ,i8rc" td#t$r t}YnwxYwddd}||d<t|dd}|j|t|j t |j ||j d|y ) zw An event with an unformattable value in the C{log_format} key still has a traceback appended. rr_c ddz Srar-r-r%r#r.zHEventAsTextTests.test_formatUnformattableWithTraceback..rgr%rbrTFrN)rrrassertIsInstancer r8rrs r#%test_formatUnformattableWithTracebackz6EventAsTextTests.test_formatUnformattableWithTracebacks   67 7  A %!  !mER  i- c!..*+Y7 ,i8rc x td#t$r t}YnwxYwddddttt di}||d<t |dd }|j |t|jd ||jt|j||jd|y ) z An event with an unformattable value in the C{log_format} key, that throws an exception when __repr__ is invoked still has a traceback appended. rrr_rcc ddz Srar-r-r%r#r.zMEventAsTextTests.test_formatUnformattableErrorWithTraceback..rgr%rhrTFrz MESSAGE LOSTN) rrrr rlrrr8rrs r#*test_formatUnformattableErrorWithTracebackz;EventAsTextTests.test_formatUnformattableErrorWithTracebacks   67 7  A  * M mo &  !mER  i- ni0 c!..*+Y7 ,i8rcddi}t|d<t|dd}|j|t|j d|y) zk If a traceback cannot be appended, a message indicating this is true is appended. rr'rTFrz'(UNABLE TO OBTAIN TRACEBACK FROM EVENT)N)rVrrr r8r!rrs r#&test_formatEventUnformattableTracebackz7EventAsTextTests.test_formatEventUnformattableTracebacksG (,%xmER  i- ?Kr%cxddi}t|dd}|j|t|jd|y)zY An event with no C{log_failure} key will not have a traceback appended. rrTFrN)rrr r8rs r#test_formatEventNonCriticalz,EventAsTextTests.test_formatEventNonCriticals<()EFER  i- 2I>r%c td#t$r t}YnwxYwddi}||d<t|dd}|jd||jd|y ) zU An exception message with multibyte characters is properly handled. u€rrrTFr TracebackNrrrr8rs r#test_formatTracebackMultibytez.EventAsTextTests.test_formatTracebackMultibytesi & &  A ()EF mER  eY' k9-rc td#t$r t}YnwxYwddi}||d<t|dd}|jd||jd |y ) zj An error raised attempting to decode the UTF still produces a valid log message. s testrrrTFrrz.CapturedError(b"\xff\xfet\x00e\x00s\x00t\x00")Nrrs r#,test_formatTracebackHandlesUTF8DecodeFailurez=EventAsTextTests.test_formatTracebackHandlesUTF8DecodeFailure*sk   ?@ @  A ()EF mER  k9- GSrc td#t$r t}YnwxYwtd}dd|d}||d<t|ddd }|j |d y ) zq If includeSystem is specified as the only option no timestamp or traceback are printed. rrABCD fake_systemrrrrFTrincludeTracebackrz[fake_system] ABCDNrrr rr7r!rrrrs r#test_eventAsTextSystemOnlyz+EventAsTextTests.test_eventAsTextSystemOnly;s~   67 7  A  8 9 '  !m ""     rc t tdt|td t d#t$r t }YnwxYwt d}dd|d}||d <t|d d d }|j|d y)zq If includeTimestamp is specified as the only option no system or traceback are printed. Nrrrrr r r rTFr z2013-09-24T11:40:47+0000 ABCD) rr r r rrr rr7rs r#test_eventAsTextTimestampOnlyz.EventAsTextTests.test_eventAsTextTimestampOnlyWs =WX XT h  67 7  A  8 9 '  !m !"    + s 4A  A c td#t$r t}YnwxYwtd}d|d}||d<t|ddd}|j |d y ) z\ If includeSystem is specified with a missing system [-#-] is used. rrr rrFTr z [-#-] ABCDNrrs r#test_eventAsTextSystemMissingz.EventAsTextTests.test_eventAsTextSystemMissingys{   67 7  A  8 9   !m ""     rc td#t$r t}YnwxYwtd}d|tjdd}||d<t |ddd }|j |d y ) z If includeSystem is specified with a missing system but namespace and level are present they are used. rrr test)rrrrrFTr z[test#info] ABCDNrrr rinforr7rs r#.test_eventAsTextSystemMissingNamespaceAndLevelz?EventAsTextTests.test_eventAsTextSystemMissingNamespaceAndLevels   67 7  A  8 9 !#   !m ""    ! rc td#t$r t}YnwxYwtd}d|tjd}||d<t |ddd}|j |d y ) zv If includeSystem is specified with a missing system but level is present, level is included. rrr )rrrrFTr z [-#info] ABCDNrrs r#&test_eventAsTextSystemMissingLevelOnlyz7EventAsTextTests.test_eventAsTextSystemMissingLevelOnlys   67 7  A  8 9 !  !m ""     rry)rDrErFrzrrrrrrrrrrrrrr-r%r#rrsK ? 99(9. L? .T" 8  D 6 : r%r)'rztypingrrrrtimer ImportErrortwisted.python.failurer!twisted.python.test.test_tzhelperr r r twisted.trialr twisted.trial.unittestr _formatrrrrrr _interfacesr_levelsrTestCaserr|rrrl Exceptionrrr-r%r#r(s0/ +II"+#UJh''UJpRO(++ROj~  1 1~ B x(( 2I S x((S S  EsCCC