
    giw                     `   d dgZ ddlZddlZddlZddlm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 dd	lmZ 	  eej$                  ej$                        j&                  Z G d
 dej,                        Z G d dej0                        Z G d d      Z G d d      Z G d dej,                        Z G d d      Zej<                  dk7  r\ G d d eeej>                        Z d Z! G d de      Z" G d dejF                        Z$ G d de      Z% G d d eejL                        Z'nE G d! d"e(      Z) G d# d eeejT                  e)$      Z  G d% de      Z" G d& d'eejV                        Z, G d( dejZ                        Z.y# e$ r ej$                  j&                  ZY Aw xY w))GLibEventLoopGLibEventLoopPolicy    N)
coroutines)Mapping)contextmanager   )_ossighelper)GLibc                   4     e Zd ZdZ fdZd Zd Zd Z xZS )_IdleSourcezqInternal helper source for idle task handling

    The only advantage is that we can keep the source around.
    c                 f    t               j                          || _        | j                  d       y NF)super__init___loopset_can_recurse)selfloop	__class__s     +/usr/lib/python3/dist-packages/gi/events.pyr   z_IdleSource.__init__2   s&    
U#    c                 r    | j                   j                  syt        | j                   j                        dfS )NFr   r   _may_iteratebool_idle_tasksr   s    r   preparez_IdleSource.prepare9   s,    zz&&DJJ**+R//r   c                 n    | j                   j                  syt        | j                   j                        S r   r   r   s    r   checkz_IdleSource.check?   s'    zz&&DJJ**++r   c                 V    | j                   j                          t        j                  S N)r   _glib_idle_dispatchr
   SOURCE_CONTINUEr   callbackargss      r   dispatchz_IdleSource.dispatchE   s    

&&(###r   )	__name__
__module____qualname____doc__r   r    r"   r*   __classcell__r   s   @r   r   r   ,   s    
$0,$r   r   c                   >     e Zd ZdZ fdZd Zd Zed        Z xZ	S )GLibTaskzThis is a simple asyncio.Task subclass that will be returned when using the
    GLibEventLoop. It adds functionality to set the priority that is used to
    iterate the task's coroutine.
    c                 d     t               j                  |i | t        j                  | _        y r$   )r   r   r
   PRIORITY_DEFAULT_glib_idle_priority)r   r)   kwargsr   s      r   r   zGLibTask.__init__P   s(    $)&)#'#8#8 r   c                 6    t        |t              sJ || _        y)z:Set the GLib priority used to iterate the task's coroutineN)
isinstanceintr5   r   prioritys     r   set_priorityzGLibTask.set_priorityU   s    (C(((#+ r   c                     | j                   S )z:Get the GLib priority used to iterate the task's coroutine)r5   r:   s     r   get_priorityzGLibTask.get_priority[   s    '''r   c                     t        |fd|i|S )Nr   )r2   )clsr   coror6   s       r   _factoryzGLibTask._factory_   s    242622r   )
r+   r,   r-   r.   r   r<   r>   classmethodrB   r/   r0   s   @r   r2   r2   J   s+    
9
,( 3 3r   r2   c                   l     e Zd ZdZd Zed        Zed        Zd Zd Z	 fdZ
d Zd	 Zd
 Zd Z xZS )_GLibEventLoopMixina  Base functionally required for both proactor and selector.

    The proactor/selector is always available through _selector, and we assume
    it has the following extra functionality that we provide:
     * _source: the GSource subclass
     * _dispatching: boolean whether it is dispatching currently
     * attach/detach: add/remove the GSource from the main context

    In principle, we simply override run_forever to call into GLib, with the
    assumption that a GSource is registered which will then call back into
    the python mainloop _run_once handler when needed. This in turn calls
    self._selector.select(), which means we just need to make sure to return
    our already prepared events at that point.
    c                     |J || _         t        j                  j                  | j                   d      | _        g | _        g | _        d| _        y r   )_contextr
   MainLoopnew
_main_loop_quit_funcsr   r   )r   main_contexts     r   r   z_GLibEventLoopMixin.__init__t   sI    '''$--++DMM5A!r   c              #     K   | j                   sd y	 d| _         | j                  j                          d d| _         | j                  j                          y# d| _         | j                  j                          w xY ww)zThis context manager ensures the EventLoop is *not* being iterated.

        It purely exists to handle the case where python code iterates the main
        context more gracefully.
        NFT)r   	_selectordetachattachr   s    r   pausedz_GLibEventLoopMixin.paused}   sj        	$ %DNN!!# $DNN!!# !%DNN!!#s   B%A "B#A??Bc              #     K   | j                   j                         sJ | j                  j                  |       | j	                         rZ	 d | j                   j                          | j                  j                          | j                  r | j                  d           y| j                          | j                  | j                         t        j                         | _        t        j                         }t        j                   | j"                  | j$                         	 t'        j(                  |        | j*                  j,                  j.                  rJ d| _        | j*                  j3                          t5        |       | _        | j6                  j3                  | j                          | j6                  j9                  d       | j:                  r+| j6                  j=                  | j:                  d   d          d d| _        | j6                  j?                          d| _        | j*                  jA                          | j                   j                          d| _        t'        j(                  d       tC        jD                  tF              5  | j                  d       ddd       t        j                   |  | j                  j                          tI        | j                        dk(  sJ d| _        y# | j                   j                          | j                  j                          | j                  r | j                  d           w w xY w# 1 sw Y   xY w# d| _        | j6                  j?                          d| _        | j*                  jA                          | j                   j                          d| _        t'        j(                  d       tC        jD                  tF              5  | j                  d       ddd       n# 1 sw Y   nxY wt        j                   |  | j                  j                          tI        | j                        dk(  sJ d| _        w xY ww)zThis context manager ensures the EventLoop is marked as running
        while other API is iterating its main context.
        The passed quit function is used to stop all recursion levels when
        stop() is called.
        Nr   )	firstiter	finalizerTzGLibEventLoop._idle_sourcer   F)%rG   acquirerK   append
is_runningreleasepop	_stopping_check_closed_set_coroutine_origin_tracking_debug	threading	get_ident
_thread_idsysget_asyncgen_hooksset_asyncgen_hooks_asyncgen_firstiter_hook_asyncgen_finalizer_hookasyncio_set_running_looprN   _source_dispatchingr   rP   r   _idle_sourceset_namer   r<   destroyrO   
contextlibsuppressAttributeErrorlen)r   	quit_funcold_agen_hookss      r   runningz_GLibEventLoopMixin.running   sh     }}$$&&&	*??+%%'  $$&>>(D$$R(* 	++DKK8#--///13333	
	#%%d+~~--:::: $DNN!!# +D 1D$$T]]3&&'CD!!..t/?/?/B1/EF %D%%' $DNN!!#MM!!#"DO%%d+$$^4 ;33E:;""N3  "t''(A---"DNU %%'  $$&>>(D$$R(* "B; ; !&D%%' $DNN!!#MM!!#"DO%%d+$$^4 ;33E:; ; ;""N3  "t''(A---"DNsm   AQ4L CQ4(C#M* BQ4M.AQ4AMQ4M'#Q4*BQ1<P	Q1P AQ11Q4c                 0    t        j                         dz  S )Ni@B )r
   get_monotonic_timer   s    r   timez_GLibEventLoopMixin.time   s    &&(722r   c                 X   | j                         st        j                  dt               | j                  du rt        j                  dt               | j
                  ry| j                  r?t        | j                  d   j                  | j                         z
  dz        }|dk\  r|S dS y)NzkGLibEventLoop is iterated without being marked as running. Missing override or invalid use of existing API!TzXGLibEventLoop is not stopping properly. Missing override or invalid use of existing API!r   i  r   )
rW   warningswarnRuntimeWarningrZ   _ready
_scheduledr9   _whenrv   r   timeouts     r   _get_timeout_msz#_GLibEventLoopMixin._get_timeout_ms   s     MM} >>T!MMj ;;?? 4??1-33diikATIJG%l711r   c                    	 |j                   j                  }|t        j                  k(  rt               j                  |||      S t        j                  ||| |      }| j                  j                  ||f       | j                  j                  d        | j                  j                  | j                  d   d          |S # t        $ r t        j                  }Y w xY w)Nc                     | d   S Nr    )xs    r   <lambda>z0_GLibEventLoopMixin._call_soon.<locals>.<lambda>   s
    AaD r   )keyr   )__self__r5   ro   r
   r4   r   
_call_soonrf   Handler   rV   sortrj   r<   )r   r(   r)   contextr;   handler   s         r   r   z_GLibEventLoopMixin._call_soon   s    	-
  ((<<H t,,,7%%hg>>$g>6 23.1 	&&t'7'7':1'=>  	-,,H	-s   C C"!C"c                 \    | j                   sJ d| _         | j                          d| _         y )NFT)r   	_run_oncer   s    r   _glib_dispatchz"_GLibEventLoopMixin._glib_dispatch   s-         " r   c                    | j                   sJ | j                         5  | j                  j                         }g }| j                  rd| j                  d   d   |k(  rO|j                  | j                  j                  d      d          | j                  r| j                  d   d   |k(  rO|D ]  }|j                           | j                  r+| j                  j                  | j                  d   d          d d d        y # 1 sw Y   y xY w)Nr   r   )	r   rQ   rj   r>   r   rV   rY   _runr<   )r   r;   ready_handlesr   s       r   r%   z'_GLibEventLoopMixin._glib_idle_dispatch  s        
 [[] 	G((557HM""t'7'7':1'='I$$T%5%5%9%9!%<Q%?@ ""t'7'7':1'='I (  !!..t/?/?/B1/EF	G 	G 	Gs   BD,ADDc                 T    d| _         | j                  r | j                  d           y y )NTr   )rZ   rK   r   s    r   stopz_GLibEventLoopMixin.stop  s*     DR " r   c                     d| j                   j                   d| j                          d| j                          d| j	                          dt        | j                        ddt        | j                        ddS )	N<z	 running=z closed=z debug=z ctx=0xXz loop=0x>)r   r+   rW   	is_closed	get_debughashrG   rJ   r   s    r   __repr__z_GLibEventLoopMixin.__repr__  sy    ''(	$//2C1D Enn&'wt~~/?.@ A$--(+8D4I!3LAO	
r   )r+   r,   r-   r.   r   r   rQ   rs   rv   r   r   r   r%   r   r   r/   r0   s   @r   rE   rE   d   sZ    " $ $& 8# 8#t3,.!G(#
r   rE   c                       e Zd Zd Zy)_GLibEventLoopRunMixinc                 T   | j                         rt        d      t        j                  | j                  j
                        5  | j                  | j                  j
                        5  t        | j                         d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nz"This event loop is already running)rW   RuntimeErrorr	   register_sigint_fallbackrJ   quitrs   g_main_loop_runr   s    r   run_foreverz"_GLibEventLoopRunMixin.run_forever+  s    ??CDD 11$//2F2FG	-LL--.	- DOO,		- 	- 	- 	- 	- 	-s$   &B+BBB	BB'N)r+   r,   r-   r   r   r   r   r   r   &  s    
	-r   r   c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )_SourceBasez3Common Source functionality for both unix and win32c                     t               j                          d| _        | j                  d       | j	                  d       t        j                  |      | _        g | _        y )NFzpython asyncio integration)	r   r   ri   r   rk   weakrefrefrN   r{   )r   selectorr   s     r   r   z_SourceBase.__init__:  sO    !
 	U#23 !X.r   c                 6    | j                         j                  S r$   )rN   r   r   s    r   r   z_SourceBase._loopP  s    ~~%%%r   c                     d| _         	 | j                         j                          d| _         t        j                  S # d| _         w xY w)NTF)ri   r   r   r
   r&   r'   s      r   r*   z_SourceBase.dispatchS  sA     	&JJL'') %D### !&Ds	   > 	Ac                 Z    | j                   st        d      | j                  }g | _        |S )Nz>gi.asyncio.Selector.select only works while it is dispatching!)ri   r   r{   )r   readys     r   
_get_readyz_SourceBase._get_ready]  s2      P  r   )	r+   r,   r-   r.   r   r   r*   r   r/   r0   s   @r   r   r   7  s    =,&$r   r   c                   @     e Zd ZdZ fdZ fdZddZd fd	Z xZS )_SelectorMixinz>A Mixin for common functionality of the Selector and Proactor.c                     t               j                          || _        || _        i | _        t        |       | _        y r$   )r   r   rG   r   
_fd_to_key_Sourcerh   r   r   r   r   s      r   r   z_SelectorMixin.__init__k  s1    
t}r   c                     | j                   r!| j                   j                          d | _         t               j                          y r$   )rh   rl   r   close)r   r   s    r   r   z_SelectorMixin.closet  s,    <<LL  "DLr   c                 6    | j                   j                         S r$   )rh   r   r~   s     r   selectz_SelectorMixin.selectz  s    ||&&((r   c                 6    t               j                  |      S r$   )r   r   )r   r   r   s     r   _real_selectz_SelectorMixin._real_select}  s    w~~g&&r   r$   )	r+   r,   r-   r.   r   r   r   r   r/   r0   s   @r   r   r   h  s    H%)' 'r   r   win32c                        e Zd ZdZej
                  ej                  ej                  ej                  ej                  ej                  hZd Z fdZ fdZd Z fdZ xZS )r   zyAn asyncio event loop that runs the python mainloop inside GLib.

        Based on the asyncio.SelectorEventLoop
        c                     t         j                  | |       i | _        t        | j                  |       }t
        j                  j                  | |       d| _        t        j                  | _
        y NgMbP?)rE   r   _GLibEventLoop__signal_handlers	_SelectorrG   rf   SelectorEventLoop_clock_resolutionr2   rB   _task_factory)r   rL   r   s      r   r   zGLibEventLoop.__init__  s\    ((|< &(D" 5H%%..tX> &*D" "*!2!2Dr   c                 j   t        j                  |      st        j                  |      rt        d      | j	                          || j
                  vr t               j                  ||g| S | j                  j                  |d      d   }|r|j                          t        j                  |      }|j                  d|        |j                  t        j                         |j!                  | j"                         |j%                  | j&                  |       |||f| j                  |<   ~y)zAdd a handler for UNIX signalz3coroutines cannot be used with add_signal_handler())NNNr   zasyncio signal watch for N)r   iscoroutineiscoroutinefunction	TypeErrorr[   _GLIB_SIGNALSr   add_signal_handlerr   getrl   r
   unix_signal_source_newrk   r<   PRIORITY_HIGHrP   rG   set_callback
_signal_cb)r   sigr(   r)   sourcer   s        r   r   z GLibEventLoop.add_signal_handler  s   %%h/:3Q3Q4   UVV 
 $,,,1uw11#xG$GG ++//5GHKF  005FOO7u=> 2 23MM$--(5+18T*BD""3'r   c                    || j                   vrt               j                  |      S 	 | j                  |   \  }}}| j                  |= |j	                          |j                          |t        j                  k(  rkt        j                  t        j                        dk(  rEt        j                  dkD  r2t        j                  t        j                  t        j                         y# t        $ r Y yw xY w)Nr   TF)r   r   remove_signal_handlerr   rl   _clear_boxedsignalSIGINTr	   PyOS_getsigstartup_sigint_ptrPyOS_setsigKeyError)r   r   r   _r   s       r   r   z#GLibEventLoop.remove_signal_handler  s    $,,,w44S99#55c:1**3/ ##% 6==($00?1D$77!; ,,|'F'F  s   B>C) )	C54C5c                 h    | j                   j                  |      \  }}} | j                  |g|  y r$   )r   r   	call_soon)r   r   rh   cbr)   s        r   r   zGLibEventLoop._signal_cb  s4     $ 6 6 : :3 ?GR DNN2%%r   c                     t               j                          t        | j                        D ]  }| j	                  |        y r$   )r   r   listr   r   )r   sr   s     r   r   zGLibEventLoop.close  s6    GMMO$001 .**1-.r   )r+   r,   r-   r.   r   SIGHUPr   SIGTERMSIGUSR1SIGUSR2SIGWINCHr   r   r   r   r   r   r/   r0   s   @r   r   r     sY    	 MMMMNNNNNNOO
,	3 	<	6	&	. 	.r   c                 F    t        | t              r| S | j                         S r$   )r8   r9   fileno)fileobjs    r   _fileobj_to_fdr     s    gs#N~~r   c                       e Zd Zd Zd Zy)r   c                 F    | j                         j                         }d|fS r   )r   r   r~   s     r   r    z_Source.prepare  s"    jjl224G '>!r   c                    g }| j                         j                  j                         D ]  }| j                  |j                        }d}|t
        j                  j                   z  r|t        j                  z  }|t
        j                  j                   z  r|t        j                  z  }|s|j                  ||f        || _        | j                         j                         }|dk(  ryt!        |      S )Nr   T)rN   r   valuesquery_unix_fd_tagr
   IOConditionOUT	selectors
EVENT_READINEVENT_WRITErV   r{   r   r   r   )r   r   r   	conditioneventsr   s         r   r"   z_Source.check  s    E~~'2299; 	0 ..sxx8	 0 0 4 444i222F 0 0 3 333i333FLL#v/	0  DKjjl224G!|;r   N)r+   r,   r-   r    r"   r   r   r   r   r     s    	"	r   r   c                       e Zd Zy)_SelectorKeyN)r+   r,   r-   r   r   r   r   r     s    r   r   c                   ,    e Zd Zd Zd ZddZd Zd Zy)_FileObjectMappingc                     || _         y r$   )fd_dict)r   r   s     r   r   z_FileObjectMapping.__init__  s	    "DLr   c                 ,    t        | j                        S r$   )rp   r   r   s    r   __len__z_FileObjectMapping.__len__   s    t||$$r   Nc                 P    t        |      }| j                  j                  ||      S r$   )r   r   r   )r   r   defaultfds       r   r   z_FileObjectMapping.get#  s#    (B<<##B00r   c                 H    | j                  |      }|t        |d      |S )Nz is not registered)r   r   )r   r   values      r   __getitem__z_FileObjectMapping.__getitem__'  s-    HHW%E}',>?@@Lr   c                 ,    t        | j                        S r$   )iterr   r   s    r   __iter__z_FileObjectMapping.__iter__-  s    %%r   r$   )r+   r,   r-   r   r  r   r  r
  r   r   r   r   r     s    	#	%	1		&r   r   c                   N     e Zd ZdZ fdZd Zd Zd Zd
dZd Z	d Z
d	 Z xZS )r   zGA Selector for gi.events.GLibEventLoop registering python IO with GLib.c                 n    t               j                  ||       t        | j                        | _        y r$   )r   r   r   r   _mapr   s      r   r   z_Selector.__init__3  s%    GWd+*4??;DIr   c                 b    | j                   j                  | j                  j                         y r$   )rh   rP   r   rG   r   s    r   rP   z_Selector.attach7  s    LL

 3 34r   c                     | j                   j                          t        |       | _         | j                  j	                         D ]  }| j                  |        y r$   )rh   rl   r   r   r   _register_key)r   r   s     r   rO   z_Selector.detach:  sG    LL  ""4=DL--/ (""3'(r   c                 l   t        j                  d      }|j                  t        j                  z  r|t         j                  j
                  z  }|j                  t        j                  z  r|t         j                  j                  z  }| j                  j                  |j                  |      |_        y r   )r
   r   r   r   r   r   r   r   rh   add_unix_fdr  r   )r   r   r   s      r   r  z_Selector._register_keyA  s~    ((+IzzI000T--000	zzI111T--111	||//	BCHr   c                     |r%|t         j                  t         j                  z   z  rt        d|      t	        |      }|| j
                  v rt        |d| d      t        ||||      }| j                  |       || j
                  |<   |S )NzInvalid events: z (FD z) is already registered)	r   r   r   
ValueErrorr   r   r   r   r  )r   r   r   datar  r   s         r   registerz_Selector.registerI  s    9//)2G2GGHH #3F:!>??(BT__$'E"5LMNNwFD9Cs#"%DOOBJr   c                     t        |      }| j                  |   }| j                  r%| j                  j                  |j                         | j                  |= |S r$   )r   r   rh   remove_unix_fdr   )r   r   r  r   s       r   
unregisterz_Selector.unregisterZ  sH    (B//"%C||++CHH5#Jr   c                      | j                   |   S r$   r  )r   r   s     r   get_keyz_Selector.get_keyg  s    99W%%r   c                     | j                   S )z_Return a mapping of file objects or file descriptors to
            selector keys.
            r  r   s    r   get_mapz_Selector.get_mapj  s     99r   r$   )r+   r,   r-   r.   r   rP   rO   r  r  r  r  r  r/   r0   s   @r   r   r   0  s/    U	<	5	(	C	"			&	r   r   c                       e Zd Zd Zy)_PushRunMixinBackMetac                     t         j                  |       }|j                  t              }g |d | ||dz      ||   ||dz   d  S )Nr      )typemroindexr   )r@   r$  idxs      r   r$  z_PushRunMixinBackMeta.mrox  sT    ((3-C))23CHS#YHC!GHc#hHS1WYHHr   N)r+   r,   r-   r$  r   r   r   r   r   s  s    
	Ir   r   c                       e Zd ZdZd Zy)r   zAn asyncio event loop that runs the python mainloop inside GLib.

        Based on the asyncio.WindowsProactorEventLoopPolicy
        c                     t         j                  | |       t        | j                  |       }t        j
                  j                  | |       d| _        t        j                  | _	        y r   )
rE   r   	_ProactorrG   rf   ProactorEventLoopr   r2   rB   r   )r   rL   proactors      r   r   zGLibEventLoop.__init__  sR    ((|< 5H%%..tX> &*D" "*!2!2Dr   N)r+   r,   r-   r.   r   r   r   r   r   r   ~  s    		3r   )	metaclassc                   6     e Zd Z fdZd Zd Zd Zd Z xZS )r   c                 F    t               j                  |       d | _        y r$   )r   r   _poll_fd)r   r+  r   s     r   r   z_Source.__init__  s    GX& !DMr   c                     | j                   J t        j                  | j                         j                  t        j
                        | _         | j                  | j                          y r$   )r/  r
   PollFDrN   _iocpIO_INadd_pollr   s    r   enablez_Source.enable  sE    ==((( KK(8(>(>

KDMMM$--(r   c                 H    | j                  | j                         d | _        y r$   )remove_pollr/  r   s    r   disablez_Source.disable  s    T]]+ DMr   c                     | j                   y| j                         j                         }t        | j                        |fS )Nr   )r/  r   r   r   r{   r~   s     r   r    z_Source.prepare  s7    }}$ jjl224G$g--r   c                    | j                   y| j                   j                  r8| j                  j                  | j	                         j                  d             | j                  ry| j                         j                         dk(  S )NFr   T)r/  reventsr{   extendrN   r   r   r   r   s    r   r"   z_Source.check  sd    }}$}}$$""4>>#3#@#@#CD{{::<//1Q66r   )	r+   r,   r-   r   r5  r8  r    r"   r/   r0   s   @r   r   r     s    	!	)	!	.
	7r   c                   .     e Zd ZdZ fdZd Zd Z xZS )r)  zGA Proactor for gi.events.GLibEventLoop registering python IO with GLib.c                     t               j                  ||       t        |       | _        | j                  j	                  |       y r$   )r   r   r   rh   rP   r   s      r   r   z_Proactor.__init__  s4    GWd+ #4=DLLL(r   c                 8    | j                   j                          y r$   )rh   r5  r   s    r   rP   z_Proactor.attach  s    LL!r   c                 8    | j                   j                          y r$   )rh   r8  r   s    r   rO   z_Proactor.detach  s    LL  "r   )r+   r,   r-   r.   r   rP   rO   r/   r0   s   @r   r)  r)    s    U	)	"	#r   r)  c                   `    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
