ó
ýGpNc           @   s£   d  Z  d d l Z d d l Z d e j f d „  ƒ  YZ e d k rŸ d d l Z d d l Z d d l Z d „  Z	 d „  Z
 d e j f d	 „  ƒ  YZ e j ƒ  n  d S(
   sI   Enhanced Thread with support for return values and exception propagation.iÿÿÿÿNt   REThreadc           B   sM   e  Z d  Z d d d d i  d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   s4   Thread with return values and exception propagation.c         C   sS   t  j j |  | | | | | | ƒ | |  _ | |  _ | |  _ d |  _ d |  _ d S(   s<   Initialize Thread, identical to threading.Thread.__init__().N(	   t	   threadingt   Threadt   __init__t   _REThread__targett   _REThread__argst   _REThread__kwargst   Nonet   _retvalt
   _exception(   t   selft   groupt   targett   namet   argst   kwargst   verbose(    (    s3   /usr/lib/python2.7/dist-packages/apport/REThread.pyR      s    				c         C   sQ   |  j  rM y |  j  |  j |  j Ž  |  _ WqM t rJ t j ƒ  |  _ qJ qM Xn  d S(   s9   Run target function, identical to threading.Thread.run().N(   R   R   R   R   t   syst   exc_infoR	   (   R
   (    (    s3   /usr/lib/python2.7/dist-packages/apport/REThread.pyt   run   s    	c         C   s*   |  j  ƒ  s t ‚ |  j s# t ‚ |  j S(   sº   Return value from target function.

        This can only be called after the thread has finished, i. e. when
        isAlive() is False and did not terminate with an exception.
        (   t   isAlivet   AssertionErrorR	   R   (   R
   (    (    s3   /usr/lib/python2.7/dist-packages/apport/REThread.pyt   return_value'   s    c         C   s   |  j  S(   sA   Return (type, value, traceback) of the exception caught in run().(   R	   (   R
   (    (    s3   /usr/lib/python2.7/dist-packages/apport/REThread.pyR   1   s    c         C   s1   |  j  r- |  j  d |  j  d |  j  d ‚ n  d S(   sb   Raise the exception caught in the thread.

        Do nothing if no exception was caught.
        i    i   i   N(   R	   (   R
   (    (    s3   /usr/lib/python2.7/dist-packages/apport/REThread.pyt	   exc_raise6   s    	N(    (	   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   (    (    (    s3   /usr/lib/python2.7/dist-packages/apport/REThread.pyR       s   	
	
	t   __main__c         C   s   t  j |  ƒ d S(   s   Test thread to just wait a bit.N(   t   timet   sleep(   t   seconds(    (    s3   /usr/lib/python2.7/dist-packages/apport/REThread.pyt   idleE   s    c         C   s   |  | S(   s"   Test thread to divide two numbers.(    (   t   xt   y(    (    s3   /usr/lib/python2.7/dist-packages/apport/REThread.pyt   divJ   s    t   _Tc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   sc   t  d t d d ƒ } | j ƒ  | j ƒ  | j ƒ  |  j | j ƒ  d ƒ |  j | j ƒ  d ƒ d S(   s   return value works properly.R   R   i*   i   i   N(   i*   i   (	   R    R"   t   startt   joinR   t   assertEqualR   R   R   (   R
   t   t(    (    s3   /usr/lib/python2.7/dist-packages/apport/REThread.pyt   test_return_valueP   s    


c         C   sl   t  d t d d ƒ } | j ƒ  |  j t | j ƒ | j ƒ  |  j | j ƒ  d ƒ |  j | j	 ƒ  d ƒ d S(   s1   REThread works if run() does not return anything.R   R   g      à?N(   g      à?(
   R    R   R$   t   assertRaisesR   R   R%   R&   R   R   (   R
   R'   (    (    s3   /usr/lib/python2.7/dist-packages/apport/REThread.pyt   test_no_return_value[   s    

c         C   sÑ   t  d t d d ƒ } | j ƒ  | j ƒ  |  j t | j ƒ |  j | j ƒ  d t	 k ƒ t
 j | j ƒ  d | j ƒ  d | j ƒ  d ƒ } |  j | d j d ƒ d t | ƒ ƒ |  j | d	 j d
 ƒ ƒ d S(   s)   exception in thread is caught and passed.R   R   i   i    i   iÿÿÿÿt   ZeroDivisionErrors   not a ZeroDivisionError:iþÿÿÿs   return x / y
N(   i   i    (   R    R"   R$   R%   R)   R   R   t
   assertTrueR   R+   t	   tracebackt   format_exceptiont
   startswitht   strt   endswith(   R
   R'   t   exc(    (    s3   /usr/lib/python2.7/dist-packages/apport/REThread.pyt   test_exceptionf   s    

 'c         C   sß   t  d t d d ƒ } | j ƒ  | j ƒ  |  j t | j ƒ t } y | j ƒ  Wn{ t	 } t
 j ƒ  } t j | d | d | d ƒ } |  j | d j d ƒ d t | ƒ ƒ |  j | d	 j d
 ƒ ƒ n X|  j | ƒ d S(   s+   exc_raise() raises caught thread exception.R   R   i   i    i   iÿÿÿÿR+   s   not a ZeroDivisionError:iþÿÿÿs   return x / y
N(   i   i    (   R    R"   R$   R%   R)   R   R   t   FalseR   t   TrueR   R   R-   R.   R,   R/   R0   R1   (   R
   R'   t   raisedt   eR2   (    (    s3   /usr/lib/python2.7/dist-packages/apport/REThread.pyt   test_exc_raiset   s    

!'(   R   R   R(   R*   R3   R8   (    (    (    s3   /usr/lib/python2.7/dist-packages/apport/REThread.pyR#   O   s   			(   R   R   R   R   R    R   t   unittestR   R-   R   R"   t   TestCaseR#   t   main(    (    (    s3   /usr/lib/python2.7/dist-packages/apport/REThread.pyt   <module>   s   4$		8