o
    Zh :                     @   s  d Z ddlmZ ddlZddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddlm"Z" ddlm#Z# ddlm$Z$ dd lm%Z% ej&j'Z'e( Z)d!Z*d"d# Z+G d$d% d%eZ,G d&d' d'e,Z-e%.e d(G d)d* d*eZ/G d+d, d,eZ0e1d-krdd.l2m3Z3 e3e4 dS dS )/a  
Tests for detecting function memory leaks (typically the ones
implemented in C). It does so by calling a function many times and
checking whether process memory usage keeps increasing between
calls or over time.
Note that this may produce false positives (especially on Windows
for some reason).
PyPy appears to be completely unstable for this framework, probably
because of how its JIT handles memory, so tests are skipped.
    )print_functionN)LINUX)MACOS)OPENBSD)POSIX)SUNOS)WINDOWS)ProcessLookupError)super)HAS_CPU_AFFINITY)HAS_CPU_FREQ)HAS_ENVIRON)
HAS_IONICE)HAS_MEMORY_MAPS)HAS_NET_IO_COUNTERS)HAS_PROC_CPU_NUM)HAS_PROC_IO_COUNTERS)
HAS_RLIMIT)HAS_SENSORS_BATTERY)HAS_SENSORS_FANS)HAS_SENSORS_TEMPERATURES)TestMemoryLeak)create_sockets)
get_testfn)process_namespace)skip_on_access_denied)spawn_testproc)system_namespace)	terminate)unittest   c                  C   s   dd } | S )zsDecorator for those Linux functions which are implemented in pure
    Python, and which we want to run faster.
    c                    s   t   fdd}|S )Nc                    sV   t r | jj}zt| j_ | g|R i |W || j_S || j_w  | g|R i |S N)r   	__class__times	FEW_TIMES)selfargskwargsbeforefun Q/var/www/html/lang_env/lib/python3.10/site-packages/psutil/tests/test_memleaks.pywrapperB   s   z5fewtimes_if_linux.<locals>.decorator.<locals>.wrapper)	functoolswraps)r*   r-   r+   r)   r,   	decoratorA   s   