ej                  d	k7  rd
 Zyy)r   a  An asyncio event loop policy that runs the GLib main loop.

    The policy allows creating a new EventLoop for threads other than the main
    thread. For the main thread, you can use get_event_loop() to retrieve the
    correct mainloop and run it.

    Note that, unlike GLib, python does not support running the EventLoop
    recursively. You should never iterate the GLib.MainContext from within
    the python EventLoop as doing so prevents asyncio events from being
    dispatched.

    As such, do not use API such as GLib.MainLoop.run or Gtk.Dialog.run.
    Instead use the proper asynchronous patterns to prevent entirely blocking
    asyncio.
    c                 .    i | _         d | _        d | _        y r$   )_loops_child_watcher!_GLibEventLoopPolicy__orig_policyr   s    r   r   zGLibEventLoopPolicy.__init__  s    "!r   c                 J   t         j                  j                         }|Ft        j                         t        j
                         u rt         j                  j                         }|+t        dt        j                         j                  d      | j                  |      S )a  Get the event loop for the current context.

        Returns an event loop object for the thread default GLib.MainContext
        or in case of the main thread for the default GLib.MainContext.

        An exception will be thrown if there is no GLib.MainContext for the
        current thread. In that case, using new_event_loop() will create a new
        main context and main loop which can subsequently attached to the thread
        by calling set_event_loop().

        Returns a new GLibEventLoop or raises an exception.
        z(There is no main context set for thread .)
