
    XhK                     T   d 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mZ	 ddl
mZ d ZddddddddZd Zd	 Z ed
dd      d        ZeZej$                  d        Zd Z ed      d        Zej$                  d        Z ed      d        Zej$                  d        Z ed      d        Zej$                  d        Z ed      d        Zej$                  d        Z ed      d        Zej$                  d        Z ed      d         Zej$                  d!        Z d" Z!d# Z"d%d$Z#y)&z4Event loop integration for the ZeroMQ-based kernels.    N)partial)Version)Applicationc                      t         j                  dk(  xr, t        t        j                         d         t        d      k\  S )zhShould we use appnope for dealing with OS X app nap?

    Checks if we are on OS X 10.9 or greater.
    darwinr   z10.9)sysplatformVmac_ver     H/var/www/html/myenv/lib/python3.12/site-packages/ipykernel/eventloops.py_use_appnoper      s4    
 <<8#M(*:*:*<Q*?(@AfI(MMr   )inlinenbaggwebaggnotebookipymplwidgetNc                        fd}|S )a0  Decorator to register an event loop to integrate with the IPython kernel

    The decorator takes names to register the event loop as for the %gui magic.
    You can provide alternative names for the same toolkit.

    The decorated function should take a single argument, the IPython kernel
    instance, arrange for the event loop to call ``kernel.do_one_iteration()``
    at least every ``kernel._poll_interval`` seconds, and start the event loop.

    :mod:`ipykernel.eventloops` provides and registers such functions
    for a few common event loops.
    c                 R     D ]  } t         |<    d  _         fd}| _         S )z#Integration registration decorator.c                      y Nr   kernels    r   <lambda>z9register_integration.<locals>.decorator.<locals>.<lambda>7   s    r   c                     | _         | S )zc@func.exit is now a decorator

            to register a function to be called on exit
            )	exit_hook)	exit_funcfuncs    r   exit_decoratorz?register_integration.<locals>.decorator.<locals>.exit_decorator9   s    
 'DNr   )loop_mapr   exit)r    namer!   toolkitnamess   `  r   	decoratorz'register_integration.<locals>.decorator2   s7      	"D!HTN	" -	 #	r   r   )r%   r&   s   ` r   register_integrationr'   $   s    $ r   c                 V    dd l ddlm} ddlm 	 ddlm  fd fd}t         d      s j                  j                  t        j                        }j                  | d	      j                   j                  j                         _         j                   j"                  j%                  |       n j                   j'                  d
        fd}|t(        _        j,                  j/                  d|       y # t        $ r  |d       fd       Y 
w xY w)Nr   )	lru_cacheQtCore)enum_helperc                      j                  | j                  d      d         t        j                  j                           S )N.r   )
attrgetter
rpartitionr   modules__package__)r$   r+   operators    r   r,   z&_notify_stream_qt.<locals>.enum_helperQ   s8    ?8&&ts';A'>?FL^L^@_``r   c                       j                   j                  d        j                  j                  j	                          y)fall back to main loopFN)_qt_notifier
setEnabledappqt_event_loopquitr   s   r   	exit_loopz$_notify_stream_qt.<locals>.exit_loopU   s-    &&u-

  %%'r   c                  N    j                   j                  d      r          yyz2fall back to main loop when there's a socket event   limitNshell_streamflush)r;   r   s   r   process_stream_eventsz0_notify_stream_qt.<locals>.process_stream_eventsZ   s&     $$1$-K .r   r6   zQtCore.QSocketNotifier.TypeTc                    t        d      sj                  j                        _        j                  j	                  d       j                  j                   d      j                         j                  j                  j                         j                  j                  t        d| z               y)2schedule fall back to main loop in [delay] seconds	_qt_timerTzQtCore.Qt.TimerType  N)hasattrQTimerr8   rG   setSingleShotsetTimerTypePreciseTimertimeoutconnectstartint)delayr+   r,   r;   r   s    r   _schedule_exitz)_notify_stream_qt.<locals>._schedule_exitn   s     v{+%}}VZZ8F**40))+6K*L*Y*YZ$$,,Y7s4%<01r   )r3   	functoolsr)   IPython.external.qt_for_kernelr+   r,   ImportErrorrI   rB   
getsockoptzmqFDQSocketNotifierReadr8   r9   r6   	activatedrO   r7   loop_qtrS   rJ   
singleShot)	r   r)   rD   fdrS   r+   r,   r;   r3   s	   `    @@@@r   _notify_stream_qtr`   G   s    #5a>(
 6>*  ++CFF3$449:??AYAY
 	%%--.CD&&t,2 ,G MMQ 56k  a	4	a 
	aas   D
 
