
H/\c           @  s  d  d l  m Z d  d l m Z d  d l j Z d Z e d Ud j d   e j	 d  D  Z
 d j d   e j	 d  D  Z y d  d l m Z Wn! e k
 r d  d l m Z n Xd  d l Z d  d l Z d  d l Z d  d l Z y d  d l Z Wn e k
 rd  d l Z n Xd  d l Z d  d l Z d  d l Z d  d	 l m Z d
   Z d e f d     YZ d e j f d     YZ e d e j  f d     Y Z! e d e j  f d     Y Z" e d e j  f d     Y Z# e d e j$ f d     Y Z$ e d e j% f d     Y Z% e d e j% f d     Y Z& e d e j' f d     Y Z' e d e j% f d     Y Z( e d e j) f d      Y Z) e d! e j* f d"     Y Z* e d# e j+ f d$     Y Z+ e, d% k re j-   n  d S(&   i(   t   print_function(   t   xrangeNs  from gevent import monkey; monkey.patch_all()
from gevent.event import Event
from gevent.lock import RLock, Semaphore, BoundedSemaphore
from gevent.thread import allocate_lock as Lock
import threading
threading.Event = Event
threading.Lock = Lock
# NOTE: We're completely patching around the allocate_lock
# patch we try to do with RLock; our monkey patch doesn't
# behave this way, but we do it in tests to make sure that
# our RLock implementation behaves correctly by itself.
# However, we must test the patched version too, so make it
# available.
threading.NativeRLock = threading.RLock
threading.RLock = RLock
threading.Semaphore = Semaphore
threading.BoundedSemaphore = BoundedSemaphore
s   
c         c  s   |  ] } d  | Vq d S(   s               %sN(    (   t   .0t   line(    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pys	   <genexpr>   s    c         c  s   |  ] } d  | Vq d S(   s                   %sN(    (   R   R   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pys	   <genexpr>   s    (   t   verbose(   t
   lock_testsc         C  s"   t  j t j o t j d  |   S(   Ns#   SKIPPED: Timeout on PyPy3 on Travis(   t   unittestt   skipIft	   greentestt   PYPY3t   RUNNING_ON_CI(   t   cls(    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   skipDueToHang4   s    t   Counterc           B  s,   e  Z d    Z d   Z d   Z d   Z RS(   c         C  s   d |  _  d  S(   Ni    (   t   value(   t   self(    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   __init__;   s    c         C  s   |  j  d 7_  d  S(   Ni   (   R   (   R   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   inc>   s    c         C  s   |  j  d 8_  d  S(   Ni   (   R   (   R   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   decA   s    c         C  s   |  j  S(   N(   R   (   R   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   getD   s    (   t   __name__t
   __module__R   R   R   R   (    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR   :   s   			t
   TestThreadc           B  s   e  Z d    Z d   Z RS(   c         C  s>   t  j j |  d | | |  _ | |  _ | |  _ | |  _ d  S(   Nt   name(   t	   threadingt   ThreadR   t   testcaset   semat   mutext   nrunning(   R   R   R   R   R   R   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR   I   s
    			c         C  s+  t  j    d } t r4 t d |  j | d f  n  |  j  |  j M |  j j   t rt t |  j j   d  n  |  j	 j
 |  j j   d  Wd  QXt j |  t r t d |  j d  n  |  j W |  j j   |  j	 j |  j j   d  t rt d	 |  j |  j j   f  n  Wd  QXWd  QXd  S(
   Ng     @s   task %s will run for %.1f usecg    .As   tasks are runningi   t   taskt   donei    s$   %s is finished. %d tasks are running(   t   randomR   t   printR   R   R   R   R   R   R   t   assertLessEqualt   timet   sleepR   t   assertGreaterEqual(   R   t   delay(    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   runP   s&    

"
(   R   R   R   R'   (    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR   H   s   	t   ThreadTestsc           B  s   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 e
 j d  d	    Z d
   Z e e d  s d   Z n  RS(   c         C  s  d } t  j d d  } t  j   } t   } g  } x t |  D] } t d | |  | | |  } | j |  t | _ t	 | d  r |  j
 | j  |  j | j  |  j t j d t |    n  | j   q@ Wt r t d  n  x | D] } | j |  |  j | j    t	 | d  r |  j | j d  |  j | j d  k  |  j t j d	 t |    q q Wt rt d
  n  |  j | j   d  d  S(   Ni
   R   i   s   <thread %d>t   idents   <TestThread\(.*, initial\)>s!   waiting for all tasks to completei    s   <TestThread\(.*, \w+ -?\d+\)>s   all tasks done(   R   t   BoundedSemaphoret   RLockR   t   rangeR   t   appendt   Falset   daemont   hasattrt   assertIsNoneR)   t   assertFalset
   assertTruet   ret   matcht   reprt   startR   R!   t   joint   is_alivet   assertNotEqualt   Nonet   assertEqualR   (   R   t   NUMTASKSR   R   t
   numrunningt   threadst   it   t(    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   test_various_opsm   s4    		"&c           s   t  j   } |  j | j d  k  t |  t |     f d   } t  j     g   t j	 | d    j
   |  j  d d  k  t  j  d =d  S(   Nc            s>   t  j   }   j |  j  t |   t |     j   d  S(   N(   R   t   currentThreadR-   R)   t   strR6   t   set(   RA   (   R   R)   (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   f   s
    

i    (    (   R   RC   R2   R)   R;   RD   R6   t   Eventt   threadt   start_new_threadt   waitt   _active(   R   RA   RF   (    (   R   R)   s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt"   test_ident_of_no_threading_threads   s    


c         C  sj   t  r t d  n  y t j d  Wn( t j k
 rN t  rJ t d  n  d  SX|  j   t j d  d  S(   Ns   with 256kB thread stack size...i   s4   platform does not support changing thread stack sizei    (   R   R!   R   t
   stack_sizeRH   t   errorRB   (   R   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   test_various_ops_small_stack   s    
c         C  sj   t  r t d  n  y t j d  Wn( t j k
 rN t  rJ t d  n  d  SX|  j   t j d  d  S(   Ns   with 1MB thread stack size...i   s4   platform does not support changing thread stack sizei    (   R   R!   R   RM   RH   RN   RB   (   R   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   test_various_ops_large_stack   s    
c         C  sy   d   } t  j   } | j   t j | | f  } | j   |  j | t  j  |  j t  j | t  j  t  j | =d  S(   Nc         S  s   t  j   |  j   d  S(   N(   R   t   current_threadt   release(   R   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyRF      s    
(	   R   t   Lockt   acquireRH   RI   t   assertInRK   t   assertIsInstancet   _DummyThread(   R   RF   R   t   tid(    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   test_foreign_thread   s    	


c           s  y d d  l  } Wn% t k
 r7 t r3 t d  n  d  SX| j j } d t f d     Y  | j    } t j	    t j	    d t j
 f     f d     Y} |   } t | _ | j   t r t d  n  t r t d  n  | | j d  |  } |  j | d	  t r#t d
  n   j   t r@t d  n  |  j | j  t rct d  n  | | j | j  |  } |  j | d  t rt d  n   j d d  |  j | j  t rt d  n  | j r| j   n  d  S(   Nis2   test_PyThreadState_SetAsyncExc can't import ctypest   AsyncExcc           B  s   e  Z RS(    (   R   R   (    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyRZ      s   t   Workerc             s)   e  Z d Z e Z     f d    Z RS(   c           sk   t  j   |  _ t |  _ y( x! t r>  j   t j d  q WWn$   k
 rf t |  _  j   n Xd  S(   Ng?(	   RH   t	   get_identt   idR.   t   finishedt   TrueRE   R#   R$   (   R   (   RZ   t   worker_saw_exceptiont   worker_started(    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR'      s    		
	N(   R   R   R;   R]   R.   R^   R'   (    (   RZ   R`   Ra   (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR[      s   s       started worker threads        trying nonsensical thread idi    s,       waiting for worker thread to get starteds"       verifying worker hasn't exiteds2       attempting to raise asynch exception in workeri   s5       waiting for worker to say it caught the exceptiont   timeouti
   s       all OK -- joining worker(   t   ctypest   ImportErrorR   R!   t	   pythonapit   PyThreadState_SetAsyncExct	   Exceptiont	   py_objectR   RG   R   R_   R/   R7   t   c_longR<   RJ   R2   R^   R]   R3   R8   (   R   Rc   t   set_async_exct	   exceptionR[   RA   t   result(    (   RZ   R`   Ra   s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt#   SKIP_test_PyThreadState_SetAsyncExc   sN    %		

	c         C  st   d   } t  j } | t  _ zH t  j d d    } |  j t j | j  |  j | t  j k d  Wd  | t  _ Xd  S(   Nc          W  s   t  j    d  S(   N(   RH   RN   (   t   _args(    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   fail_new_thread   s    t   targetc           S  s   d  S(   N(   R;   (    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   <lambda>%  s    s:   Failed to cleanup _limbo map on failure of Thread.start().(	   R   t   _start_new_threadR   t   assertRaisesRH   RN   R7   R2   t   _limbo(   R   Ro   Rr   RA   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   test_limbo_cleanup  s    			c         C  s   y- d d  l  } t | d  t | j d  Wn+ t t f k
 rZ t rV t d  n  d  SX~ d d  l } | j t	 j
 d d d d t g  } |  j | d	  d  S(
   NiRe   t   PyGILState_Ensures6   test_finalize_with_runnning_thread can't import ctypess   -Wt   ignores   -cs  if 1:
%s
            import ctypes, sys, time
            try:
                import thread
            except ImportError:
                import _thread as thread # Py3

            # This lock is used as a simple event variable.
            ready = thread.allocate_lock()
            ready.acquire()

            # Module globals are cleared before __del__ is run
            # So we save the functions in class dict
            class C:
                ensure = ctypes.pythonapi.PyGILState_Ensure
                release = ctypes.pythonapi.PyGILState_Release
                def __del__(self):
                    state = self.ensure()
                    self.release(state)

            def waitingThread():
                x = C()
                ready.release()
                time.sleep(100)

            thread.start_new_thread(waitingThread, ())
            ready.acquire()  # Be sure the other thread is waiting.
            sys.exit(42)
            i*   (   Rc   t   getattrRe   Rd   t   AttributeErrorR   R!   t
   subprocesst   callt   syst
   executablet   setup_3R<   (   R   Rc   Rz   t   rc(    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   test_finalize_runnning_thread-  s    t   hangsc         C  s   d d  l  } | j t j d d d d t g d | j d | j } | j   \ } } | j   } | j d  } | j d  } t	 j
 d	 |  s t t |    t	 j d
 d | t	 j  j   } d  S(   Nis   -WRw   s   -cs  if 1:
%s
                import threading
                from time import sleep

                def child():
                    sleep(1)
                    # As a non-daemon thread we SHOULD wake up and nothing
                    # should be torn down yet
                    print("Woke up, sleep function is: %%r" %% sleep)

                threading.Thread(target=child).start()
                raise SystemExit
            t   stdoutt   stderrs   utf-8s+   ^Woke up, sleep function is: <.*?sleep.*?>$s   ^\[\d+ refs\]t    (   Rz   t   PopenR|   R}   t   setup_4t   PIPEt   communicatet   stript   decodeR4   R5   t   AssertionErrorR6   t   subt	   MULTILINE(   R   Rz   t   pR   R   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   test_join_nondaemon_on_shutdown]  s    	$c      
   C  s   t  j } d d  l } | j    | j d t  t j   } z xz t d d  D]i } t j	 | d  t  j
 d d    } | j   | j   |   } |  j | | k d | | f  qQ WWd  t j	 |  XWd  QXd  S(	   NiRw   i   id   i   Rp   c           S  s   d  S(   N(   R;   (    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyRq     s    s&   #1703448 triggered after %d trials: %s(   R   t	   enumeratet   warningst   catch_warningst   simplefiltert   DeprecationWarningR|   t   getcheckintervalR   t   setcheckintervalR   R7   R8   R2   (   R   t   enumR   t   old_intervalR@   RA   t   l(    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   test_enumerate_after_join}  s    	

	t   pypy_version_infoc         C  s   d t  f d     Y} | d t  } t j |  } | j j   ~ |  j |   d d t j |    | d t	  } t j |  } | j j   ~ |  j |   d d t j |    d  S(   Nt   RunSelfFunctionc           B  s   e  Z d    Z d   Z RS(   c      	   S  sH   | |  _  t j d |  j d |  f d i |  d 6 |  _ |  j j   d  S(   NRp   t   argst   kwargst   yet_another(   t   should_raiseR   R   t   _runRH   R7   (   R   R   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR     s
    		c         S  s   |  j  r t  n  d  S(   N(   R   t
   SystemExit(   R   t
   _other_reft   _yet_another(    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR     s    	(   R   R   R   R   (    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR     s   		R   t   msgs   %d references still around(
   t   objectR.   t   weakreft   refRH   R8   R1   R|   t   getrefcountR_   (   R   R   t   cyclic_objectt   weak_cyclic_objectt   raising_cyclic_objectt   weak_raising_cyclic_object(    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   test_no_refcycle_through_target  s    (   R   R   RB   RL   RO   RP   RY   Rm   Ru   R   R   t   skipOnLibuvOnPyPyOnWinR   R   R0   R|   R   (    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR(   h   s   	#					G		0 	t   ThreadJoinOnShutdownc           B  sJ   e  Z d    Z e j d  d    Z e j d  d    Z d   Z RS(   c         C  s   d t  | } d d  l } | j t j d d d | g d | j } | j   } | j j   j	 d d  } | j j
   |  j | d	  |  j | d
 d  |  j | d d  d  S(   Ns   if 1:
%s
            import sys, os, time, threading
            # a thread, which waits for the main program to terminate
            def joiningfunc(mainthread):
                mainthread.join()
                print('end of thread')
        
is   -WRw   s   -cR   s   R   s   end of main
end of thread
i   s   interpreter was blockedi    s   Unexpected error(   R~   Rz   R   R|   R}   R   RJ   R   t   readt   replacet   closeR<   R:   (   R   t   scriptRz   R   R   t   data(    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   _run_and_join  s    *R   c         C  s   d } |  j  |  d  S(   Ns   if 1:
            import os
            t = threading.Thread(target=joiningfunc,
                                 args=(threading.current_thread(),))
            t.start()
            time.sleep(0.1)
            print('end of main')
            (   R   (   R   R   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   test_1_join_on_shutdown  s    
s   Sometimes randomly times outc         C  s6   d d  l  } t | d  s d  Sd } |  j |  d  S(   Nit   forksG  if 1:
            childpid = os.fork()
            if childpid != 0:
                os.waitpid(childpid, 0)
                sys.exit(0)

            t = threading.Thread(target=joiningfunc,
                                 args=(threading.current_thread(),))
            t.start()
            print('end of main')
            (   t   osR0   R   (   R   R   R   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   test_2_join_in_forked_process  s
    c         C  s6   d d  l  } t | d  s d  Sd } |  j |  d  S(   NiR   s5  if 1:
            main_thread = threading.current_thread()
            def worker():
                threading._after_fork = lambda: None
                childpid = os.fork()
                if childpid != 0:
                    os.waitpid(childpid, 0)
                    sys.exit(0)

                t = threading.Thread(target=joiningfunc,
                                     args=(main_thread,))
                print('end of main')
                t.start()
                t.join() # Should not block: main_thread is already stopped

            w = threading.Thread(target=worker)
            w.start()
            import sys
            if sys.version_info[:2] >= (3, 7) or (sys.version_info[:2] >= (3, 5) and hasattr(sys, 'pypy_version_info') and sys.platform != 'darwin'):
                w.join()
            (   R   R0   R   (   R   R   R   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt!   test_3_join_in_forked_from_thread  s
    )
(	   R   R   R   R   R   R   t   skipOnPyPy3OnCIR   R   (    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR     s   	t   ThreadingExceptionTestsc           B  s,   e  Z d    Z d   Z d   Z d   Z RS(   c         C  s-   t  j   } | j   |  j t | j  d  S(   N(   R   R   R7   Rs   t   RuntimeError(   R   t   thread_(    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   test_start_thread_again*  s    
c         C  s#   t  j   } |  j t | j  d  S(   N(   R   RQ   Rs   R   R8   (   R   RQ   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   test_joining_current_thread/  s    c         C  s#   t  j   } |  j t | j  d  S(   N(   R   R   Rs   R   R8   (   R   R   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   test_joining_inactive_thread3  s    c         C  s3   t  j   } | j   |  j t t | d t  d  S(   NR/   (   R   R   R7   Rs   R   t   setattrR_   (   R   R   (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   test_daemonize_active_thread7  s    
(   R   R   R   R   R   R   (    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR   %  s   			t	   LockTestsc           B  s   e  Z e e j  Z RS(    (   R   R   t   staticmethodR   RS   t   locktype(    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR   =  s   t
   RLockTestsc           B  s   e  Z e e j  Z RS(    (   R   R   R   R   R+   R   (    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR   A  s   t   NativeRLockTestsc           B  s   e  Z e e j  Z RS(    (   R   R   R   R   t   NativeRLockR   (    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR   E  s   t
   EventTestsc           B  s   e  Z e e j  Z RS(    (   R   R   R   R   RG   t	   eventtype(    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR   K  s   t   ConditionAsRLockTestsc           B  s   e  Z e e j  Z RS(    (   R   R   R   R   t	   ConditionR   (    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR   O  s   t   ConditionTestsc           B  s   e  Z e e j  Z RS(    (   R   R   R   R   R   t   condtype(    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR   T  s   t   SemaphoreTestsc           B  s   e  Z e e j  Z RS(    (   R   R   R   R   t	   Semaphoret   semtype(    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR   X  s   t   BoundedSemaphoreTestsc           B  s   e  Z e e j  Z RS(    (   R   R   R   R   R*   R   (    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyR   \  s   t   __main__(.   t
   __future__R    t   gevent.testing.sixR   t   gevent.testingt   testingR   t   setup_R8   t   splitR~   R   t   test.supportR   Rd   t   test.test_supportR    R4   R|   R   RH   t   _threadR#   R   R   t   gevent.testsR   R   R   R   R   R   t   TestCaseR(   R   R   R   R   R   R   R   R   R   R   R   t   main(    (    (    s=   lib/python2.7/site-packages/gevent/tests/test__threading_2.pyt   <module>   sd   ""	  Lp