
    F\hO                        S r SSK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J	r	J
r
JrJrJrJrJrJrJrJrJrJr  \" \\\\\\15      r \  S r " S S\5      r\\\4rS rS r S	 r!S
 r"SS jr#SS jr$\$r%SS jr& " S S5      r' " S S\'5      r(S r)SS jr*\RV                  S:X  a   " S S5      r, " S S\'5      r-gg! \ a    0 r N~f = f)a  Basic infrastructure for asynchronous socket service clients and servers.

There are only two ways to have a program on a single processor do "more
than one thing at a time".  Multi-threaded programming is the simplest and
most popular way to do it, but there is another very different technique,
that lets you have nearly all the advantages of multi-threading, without
actually using multiple threads. it's really only practical if your program
is largely I/O bound. If your program is CPU bound, then pre-emptive
scheduled threads are probably what you really need. Network servers are
rarely CPU-bound, however.

If your operating system supports the select() system call in its I/O
library (and nearly all do), then you can use it to juggle multiple
communication channels at once; doing other work while your I/O is taking
place in the "background."  Although this strategy can seem strange and
complex, especially at first, it is in many ways easier to understand and
control than multi-threaded programming. The module documented here solves
many of the difficult problems for you, making the task of building
sophisticated high-performance network servers and clients a snap.
    N)EALREADYEINPROGRESSEWOULDBLOCK
ECONNRESETEINVALENOTCONN	ESHUTDOWNEISCONNEBADFECONNABORTEDEPIPEEAGAIN	errorcodec                      [         R                  " U 5      $ ! [        [        [        4 a    U [
        ;   a  [
        U    s $ SU -  s $ f = f)NzUnknown error %s)osstrerror
ValueErrorOverflowError	NameErrorr   )errs    ,/usr/lib/python3.13/test/support/asyncore.py	_strerrorr   J   sI    '{{3y1 ')S>!!3&&'s    (A
A
	A