D('D(qtqt5qt6c                     t        |        d| j                  _        | j                  j                  }t	        |d      r|j                         n|j                          d| j                  _        y)z,Event loop for all supported versions of Qt.TexecFN)r`   r8   _in_event_loopr9   rI   re   exec_)r   els     r   r]   r]      sR     f !%FJJ 
	!	!BV$BGGI"((* %FJJr   c                 8    | j                   j                          y r   )r8   r#   r   s    r   loop_qt_exitrj      s    
JJOOr   c                 @    d| _         | j                          d| _         y)zInner-loop for running the Wx eventloop

    Pulled from guisupport.start_event_loop in IPython < 5.2,
    since IPython 5.2 only checks `get_ipython().active_eventloop` is defined,
    rather than if the eventloop is actually running.
    TFN)rf   MainLoop)r8   s    r   _loop_wxrm      s     CLLNCr   wxc                     ddl t        d j                  z         fd G fddj                         G fddj                        }t         d	d      r t         j                  j                        s |d
       _        ddl}t        |j                  |j                              s&|j                  |j                  |j                         t         j                         y)z*Start a kernel with wx event loop support.r   NrH   c                  t     j                   j                  d      r j                  j                          yy)zwake from wxr>   r?   N)rB   rC   r8   ExitMainLoopr   s   r   wakezloop_wx.<locals>.wake   s2    $$1$-JJ##% .r   c                        e Zd Z fdZd Zy)loop_wx.<locals>.TimerFramec                     j                   j                  | d d       j                  |       | _        | j                  j	                         | j                  j                  | j                         || _        y )N)	Frame__init__TimertimerStartBind	EVT_TIMERon_timerr    )selfr    poll_intervalrn   s     r   rx   z$loop_wx.<locals>.TimerFrame.__init__   sW    HHdD"-$DJJJ]+IIbllDMM2DIr   c                 $    | j                          y r   )r    )r   events     r   r~   z$loop_wx.<locals>.TimerFrame.on_timer   s    IIKr   N)__name__
__module____qualname__rx   r~   )r   rn   s   r   
TimerFramert      s    		r   r   c                       e Zd Z fdZy)loop_wx.<locals>.IPWxAppc                 V           | _         | j                   j                  d       y)NFT)frameShow)r   r   rr   s    r   OnInitzloop_wx.<locals>.IPWxApp.OnInit   s"    #D)DJJJOOE"r   N)r   r   r   r   )r   rr   s   r   IPWxAppr      s    	r   r   r8   F)redirect)rn   rQ   _poll_intervalrw   Appgetattr
isinstancer8   signalcallable	getsignalSIGINTdefault_int_handlerrm   )r   r   r   r   r   rr   rn   s   `  @@@@r   loop_wxr      s      v4445M
RXX 
"&&  FE4(Z

BFF-Ke,

 F$$V]]34fmmV%?%?@VZZr   c                 ,    ddl }|j                          y)zExit the wx loop.r   N)rn   Exit)r   rn   s     r   loop_wx_exitr      s     GGIr   tkc                 ^   	
 ddl m}m}  |       	t        	d      r G d d      }	 fd fd}	fd}|t        _         |	       _        	j                  j                   j                  j                  t        j                        ||       	j                  d|       	j                          y	dd	l
dd	l}|j#                           j$                  }t'        d
 j(                  z         G 
 fdd      } |	|       _         j                  j+                          y	)z&Start a kernel with the Tk event loop.r   )READABLETkcreatefilehandlerc                       e Zd Zd Zy) loop_tk.<locals>.BasicAppWrapperc                 F    || _         | j                   j                          y r   )r8   withdraw)r   r8   s     r   rx   z)loop_tk.<locals>.BasicAppWrapper.__init__   s    !!#r   N)r   r   r   rx   r   r   r   BasicAppWrapperr      s    $r   r   c                       j                   j                  j                  j                  t        j
                                j                           j                          `y)r5   N)	r   deletefilehandlerrB   rW   rX   rY   r:   destroyapp_wrapper)r8   r   s   r   r;   zloop_tk.<locals>.exit_loop   sB    FF$$V%8%8%C%CCFF%KLHHJKKM"r   c                  N    j                   j                  d      r         yyr=   rA   )akwr;   r   s     r   rD   z&loop_tk.<locals>.process_stream_events   s$    ""((q(1 2r   c                 B    j                  t        d| z               y)rF   rH   N)afterrQ   )rR   r8   r;   s    r   rS   zloop_tk.<locals>._schedule_exit  s    IIc$,'3r   NrH   c                   (    e Zd Zd Z fdZd Zy) loop_tk.<locals>.TimedAppWrapperc                 T    || _         | j                   j                          || _        y r   )r8   r   r    )r   r8   r    s      r   rx   z)loop_tk.<locals>.TimedAppWrapper.__init__#  s     !!# 	r   c                    j                         }	 |j                  | j                                | j                  j                  | j                         y # t        $ r j                  j                  d       Y Mw xY w)NzError in message handler)	get_event_looprun_until_completer    	Exceptionlog	exceptionr8   r   r~   )r   loopasyncior   r   s     r   r~   z)loop_tk.<locals>.TimedAppWrapper.on_timer(  si    --/E++DIIK8 }dmm< ! EJJ(()CDEs   A $B ?B c                 X    | j                          | j                  j                          y r   )r~   r8   mainloop)r   s    r   rP   z&loop_tk.<locals>.TimedAppWrapper.start0  s    !!#r   N)r   r   r   rx   r~   rP   )r   r   r   s   r   TimedAppWrapperr   "  s    !
