
    h(1                     R   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
r
SSKrSSKrSSKJrJr  SSKJr  SSKJrJrJr  SSKJr  \R0                  " 5       rS\l        S	 r " S
 S\R8                  5      rS rS r\" \\\5       " S S\R@                  \5      5       r!g)z<
Windows Process Management, used with reactor.spawnProcess
    N)implementer)_pollingfileerror)BaseProcess)	IConsumerIProcessTransport	IProducer)quoteArguments   c                 V    [        U 5        [        R                  R                  5         g N)printsysstdoutflush)msgs    f/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/internet/_dumbwin32proc.pydebugr   $   s    	#JJJ    c                        \ rS rSrS rS rSrg)_Reaper)   c                     Xl         g r   proc)selfr   s     r   __init___Reaper.__init__*   s    	r   c                 *   [         R                  " U R                  R                  S5      [         R                  :w  a  g[
        R                  " U R                  R                  5      nU R                  5         U R                  R                  U5        gNr   )	
win32eventWaitForSingleObjectr   hProcessWAIT_OBJECT_0win32processGetExitCodeProcess
deactivateprocessEnded)r   exitCodes     r   	checkWork_Reaper.checkWork-   sh    **499+=+=qA''( 224993E3EF		x(r   r   N)__name__
__module____qualname____firstlineno__r   r*   __static_attributes__ r   r   r   r   )   s    	r   r   c                     [        U 5       nUR                  S5      S:X  a+  UR                  S5      R                  S5      nUsSSS5        $  SSS5        g! , (       d  f       g= f)a  
Look for a #! line, and return the value following the #! if one exists, or
None if this file is not a script.

I don't know if there are any conventions for quoting in Windows shebang
lines, so this doesn't support any; therefore, you may not pass any
arguments to scripts invoked as filters.  That's probably wrong, so if
somebody knows more about the cultural expectations on Windows, please feel
free to fix.

This shebang line support was added in support of the CGI tests;
appropriately enough, I determined that shebang lines are culturally
accepted in the Windows world through this page::

    http://www.cgi101.com/learn/connect/winxp.html

@param filename: str representing a filename

@return: a str representing another filename.
   z#!i   
N)openreadreadlinestrip)filenamefexes      r   _findShebangr<   9   sM    * 
h166!9**T"((.C 
 
s   7A
A%c                 &    U R                   S   S:H  $ )z
Determine if a pywintypes.error is telling us that the given process is
'not a valid win32 application', i.e. not a PE format executable.

@param pywinerr: a pywintypes.error instance raised by CreateProcess

@return: a boolean
r      )args)pywinerrs    r   _invalidWin32ApprA   T   s     ==s""r   c                       \ 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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S rS\4S jrSrg)Processe   aj  
A process that integrates with the Twisted event loop.

If your subprocess is a python program, you need to:

 - Run python.exe with the '-u' command line option - this turns on
   unbuffered I/O. Buffering stdout/err/in can cause problems, see e.g.
   http://support.microsoft.com/default.aspx?scid=kb;EN-US;q1903

 - If you don't want Windows messing with data passed over
   stdin/out/err, set the pipes to be in binary mode::

    import os, sys, mscvrt
    msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
    msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)

