
    ho2                     F   S r SSKrSSKJrJrJrJr  SSKJr  SSK	J
r
  SSKJr  SSKJrJrJr  SSKJr  SS	KJr  S
SKJrJr  / 4S jr " S S\5      rS rS\\/ S4   /S4   S\/ S4   S\/ S4   4S jr\" \5       " S S\
R8                  \
R:                  5      5       rg)aC  
This module provides base support for Twisted to interact with the glib/gtk
mainloops.

The classes in this module should not be used directly, but rather you should
import gireactor or gtk3reactor for GObject Introspection based applications,
or glib2reactor or gtk2reactor for applications using legacy static bindings.
    N)AnyCallableDictSet)implementer)	posixbase)FileDescriptor)IReactorFDSetIReadDescriptorIWriteDescriptor)log)MonkeyPatcher   )_IWaker
_UnixWakerc                     U  H.  n[         R                  R                  U5      c  M%  [        U5      e   U H  nS[         R                  U'   M     g)a+  
Check whether the given modules were imported, and if requested, ensure
they will not be importable in the future.

@param moduleNames: A list of module names we make sure aren't imported.
@type moduleNames: C{list} of C{str}

@param preventImports: A list of module name whose future imports should
    be prevented.
@type preventImports: C{list} of C{str}

@param errorMessage: Message to use when raising an C{ImportError}.
@type errorMessage: C{str}

@raise ImportError: with given error message if a given module name
    has already been imported.
N)sysmodulesgetImportError)moduleNameserrorMessagepreventImportsnames       a/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/internet/_glibbase.pyensureNotImportedr      sF    $ ;;??4 ,l++ 
  D     c                   <   ^  \ rS rSrSrU 4S jrSU 4S jjrSrU =r$ )	GlibWaker7   z'
Run scheduled events after waking up.
c                 .   > [         TU ]  5         Xl        g N)super__init__reactor)selfr%   	__class__s     r   r$   GlibWaker.__init__<   s    r   c                 V   > [         TU ]  5         U R                  R                  5         g r"   )r#   doReadr%   	_simulate)r&   r'   s    r   r*   GlibWaker.doRead@   s     r   )r%   )returnN)	__name__
__module____qualname____firstlineno____doc__r$   r*   __static_attributes____classcell__r'   s   @r   r   r   7   s    ! !r   r   c                  L    SSK Jn   [        5       nUR                  U SS5        U$ )a:  
Integrate glib's wakeup file descriptor usage and our own.

Python supports only one wakeup file descriptor at a time and both Twisted
and glib want to use it.

This is a context manager that can be wrapped around the whole glib
reactor main loop which makes our signal handling work with glib's signal
handling.
r   )_ossighelper_wakeup_fd_is_activeT)gir7   r   addPatch)
signalGluepatchers     r   _signalGluer=   E   s&     .oGZ!7>Nr   idleAddloopQuitr-   c                    ^ ^ U U4S j$ )zu
Combine the C{glib.idle_add} and C{glib.MainLoop.quit} functions into a
function suitable for crashing the reactor.
c                     > T " T5      $ r"    r>   r?   s   r   <lambda>_loopQuitter.<locals>.<lambda>^   s
    78$r   rB   rC   s   ``r   _loopQuitterrF   W   s     %$r   c            	          ^  \ rS rSrSrS\4S jrSS\S\S\SS4U 4S	 jjjr	U 4S
 jr
S rS rS rS rS rS rS rS rS rS rS rSS jrS rS rS S jrS rS rS rSrU =r$ )!GlibReactorBasea   a  
Base class for GObject event loop reactors.

Notification for I/O events (reads and writes on file descriptors) is done
by the gobject-based event loop. File descriptors are registered with
gobject with the appropriate flags for read/write/disconnect notification.

Time-based events, the results of C{callLater} and C{callFromThread}, are
handled differently. Rather than registering each event with gobject, a
single gobject timeout is registered for the earliest scheduled event, the
output of C{reactor.timeout()}. For example, if there are timeouts in 1, 2
and 3.4 seconds, a single timeout is registered for 1 second in the
future. When this timeout is hit, C{_simulate} is called, which calls the
appropriate Twisted-level handlers, and a new timeout is added to gobject
by the C{_reschedule} method.

To handle C{callFromThread} events, we use a custom waker that calls
C{_simulate} whenever it wakes up.

@ivar _sources: A dictionary mapping L{FileDescriptor} instances to
    GSource handles.

