o
    tPf#"                     @   s   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d Ze
jr0dd Zned	d ZdddZedd ZeZzd dlZW n eyV   eZeZY nw G dd deZedddZedd ZG dd deZdS )    )absolute_importprint_functiondivisionN   )sysinfoc                    s   t   fdd}|S )Nc                     sP   t   t   t  }t   z di | W |rt   S S |r't   w w )N )gccollect	isenableddisableenable)kwenabledfuncr   Y/var/www/html/humari/django-venv/lib/python3.10/site-packages/gevent/testing/openfiles.pyf3   s   

z_collects.<locals>.f)	functoolswraps)r   r   r   r   r   	_collects%   s   r   c                   C   s
   t d)Nzlsof not expected on Windows)unittestSkipTestr   r   r   r   	_run_lsofC      
r   c                  C   s   dd l } t }| d\}}t| d||f }t|r$tdt|}|	 
 }W d    n1 s9w   Y  t| |S )Nr   get_open_fileszlsof -p %s > %szlsof failed)tempfileosgetpidmkstempclosesystemr   r   openreadstripremove)r   pidfdtmpnamelsof_commandfobjdatar   r   r   r   F   s   




Fc                 K   s   t  }i }|dD ]]}| }|r|drq
td|}|d d \}}}}	|	d d  s4|	 rg| s=|	d  r=q
|	d  sKt|	d d nt|	}	|	|v rc|	||||	|f}
td|
 |||	< q
|sqtd|f ||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:
%sr*   )	r   splitr#   
startswithreisdigitintgetAssertionError)pipes_kwargsr*   resultsliner/   _command_pid_userr&   paramsr   r   r   default_get_open_filesV   s*   $r>   c               
   C   sX   t jdrdt   } tt | S z
ttddd W S  ttt	j
fy+   Y dS w )N/proc//proc/%d/fdT)r6   r   r   )r   pathexistsr   lenlistdirr   OSErrorr5   r   r   )fd_directoryr   r   r   default_get_number_open_filesq   s   rG   c                   @   s   e Zd ZdZdd ZdS )_TrivialOpenFiler&   c                 C   s
   || _ d S NrI   )selfr&   r   r   r   __init__   r   z_TrivialOpenFile.__init__N)__name__
__module____qualname__	__slots__rL   r   r   r   r   rH      s    rH   Tc           
   	   K   s   i }t dD ]9}z.| rtjdrdt  }t|}ng }t }| |d< |d  |	d7  < W  n t
y?   Y qw td|d D ]}|||j< qI|D ]}||vrct|}	t|	||	< qS|d  d|fg7  < |d  d|fg7  < |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.
           r?   r@   r*   allzUnable to read open fileszFrom psutilzfd files)ranger   rA   rB   r   rD   psutilProcess
open_filesconnectionsrE   r   r   r&   r3   rH   )
count_closing_as_open_kwr8   _rF   fd_filesprocessxfd_strr&   r   r   r   r      s2   
r   c                  C   s(   t  } z|  W S  ty   Y dS w )Nr   )rT   rU   num_fdsAttributeError)r\   r   r   r   get_number_open_files   s   
ra   c                       s,   e Zd ZdZ fddZ fddZ  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                    s   t  | _tt|   d S rJ   )ra   (_DoesNotLeakFilesMixin__open_files_countsuperrb   setUp)rK   	__class__r   r   re      s   zDoesNotLeakFilesMixin.setUpc                    s8   t t|   t }|| jkrtd| j|t f d S )Nz/Too many open files. Before: %s < After: %s.
%s)rd   rb   tearDownra   rc   r5   r   )rK   afterrf   r   r   rh      s   
zDoesNotLeakFilesMixin.tearDown)rM   rN   rO   __doc__re   rh   __classcell__r   r   rf   r   rb      s    rb   )F)T)
__future__r   r   r   r   r   r1   r   r    r   r   WINr   r>   rG   lsof_get_open_filesrT   ImportErrorr   ra   objectrH   rb   r   r   r   r   <module>   s8   	



6