=$r   r   )tkinterr   r   rI   loop_tkrS   r   r   r   rB   rW   rX   rY   r   r   r   nest_asyncioapplydo_one_iterationrQ   r   rP   )r   r   r   r   rD   rS   r   doir   r8   r   r;   r   s   `        @@@@r   r   r      s     %
$C s'(	$ 	$
	#		4 "0 -S1  **3662H>S	
 			!*+ 	%%D6#8#889	$ 	$$ -S#6  "r   c                 |    	 | j                   j                  j                          | ` y# t        t        f$ r Y yw xY w)zExit the tk loop.N)r   r8   r   RuntimeErrorAttributeErrorr   s    r   loop_tk_exitr   8  s<    &&(.) s   &) ;;gtkc                 N    ddl m}  ||       }|j                          || _        yz6Start the kernel, coordinating with the GTK event loopr>   )GTKEmbedN)gui.gtkembedr   rP   _gtkr   r   
gtk_kernels      r   loop_gtkr   B  s$     '&!JFKr   c                 8    | j                   j                          y)zExit the gtk loop.Nr   stopr   s    r   loop_gtk_exitr   L       KKr   gtk3c                 N    ddl m}  ||       }|j                          || _        yr   )gui.gtk3embedr   rP   r   r   s      r   	loop_gtk3r   R  s$     (&!JFKr   c                 8    | j                   j                          y)zExit the gtk3 loop.Nr   r   s    r   loop_gtk3_exitr   \  r   r   osxc                    ddl m}m t        j                  fd}| j
                  j                  sl	 	 |t        _         || j                         | j                  j                  d      r	 t        _        y	 t        _        | j
                  j                  skyy# t        $ r  w xY w# t        $ r t        dt        j                         Y Xw xY w# t        _        w xY w)znStart the kernel, coordinating with the Cocoa CFRunLoop event loop
    via the matplotlib MacOSX backend.
    r>   )r   r   c                 p             | t         u rt        dt        j                         y | ||       y)z.don't let KeyboardInterrupts look like crashesz%KeyboardInterrupt caught in CFRunLoopfileN)KeyboardInterruptprintr   
__stdout__)etypevaluetbreal_excepthookr   s      r   
handle_intzloop_cocoa.<locals>.handle_intk  s.     	%%9OE5"-r   r?   Nz"KeyboardInterrupt caught in kernelr   )_eventloop_macosr   r   r   
excepthookshellexit_nowr   rB   rC   BaseExceptionr   r   r   )r   r   r   r   r   s      @@r   
loop_cocoar   b  s    
 1nnO. ll##	-!+../&&,,1,5 -CN 6 -CN% ll## !   	M6S^^L	M
 -CNs5   9B$ ?C $B//B2 2$CC CC C)c                     ddl m}  |        y)zExit the cocoa loop.r>   )r   N)r   r   )r   r   s     r   loop_cocoa_exitr     s     'Fr   r   c                 6   ddl }|j                         j                         ryj                         r!|j	                         |j                         d_        fd}t        || j                        }j                  | j                  j                  t        j                        |       j                  |       	 d}	 j                          j                  rj#                          ||y# t        $ r Y @t         $ r}|}Y d}~;d}~ww xY w)z/Start a kernel with asyncio event loop support.r   NFc                 L    | j                  d      rj                          yyr=   )rC   r   )streamr   s    r   rD   z+loop_asyncio.<locals>.process_stream_events  s    <<a< IIK !r   )r   r   
is_running	is_closednew_event_loopset_event_loop_should_closer   rB   
add_readerrW   rX   rY   	call_soonrun_foreverr   r   close)r   r   rD   notifiererrorer   s         @r   loop_asyncior     s     !!#D~~%%'t$D
 ,f.A.ABHOOF''22366:HENN8
	
 JJLK ! 	 	E	s   C9 9	DDDDc                     ddl }|j                         fd}j                         r |        yj                         s"j	                  |       j                          yy)zExit hook for asyncior   Nc                 z   K   t         d      r j                          d _         j                          y w)Nshutdown_asyncgensT)rI   r  r   r   )r   s   r   