@ivar _reads: A set of L{FileDescriptor} instances currently monitored for
    reading.

@ivar _writes: A set of L{FileDescriptor} instances currently monitored for
    writing.

@ivar _simtag: A GSource handle for the next L{simulate} call.
r-   c                     [        U 5      $ r"   )r   r&   s    r   _wakerFactoryGlibReactorBase._wakerFactory   s    r   glib_module
gtk_moduleuseGtkNc                   > S U l         [        5       U l        [        5       U l        0 U l        Xl        UR                  R                  UR                  R                  -  UR                  R                  -  U l
        UR                  R                  U l        UR                  R                  U l        U R                  U R                  -  U l        U R                  U R                  -  U l        ["        TU ]I  5         U R
                  R&                  U l        U R
                  R*                  U l        U R
                  R/                  5       U l        U R0                  R2                  U l        U R0                  R6                  U l        U R
                  R;                  5       U l        [?        U R
                  R@                  U R<                  RB                  5      U l"        U R<                  RF                  U l$        g r"   )%_simtagset_reads_writes_sources_glibIOConditionHUPERRNVAL_POLL_DISCONNECTEDIN_POLL_INOUT	_POLL_OUTINFLAGSOUTFLAGSr#   r$   source_remove_source_removetimeout_add_timeout_addmain_context_defaultcontextpending_pending	iteration
_iterationMainLooplooprF   idle_addquit_crashrun_run)r&   rN   rO   rP   r'   s       r   r$   GlibReactorBase.__init__   sg   ,/E.1e35 
 ##''%%))*%%**+ 	
 $//22$0044
 }}t'>'>>)@)@@"jj66 JJ22zz668,,,,00JJ'')	"4::#6#6		GIIMM	r   c                 T   >^  [         TT ]  5         U 4S jnT R                  SU5        g)zY
Make sure the reactor's signal handlers are installed despite any
outside interference.
c                  n   > T R                   R                  5         T R                   R                  5         g r"   )_signals	uninstallinstallrK   s   r   reinitSignals:GlibReactorBase._reallyStartRunning.<locals>.reinitSignals   s#    MM##%MM!!#r   r   N)r#   _reallyStartRunning	callLater)r&   rz   r'   s   ` r   r|   #GlibReactorBase._reallyStartRunning   s%     	#%	$ 	q-(r   c                    ^^ [        TS5      (       a  UU4S jnTR                  5       nOTnTnU R                  R                  UU R                  R                  UU5      $ )Nfilenoc                    > T" TU5      $ r"   rB   )ignored	conditioncallbacksources     r   wrapper*GlibReactorBase.input_add.<locals>.wrapper   s    	22r   )hasattrr   rW   io_add_watchPRIORITY_DEFAULT_IDLE)r&   r   r   r   r   r   s    ` `  r   	input_addGlibReactorBase.input_add   sY    68$$3 ]]_FFGzz&&JJ,,	
 	