z$fewtimes_if_linux.<locals>.decoratorr+   )r0   r+   r+   r,   fewtimes_if_linux=   s   r1   c                   @   s  e Zd ZdZeZdd Ze dd Ze dd Z	e dd	 Z
e d
d Zee de dd Zee de dd Ze dd Zdd Zdd Zee ddd Zee ddd Zee de dd Zeeddd  Ze d!d" Ze eed#d$d% Zee d&d'd( Zee de d)d* Z e d+d, Z!e eed#d-d. Z"e d/d0 Z#e ee$ dd1d2 Z%e d3d4 Z&e d5d6 Z'ee de d7d8 Z(d9d: Z)e d;d< Z*ee+ dd=d> Z,ee+ dd?d@ Z-e dAdB Z.ee/ de dCdD Z0ee1 dEee2 ddFdG Z3ee1 dEee2 ddHdI Z4e eedJdKdL Z5ee6 ddMdN Z7ee d&dOdP Z8dQS )RTestProcessObjectLeaksz$Test leaks of Process class methods.c                 C   s    t d }|| |j|j  d S r!   )r   test_class_coverageZgetterssettersr%   nsr+   r+   r,   test_coverage[      z$TestProcessObjectLeaks.test_coveragec                 C      |  | jj d S r!   )executeprocnamer%   r+   r+   r,   	test_name_      z TestProcessObjectLeaks.test_namec                 C   r9   r!   )r:   r;   Zcmdliner=   r+   r+   r,   test_cmdlinec   r?   z#TestProcessObjectLeaks.test_cmdlinec                 C   r9   r!   )r:   r;   exer=   r+   r+   r,   test_exeg   r?   zTestProcessObjectLeaks.test_exec                 C   r9   r!   )r:   r;   Zppidr=   r+   r+   r,   	test_ppidk   r?   z TestProcessObjectLeaks.test_ppidz
POSIX onlyc                 C   r9   r!   )r:   r;   Zuidsr=   r+   r+   r,   	test_uidso      z TestProcessObjectLeaks.test_uidsc                 C   r9   r!   )r:   r;   gidsr=   r+   r+   r,   	test_gidst   rE   z TestProcessObjectLeaks.test_gidsc                 C   r9   r!   )r:   r;   statusr=   r+   r+   r,   test_statusy   r?   z"TestProcessObjectLeaks.test_statusc                 C   r9   r!   )r:   r;   nicer=   r+   r+   r,   	test_nice}      z TestProcessObjectLeaks.test_nicec                    s    t    fdd d S )Nc                         j  S r!   )r;   rJ   r+   Znicenessr%   r+   r,   <lambda>       z6TestProcessObjectLeaks.test_nice_set.<locals>.<lambda>)thisprocrJ   r:   r=   r+   rN   r,   test_nice_set   r8   z$TestProcessObjectLeaks.test_nice_setnot supportedc                 C   r9   r!   )r:   r;   ionicer=   r+   r+   r,   test_ionice   r?   z"TestProcessObjectLeaks.test_ionicec                    s\   t rt   fdd d S   fdd ttjt	 dd} 
t| d S )Nc                      s    j S r!   )r;   rT   r+   r%   valuer+   r,   rO      rP   z8TestProcessObjectLeaks.test_ionice_set.<locals>.<lambda>c                          j tjS r!   )r;   rT   psutilZIOPRIO_CLASS_NONEr+   r=   r+   r,   rO          r   )r   rQ   rT   r:   r.   partialcextZproc_ioprio_setosgetpidexecute_w_excOSErrorr%   r*   r+   rV   r,   test_ionice_set   s   z&TestProcessObjectLeaks.test_ionice_setc                 C   r9   r!   )r:   r;   Zio_countersr=   r+   r+   r,   test_io_counters   rE   z'TestProcessObjectLeaks.test_io_counterszworthless on POSIXc                 C   s   t    | | jj d S r!   )rY   Processusernamer:   r;   r=   r+   r+   r,   test_username   s   z$TestProcessObjectLeaks.test_usernamec                 C   r9   r!   )r:   r;   Zcreate_timer=   r+   r+   r,   test_create_time   r?   z'TestProcessObjectLeaks.test_create_time)Zonly_ifc                 C   r9   r!   )r:   r;   Znum_threadsr=   r+   r+   r,   test_num_threads   rE   z'TestProcessObjectLeaks.test_num_threadsWINDOWS onlyc                 C   r9   r!   )r:   r;   Znum_handlesr=   r+   r+   r,   test_num_handles   r?   z'TestProcessObjectLeaks.test_num_handlesc                 C   r9   r!   )r:   r;   Znum_fdsr=   r+   r+   r,   test_num_fds   rE   z#TestProcessObjectLeaks.test_num_fdsc                 C   r9   r!   )r:   r;   Znum_ctx_switchesr=   r+   r+   r,   test_num_ctx_switches   r?   z,TestProcessObjectLeaks.test_num_ctx_switchesc                 C   r9   r!   )r:   r;   threadsr=   r+   r+   r,   test_threads   rE   z#TestProcessObjectLeaks.test_threadsc                 C   r9   r!   )r:   r;   	cpu_timesr=   r+   r+   r,   test_cpu_times   r?   z%TestProcessObjectLeaks.test_cpu_timesc                 C   r9   r!   )r:   r;   Zcpu_numr=   r+   r+   r,   test_cpu_num   rE   z#TestProcessObjectLeaks.test_cpu_numc                 C   r9   r!   )r:   r;   Zmemory_infor=   r+   r+   r,   test_memory_info   r?   z'TestProcessObjectLeaks.test_memory_infoc                 C   r9   r!   )r:   r;   Zmemory_full_infor=   r+   r+   r,   test_memory_full_info   r?   z,TestProcessObjectLeaks.test_memory_full_infoc                 C   r9   r!   )r:   r;   Zterminalr=   r+   r+   r,   test_terminal   rE   z$TestProcessObjectLeaks.test_terminalc                 C   s$   t rtn| j}| j| jj|d d S )Nr#   )r   r$   r#   r:   r;   resumer%   r#   r+   r+   r,   test_resume      z"TestProcessObjectLeaks.test_resumec                 C   r9   r!   )r:   r;   cwdr=   r+   r+   r,   test_cwd   r?   zTestProcessObjectLeaks.test_cwdc                 C   r9   r!   )r:   r;   cpu_affinityr=   r+   r+   r,   test_cpu_affinity   r?   z(TestProcessObjectLeaks.test_cpu_affinityc                    s4   t    fdd tfdd d S )Nc                      rM   r!   r;   r}   r+   Zaffinityr%   r+   r,   rO      rP   z>TestProcessObjectLeaks.test_cpu_affinity_set.<locals>.<lambda>c                      s    j dgS Nr[   r   r+   r=   r+   r,   rO      rZ   )rQ   r}   r:   r`   
ValueErrorr=   r+   r   r,   test_cpu_affinity_set   s
   z,TestProcessObjectLeaks.test_cpu_affinity_setc                 C   s@   t t d | | jj W d    d S 1 sw   Y  d S )Nw)openr   r:   r;   Z