close_loopz%loop_asyncio_exit.<locals>.close_loop  s2     4-.))++!		s   8;)r   r   r   r   r   r   )r   r   r  r   s      @r   loop_asyncio_exitr    sR     !!#D ^^
+

 r   c                    t         j                  j                  dd      }ddlm}m}m}m}m}  |       }|d|d|d|di}| | dk7  r||   | k7  rt        d||    d	       y| | dk7  r||   | k7  rt        d
|  d| d       y| dk(  r	 ddl
}	dt         j                  d<   nW| dk(  r	 ddl}dt         j                  d<   n9| dk(  r$dt         j                  v r"t         j                  d= nt        d|  d       y	 y# t        $ r> 	 ddl}
dt         j                  d<   n"# t        $ r dt         j                  d<   Y nw xY wY Hw xY w# t        $ r> 	 ddl}dt         j                  d<   n"# t        $ r dt         j                  d<   Y nw xY wY w xY w# t        $ r;}dt         j                  v rt         j                  d= t        d|        Y d}~yd}~ww xY w)a  
    Sets the QT_API environment variable by trying to import PyQtx or PySidex.

    The user can generically request `qt` or a specific Qt version, e.g. `qt6`.
    For a generic Qt request, we let the mechanism in IPython choose the best
    available version by leaving the `QT_API` environment variable blank.

    For specific versions, we check to see whether the PyQt or PySide
    implementations are present and set `QT_API` accordingly to indicate to
    IPython which version we want. If neither implementation is present, we
    leave the environment variable set so IPython will generate a helpful error
    message.

    Notes
    -----
    - If the environment variable is already set, it will be used unchanged,
      regardless of what the user requested.
    QT_APINr   )QT_API_PYQT5QT_API_PYQT6QT_API_PYSIDE2QT_API_PYSIDE6
loaded_apirb   rc   ra   z9Cannot switch Qt versions for this session; you must use r.   zRequest for "zC" will be ignored because `QT_API` environment variable is set to ""pyqt5pyside2pyqt6pyside6zUnrecognized Qt version: z". Should be "qt5", "qt6", or "qt".z$QT_API couldn't be set due to error )osenvirongetIPython.external.qt_loadersr  r  r	  r
  r  r   PyQt5rV   PySide2PyQt6PySide6r   )guiqt_apir  r  r	  r
  r  loaded
qt_env2guir  r  r  r  r   s                 r   set_qt_api_env_from_guir    s   & ZZ^^Hd+F  \F 	ee	J cTkj.@C.GI*U[J\I]]^_`cTkf$u %339(!= %<
3'.

8$ E\
3'.

8$ D[2::%JJx(-cU2TUV;  33"+4BJJx(" 3+2BJJx(33  33"+4BJJx(" 3+2BJJx(33$  rzz!

8$8<=s~   
C; (E ;	EDED<9E;D<<EE	FE'&F'FFFFF	G1GGc                     t        |d      ryt        |        ddlm} ddlm}  |dg      |_        |j                  |j                        |j                  _        y)z?Sets the `QT_API` environment variable if it isn't already set.r8   Nr   r*   )get_app_qt4 )	rI   r  rU   r+   IPython.lib.guisupportr  r8   
QEventLoopr9   )r  r   r+   r  s       r   make_qt_app_for_kernelr#  +  sJ    vu 	C  62cU#FJ%00<FJJr   c                 (   | t         vr%d| dt         j                          }t        |      |Bt        j                         rt        t        j                         dd      }|d}t        |      | It        |d      rt        |d       t        |d      rt        |d       t        |d      r*t        |d       n| j                  d	      rt        | |       t         |    }|r'|j                  |j                  |urd
}t        |      ||_        y)z#Enable integration with a given GUIzInvalid GUI request z, valid ones are:Nr   z\You didn't specify a kernel, and no IPython Application with a kernel appears to be running.r8   r6   rG   ra   z'Cannot activate multiple GUI eventloops)r"   keys
ValueErrorr   initializedr   instancer   rI   delattr
startswithr#  	eventloop)r  r   r   msgr   s        r   
enable_guir-  <  s   
("3'):8==?:KLm~""$[113XtDF>S  s##
{65!FE"6>*FN+6;'FK(>>$"3/C=D!!-&2B2B$2N73Fr   r   )$__doc__r  r	   r   rT   r   rX   packaging.versionr   r
   traitlets.config.applicationr   r   r"   r'   r`   r]   loop_qt5r#   rj   rm   r   r   r   r   r   r   r   r   r   r   r   r  r  r#  r-  r   r   r   <module>r2     s   :
 
  
  
 * 4N 
 F=7@ dE5)
& *
&  	 	 d1 1h 	  dL# L#^ 	  e  
 
 f   
 e#- #-L   i % !%P  (Yx="!r   