r   c                 H    [         R                  " XR                  XU5        g)z0
Called by event loop when an I/O event occurs.
T)r   callWithLogger_doReadOrWrite)r&   r   r   s      r   _ioEventCallback GlibReactorBase._ioEventCallback   s     	6#6#6	Rr   c                     X;   a  gUnX;   a"  U R                  U R                  U   5        Xe-  nU R                  XU R                  5      U R                  U'   UR	                  U5        g)z
Add the given L{FileDescriptor} for monitoring either for reading or
writing. If the file is already monitored for the other operation, we
delete the previous registration and re-register it for both reading
and writing.
N)rd   rV   r   r   add)r&   r   primaryotherprimaryFlag	otherFlagflagss          r   _addGlibReactorBase._add   sa     ?f 56E $vd>S>S TfFr   c                 |    U R                  XR                  U R                  U R                  U R                  5        g)zC
Add a L{FileDescriptor} for monitoring of data available to read.
N)r   rT   rU   ra   rb   r&   readers     r   	addReaderGlibReactorBase.addReader   s&     			&++t||T\\4==Qr   c                 |    U R                  XR                  U R                  U R                  U R                  5        g)z?
Add a L{FileDescriptor} for monitoring ability to write data.
N)r   rU   rT   rb   ra   r&   writers     r   	addWriterGlibReactorBase.addWriter   s&     			&,,T]]DLLQr   c                 ,    [        U R                  5      $ )zG
Retrieve the list of current L{FileDescriptor} monitored for reading.
)listrT   rK   s    r   
getReadersGlibReactorBase.getReaders   s     DKK  r   c                 ,    [        U R                  5      $ )zG
Retrieve the list of current L{FileDescriptor} monitored for writing.
)r   rU   rK   s    r   
getWritersGlibReactorBase.getWriters  s     DLL!!r   c                 N    U R                  U R                  U R                  5      $ )z:
Remove monitoring for all registered L{FileDescriptor}s.
)
_removeAllrT   rU   rK   s    r   	removeAllGlibReactorBase.removeAll	  s     t{{DLL99r   c                    X;  a  gU R                  U R                  U   5        UR                  U5        X;   a*  U R                  XU R                  5      U R                  U'   gU R                  R                  U5        g)z
Remove monitoring the given L{FileDescriptor} for either reading or
writing. If it's still monitored for the other operation, we
re-register the L{FileDescriptor} for only that operation.
N)rd   rV   remover   r   pop)r&   r   r   r   r   s        r   _removeGlibReactorBase._remove  sf      DMM&12v?$(NN6$BWBW$XDMM&!MMf%r   c                 f    U R                  XR                  U R                  U R                  5        g)z:
Stop monitoring the given L{FileDescriptor} for reading.
N)r   rT   rU   rb   r   s     r   removeReaderGlibReactorBase.removeReader  s      	V[[$,,Fr   c                 f    U R                  XR                  U R                  U R                  5        g)z:
Stop monitoring the given L{FileDescriptor} for writing.
N)r   rU   rT   ra   r   s     r   removeWriterGlibReactorBase.removeWriter$  s      	V\\4;;Er   c                     U R                  5         U R                  5       (       a)  U R                  S5        U R                  5       (       a  M(  gg)z^
One iteration of the event loop, for trial's use.

This is not used for actual reactor runs.
r   N)runUntilCurrentrj   rl   )r&   delays     r   iterateGlibReactorBase.iterate*  s4     	mmooOOA mmoor   c                 b    [         R                  R                  U 5        U R                  5         g)z
Crash the reactor.
N)r   PosixReactorBasecrashrq   rK   s    r   r   GlibReactorBase.crash4  s!     	""((.r   c                 b    [         R                  R                  U 5        U R                  5         g)z
Stop the reactor.
N)r   r   stopwakeUprK   s    r   r   GlibReactorBase.stop;  s#     	""''- 	r   c                     [        5          U R                  U R                  5        U R                  US9  U R                  (       a  U R                  5         SSS5        g! , (       d  f       g= f)z
Run the reactor.
)installSignalHandlersN)r=   callWhenRunning_reschedulestartRunning_startedrs   )r&   r   s     r   rr   GlibReactorBase.runI  sI     ]  !1!124IJ}}			 ]]s   AA  
A.c                 n    [         R                  R                  " U /UQ70 UD6nU R                  5         U$ )z
Schedule a C{DelayedCall}.
)r   r   r}   r   )r&   argskwargsresults       r   r}   GlibReactorBase.callLaterS  s7     ++55dLTLVL 	r   c                 
   U R                   b"  U R                  U R                   5        SU l         U R                  5       nUbA  U R                  [	        US-  5      U R
                  U R                  R                  S9U l         gg)z+
Schedule a glib timeout for C{_simulate}.
Ni  )priority)rR   rd   timeoutrf   intr+   rW   r   )r&   r   s     r   r   GlibReactorBase._reschedule]  st     <<#-DL,,.,,GdN#99 - DL r   c                 D    U R                  5         U R                  5         g)zH
Run timers, and then reschedule glib timeout for next scheduled event.
N)r   r   rK   s    r   r+   GlibReactorBase._simulatel  s     	r   )ra   rb   r\   r^   r`   rq   rW   rl   rj   rT   rs   rR   rd   rV   rf   rU   rh   rn   )F)r   )T)r.   r/   r0   r1   r2   r   rL   r   boolr$   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rr   r}   r   r+   r3   r4   r5   s   @r   rH   rH   a   s    Bw "C "S "$ "SW " "B)B
" RR!":&GF r   rH   )r2   r   typingr   r   r   r   zope.interfacer   twisted.internetr   twisted.internet.abstractr	   twisted.internet.interfacesr
   r   r   twisted.pythonr   twisted.python.monkeyr   rw   r   r   r   r   r=   rF   r   _PollLikeMixinrH   rB   r   r   <module>r      s   
  + + & & 4 X X  / ) AC !6!
 !$%xD)*D01%=Eb$h=O%b$h% ]Oi00)2J2J O Or   