open_filesr=   r+   r+   r,   test_open_files   s   "z&TestProcessObjectLeaks.test_open_filesc                 C   r9   r!   )r:   r;   Zmemory_mapsr=   r+   r+   r,   test_memory_maps   rE   z'TestProcessObjectLeaks.test_memory_mapsz
LINUX onlyc                    s      fdd d S )Nc                      rX   r!   r;   rlimitrY   RLIMIT_NOFILEr+   r=   r+   r,   rO      rZ   z4TestProcessObjectLeaks.test_rlimit.<locals>.<lambda>r:   r=   r+   r=   r,   test_rlimit   s   z"TestProcessObjectLeaks.test_rlimitc                    s<   t tj  fdd ttffdd d S )Nc                      s   j tj S r!   r   r+   limitr%   r+   r,   rO      s    z8TestProcessObjectLeaks.test_rlimit_set.<locals>.<lambda>c                      s    j dS r   )r;   r   r+   r=   r+   r,   rO      rP   )rQ   r   rY   r   r:   r`   ra   r   r=   r+   r   r,   test_rlimit_set   s   z&TestProcessObjectLeaks.test_rlimit_setzworthless on WINDOWSc                    sL   t   trdnd  fdd W d    d S 1 sw   Y  d S )NZinetallc                      rM   r!   )r;   connectionsr+   kindr%   r+   r,   rO     rP   z9TestProcessObjectLeaks.test_connections.<locals>.<lambda>)r   r   r:   r=   r+   r   r,   test_connections   s   "z'TestProcessObjectLeaks.test_connectionsc                 C   r9   r!   )r:   r;   environr=   r+   r+   r,   test_environ  r?   z#TestProcessObjectLeaks.test_environc                 C      |  dd  d S )Nc                   S      t t S r!   )r]   	proc_infor^   r_   r+   r+   r+   r,   rO   	  rZ   z7TestProcessObjectLeaks.test_proc_info.<locals>.<lambda>r   r=   r+   r+   r,   test_proc_info  r?   z%TestProcessObjectLeaks.test_proc_infoN)9__name__
__module____qualname____doc__rQ   r;   r7   r1   r>   r@   rB   rC   r   skipIfr   rD   rG   rI   rK   rR   r   rU   rc   r   rd   rg   rh   r   r   ri   r   rk   rl   rm   ro   rq   r   rr   rs   rt   ru   ry   r|   r   r~   r   r   r   r   r   r   r   r   r   r   r   r   r+   r+   r+   r,   r2   V   s    






	













r2   c                       sx   e Zd ZdZe fddZe fddZdd Zer8dd	 Z	d
d Z
dd Zdd Zdd Zdd Z  ZS   ZS )TestTerminatedProcessLeakszRepeat the tests above looking for leaks occurring when dealing
    with terminated processes raising NoSuchProcess exception.
    The C functions are still invoked but will follow different code
    paths. We'll check those code paths.
    c                    s:   t    t | _t| jj| _| j  | j	  d S r!   )
