
    i#"                     2   d dl mZmZmZ d dlZd dlZd dlZd dlZd dlZddl	m
Z
 d Ze
j        rd Zned             Zdd	Zed
             ZeZ	 d dlZ G d de          Zedd            Zed             Zn# e$ r eZeZY nw xY w G d de          ZdS )    )absolute_importprint_functiondivisionN   )sysinfoc                 F     t          j                    fd            }|S )Nc                     t          j                     t          j                     t          j                    }t          j                     	  di | |rt          j                     S S # |rt          j                     w w xY w)N )gccollect	isenableddisableenable)kwenabledfuncs     C/usr/local/lib/python3.11/dist-packages/gevent/testing/openfiles.pyfz_collects.<locals>.f3   sz    



,..

	4::":: 	w 	s   A- -B)	functoolswraps)r   r   s   ` r   	_collectsr   %   s:     _T
 
 
 
 
 H    c                  *    t          j        d          )Nzlsof not expected on Windows)unittestSkipTestr
   r   r   	_run_lsofr   C   s     >???r   c                     dd l } t          j                    }|                     d          \  }}t          j        |           d|d|}t          j        |          rt          j        d          t          |          5 }|	                                
                                }d d d            n# 1 swxY w Y   t          j        |           |S )Nr   get_open_fileszlsof -p z > zlsof failed)tempfileosgetpidmkstempclosesystemr   r   openreadstripremove)r   pidfdtmpnamelsof_commandfobjdatas          r   r   r   F   s    ikk&&'788G
,/CC99\"" 	3#M222']] 	'd99;;$$&&D	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	'
	's   'B66B:=B:Fc                    t                      }i }|                    d          D ]}|                                }|r|                    d          r/t	          j        d|          }|d d         \  }}}}	|	d d                                         s|	                                r| s|	d                                         r|	d                                         st          |	d d                   nt          |	          }	|	|v r,|	|||                    |	          |f}
t          d|
z            |||	<   |st          d|          ||d<   |S )	N
COMMANDz\s+   zUerror when parsing lsof output: duplicate fd=%r
line=%r
split=%r
previous=%r
data:
%szfailed to parse lsof:
r.   )	r   splitr'   
startswithreisdigitintgetAssertionError)pipes_kwargsr.   resultsliner4   _command_pid_userr*   paramss              r   default_get_open_filesrC   V   sy   ;;DG

4    zz|| 	ty11 	&& %*"1"I!$rcrc7?? 	

 	 RV^^-- %'V^^%5%5BRW3r77BW}}dE7;;r??DA$  &B  EK  &K  L  L  LGBK EnDDCDDDGFONr   c                  6   t           j                            d          r7dt          j                    z  } t	          t          j        |                     S 	 t	          t          d                    dz
  S # t          t          t          j
        f$ r Y dS w xY w)N/proc//proc/%d/fdT)r;   r   r   )r    pathexistsr!   lenlistdirr   OSErrorr:   r   r   )fd_directorys    r   default_get_number_open_filesrM   q   s    	w~~h -$ry{{22:l++,,,>---..22^X%67   qqs   A8 8BBc                       e Zd ZdZd ZdS )_TrivialOpenFiler*   c                     || _         d S NrP   )selfr*   s     r   __init__z_TrivialOpenFile.__init__   s    DGGGr   N)__name__
__module____qualname__	__slots__rT   r
   r   r   rO   rO      s(        		 	 	 	 	r   rO   Tc                 z   i }t          d          D ]}	 | rJt          j                            d          r+dt          j                    z  }t          j        |          }ng }t          j                    }|                                |d<   |dxx         |	                    d          z  cc<    n$# t          $ r Y w xY wt          j        d          |d         D ]}|||j        <   |D ]'}||vr!t          |          }	t          |	          ||	<   (|dxx         d|fgz  cc<   |dxx         d|fgz  cc<   |S )	a  
        Return a list of popenfile and pconn objects.

        Note that other than `fd`, they have different attributes.

        .. important:: If you want to find open sockets, on Windows
           and linux, it is important that the socket at least be listening
           (socket.listen(1)). Unlike the lsof implementation, this will only
           return sockets in a state like that.
           rE   rF   r.   allzUnable to read open fileszFrom psutilzfd files)ranger    rG   rH   r!   rJ   psutilProcess
open_filesconnectionsrK   r   r   r*   r8   rO   )
count_closing_as_open_kwr=   _rL   fd_filesprocessxfd_strr*   s
             r   r   r      s    q 	A 	AA( "RW^^H-E-E " $129;;#>L!z,77HH!H .**")"4"4"6"67#6#6u#=#==    #$?@@@ 	 	AGADMM 	3 	3FW$$[[.r22]G455Z233s   BB22
B?>B?c                  t    t          j                    } 	 |                                 S # t          $ r Y dS w xY w)Nr   )r]   r^   num_fdsAttributeError)re   s    r   get_number_open_filesrk      sF    .""	??$$$ 	 	 	11	s   ) 
77c                   ,     e Zd ZdZ fdZ fdZ xZS )DoesNotLeakFilesMixinz
    A test case mixin that helps find a method that's leaking an
    open file.

    Only mix this in when needed to debug, it slows tests down.
    c                 |    t                      | _        t          t          |                                            d S rR   )rk   (_DoesNotLeakFilesMixin__open_files_countsuperrm   setUp)rS   	__class__s    r   rq   zDoesNotLeakFilesMixin.setUp   s4    "7"9"9#T**0022222r   c           	          t          t          |                                            t                      }|| j        k    r)t          d| j        d|dt                                d S )NzToo many open files. Before: z
 < After: z.
)rp   rm   tearDownrk   ro   r:   r   )rS   afterrr   s     r   rt   zDoesNotLeakFilesMixin.tearDown   sy    #T**33555%''4*** .+++EE"$$$   +*r   )rU   rV   rW   __doc__rq   rt   __classcell__)rr   s   @r   rm   rm      s[         3 3 3 3 3
 
 
 
 
 
 
 
 
r   rm   )F)T)
__future__r   r   r   r    r   r6   r   r    r   r   WINr   rC   rM   lsof_get_open_filesr]   objectrO   r   rk   ImportErrorrm   r
   r   r   <module>r~      s  ( A @ @ @ @ @ @ @ @ @ 				  				 				            : ; @ @ @ @   Y   6 	 	 	 - K MMM
    6   
 4 4 4 Y4l   Y C  : : :+N9:V    F     s   A: :	BB