r
   MainContextget_thread_defaultr^   current_threadmain_threadr  r   nameget_event_loop_for_context)r   ctxs     r   get_event_loopz"GLibEventLoopPolicy.get_event_loop  s     113;93359N9N9PP""**,C ;:9;S;S;U;Z;Z:]]^_  ..s33r   c                    	 | j                   t        |         }|j                         s|S 	 t	        |      | j                   t        |      <   | j
                  rJ|t        j                  j                         k(  r)| j
                  j                  | j                                | j                   t        |         S # t        $ r Y w xY w)z*Get the event loop for a specific context.)rC  r   r   r   r   rD  r
   rH  r  attach_looprO  )r   rN  r   s      r   rM  z.GLibEventLoopPolicy.get_event_loop_for_context  s    
	;;tCy)D>># $
 "/s!3DI3$*:*:*B*B*D#D++D,?,?,AB{{49%%  		s   )B< <	CCc                    |t        |t              sJ t        j                  j	                         x}}|Ft        j                         t        j                         u rt        j                  j                         }|| j                  j                  t        |      d      }|r^t        |j                        t        |      k7  rt        j                  dt               |r t        j                  j!                  |       yyy|r+t#        dt        j                         j$                  d      t        j                  j'                  |j                         || j                  t        |j                        <   y)zSet the event loop for the current context (python thread) to loop.

        This is only permitted if the thread has no thread default main context
        with the main thread using the default main context.
        Nz<GMainContext was changed unknowingly by asyncio integration!zThread zN already has a main context, get_event_loop() will create a new loop if needed)r8   r   r
   rH  rI  r^   rJ  rK  r  rC  rY   r   rG   rx   ry   rz   pop_thread_defaultr   rL  push_thread_default)r   r   rN  ctx_tdolds        r   set_event_loopz"GLibEventLoopPolicy.set_event_loop  s4    |z$>>>''::<<f;93359N9N9PP""**,C<++//$s)T2C%c2MMV& $$77?   "i668==@ AH H 
 00?/3DKKT]]+,r   c                 <    t        t        j                               S )zYCreate and return a new event loop that iterates a new
        GLib.MainContext.
        )r   r
   rH  r   s    r   new_event_loopz"GLibEventLoopPolicy.new_event_loop5  s     T--/00r   c                 b    t        j                         | _        t        j                  |        | S r$   )rf   get_event_loop_policyrE  set_event_loop_policyr   s    r   	__enter__zGLibEventLoopPolicy.__enter__;  s&    $::<%%d+r   c                     | j                   j                         D ]  }|j                           t        j                  | j
                         yr   )rC  r   r   rf   r\  rE  )r   exc_type	exc_value	tracebackr   s        r   __exit__zGLibEventLoopPolicy.__exit__@  sD    
 KK&&( 	DJJL	 	%%d&8&89 r   r   c                    | j                   jt        j                         | _         t        j                         t        j
                         u r)| j                   j                  | j                                | j                   S r$   )rD  rf   ThreadedChildWatcherr^   rJ  rK  rQ  rO  r   s    r   get_child_watcherz%GLibEventLoopPolicy.get_child_watcherX  sb    ""*&-&B&B&D#++-1F1F1HH''33D4G4G4IJ&&&r   N)r+   r,   r-   r.   r   rO  rM  rW  rY  r]  rb  ra   platformre  r   r   r   r   r     sC     "48&"!4F1
, ||w	' r   )/__all__rm   ra   rf   r   r   r^   r   r   rx   collections.abcr   r    r	   gi.repositoryr
   r   rH   runr   ro   Sourcer   Taskr2   rE   r   r   r   rf  r   r   r   r   SelectorKeyr   r   BaseSelectorr   r#  r   r*  IocpProactorr)  AbstractEventLoopPolicyr   r   r   r   <module>rr     s  * 1
2  
        # %  (DMM4==9==O
$$++ $<3w|| 34
 
D- -".$++ .b' '2 <<7p.3W5N5Np.d + :y,, &W &(>NI$:$: >F	I 	I3!!'	32$7+ $7L#NG$8$8 #"Q''99 Q'M  (mm''O(s   'F F-,F-