r
   
setUpClassr   subprY   re   pidr;   killwaitclsr"   r+   r,   r     s
   

z%TestTerminatedProcessLeaks.setUpClassc                    s   t    t| j d S r!   )r
   tearDownClassr   r   r   r   r+   r,   r     s   
z(TestTerminatedProcessLeaks.tearDownClassc                 C   s$   z|  W d S  t jy   Y d S w r!   )rY   ZNoSuchProcessrb   r+   r+   r,   call   s
   zTestTerminatedProcessLeaks.callc                 C   r9   r!   )r:   r;   r   r=   r+   r+   r,   	test_kill(  rL   z$TestTerminatedProcessLeaks.test_killc                 C   r9   r!   )r:   r;   r   r=   r+   r+   r,   test_terminate+  rL   z)TestTerminatedProcessLeaks.test_terminatec                 C   r9   r!   )r:   r;   Zsuspendr=   r+   r+   r,   test_suspend.  rL   z'TestTerminatedProcessLeaks.test_suspendc                 C   r9   r!   )r:   r;   rw   r=   r+   r+   r,   ry   1  rL   z&TestTerminatedProcessLeaks.test_resumec                 C   r9   r!   )r:   r;   r   r=   r+   r+   r,   	test_wait4  rL   z$TestTerminatedProcessLeaks.test_waitc                    s    fdd}  | d S )Nc                      s&   zt  jjW S  ty   Y d S w r!   )r]   r   r;   r   r	   r+   r=   r+   r,   r   9  s
   z7TestTerminatedProcessLeaks.test_proc_info.<locals>.callr   )r%   r   r+   r=   r,   r   7  s   z)TestTerminatedProcessLeaks.test_proc_info)r   r   r   r   classmethodr   r   r   r   r   r   r   ry   r   r   __classcell__r+   r+   r   r,   r     s    r   rj   c                   @   s   e Zd Zdd Zdd ZdS )TestProcessDualImplementationc                 C   r   )Nc                   S      t jt ddS )NTZuse_pebr]   Zproc_cmdliner^   r_   r+   r+   r+   r,   rO   F      zETestProcessDualImplementation.test_cmdline_peb_true.<locals>.<lambda>r   r=   r+   r+   r,   test_cmdline_peb_trueE  rL   z3TestProcessDualImplementation.test_cmdline_peb_truec                 C   r   )Nc                   S   r   )NFr   r   r+   r+   r+   r,   rO   I  r   zFTestProcessDualImplementation.test_cmdline_peb_false.<locals>.<lambda>r   r=   r+   r+   r,   test_cmdline_peb_falseH  rL   z4TestProcessDualImplementation.test_cmdline_peb_falseN)r   r   r   r   r   r+   r+   r+   r,   r   B  s    r   c                   @   s  e Zd ZdZdd Ze dd Ze dd Ze dd	 Ze d