r   c                   ^ ^^^^^ [         R                  R                  T U5        [        R                  " T U5        [        R
                  " 5       nSUl        [        R                  " US5      u  T l	        n[        R                  " US5      u  T l
        n	[        R                  " US5      u  n
T l        [        R                  " T R                  [        R                  SS5        [        R                  " 5       mUTl        U	Tl        U
Tl        [        R&                  Tl        [*        R,                  " 5       n[*        R.                  " UT R                  USS[0        R2                  5      n[4        R6                  " T R                  5        UT l	        [*        R.                  " UT R                  USS[0        R2                  5      n[4        R6                  " T R                  5        UT l
        [*        R.                  " UT R                  USS[0        R2                  5      n[4        R6                  " T R                  5        UT l        [8        R:                  R=                  5       mTR?                  U=(       d    0 5        TRA                  5        VVs0 s H0  u  p[8        RB                  " U5      [8        RB                  " U5      _M2     snnmU Vs/ s H  n[8        RB                  " U5      PM     nn[E        U5      mT(       a  [8        RB                  " T5      OTmT(       a  [8        RB                  " T5      OTmUUUUUU 4S jn U" 5         [4        R6                  " U	5        [4        R6                  " U5        [4        R6                  " U
5        [         RT                  " T R                  U 4S jT RV                  5      T l,        [         RT                  " T R                  U 4S	 jT RZ                  5      T l.        [         R^                  " T R                  T R`                  5      T l1        T RX                  T R\                  T Rb                  4 H  nT Re                  U5        M     T Rf                  Ri                  T 5        T Re                  [k        T 5      5        gs  snnf s  snf ! [F        RH                   a  n[K        U5      (       d  [M        U5      e[O        T5      nUc  [M        ST-  5      e[Q        U5      nURS                  ST5        [E        U5      mTnUm U" 5          SnAGN! [F        RH                   a3  n[K        U5      (       a  [M        U< SU< S35      e[M        U5      eSnAff = fSnAff = f)
z
Create a new child process.
r   r   Nc                     > [         R                  n [        R                  " TTS S SU TTT5	      u  Tl        Tl        Tl        ng Nr   )win32conCREATE_NO_WINDOWr%   CreateProcessr#   hThreadpid)flagsdwTidStartupInfocmdlinecommandenvpathr   s     r   doCreate"Process.__init__.<locals>.doCreate   s@    --E;G;U;U$aT;<8DM4<5r   zD%r is neither a Windows executable, nor a script with a shebang linez has an invalid shebang line: z is not a valid executablec                 <   > TR                   R                  SU 5      $ rG   protochildDataReceiveddatar   s    r   <lambda>"Process.__init__.<locals>.<lambda>       55a>r   c                 <   > TR                   R                  SU 5      $ Nr3   rW   rZ   s    r   r\   r]      r^   r   )6r   _PollingTimerr   r   win32securitySECURITY_ATTRIBUTESbInheritHandle	win32pipe
CreatePipehStdoutRhStderrRhStdinWSetNamedPipeHandleStatePIPE_NOWAITr%   STARTUPINFO
hStdOutput	hStdError	hStdInputSTARTF_USESTDHANDLESdwFlagswin32apiGetCurrentProcessDuplicateHandlerH   DUPLICATE_SAME_ACCESS	win32fileCloseHandleosenvironcopyupdateitemsfsdecoder
   
pywintypesr   rA   OSErrorr<   listinsert_PollableReadPipeoutConnectionLostr   errConnectionLoststderr_PollableWritePipeinConnectionLoststdin_addPollableResourcerX   makeConnectionr   )r   reactorprotocolrQ   r?   environmentrS   sAttrshStdoutWhStderrWhStdinR
currentPidtmpkeyvaluexrT   pwtesheborigcmdpwte2pipewatcherrO   rP   rR   s   `  `  `               @@@r   r   Process.__init__|   sK    	""++D':T8, 224 ! #,"6"6vq"Ax"+"6"6vq"Ax ) 4 4VQ ?))LL)//t	

 #..0!) ( '*?? //1
&&z1a9W9W
 	dmm,&&z1a9W9W
 	dmm,&&j!Q8V8V
 	dll+ jjoo

;$"%FIiikRk
r{{3U!33kR )--1A- & +2"++g&w$(r{{4 d	 	 	-JD 	h'h'g& #44MM>""
 #44MM>""
 "44LL$//

  ;;TZZ?K%%k2 @ 	

!!$'!!'$-0a S ."  	-#D)) dm# $G,<!;=DE 
  :DKK7+,T2G%G"G
- 

%++ -+E22")BI4!Q#  &en,-/	-sC   7R R%R* *U>>A#U9"T//U6.U11U66U99U>c                     U R                   c  [        R                  " 5       eUS;   a"  [        R                  " U R
                  S5        g g )N)INTTERMKILLr   )rL   r   ProcessExitedAlreadyr%   TerminateProcessr#   )r   signalIDs     r   signalProcessProcess.signalProcess  s=    88,,....))$--; /r   c                 f    US:X  a  [         R                  " U5      $ [         R                  " U5      $ r    )r   ProcessDoneProcessTerminated)r   statuss     r   
_getReasonProcess._getReason
  s+    Q;$$V,,&&v..r   c                 :    U R                   R                  U5        g)z9
Write data to the process' stdin.

@type data: C{bytes}
N)r   write)r   r[   s     r   r   Process.write  s     	

r   c                 :    U R                   R                  U5        g)zC
Write data to the process' stdin.

@type seq: C{list} of C{bytes}
N)r   writeSequence)r   seqs     r   r   Process.writeSequence  s     	

  %r   c                 \    US:X  a  U R                   R                  U5        g[        U5      e)a  
Similar to L{ITransport.write} but also allows the file descriptor in
the child process which will receive the bytes to be specified.

This implementation is limited to writing to the child's standard input.

@param fd: The file descriptor to which to write.  Only stdin (C{0}) is
    supported.
@type fd: C{int}

@param data: The bytes to write.
@type data: C{bytes}

@return: L{None}

@raise KeyError: If C{fd} is anything other than the stdin file
    descriptor (C{0}).
r   N)r   r   KeyError)r   fdr[   s      r   writeToChildProcess.writeToChild  s'    & 7JJT"2,r   c                     US:X  a  U R                  5         g US:X  a  U R                  5         g US:X  a  U R                  5         g [        S5      e)Nr   r   r3   z4Only standard-IO file descriptors available on win32)
closeStdincloseStdoutcloseStderrNotImplementedError)r   r   s     r   closeChildFDProcess.closeChildFD7  sI    7OO1W1W%F r   c                 8    U R                   R                  5         g)zClose the process' stdin.N)r   closer   s    r   r   Process.closeStdinC  s    

r   c                 8    U R                   R                  5         g r   )r   r   r   s    r   r   Process.closeStderrG      r   c                 8    U R                   R                  5         g r   )r   r   r   s    r   r   Process.closeStdoutJ  r   r   c                 d    U R                  5         U R                  5         U R                  5         g)z(
Close the process' stdout, in and err.
N)r   r   r   r   s    r   loseConnectionProcess.loseConnectionM  s&     	r   c                 Z    U R                   R                  S5        U R                  5         g rG   rX   childConnectionLostconnectionLostNotifyr   s    r   r   Process.outConnectionLostU       

&&q)!!#r   c                 Z    U R                   R                  S5        U R                  5         g r`   r   r   s    r   r   Process.errConnectionLostY  r   r   c                 Z    U R                   R                  S5        U R                  5         g r    r   r   s    r   r   Process.inConnectionLost]  r   r   c                 N    U =R                   S-  sl         U R                  5         g)zC
Will be called 3 times, by stdout/err threads and process handle.
r   N)closedNotifiesmaybeCallProcessEndedr   s    r   r   Process.connectionLostNotifya  s!     	q ""$r   c                    U R                   S:X  aw  U R                  (       ae  [        R                  " U R                  5        [        R                  " U R
                  5        S U l        S U l        [        R                  " U 5        g g g )N   )r   lostProcessrv   rw   r#   rK   r   r   r   s    r   r   Process.maybeCallProcessEndedh  s`    !#(8(8!!$--0!!$,,/ DMDL--d3 )9#r   c                 :    U R                   R                  X5        g r   )r   registerProducer)r   producer	streamings      r   r   Process.registerProducerq  s    

##H8r   c                 8    U R                   R                  5         g r   )r   unregisterProducerr   s    r   r   Process.unregisterProducert  s    

%%'r   c                 $    U R                  5         g r   )_pauser   s    r   pauseProducingProcess.pauseProducingx  s    r   c                 $    U R                  5         g r   )_unpauser   s    r   resumeProducingProcess.resumeProducing{  s    r   c                 $    U R                  5         g r   )r   r   s    r   stopProducingProcess.stopProducing~  s    r   c                     [        S5      e)NzUnimplemented: Process.getHostr   r   s    r   getHostProcess.getHost      !"BCCr   c                     [        S5      e)NzUnimplemented: Process.getPeerr   r   s    r   getPeerProcess.getPeer  r   r   returnc                 P    SU R                   R                   SU R                   S3$ )z0
Return a string representation of the process.
<z pid=>)	__class__r,   rL   r   s    r   __repr__Process.__repr__  s(     4>>**+5
!<<r   )	r#   rh   ri   rg   rK   rL   r   r   r   N) r,   r-   r.   r/   __doc__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   strr   r0   r1   r   r   rC   rC   e   s    & NF1P</
&0
$$$%49(DD=# =r   rC   )"r   rx   r   zope.interfacer   r~   rr   rH   r!   rv   re   r%   rb   twisted.internetr   r   twisted.internet._baseprocessr   twisted.internet.interfacesr   r   r	   twisted.python.win32r
   rc   PIPE_ATTRS_INHERITABLErd   r   _PollableResourcer   r<   rA   ra   rC   r1   r   r   <module>r     s   

 
 
 &         0 5 O O / '::< ()  %
l,,  6#" 	95g=l((+ g= 6g=r   