c                       \ rS rSrSrg)ExitNowR    N)__name__
__module____qualname____firstlineno____static_attributes__r       r   r   r   R   s    r"   r   c                 h     U R                  5         g ! [         a    e   U R                  5          g = fN)handle_read_event_reraised_exceptionshandle_errorobjs    r   readr*   W   1         1c                 h     U R                  5         g ! [         a    e   U R                  5          g = fr$   )handle_write_eventr&   r'   r(   s    r   writer/   _   s1      r,   c                 h     U R                  5         g ! [         a    e   U R                  5          g = fr$   )handle_expt_eventr&   r'   r(   s    r   
_exceptionr2   g   r+   r,   c                 d    U[         R                  -  (       a  U R                  5         U[         R                  -  (       a  U R	                  5         U[         R
                  -  (       a  U R                  5         U[         R                  [         R                  -  [         R                  -  -  (       a  U R                  5         g g ! [         aC  nUR                  [        ;  a  U R                  5          S nAg U R                  5          S nAg S nAf[         a    e   U R                  5          g = fr$   )selectPOLLINr%   POLLOUTr.   POLLPRIr1   POLLHUPPOLLERRPOLLNVALhandle_closeOSErrorerrno_DISCONNECTEDr'   r&   )r)   flagses      r   	readwriterA   o   s    6== !!#6>>!""$6>>!!!#FNNV^^3fooEF G 77-' s   CC 
D/$D9DD/c                    Uc  [         nU(       Gaq  / n/ n/ n[        UR                  5       5       H  u  pVUR                  5       nUR	                  5       nU(       a  UR                  U5        U(       a"  UR                  (       d  UR                  U5        U(       d	  U(       d  Mv  UR                  U5        M     / Us=:X  a  Us=:X  a  U:X  a  O  O[        R                  " U 5        g [        R                  " X#X@5      u  p#nU H$  nUR                  U5      nUc  M  [        U5        M&     U H$  nUR                  U5      nUc  M  [        U5        M&     U H$  nUR                  U5      nUc  M  [        U5        M&     g g r$   )
socket_maplistitemsreadablewritableappend	acceptingtimesleepr4   getr*   r/   r2   )	timeoutmaprwr@   fdr)   is_ris_ws	            r   pollrT      s/   
{
BBCIIK(GB<<>D<<>DCMMtt ) a1JJw--a1aB''"+C{I	  B''"+C{#J	  B''"+C{sO	 = r"   c                 L   Uc  [         nU b  [        U S-  5      n [        R                  " 5       nU(       a  [	        UR                  5       5       H  u  p4SnUR                  5       (       a$  U[        R                  [        R                  -  -  nUR                  5       (       a$  UR                  (       d  U[        R                  -  nU(       d  M  UR                  X55        M     UR                  U 5      nU H&  u  p5UR                  U5      nUc  M  [        XE5        M(     g g )Ni  r   )rC   intr4   rT   rD   rE   rF   r5   r7   rG   rI   r6   registerrL   rA   )rM   rN   pollsterrQ   r)   r?   rO   s          r   poll2rY      s    
{gdl#{{}H
CIIK(GBE||~~77||~~cmm'u!!", ) MM'"IB''"+C{c!	  r"   c                     Uc  [         nU(       a  [        [        S5      (       a  [        nO[        nUc  U(       a  U" X5        U(       a  M  g g U(       a%  US:  a  U" X5        US-
  nU(       a	  US:  a  M  g g g g )NrT   r      )rC   hasattrr4   rY   rT   )rM   use_pollrN   countpoll_funs        r   loopr`      si    
{GFF++}W" c eaiW"AIE eaicicr"   c                   6   \ rS rSrSrSrSrSrSrSr	\
" S15      rS$S jrS rS%S jrS%S jr\R"                  \R$                  4S	 jrS%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 r#S r$S r%S r&S r'S r(S r)S  r*S! r+S" r,S#r-g)'
dispatcher   FNwarningc                 l   Uc  [         U l        OX l        S U l        U(       a@  UR                  S5        U R	                  X5        SU l         UR                  5       U l        g S U l        g ! [         a=  nUR                  [        [        4;   a  SU l         S nAg U R                  U5        e S nAff = f)NFT)rC   _map_filenosetblocking
set_socket	connectedgetpeernameaddrr<   r=   r   r   del_channelsocket)selfsockrN   r   s       r   __init__dispatcher.__init__   s    ;"DII U#OOD&!DN ,,.	 DK  
996 22 &+DNN
 $$S)
s   A, ,
B36!B.B..B3c                    U R                   R                  S-   U R                   R                  -   /nU R                  (       a#  U R                  (       a  UR                  S5        O"U R                  (       a  UR                  S5        U R                  b   UR                  SU R                  -  5        SSR                  U5      [        U 5      4-  $ ! [         a'    UR                  [        U R                  5      5         NOf = f)N.	listeningrj   z%s:%dz<%s at %#x> )	__class__r   r   rI   rl   rH   rj   	TypeErrorreprjoinid)ro   statuss     r   __repr__dispatcher.__repr__  s    ..++C/0K0KKL>>diiMM+&^^MM+&99 /g		12  0"T(;;;  /d499o./s   C .DDc                 >    Uc  U R                   nXU R                  '   g r$   )rf   rg   )ro   rN   s     r   add_channeldispatcher.add_channel  s    ;))C DLLr"   c                 V    U R                   nUc  U R                  nX!;   a  X	 S U l         g r$   )rg   rf   )ro   rN   rQ   s      r   rm   dispatcher.del_channel  s*    \\;))C9r"   c                     X4U l         [        R                  " X5      nUR                  S5        U R                  U5        g NF)family_and_typern   rh   ri   )ro   familytyperp   s       r   create_socketdispatcher.create_socket  s4    %|}}V*r"   c                 \    Xl         UR                  5       U l        U R                  U5        g r$   )rn   filenorg   r   ro   rp   rN   s      r   ri   dispatcher.set_socket$  s"    {{}r"   c                 
    U R                   R                  [         R                  [         R                  U R                   R	                  [         R                  [         R                  5      S-  5        g ! [
         a     g f = f)Nr[   )rn   
setsockopt
SOL_SOCKETSO_REUSEADDR
getsockoptr<   ro   s    r   set_reuse_addrdispatcher.set_reuse_addr)  sj    	KK""!!6#6#6&&v'8'8'-':':<>?@
  		s   A2A5 5
BBc                     gNTr   r   s    r   rF   dispatcher.readable:      r"   c                     gr   r   r   s    r   rG   dispatcher.writable=  r   r"   c                 ~    SU l         [        R                  S:X  a  US:  a  SnU R                  R	                  U5      $ )NTnt   )rI   r   namern   listen)ro   nums     r   r   dispatcher.listenD  s4    77d?sQwC{{!!#&&r"   c                 D    Xl         U R                  R                  U5      $ r$   )rl   rn   bind)ro   rl   s     r   r   dispatcher.bindJ  s    	{{%%r"   c                 4   SU l         SU l        U R                  R                  U5      nU[        [
        [        4;   d  U[        :X  a  [        R                  S:X  a  Xl
        g US[        4;   a  Xl
        U R                  5         g [        U[        U   5      e)NFTr   r   )rj   
connectingrn   
connect_exr   r   r   r   r   r   rl   r
   handle_connect_eventr<   r   )ro   addressr   s      r   connectdispatcher.connectN  sz    kk$$W-;+66&=RWW_I1g,I%%'#y~..r"   c                      U R                   R                  5       u  pX4$ ! [         a     g [         a*  nUR                  [
        [        [        4;   a   S nAg e S nAff = fr$   )rn   acceptrx   r<   r=   r   r   r   )ro   connrl   whys       r   r   dispatcher.accept\  s[    
	++-JD :  	 	yy[,??		s   ! 
A 	A AAA c                      U R                   R                  U5      nU$ ! [         aH  nUR                  [        :X  a   S nAgUR                  [
        ;   a  U R                  5          S nAge S nAff = fNr   )rn   sendr<   r=   r   r>   r;   )ro   dataresultr   s       r   r   dispatcher.sendj  s]    
	[[%%d+FM 	yyK'm+!!#	s     
A1A,$A,+A,,A1c                      U R                   R                  U5      nU(       d  U R                  5         gU$ ! [         a/  nUR                  [
        ;   a  U R                  5          S nAge S nAff = fNr"   )rn   recvr;   r<   r=   r>   )ro   buffer_sizer   r   s       r   r   dispatcher.recvw  sb    	;;##K0D !!# 	yyM)!!#	s   27 7 
A0$A+*A++A0c                    SU l         SU l        SU l        U R                  5         U R                  b   U R                  R                  5         g g ! [         a%  nUR                  [        [        4;  a  e  S nAg S nAff = fr   )
rj   rI   r   rm   rn   closer<   r=   r   r   )ro   r   s     r   r   dispatcher.close  sr    ;;"!!# #  99Xu$55 6s   A 
A?A::A?c                 Z    [         R                  R                  S[        U5      -  5        g )Nzlog: %s
)sysstderrr/   str)ro   messages     r   logdispatcher.log  s    

s7|34r"   c                 H    X R                   ;  a  [        U< SU< 35        g g )Nz: )ignore_log_typesprint)ro   r   r   s      r   log_infodispatcher.log_info  s     ,,,dG,- -r"   c                     U R                   (       a  U R                  5         g U R                  (       d2  U R                  (       a  U R	                  5         U R                  5         g U R                  5         g r$   )rI   handle_acceptrj   r   r   handle_readr   s    r   r%   dispatcher.handle_read_event  sI    >>  ))+r"   c                     U R                   R                  [         R                  [         R                  5      nUS:w  a  [	        U[        U5      5      eU R                  5         SU l        SU l        g )Nr   TF)	rn   r   r   SO_ERRORr<   r   handle_connectrj   r   ro   r   s     r   r   dispatcher.handle_connect_event  sT    kk$$V%6%6H!8#y~..r"   c                     U R                   (       a  g U R                  (       d!  U R                  (       a  U R                  5         U R	                  5         g r$   )rI   rj   r   r   handle_writer   s    r   r.   dispatcher.handle_write_event  s4    >> ~~))+r"   c                     U R                   R                  [         R                  [         R                  5      nUS:w  a  U R	                  5         g U R                  5         g r   )rn   r   r   r   r;   handle_exptr   s     r   r1   dispatcher.handle_expt_event  sE     kk$$V%6%6H!8 r"   c                     [        5       u  pp4 [        U 5      nU R                  SU< SU< SU< SU< S3	S5        U R	                  5         g !   S[        U 5      -  n NF= f)Nz)<__repr__(self) failed for object at %0x>z-uncaptured python exception, closing channel z (:rv   )error)compact_tracebackry   r{   r   r;   )ro   niltvtbinfo	self_reprs         r   r'   dispatcher.handle_error  sf    -/	OT
I 		 	 		OCbhNIs   A A c                 (    U R                  SS5        g )Nz!unhandled incoming priority eventrd   r   r   s    r   r   dispatcher.handle_expt  s    99Er"   c                 (    U R                  SS5        g )Nzunhandled read eventrd   r   r   s    r   r   dispatcher.handle_read  s    ,i8r"   c                 (    U R                  SS5        g )Nzunhandled write eventrd   r   r   s    r   r   dispatcher.handle_write  s    -y9r"   c                 (    U R                  SS5        g )Nzunhandled connect eventrd   r   r   s    r   r   dispatcher.handle_connect  s    /;r"   c                 J    U R                  5       nUb  U R                  " U6   g g r$   )r   handle_accepted)ro   pairs     r   r   dispatcher.handle_accept  s&    {{}  $' r"   c                 H    UR                  5         U R                  SS5        g )Nzunhandled accepted eventrd   )r   r   )ro   rp   rl   s      r   r   dispatcher.handle_accepted  s    

0)<r"   c                 H    U R                  SS5        U R                  5         g )Nzunhandled close eventrd   )r   r   r   s    r   r;   dispatcher.handle_close  s    -y9

r"   )rg   rf   rI   rl   rj   r   r   rn   NNr$   )info).r   r   r   r    debugrj   rI   r   closingrl   	frozensetr   rq   r}   r   rm   rn   AF_INETSOCK_STREAMr   ri   r   rF   rG   r   r   r   r   r   r   r   r   r   r%   r   r.   r1   r'   r   r   r   r   r   r   r;   r!   r   r"   r   rb   rb      s    EIIJGD )-@<! $*>>8J8J 
	"'&/$
 5.
 	(F9:<(
=r"   rb   c                   6    \ rS rSrS	S jrS rS rS rS rSr	g)
dispatcher_with_sendi   Nc                 >    [         R                  XU5        SU l        g r   )rb   rq   
out_bufferr   s      r   rq   dispatcher_with_send.__init__  s    D,r"   c                 t    Sn[         R                  X R                  S S 5      nU R                  US  U l        g )Nr   i   )rb   r   r  )ro   num_sents     r   initiate_send"dispatcher_with_send.initiate_send  s2    ??4%)@A//()4r"   c                 $    U R                  5         g r$   )r  r   s    r   r   !dispatcher_with_send.handle_write  s    r"   c                 \    U R                   (       + =(       d    [        U R                  5      $ r$   )rj   lenr  r   s    r   rG   dispatcher_with_send.writable  s    NN";s4??';;r"   c                     U R                   (       a  U R                  S[        U5      -  5        U R                  U-   U l        U R	                  5         g )Nz
sending %s)r   r   ry   r  r  )ro   r   s     r   r   dispatcher_with_send.send  s:    ::MM,d34//D0r"   )r  r   )
r   r   r   r    rq   r  r   rG   r   r!   r   r"   r   r   r      s    5
<r"   r   c                     [         R                  " 5       n U R                  nU(       d  [        S5      e/ nU(       ax  UR	                  UR
                  R                  R                  UR
                  R                  R                  [        UR                  5      45        UR                  nU(       a  Mx  AUS   u  p4nSR                  U Vs/ s H  nSU-  PM
     sn5      nX4U4[        U 5      X4$ s  snf )Nztraceback does not existrv   z
[%s|%s|%s])r   	exception__traceback__AssertionErrorrH   tb_framef_codeco_filenameco_namer   	tb_linenotb_nextrz   r   )exctbr   filefunctionlinexr   s           r   r   r     s    
--/C			B788F
KK**KK&& 	
 ZZ " 	!":DD88v6v!\A%v67DD!49c77 7s   C3c                 6   U c  [         n [        U R                  5       5       H  n UR                  5         M     U R                  5         g ! [         a-  nUR
                  [        :X  a   S nAMM  U(       d  e  S nAM[  S nAf[         a    e   U(       d  e  Mu  = fr$   )	rC   rD   valuesr   r<   r=   r   r&   clear)rN   
ignore_allr  s      r   	close_allr$  0  s|    
{#**,	GGI   IIK  	ww%  # 		 s   A
BA>0A>>Bposixc                   J    \ rS rSrS rS rS rS rSS jr\r	\r
S rS	 rS
rg)file_wrapperiP  c                 :    [         R                  " U5      U l        g r$   )r   duprQ   ro   rQ   s     r   rq   file_wrapper.__init__U  s    ffRjDGr"   c                 ~    U R                   S:  a  [        R                  " SU -  [        U S9  U R	                  5         g )Nr   zunclosed file %r)source)rQ   warningswarnResourceWarningr   r   s    r   __del__file_wrapper.__del__X  s/    ww!|047%)+JJLr"   c                 D    [         R                  " U R                  /UQ76 $ r$   )r   r*   rQ   ro   argss     r   r   file_wrapper.recv^  s    77477*T**r"   c                 D    [         R                  " U R                  /UQ76 $ r$   )r   r/   rQ   r4  s     r   r   file_wrapper.senda  s    88DGG+d++r"   Nc                 x    U[         R                  :X  a  U[         R                  :X  a  U(       d  g[        S5      e)Nr   z-Only asyncore specific behaviour implemented.)rn   r   r   NotImplementedError)ro   leveloptnamebuflens       r   r   file_wrapper.getsockoptd  s4    ***6??*% '5 6 6r"   c                 x    U R                   S:  a  g U R                   nSU l         [        R                  " U5        g )Nr   r  )rQ   r   r   r*  s     r   r   file_wrapper.closeo  s+    ww{BDGHHRLr"   c                     U R                   $ r$   rQ   r   s    r   r   file_wrapper.filenov  s    77Nr"   rB  r$   )r   r   r   r    rq   r1  r   r   r   r*   r/   r   r   r!   r   r"   r   r'  r'  P  s1    
	!		+	,	6 		r"   r'  c                   $    \ rS rSrSS jrS rSrg)file_dispatcheriy  Nc                     [         R                  U S U5        SU l         UR                  5       nU R                  U5        [        R                  " US5        g ! [         a     N5f = f)NTF)rb   rq   rj   r   AttributeErrorset_filer   set_blocking)ro   rQ   rN   s      r   rq   file_dispatcher.__init__{  s[    dC0!DNYY[ MM"OOB&	 " s   A 
A&%A&c                     [        U5      U l        U R                  R                  5       U l        U R	                  5         g r$   )r'  rn   r   rg   r   r*  s     r   rH  file_dispatcher.set_file  s.    &r*DK;;--/DLr"   )rg   rj   rn   r$   )r   r   r   r    rq   rH  r!   r   r"   r   rE  rE  y  s    		'	r"   rE  )g        N)g      >@FNNr   ).__doc__r4   rn   r   rJ   r.  r   r=   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r>   rC   r   r   	Exceptionr   KeyboardInterrupt
SystemExitr&   r*   r/   r2   rA   rT   rY   poll3r`   rb   r   r   r$  r   r'  rE  r   r"   r   <module>rR     s
  B*   
   	   
 :xL% " #'	i 	  !2J? (%N"4 	$a aP	: 68*> 77g' 'R* U Q  Js   B> >C	C	