d Z	e dd Z
e ee ddd Zee ddd Zdd Zeeddd Zdd Zdd Zdd Zeeojejd d e d!d" Ze d#d$ Ze ee dd%d& Ze eeoe  d'kd(d)d* Z!d+d, Z"d-d. Z#e ee$ dd/d0 Z%e ee& dd1d2 Z'e ee( dd3d4 Z)e d5d6 Z*d7d8 Z+d9d: Z,erd;d< Z-d=d> Z.d?d@ Z/dAdB Z0dCdD Z1dES dES )FTestModuleFunctionsLeaksz&Test leaks of psutil module functions.c                 C   s   t  }|| |j d S r!   )r   r3   r   r5   r+   r+   r,   r7   T  s   z&TestModuleFunctionsLeaks.test_coveragec                 C   r   )Nc                   S      t jddS )NTZlogicalrY   	cpu_countr+   r+   r+   r,   rO   \  rP   z9TestModuleFunctionsLeaks.test_cpu_count.<locals>.<lambda>r   r=   r+   r+   r,   test_cpu_countZ  r?   z'TestModuleFunctionsLeaks.test_cpu_countc                 C   r   )Nc                   S   r   )NFr   r   r+   r+   r+   r,   rO   `  rP   z?TestModuleFunctionsLeaks.test_cpu_count_cores.<locals>.<lambda>r   r=   r+   r+   r,   test_cpu_count_cores^  r?   z-TestModuleFunctionsLeaks.test_cpu_count_coresc                 C      |  tj d S r!   )r:   rY   rp   r=   r+   r+   r,   rq   b     z'TestModuleFunctionsLeaks.test_cpu_timesc                 C   r   )Nc                   S   r   )NT)Zpercpu)rY   rp   r+   r+   r+   r,   rO   h  rP   z=TestModuleFunctionsLeaks.test_per_cpu_times.<locals>.<lambda>r   r=   r+   r+   r,   test_per_cpu_timesf  r?   z+TestModuleFunctionsLeaks.test_per_cpu_timesc                 C   r   r!   )r:   rY   Z	cpu_statsr=   r+   r+   r,   test_cpu_statsj  r   z'TestModuleFunctionsLeaks.test_cpu_statsrS   c                 C   r   r!   )r:   rY   Zcpu_freqr=   r+   r+   r,   test_cpu_freqn     z&TestModuleFunctionsLeaks.test_cpu_freqrj   c                 C   s   t   | t j d S r!   )rY   
getloadavgr:   r=   r+   r+   r,   test_getloadavgs  s   z(TestModuleFunctionsLeaks.test_getloadavgc                 C   r   r!   )r:   rY   Zvirtual_memoryr=   r+   r+   r,   test_virtual_memoryz     z,TestModuleFunctionsLeaks.test_virtual_memoryz&worthless on SUNOS (uses a subprocess)c                 C   r   r!   )r:   rY   Zswap_memoryr=   r+   r+   r,   test_swap_memory~  r   z)TestModuleFunctionsLeaks.test_swap_memoryc                 C   $   t rtn| j}| jdd |d d S )Nc                   S   r   r!   )rY   Z
pid_existsr^   r_   r+   r+   r+   r,   rO     rZ   z:TestModuleFunctionsLeaks.test_pid_exists.<locals>.<lambda>rv   r   r$   r#   r:   rx   r+   r+   r,   test_pid_exists  rz   z(TestModuleFunctionsLeaks.test_pid_existsc                 C   r   )Nc                   S   
   t dS )N.)rY   
