
    f5_              	          S S/r SSKrSSKrSSKJr  SSKrSSKrSSKrSSKrSSKrSSK	J
r
  SSKJr  SSKJr   \" \R                   \R                   5      R"                  r " S	 S
5      r " S S5      r " S S\R,                  5      r " S S5      r\R2                  S:w  aQ   " S S\\\R4                  5      rS r " S S\5      r " S S\R<                  5      r " S S\\R@                  5      r!OB " S S\"5      r# " S S\\\RH                  \#S9r " S S\5      r " S S \\RJ                  5      r& " S! S \RN                  5      r(g! \ a    \R                   R"                  r GNf = f)"GLibEventLoopPolicyGLibEventLoop    N)
coroutines)contextmanager   )_ossighelper)GLibc                   V    \ rS rSrSrS r\S 5       r\S 5       rS r	S r
S rS	 rS
rg)_GLibEventLoopMixin*   a  
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                     Uc   eXl         [        R                  R                  U R                   S5      U l        / U l        g NF)_contextr	   MainLoopnew
_main_loop_quit_funcs)selfmain_contexts     +/usr/lib/python3/dist-packages/gi/events.py__init___GLibEventLoopMixin.__init__;   s7    '''$--++DMM5A    c              #   :  #    U R                  5       (       a%  U R                  R                  R                  (       a  Sv   g U R                  R	                  5         Sv   U R                  R                  5         g! U R                  R                  5         f = f7f)zThis context manager ensures the EventLoop is *not* being iterated.

It purely exist to handle the case where python code iterates the main
context more gracefully.N)
is_running	_selector_source_dispatchingdetachattachr   s    r   paused_GLibEventLoopMixin.pausedB   sd        DNN$:$:$G$G	$NN!!#NN!!#DNN!!#s   A BA< !B<BBc              #     #    U R                   R                  5       (       d   eU R                  R                  U5        U R	                  5       (       a_   Sv   U R                   R                  5         U R                  R                  5         U R                  (       a  U R                  S   " 5         gU R                  5         U R                  U R                  5        [        R                  " 5       U l        [        R                  " 5       n[        R                   " U R"                  U R$                  S9   [&        R(                  " U 5        U R*                  R,                  R.                  (       a   eU R*                  R1                  5         Sv   U R*                  R3                  5         U R                   R                  5         SU l        [&        R(                  " S5         U R                  S5        [        R                   " U6   U R                  R                  5         [7        U R                  5      S:X  d   eSU l        g! U R                   R                  5         U R                  R                  5         U R                  (       a  U R                  S   " 5         f f = f! [4         a     Nf = f! U R*                  R3                  5         U R                   R                  5         SU l        [&        R(                  " S5         U R                  S5        O! [4         a     Of = f[        R                   " U6   U R                  R                  5         [7        U R                  5      S:X  d   eSU l        f = f7f)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.
N)	firstiter	finalizerFr   )r   acquirer   appendr   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_loopr   r   r   r    r   AttributeErrorlen)r   	quit_funcold_agen_hookss      r   running_GLibEventLoopMixin.runningS   s     }}$$&&&&	*??+%%'  $$&>>$$R(* 	++DKK8#--///1)F)F)-)F)F	H	#%%d+~~--::::NN!!#NN!!#MM!!#"DO%%d+33E: ""N3  "t''(A---"DNC %%'  $$&>>$$R(* "0 "  NN!!#MM!!#"DO%%d+33E:! ""N3  "t''(A---"DNs   AM<I CM<7AJ4 AM<$J$ 5AM<AJ!!M<$
J1.M<0J11M<4AM9LM9
L&#M9%L&&AM99M<c                 2    [         R                  " 5       S-  $ )Ni@B )r	   get_monotonic_timer!   s    r   time_GLibEventLoopMixin.time   s    &&(722r   c                 ~   U R                  5       (       d  [        R                  " S[        5        U R                  SL a  [        R                  " S[        5        U R
                  (       a  gU R                  (       a@  [        U R                  S   R                  U R                  5       -
  S-  5      nUS:  a  U$ S$ g)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%   )
r   warningswarnRuntimeWarningr,   _ready
_scheduledint_whenrB   r   timeouts     r   _get_timeout_ms#_GLibEventLoopMixin._get_timeout_ms   s      MM  H  JX  Y>>T!MMt  wE  F;;?? 4??1-33diikATIJG%l711r   c                 ^    SU l         U R                  (       a  U R                  S   " 5         g g )NTr%   )r,   r   r!   s    r   stop_GLibEventLoopMixin.stop   s(    R " r   c                     SU R                   R                   SU R                  5        SU R                  5        SU R	                  5        S[        U R                  5      S S[        U R                  5      S S3$ )	N<z	 running=z closed=z debug=z ctx=0xXz loop=0x>)	__class____name__r   	is_closed	get_debughashr   r   r!   s    r   __repr___GLibEventLoopMixin.__repr__   sy    ''(	$//2C1D Enn&'wt~~/?.@ A$--(+8D4I!3LAO	
r   )r   r   r   r,   r2   N)rX   
__module____qualname____firstlineno____doc__r   r   r"   r>   rB   rN   rQ   r\   __static_attributes__ r   r   r   r   *   sI      $ $  /# /#b3 #
r   r   c                       \ rS rSrS rSrg)_GLibEventLoopRunMixin   c                 t   U R                  5       (       a  [        S5      e[        R                  " U R                  R
                  5         U R                  U R                  R
                  5         [        U R                  5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)Nz"This event loop is already running)r   RuntimeErrorr   register_sigint_fallbackr   quitr>   g_main_loop_runr!   s    r   run_forever"_GLibEventLoopRunMixin.run_forever   st    ??CDD224??3G3GHdoo2230 4 IH33 IHs$   &B)1BB)
B&	"B))
B7rc   N)rX   r^   r_   r`   rl   rb   rc   r   r   re   re      s    
1r   re   c                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )_SourceBase   z3Common Source functionality for both unix and win32c                    > [        5       R                  5         SU l        U R                  S5        U R	                  S5        Xl        [        R                  " UR                  5      U l        / U l	        g )NFzpython asyncio integration)
superr   r   set_can_recurseset_namer   weakrefref_looprH   )r   selectorrW   s     r   r   _SourceBase.__init__   sX    !
 	U#23!
 [[0
r   c                     SU l          U R                  5       R                  5         SU l         [        R                  $ ! SU l         f = f)NTF)r   rw   	_run_oncer	   SOURCE_CONTINUE)r   callbackargss      r   dispatch_SourceBase.dispatch   sA     	&JJL""$ %D### !&Ds	   > 	Ac                 d    U R                   (       d  [        S5      eU R                  n/ U l        U$ )Nz>gi.asyncio.Selector.select only works while it is dispatching!)r   rh   rH   )r   readys     r   
_get_ready_SourceBase._get_ready   s,      _``r   )r   rw   rH   r   )
rX   r^   r_   r`   ra   r   r   r   rb   __classcell__rW   s   @r   ro   ro      s    =($ r   ro   c                   R   ^  \ rS rSrSrU 4S jrU 4S jrSS jrSU 4S jjrSr	U =r
$ )	_SelectorMixin   z>A Mixin for common functionality of the Selector and Proactor.c                 |   > [        5       R                  5         Xl        X l        0 U l        [        U 5      U l        g N)rr   r   r   rw   
_fd_to_key_Sourcer   r   contextlooprW   s      r   r   _SelectorMixin.__init__   s-    
t}r   c                    > U R                   (       a!  U R                   R                  5         S U l         [        5       R                  5         g r   )r   destroyrr   close)r   rW   s    r   r   _SelectorMixin.close   s,    <<LL  "DLr   c                 6    U R                   R                  5       $ r   )r   r   rL   s     r   select_SelectorMixin.select   s    ||&&((r   c                 6   > [        5       R                  U5      $ r   )rr   r   )r   rM   rW   s     r   _real_select_SelectorMixin._real_select   s    w~~g&&r   )r   r   rw   r   r   )rX   r^   r_   r`   ra   r   r   r   r   rb   r   r   s   @r   r   r      s    H%)' 'r   r   win32c                      ^  \ rS rSrSr\R                  \R                  \R                  \R                  \R                  \R                  1rS rU 4S jrU 4S jrS rU 4S jrSrU =r$ )	r      zhAn asyncio event loop that runs the python mainloop inside GLib.

Based on the asyncio.SelectorEventLoopc                     [         R                  X5        0 U l        [        U R                  U 5      n[
        R                  R                  X5        SU l        g NgMbP?)r   r   _GLibEventLoop__signal_handlers	_Selectorr   r8   SelectorEventLoop_clock_resolution)r   r   rx   s      r   r   GLibEventLoop.__init__  sH    ((< &(D" 5H%%..t> &*D"r   c                   > [         R                  " U5      (       d  [         R                  " U5      (       a  [        S5      eU R	                  5         XR
                  ;  a  [        5       R                  " X/UQ76 $ U R                  R                  US5      u  n  nU(       a  UR                  5         [        R                  " U5      nUR                  SU 35        UR                  [        R                  5        UR!                  U R"                  5        UR%                  U R&                  U5        XBU4U R                  U'   Ag)zAdd a handler for UNIX signalz3coroutines cannot be used with add_signal_handler())NNNzasyncio signal watch for N)r   iscoroutineiscoroutinefunction	TypeErrorr-   _GLIB_SIGNALSrr   add_signal_handlerr   getr   r	   unix_signal_source_newrt   set_priorityPRIORITY_HIGHr    r   set_callback
_signal_cb)r   sigr}   r~   source_rW   s         r   r    GLibEventLoop.add_signal_handler  s    &&x00228<< !< = = 
 ,,,w11#G$GG  1155c;MNLFAq  005FOO7u=> 2 23MM$--(5+1T*BD""3'r   c                   > XR                   ;  a  [        5       R                  U5      $  U R                  U   u  n  nU R                  U	 UR	                  5         UR                  5         U[        R                  :X  ao  [        R                  " [        R                  5      S:X  aG  [        R                  S:  a3  [        R                  " [        R                  [        R                  5        g! [         a     gf = f)Nr   TF)r   rr   remove_signal_handlerr   r   _clear_boxedsignalSIGINTr   PyOS_getsigstartup_sigint_ptrPyOS_setsigKeyError)r   r   r   r   rW   s       r   r   #GLibEventLoop.remove_signal_handler7  s    ,,,w44S99#55c:1**3/ ##%
 &--'L,D,DV]],SWX,X#66:$00@_@_` s   CC/ /
C<;C<c                 f    U R                   R                  U5      u  p#nU R                  " U/UQ76   g r   )r   r   	call_soon)r   r   r   cbr~   s        r   r   GLibEventLoop._signal_cbM  s0    #5599#>F NN2%%r   c                    > [        5       R                  5         [        U R                  5       H  nU R	                  U5        M     g r   )rr   r   listr   r   )r   srW   s     r   r   GLibEventLoop.closeS  s1    GMMO$001**1- 2r   )__signal_handlersr   )rX   r^   r_   r`   ra   r   SIGHUPr   SIGTERMSIGUSR1SIGUSR2SIGWINCHr   r   r   r   r   r   rb   r   r   s   @r   r   r      sX    	2  v~~v~~W]WeWegmgvgvw	*	<	,	&	. 	.r   c                 P    [        U [        5      (       a  U $ U R                  5       $ r   )
isinstancerJ   fileno)fileobjs    r   _fileobj_to_fdr   X  s!    gs##N>>##r   c                        \ rS rSrS rS rSrg)r   i_  c                 F    U R                  5       R                  5       nSU4$ r   )rw   rN   rL   s     r   prepare_Source.prepare`  s"    jjl224G '>!r   c                    / nU R                   R                  R                  5        H  nU R                  UR                  5      nSnU[
        R                  R                  ) -  (       a  U[        R                  -  nU[
        R                  R                  ) -  (       a  U[        R                  -  nU(       d  M  UR                  X$45        M     Xl        U R                  5       R                  5       nUS:X  a  g[!        U5      $ )Nr   T)r   r   valuesquery_unix_fd_tagr	   IOConditionOUT	selectors
EVENT_READINEVENT_WRITEr)   rH   rw   rN   bool)r   r   key	conditioneventsrM   s         r   check_Source.checkg  s    E~~00779 ..sxx8	 0 0 4 444i222F 0 0 3 333i333F6LL#/ :  Kjjl224G!|;r   )rH   N)rX   r^   r_   r`   r   r   rb   rc   r   r   r   r   _  s    	"	r   r   c                       \ rS rSrSrg)_SelectorKeyi|  rc   N)rX   r^   r_   r`   rb   rc   r   r   r   r   |  s    r   r   c                   F    \ rS rSrSrS rS rS rSS jrS r	S	 r
S
 rSrg)r   i  zGA Selector for gi.events.GLibEventLoop registering python IO with GLib.c                 b    U R                   R                  U R                  R                  5        g r   )r   r    rw   r   r!   s    r   r    _Selector.attach  s    LL

 3 34r   c                     U R                   R                  5         [        U 5      U l         U R                  R	                  5        H  nU R                  U5        M     g r   )r   r   r   r   r   _register_key)r   r   s     r   r   _Selector.detach  sB    LL  ""4=DL--/""3' 0r   c                    [         R                  " S5      nUR                  [        R                  -  (       a  U[         R                  R
                  -  nUR                  [        R                  -  (       a  U[         R                  R                  -  nU R                  R                  UR                  U5      Ul        g )Nr   )r	   r   r   r   r   r   r   r   r   add_unix_fdfdr   )r   r   r   s      r   r   _Selector._register_key  s~    ((+IzzI000T--000	zzI111T--111	||//	BCHr   Nc                 *   U(       a*  U[         R                  [         R                  -  ) -  (       a  [        SR	                  U5      5      e[        U5      nX@R                  ;  d   e[        XX#5      nU R                  U5        XPR                  U'   U$ )NzInvalid events: {!r})	r   r   r   
ValueErrorformatr   r   r   r   )r   r   r   datar   r   s         r   register_Selector.register  s|    9+?+?)BWBW+W)X X !7!>!>v!FGG(B__,,,wF9Cs#"%OOBJr   c                     [        U5      nU R                  U   nU R                  (       a%  U R                  R                  UR                  5        U R                  U	 U$ r   )r   r   r   remove_unix_fdr   )r   r   r   r   s       r   
unregister_Selector.unregister  sH    (B//"%C||++CHH5#Jr   c                 6    [        U5      nU R                  U   $ r   )r   r   )r   r   r   s      r   get_key_Selector.get_key  s    (B??2&&r   c                 v    U R                   R                  5        Vs0 s H  oR                  U_M     sn$ s  snf )z2Return a mapping of file objects to selector keys.)r   r   r   )r   ks     r   get_map_Selector.get_map  s1    
 +///*@*@*BC*BQIIqL*BCCCs   6r   r   )rX   r^   r_   r`   ra   r    r   r   r   r   r   r  rb   rc   r   r   r   r     s*    U	5	(	C				'	Dr   r   c                       \ rS rSrS rSrg)_PushRunMixinBackMetai  c                     [         R                  U 5      nUR                  [        5      n/ US U QXS-      PX   PXS-   S  Q$ )Nr      )typemroindexre   )clsr
  idxs      r   r
  _PushRunMixinBackMeta.mro  sN    ((3-C))23CGS#YG!GGchG1WXGGr   rc   N)rX   r^   r_   r`   r
  rb   rc   r   r   r  r    s    
	Hr   r  c                       \ rS rSrSrS rSrg)r   i  zuAn asyncio event loop that runs the python mainloop inside GLib.

Based on the asyncio.WindowsProactorEventLoopPolicyc                     [         R                  X5        [        U R                  U 5      n[        R
                  R                  X5        SU l        g r   )r   r   	_Proactorr   r8   ProactorEventLoopr   )r   r   proactors      r   r   r     s>    ((< 5H%%..t> &*D"r   )r   N)rX   r^   r_   r`   ra   r   rb   rc   r   r   r   r     s    	?
	*r   )	metaclassc                   @   ^  \ rS rSrU 4S jrS rS rS rS rSr	U =r
$ )r   i  c                 R   > Xl         [        5       R                  U5        S U l        g r   )	_proactorrr   r   _poll_fd)r   r  rW   s     r   r   _Source.__init__  s!    %NGX& !DMr   c                     U R                   b   e[        R                  " U R                  R                  [        R
                  5      U l         U R                  U R                   5        g r   )r  r	   PollFDr  _iocpIO_INadd_pollr!   s    r   enable_Source.enable  sB    ==((( KK(<(<djjIDMMM$--(r   c                 H    U R                  U R                  5        S U l        g r   )remove_pollr  r!   s    r   disable_Source.disable  s    T]]+ DMr   c                     U R                   c  gU R                  5       R                  5       n[        U R                  5      U4$ )N)Fr%   )r  rw   rN   r   rH   rL   s     r   r   r     s7    }}$ jjl224G$g--r   c                 (   U R                   c  gU R                   R                  (       a4  U R                  R                  U R                  R                  S5      5        U R                  (       a  gU R                  5       R                  5       S:X  a  gg)NFr   T)r  reventsrH   extendr  r   rw   rN   r!   s    r   r   r     sd    }}$}}$$""4>>#>#>q#AB{{zz|++-2r   )r  r  )rX   r^   r_   r`   r   r  r#  r   r   rb   r   r   s   @r   r   r     s!    	!	)	!	.	 	r   c                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )r  i  zGA Proactor for gi.events.GLibEventLoop registering python IO with GLib.c                    > [        5       R                  X5        [        U 5      U l        U R                  R	                  U5        g r   )rr   r   r   r   r    r   s      r   r   _Proactor.__init__  s2    GW+ #4=DLLL(r   c                 8    U R                   R                  5         g r   )r   r  r!   s    r   r    _Proactor.attach  s    LL!r   c                 8    U R                   R                  5         g r   )r   r#  r!   s    r   r   _Proactor.detach  s    LL  "r   r  )
rX   r^   r_   r`   ra   r   r    r   rb   r   r   s   @r   r  r    s    U	)	"	# 	#r   r  c                   b    \ rS rSrSrS rS rS rS rS r	\
R                  S:w  a  S	 rS
rgS
rg)r   i  ae  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                      0 U l         S U l        g r   )_loops_child_watcherr!   s    r   r   GLibEventLoopPolicy.__init__$  s    "r   c                 T   [         R                  R                  5       nUcI  [        R                  " 5       [        R
                  " 5       L a  [         R                  R                  5       nUc+  [        S[        R                  " 5       R                  -  5      eU R                  U5      $ )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.)
r	   MainContextget_thread_defaultr0   current_threadmain_threaddefaultrh   nameget_event_loop_for_context)r   ctxs     r   get_event_loop"GLibEventLoopPolicy.get_event_loop(  s     113;93359N9N9PP""**,C ;L!*!9!9!;!@!@ A B B ..s33r   c                     U R                   [        U5         nUR                  5       (       d  U$  [	        U5      U R                   [        U5      '   U R
                  (       aK  U[        R                  R                  5       :X  a)  U R
                  R                  U R                  5       5        U R                   [        U5         $ ! [         a     Nf = f)z*Get the event loop for a specific context.)r2  r[   rY   r   r   r3  r	   r6  r:  attach_loopr>  )r   r=  r   s      r   r<  .GLibEventLoopPolicy.get_event_loop_for_contextC  s    
	;;tCy)D>>## $
 "/s!3DI3$*:*:*B*B*D#D++D,?,?,AB{{49%%  		s   .C 
CCc                 0   Ub  [        U[        5      (       d   e[        R                  R	                  5       =p#UcI  [
        R                  " 5       [
        R                  " 5       L a  [        R                  R                  5       nUc  U R                  R                  [        U5      S5      nU(       ae  [        UR                  5      [        U5      :w  a  [        R                  " S[        5        U(       a   [        R                  R!                  U5        gggU(       a+  [#        S[
        R                  " 5       R$                  -  5      e[        R                  R'                  UR                  5        XR                  [        UR                  5      '   g)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!zWThread %r already has a main context, get_event_loop() will create a new loop if needed)r   r   r	   r6  r7  r0   r8  r9  r:  r2  r+   r[   r   rE   rF   rG   pop_thread_defaultrh   r;  push_thread_default)r   r   r=  ctx_tdolds        r   set_event_loop"GLibEventLoopPolicy.set_event_loopT  s(    |z$>>>>''::<<;93359N9N9PP""**,C<++//$s)T2C%c2MM"`bpq$$77?   "#|%.%=%=%?%D%D$E F F 00?/3KKT]]+,r   c                 >    [        [        R                  " 5       5      $ )zHCreate and return a new event loop that iterates a new
GLib.MainContext.)r   r	   r6  r!   s    r   new_event_loop"GLibEventLoopPolicy.new_event_loops  s     T--/00r   r   c                    U R                   cn  [        R                  " 5       U l         [        R                  " 5       [        R
                  " 5       L a)  U R                   R                  U R                  5       5        U R                   $ r   )r3  r8   ThreadedChildWatcherr0   r8  r9  rA  r>  r!   s    r   get_child_watcher%GLibEventLoopPolicy.get_child_watcher  sb    ""*&-&B&B&D#++-1F1F1HH''33D4G4G4IJ&&&r   )r3  r2  N)rX   r^   r_   r`   ra   r   r>  r<  rH  rK  r3   platformrO  rb   rc   r   r   r   r     s8     #46&"4>1 ||w	' r   ))__all__r3   r8   r   r   r0   r   ru   rE   
contextlibr    r   gi.repositoryr	   rr   r   runrk   r:   r   re   Sourcero   r   rQ  r   r   r   r   SelectorKeyr   BaseSelectorr   r	  r  r  IocpProactorr  AbstractEventLoopPolicyr   rc   r   r   <module>r\     se  * !/
2 
        %  (DMM4==9==O
y
 y
x1 1&$++ &R' '2 <<7^.+-CWE^E^ ^.@$+ :y,, 9DNI$:$: 9Dz	H 	H*+-CWE^E^j * (+ (T#NG$8$8 #"w''99 w'[  (mm''O(s   'E E-,E-