
L~Mc           @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z d d l m	 Z	 d d l
 m Z m Z d e f d     YZ d e	 j f d	     YZ d
 e f d     YZ d d d     YZ d d d     YZ d e e f d     YZ d d  d     YZ d e e	 j f d     YZ d e e	 j f d     YZ d e	 j f d     YZ d e	 j f d     YZ d e	 j f d     YZ d S(!   s"   
Tests for L{twisted.python.log}.
iN(   t   StringIO(   t   unittest(   t   logt   failuret   FakeWarningc           B   s   e  Z d  Z RS(   s   
    A unique L{Warning} subclass used by tests for interactions of
    L{twisted.python.log} with the L{warnings} module.
    (   t   __name__t
   __module__t   __doc__(    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyR      s   t   LogTestc           B   sP   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 RS(   c         C   sB   g  |  _  |  j  j |  _ t j |  j  |  j t j |  j  d  S(   N(   t   catchert   appendt   observerR   t   addObservert
   addCleanupt   removeObserver(   t   self(    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   setUp   s    	c         C   s   |  j  } t j d d t | j   } |  j | d d d  |  j | d t  |  j | j d   |  j t |  d  d  S(   Nt   testt   testShouldCatcht   messagei    t   time(	   R	   R   t   msgt   Truet   popt   assertEqualst
   failUnlesst   has_keyt   len(   R   R	   t   i(    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   testObservation!   s    	c         C   s   |  j  } t j i d d 6d d 6d d 6t j i d d 6t j d d d	 | j   } |  j | d d  |  j | d d  |  j | d d	  |  j | d
 d d  d  S(   Ns   not the defaultt	   subsystemt   at   subsubsystemt   ct   othert   bt   foot   dR   i    (   R	   R   t   callWithContextR   R   R   (   R   R	   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   testContext+   s    	c         C   s|   xu d t    t  f t j t    t f g D]I \ } } t j |  |  j j   } |  j | d d  |  j	 |  q+ Wd  S(   Ns   hello worldt   isErrori   (   s   hello worlds   hello world(
   t   KeyErrorR   t   Failuret   RuntimeErrorR   t   errR	   R   R   t   flushLoggedErrors(   R   t   et   igR   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt
   testErrors8   s    %c         C   s   x d t    t  f t j t    t f g D]` \ } } t j | d  |  j j   } |  j | d d  |  j | d d  |  j	 |  q+ Wd  S(   Ns   hello worldt   foobarR(   i   t   why(   s   hello worlds   hello world(
   R)   R   R*   R+   R   R,   R	   R   R   R-   (   R   R.   R/   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   testErrorsWithWhyA   s    %c         C   s  g  } g  } d   } x= | j  | | j  g D]& } t j |  |  j t j |  q+ Wx t d  D] } g  | (g  | (t j d  |  j t  } |  j	 t
 |  d  |  j t
 |  d  |  j t
 |  d  |  j | d d d	  |  j | d d d
  qb Wd S(   s  
        Exceptions raised by log observers are logged but the observer which
        raised the exception remains registered with the publisher.  These
        exceptions do not prevent the event from being sent to other observers
        registered with the publisher.
        c         S   s   d d d  S(   Ni   i    (    (   t   events(    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   brokenU   s    i   s   Howdy, y'all.i   i   R   i    N(   s   Howdy, y'all.(   s   Howdy, y'all.(   R
   R   R   R   R   t   xrangeR   R-   t   ZeroDivisionErrort   assertEqualR   R   (   R   t   L1t   L2R5   R   R   t   excs(    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_erroneousErrorsL   s     	c         C   s   t  j   } | j |  j  | j t d  t d d  |  j j   } |  j | d | d  |  j |  j g   | j t d  t d d d t	   |  j j   } |  j | d | d  |  j |  j g   d S(   sy   
        L{twisted.python.log.showwarning} emits the warning as a message
        to the Twisted logging system.
        s   unique warning messages   warning-filename.pyi   t   formatsQ   warning-filename.py:27: twisted.test.test_log.FakeWarning: unique warning messaget   lineN(
   R   t   LogPublisherR   R   t   showwarningR   R	   R   R8   t   object(   R   t	   publishert   event(    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_showwarningv   s$    
c      	   C   s   d } t  } d } d } t   } t j | | | | d | |  j | j   t j | | | |   t j	 d
 k r d } t   } t j | | | | d | d | |  j | j   t j | | | | |   n  d	 S(   s   
        L{twisted.python.log.showwarning} passes warnings with an explicit file
        target on to the underlying Python warning system.
        s   another unique messages   warning-filename.pyi   t   filei   i   s   hello worldR>   N(   i   i   (
   R   R    R   R@   R8   t   getvaluet   warningst   formatwarningt   syst   version_info(   R   R   t   categoryt   filenamet   linenot   outputR>   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_warningToFile   s$    				(
   R   R   R   R   R'   R0   R3   R<   RD   RO   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyR      s   		
					*	 t   FakeFilec           B   s   e  Z d    Z d   Z RS(   c         C   s   |  j  |  d  S(   N(   R
   (   R   t   bytes(    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   write   s    c         C   s   d  S(   N(    (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   flush   s    (   R   R   RR   RS   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyRP      s   	t   EvilStrc           B   s   e  Z d    Z RS(   c         C   s   d d d  S(   Ni   i    (    (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   __str__   s    (   R   R   RU   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyRT      s   t   EvilReprc           B   s   e  Z d    Z d   Z RS(   c         C   s   d S(   Ns   Happy Evil Repr(    (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyRU      s    c         C   s   d d d  S(   Ni   i    (    (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   __repr__   s    (   R   R   RU   RW   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyRV      s   	t   EvilReprStrc           B   s   e  Z RS(    (   R   R   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyRX      s   t   LogPublisherTestCaseMixinc           B   s   e  Z d    Z d   Z RS(   c         C   s   t    |  _ t j   |  _ t j |  j  |  _ |  j j |  j j  y t	 d  Wn t
 k
 rp d |  _ n' Xt t  t j   |  _ t j d  d S(   s   
        Add a log observer which records log events in C{self.out}.  Also,
        make sure the default string encoding is ASCII so that
        L{testSingleUnicode} can test the behavior of logging unencodable
        unicode messages.
        u   ½t   asciiN(   RP   t   outR   R?   t   lpt   FileLogObservert   floR   t   emitt   strt   UnicodeEncodeErrort   Nonet   _origEncodingt   reloadRI   t   getdefaultencodingt   setdefaultencoding(   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyR      s    
c         C   s`   x1 |  j  D]& } |  j t | t  d | f  q
 W|  j d k	 r\ t j |  j  t ` n  d S(   s   
        Verify that everything written to the fake file C{self.out} was a
        C{str}.  Also, restore the default string encoding to its previous
        setting, if it was modified by L{setUp}.
        s   %r was not a stringN(   R[   R   t
   isinstanceR`   Rc   Rb   RI   Rf   (   R   t   chunk(    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   tearDown   s
    $(   R   R   R   Ri   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyRY      s   	t   LogPublisherTestCasec           B   s#   e  Z d    Z d   Z d   Z RS(   c         C   s-   |  j  j d  |  j t |  j  d  d  S(   Ns   Hello, world.i   (   R\   R   R   R   R[   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   testSingleString   s    c         C   s0   |  j  j d d  |  j t |  j  d  d  S(   Ns   Hello, s   world.i   (   R\   R   R   R   R[   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   testMultipleString   s    c         C   s[   |  j  j d  |  j t |  j  d  |  j d |  j d  |  j d |  j d  d  S(   Nu   Hello, ½ world.i   s   with str errori    Ra   (   R\   R   R   R   R[   t   assertIn(   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   testSingleUnicode   s    (   R   R   Rk   Rl   Rn   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyRj      s   		t   FileObserverTestCasec           B   s   e  Z d    Z e e d d  d k r3 d e _ n  d   Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z RS(   c      	   C   sh  d } t  j |  } d } t  j |  } t j j d d  } z d t j d <t  j   |  j |  j j	 |  d  |  j |  j j	 |  d  d t j d <t  j   |  j |  j j	 |  d  |  j |  j j	 |  d  d t j d <t  j   |  j |  j j	 |  d  |  j |  j j	 |  d  Wd | d k rLt j d =n | t j d <t  j   Xd S(   s  
        Attempt to verify that L{FileLogObserver.getTimezoneOffset} returns
        correct values for the current C{TZ} environment setting.  Do this
        by setting C{TZ} to various well-known values and asserting that the
        reported offset is correct.
        i  i   i   i    i   i   i   i  i   i   t   TZs   America/New_Yorki@8  iPF  s   Europe/Berliniis   Africa/JohannesburgN(	   i  i   i   i    i    i    i   i   i   (	   i  i   i   i    i    i    i   i   i    (
   R   t   mktimet   ost   environt   getRb   t   tzsetR8   R^   t   getTimezoneOffset(   R   t   localDaylightTuplet   utcDaylightTimestampt   localStandardTuplet   utcStandardTimestampt   originalTimezone(    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_getTimezoneOffset  sD    


Ru   sW   Platform cannot change timezone, cannot verify correct offsets in well-known timezones.c      
   C   so  t  j d  t  j } d
   |  j _ |  j |  j j |  d  d   |  j _ |  j |  j j |  d  d   |  j _ |  j |  j j |  d  d   |  j _ |  j |  j j |  d  d   |  j _ |  j |  j j |  d  d   |  j _ |  j |  j j |  d  d   |  j _ |  j |  j j |  d  d |  j _ |  j |  j j |  d  d S(   su   
        Test the method of L{FileLogObserver} which turns a timestamp into a
        human-readable string.
        i  i   i   i   i   i   i   i   i    c         S   s   d S(   NiPF  (    (   t   when(    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   <lambda>H  s    s   2001-02-02 23:05:06-0500c         S   s   d S(   Ni(    (   R}   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyR~   L  s    s   2001-02-03 05:05:06+0100c         S   s   d S(   NiPe(    (   R}   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyR~   P  s    s   2001-02-03 15:05:06+1100c         S   s   d S(   Ni  (    (   R}   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyR~   T  s    s   2001-02-03 02:35:06-0130c         S   s   d S(   Ni(    (   R}   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyR~   X  s    s   2001-02-03 05:35:06+0130c         S   s   d S(   Ni  (    (   R}   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyR~   ]  s    s   2001-02-03 03:35:06-0030c         S   s   d S(   Ni(    (   R}   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyR~   a  s    s   2001-02-03 04:35:06+0030s   %Y %ms   2001 02N(	   i  i   i   i   i   i   i   i   i    (   R   Rq   t   timezoneR^   Rv   R   t
   formatTimet
   timeFormat(   R   R}   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_timeFormatting>  s"    c         C   sG   |  j  j t    |  j t |  j  d  |  j d |  j d  d  S(   Ni   t   UNFORMATTABLEi    (   R\   R   RT   R   R   R[   t   assertNotIn(   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt%   test_loggingAnObjectWithBroken__str__l  s    c         C   sP   |  j  j d d d t    |  j t |  j  d  |  j d |  j d  d  S(   NR=   s   %(blat)st   blati   s-   Invalid format string or unformattable objecti    (   R\   R   RT   R   R   R[   Rm   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt(   test_formattingAnObjectWithBroken__str__t  s    c         C   sM   |  j  j d d t   |  j t |  j  d  |  j d |  j d  d  S(   Nt   huht   systemi   s-   Invalid format string or unformattable objecti    (   R\   R   RT   R   R   R[   Rm   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_brokenSystem__str__z  s    c         C   sS   |  j  j d d d t   g  |  j t |  j  d  |  j d |  j d  d  S(   NR=   s   %(blat)sR   i   s   UNFORMATTABLE OBJECTi    (   R\   R   RV   R   R   R[   Rm   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt1   test_formattingAnObjectWithBroken__repr__Indirect  s    c         C   sP   |  j  j d d t   g |  j t |  j  d  |  j d |  j d  d  S(   NR   R   i   s   UNFORMATTABLE OBJECTi    (   R\   R   RV   R   R   R[   Rm   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt%   test_systemWithBroker__repr__Indirect  s    c         C   sM   |  j  j d d d d  |  j t |  j  d  |  j d |  j d  d  S(   NR=   s
   hooj %s %sR   i   s-   Invalid format string or unformattable objecti    (   R\   R   R   R   R[   Rm   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_simpleBrokenFormat  s    c         C   sM   |  j  j d d d d  |  j t |  j  d  |  j d |  j d  d  S(   NR=   i*   R   i   s-   Invalid format string or unformattable objecti    (   R\   R   R   R   R[   Rm   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_ridiculousFormat  s    c         C   sP   |  j  j d t   d d  |  j t |  j  d  |  j d |  j d  d  S(   NR=   R   i   t   PATHOLOGICALi    (   R\   R   RX   R   R   R[   Rm   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt!   test_evilFormat__repr__And__str__  s    c         C   s6   |  j  j d d d t  |  j t |  j  d  d S(   sP   
        This kind of eventDict used to fail silently, so test it does.
        R   t    R(   i    N(   R\   R   t   FalseR   R   R[   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_strangeEventDict  s    c         C   s   t    t _ |  j t t d t j  d   } t j |  |  j t j |  t j	 t j  } |  j | j
  |  j t j t j  t j } t j	 t j  } |  j t j |  d S(   s   
        There are some obscure error conditions that can occur when logging is
        started twice. See http://twistedmatrix.com/trac/ticket/3289 for more
        information.
        t   stdoutc         S   s+   |  d r' t  j j |  d j    n  d  S(   NR(   R   (   RI   t
   __stdout__RR   t   getTraceback(   t	   eventDict(    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt	   showError  s    
N(   R    RI   R   R   t   setattrR   R   R   R   t   startLoggingt   stopt   assertIsInstancet   StdioOnnaStickt   assertIdentical(   R   R   R   t
   fakeStdout(    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_startLoggingTwice  s    			N(   R   R   R|   t   getattrR   Rb   t   skipR   R   R   R   R   R   R   R   R   R   R   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyRo     s   	1	.									t   PythonLoggingObserverTestCasec           B   sM   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 RS(   s5   
    Test the bridge with python logging module.
    c         C   s   t    |  _ t j d  } | j   |  _ | j t j  t j |  j  |  _	 t j
 t j  } |  j	 j |  | j |  j	  t j   |  _ t j   |  _ |  j j |  j j  d  S(   NR   (   R    R[   t   loggingt	   getLoggert   getEffectiveLevelt   originalLevelt   setLevelt   DEBUGt   StreamHandlert   hdlrt	   Formattert   BASIC_FORMATt   setFormattert
   addHandlerR   R?   R\   t   PythonLoggingObservert   obsR   R_   (   R   t
   rootLoggert   fmt(    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyR     s    c         C   s=   t  j d  } | j |  j  | j |  j  t  j   d  S(   NR   (   R   R   t   removeHandlerR   R   R   t   shutdown(   R   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyRi     s    c         C   sF   |  j  j d  |  j d |  j j    |  j d |  j j    d S(   s<   
        Test simple output, and default log level.
        s   Hello, world.t   INFON(   R\   R   Rm   R[   RF   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_singleString  s    c         C   sE   |  j  j d t j t d   d t  |  j d |  j j    d S(   s$   
        Test error output.
        R   s   That is bad.R(   t   ERRORN(	   R\   R   R   R*   t
   ValueErrorR   Rm   R[   RF   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_errorString  s    (c         C   s<   |  j  j d d d d d d  |  j d |  j j    d S(	   s-   
        Test logging with a format.
        R=   s   %(bar)s oo %(foo)st   bart   HelloR$   t   worlds   Hello oo worldN(   R\   R   Rm   R[   RF   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_formatString  s    c         C   s   |  j  j d d t j |  j d |  j j    |  j d |  j j    |  j j   |  j  j d d t j |  j d |  j j    |  j d |  j j    d S(   sG   
        Test the logLevel keyword for customizing level used.
        s	   Spam egg.t   logLevelR   s   Foo bar.t   WARNINGN(	   R\   R   R   R   Rm   R[   RF   t   resetR   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_customLevel  s    c         C   s6   |  j  j d d d t  |  j |  j j   d  d S(   sx   
        Verify that an event dictionary which is not an error and has an empty
        message isn't recorded.
        R   R   R(   N(   R\   R   R   R   R[   RF   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyR     s    (
   R   R   R   R   Ri   R   R   R   R   R   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyR     s   						t    PythonLoggingIntegrationTestCasec           B   s    e  Z d  Z d   Z d   Z RS(   s4   
    Test integration of python logging bridge.
    c         C   s   t  j } t  j } g  } zi | j t  _ | j t  _ t  j   } | j   |  j | d | j  | j	   |  j t
 |  d  Wd | t  _ | t  _ Xd S(   s~   
        Test that start and stop methods of the observer actually register
        and unregister to the log system.
        i    N(   R   R   R   R
   t   removeR   t   startR   R_   R   R   (   R   t   oldAddObservert   oldRemoveObservert   lR   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_startStopObserver	  s    		

	c            s   d t  j f   f d     Y      } g  } t  j j } z6 | j t  j _ | j d  |  j t |  d  Wd | t  j _ Xd S(   s   
        Test that we can inherit L{log.PythonLoggingObserver} and use super:
        that's basically a validation that L{log.PythonLoggingObserver} is
        new-style class.
        t
   MyObserverc              s   e  Z   f d    Z RS(   c            s   t    |   j |  d  S(   N(   t   superR_   (   R   R   (   R   (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyR_   $  s    (   R   R   R_   (    (   R   (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyR   #  s   R$   i   N(   R   R   R_   R
   R   R   (   R   R   R   t   oldEmit(    (   R   s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_inheritance  s    	(   R   R   R   R   R   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyR     s   	t   DefaultObserverTestCasec           B   s   e  Z d  Z d   Z RS(   s$   
    Test the default observer.
    c         C   s   d d l  m  } t j   } |   | _ | j   d } t j t   |  |  j   } |  j | | j j	    |  j
 t |  d  | j   d S(   su   
        The reason argument passed to log.err() appears in the report
        generated by DefaultObserver.
        i(   R    s   The reason.i   N(   R    R   t   DefaultObservert   stderrR   R,   t	   ExceptionR-   t   assertSubstringRF   R   R   R   (   R   R    R   t   reasont   errors(    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   test_failureLogger6  s    
(   R   R   R   R   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyR   1  s   (    (    (    (   R   Rr   RI   R   R   RG   t	   cStringIOR    t   twisted.trialR   t   twisted.pythonR   R   t   WarningR   t   TestCaseR   t   listRP   RT   RV   RX   RY   Rj   Ro   R   R   R   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/test/test_log.pyt   <module>   s    <'D,