disk_usager+   r+   r+   r,   rO        
 z:TestModuleFunctionsLeaks.test_disk_usage.<locals>.<lambda>rv   r   rx   r+   r+   r,   test_disk_usage  rz   z(TestModuleFunctionsLeaks.test_disk_usagec                 C   r   r!   )r:   rY   Zdisk_partitionsr=   r+   r+   r,   test_disk_partitions  r   z-TestModuleFunctionsLeaks.test_disk_partitionsz/proc/diskstatsz3/proc/diskstats not available on this Linux versionc                 C   r   )Nc                   S   r   NF)Znowrap)rY   Zdisk_io_countersr+   r+   r+   r,   rO     rP   z@TestModuleFunctionsLeaks.test_disk_io_counters.<locals>.<lambda>r   r=   r+   r+   r,   test_disk_io_counters  s   z.TestModuleFunctionsLeaks.test_disk_io_countersc                 C   r   r!   )r:   rY   Zpidsr=   r+   r+   r,   	test_pids  r   z"TestModuleFunctionsLeaks.test_pidsc                 C   r   )Nc                   S   r   r   )rY   Znet_io_countersr+   r+   r+   r,   rO     rP   z?TestModuleFunctionsLeaks.test_net_io_counters.<locals>.<lambda>r   r=   r+   r+   r,   test_net_io_counters  rE   z-TestModuleFunctionsLeaks.test_net_io_countersr   zneed root accessc                 C   sF   t jdd t  | dd  W d    d S 1 sw   Y  d S )Nr   r   c                   S   r   )Nr   r   )rY   net_connectionsr+   r+   r+   r,   rO     rP   z?TestModuleFunctionsLeaks.test_net_connections.<locals>.<lambda>)rY   r   r   r:   r=   r+   r+   r,   test_net_connections  s   "z-TestModuleFunctionsLeaks.test_net_connectionsc                 C   s"   t rdn| j}| jtj|d d S )Ni @ )	tolerance)r   r   r:   rY   Znet_if_addrs)r%   r   r+   r+   r,   test_net_if_addrs  s   z*TestModuleFunctionsLeaks.test_net_if_addrsc                 C   r   r!   )r:   rY   Znet_if_statsr=   r+   r+   r,   test_net_if_stats  r   z*TestModuleFunctionsLeaks.test_net_if_statsc                 C   r   r!   )r:   rY   Zsensors_batteryr=   r+   r+   r,   test_sensors_battery  r   z-TestModuleFunctionsLeaks.test_sensors_batteryc                 C   r   r!   )r:   rY   Zsensors_temperaturesr=   r+   r+   r,   test_sensors_temperatures  r   z2TestModuleFunctionsLeaks.test_sensors_temperaturesc                 C   r   r!   )r:   rY   Zsensors_fansr=   r+   r+   r,   test_sensors_fans  r   z*TestModuleFunctionsLeaks.test_sensors_fansc                 C   r   r!   )r:   rY   Z	boot_timer=   r+   r+   r,   test_boot_time  r   z'TestModuleFunctionsLeaks.test_boot_timec                 C   r   r!   )r:   rY   Zusersr=   r+   r+   r,   
test_users  r   z#TestModuleFunctionsLeaks.test_usersc                 C   r   )Nc                   S   r   )NF)rY   Z
_set_debugr+   r+   r+   r,   rO     r   z9TestModuleFunctionsLeaks.test_set_debug.<locals>.<lambda>r   r=   r+   r+   r,   test_set_debug  rL   z'TestModuleFunctionsLeaks.test_set_debugc                 C   r   r!   )r:   r]   Zwinservice_enumerater=   r+   r+   r,   test_win_service_iter  r   z.TestModuleFunctionsLeaks.test_win_service_iterc                 C   s   d S r!   r+   r=   r+   r+   r,   test_win_service_get  s   z-TestModuleFunctionsLeaks.test_win_service_getc                    &   t t   |  fdd d S )Nc                      
   t  S r!   )r]   Zwinservice_query_configr+   r<   r+   r,   rO     r   zFTestModuleFunctionsLeaks.test_win_service_get_config.<locals>.<lambda>nextrY   Zwin_service_iterr<   r:   r=   r+   r   r,   test_win_service_get_config     z4TestModuleFunctionsLeaks.test_win_service_get_configc                    r   )Nc                      r   r!   )r]   Zwinservice_query_statusr+   r   r+   r,   rO     r   zFTestModuleFunctionsLeaks.test_win_service_get_status.<locals>.<lambda>r   r=   r+   r   r,   test_win_service_get_status  r   z4TestModuleFunctionsLeaks.test_win_service_get_statusc                    r   )Nc                      r   r!   )r]   Zwinservice_query_descrr+   r   r+   r,   rO     r   zKTestModuleFunctionsLeaks.test_win_service_get_description.<locals>.<lambda>r   r=   r+   r   r,    test_win_service_get_description  r   z9TestModuleFunctionsLeaks.test_win_service_get_descriptionN)2r   r   r   r   r7   r1   r   r   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r^   pathexistsr   r   r   r   r   getuidr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r+   r+   r+   r,   r   Q  sv    









r   __main__)run_from_name)5r   
__future__r   r.   r^   rY   Zpsutil._commonr   r   r   r   r   r   Zpsutil._compatr	   r
   Zpsutil.testsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Z_psplatformr]   re   rQ   r$   r1   r2   r   r   r   r   r   Zpsutil.tests.runnerr   __file__r+   r+   r+   r,   <module>   sd    76 
