o
    F+ iu                     @   s  d Z ddlZddlZddlZddlZddlZddlZddl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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 erese  ed ddl Z ddl!Z!ddl"Z"ddl#Z#W d   n1 sw   Y  erddl$m%Z% ej&j'Z'ej(j)e ddej(j)eddG dd deZ*dd Z+e,fddZ-G dd de*Z.G dd de*Z/G d d! d!e*Z0G d"d# d#e*Z1G d$d% d%e*Z2ej(j)e ddG d&d' d'eZ3ej(j)e ddG d(d) d)eZ4ej(j)e ddG d*d+ d+eZ5dS ),zWindows specific tests.    N)mock)WINDOWS)GITHUB_ACTIONS)HAS_BATTERY)IS_64BIT)PYPY)TOLERANCE_DISK_USAGE)TOLERANCE_SYS_MEM)PsutilTestCase)pytest)retry_on_failure)sh)spawn_subproc)	terminateignore)convert_oserrorzWINDOWS onlyreasonzpywin32 not available on PYPYc                   @   s   e Zd ZdS )WindowsTestCaseN)__name__
__module____qualname__ r   r   e/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/psutil/tests/test_windows.pyr   5   s    r   c                 C   s(   t ds
tdS d|  d}t|S )zCurrently not used, but available just in case. Usage:

    >>> powershell(
        "Get-CIMInstance Win32_PageFileUsage | Select AllocatedBaseSize")
    zpowershell.exezpowershell.exe not availablezhpowershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -Command "")shutilwhichr   skipr   )cmdcmdliner   r   r   
powershell;   s   

r    c                    sh   t d|  d|  }d| dd  } dur2d|v r.t fdd| D S  |S |S )	zCurrently not used, but available just in case. Usage:

    >>> wmic("Win32_OperatingSystem", "FreePhysicalMemory")
    2134124534
    z
wmic path z get     N,c                 3   s    | ]} |V  qd S Nr   .0x	converterr   r   	<genexpr>T   s    zwmic.<locals>.<genexpr>)r   stripjoin
splitlinestuplesplit)pathwhatr)   outdatar   r(   r   wmicJ   s   r4   c                   @   sR   e Zd Zejjdejvdddd Zdd Z	dd	 Z
d
d Zdd Zdd ZdS )TestCpuAPIsNUMBER_OF_PROCESSORSz-NUMBER_OF_PROCESSORS env var is not availabler   c                 C   s"   t tjd }|t ksJ d S )Nr6   )intosenvironpsutil	cpu_count)selfZnum_cpusr   r   r   &test_cpu_count_vs_NUMBER_OF_PROCESSORSa   s   z2TestCpuAPIs.test_cpu_count_vs_NUMBER_OF_PROCESSORSc                 C   s$   t  d }t }||ksJ d S )N   )win32apiZGetSystemInfor:   r;   r<   	sys_valuepsutil_valuer   r   r   test_cpu_count_vs_GetSystemInfok   s   z+TestCpuAPIs.test_cpu_count_vs_GetSystemInfoc                 C   s2   t  }tdd | D }t |ksJ d S )Nc                 s       | ]}|j V  qd S r$   )ZNumberOfLogicalProcessorsr&   procr   r   r   r*   t   s    
z<TestCpuAPIs.test_cpu_count_logical_vs_wmi.<locals>.<genexpr>wmiWMIsumWin32_Processorr:   r;   )r<   wZprocsr   r   r   test_cpu_count_logical_vs_wmir   s
   z)TestCpuAPIs.test_cpu_count_logical_vs_wmic                 C   s6   t  }tdd | D }tjdd|ksJ d S )Nc                 s   rD   r$   )ZNumberOfCoresrE   r   r   r   r*   {   s    z:TestCpuAPIs.test_cpu_count_cores_vs_wmi.<locals>.<genexpr>F)ZlogicalrG   )r<   rL   Zcoresr   r   r   test_cpu_count_cores_vs_wmiy   s   z'TestCpuAPIs.test_cpu_count_cores_vs_wmic                 C   s    t  tt jddksJ d S )NT)Zpercpu)r:   r;   len	cpu_timesr<   r   r   r   test_cpu_count_vs_cpu_times~   s    z'TestCpuAPIs.test_cpu_count_vs_cpu_timesc                 C   s@   t  }| d }|jt jksJ |jt jksJ d S Nr   )	rH   rI   rK   ZCurrentClockSpeedr:   Zcpu_freqcurrentZMaxClockSpeedmax)r<   rL   rF   r   r   r   test_cpu_freq   s   zTestCpuAPIs.test_cpu_freqN)r   r   r   r   markskipifr8   r9   r=   rC   rM   rN   rR   rV   r   r   r   r   r5   `   s    
r5   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Ze dd Z	e dd Z
e dd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )TestSystemAPIsc                 C   sX   t d}tjdd }|D ]}d|dd v rq||vr)t|d  S qd S )Nzipconfig /allT)Zperniczpseudo-interface -z+ nic wasn't found in 'ipconfig /all' output)r   r:   Znet_io_counterskeysreplacelowerr   fail)r<   r2   ZnicsZnicr   r   r   test_nic_names   s   zTestSystemAPIs.test_nic_namesc                 C   s,   t   d }t|jt jksJ d S rS   )rH   rI   ZWin32_ComputerSystemr7   ZTotalPhysicalMemoryr:   virtual_memorytotalr<   rL   r   r   r   test_total_phymem   s   z TestSystemAPIs.test_total_phymemc                 C   s4   t   d }tt|jt j t	k sJ d S rS   )
rH   rI   Win32_PerfRawData_PerfOS_Memoryabsr7   ZAvailableBytesr:   ra   freer	   rc   r   r   r   test_free_phymem   s   zTestSystemAPIs.test_free_phymemc                 C   sl   t   d }t|jt j t jksJ t jdkr2t j	dks)J t j
dks4J d S d S rS   )rH   rI   re   r7   ZCommitLimitr:   ra   rb   swap_memoryrg   usedrc   r   r   r   test_total_swapmem   s   z!TestSystemAPIs.test_total_swapmemc                 C   s   t  jdkr<t jddd }t|jd t|j }t  j	dks&J t
t  j	| dk s3J t  j	dks>J d S d S )Nr   Z_Total)Named   r>   )r:   ri   rb   rH   rI   Z#Win32_PerfRawData_PerfOS_PagingFiler7   ZPercentUsageZPercentUsage_Basepercentrf   )r<   rL   ZpercentSwapr   r   r   test_percent_swapmem   s   z#TestSystemAPIs.test_percent_swapmemc                 C   s6   t   }dd |D }tt }||ksJ d S )Nc                 S   s   h | ]}|j qS r   Z	ProcessIdr%   r   r   r   	<setcomp>   s    z+TestSystemAPIs.test_pids.<locals>.<setcomp>)rH   rI   Win32_Processsetr:   pids)r<   rL   Zwmi_pidsZpsutil_pidsr   r   r   	test_pids   s   zTestSystemAPIs.test_pidsc              
   C   s  t jdd}t  }|D ]s}|D ]d}|jdd|jkrv|js# n^d|j	v r* nW|j
dr2 nOzt |j}W n
 tyD   Y  n=w |jt|jksOJ t|j}|j|ks[J t|j| dkrttd|j d	|     S  nqtd
|  S qd S )NTall\r!   cdromA:i   zpsutil=z, wmi=zcan't find partition )r:   disk_partitionsrH   rI   ZWin32_LogicalDiskZdevicer]   ZDeviceID
mountpointopts
startswith
disk_usageFileNotFoundErrorrb   r7   SizeZ	FreeSpacerg   rf   r   r_   )r<   Zps_partsZ	wmi_partsZps_partZwmi_partusageZwmi_freer   r   r   
test_disks   s8   

zTestSystemAPIs.test_disksc                 C   s   t  D ]9}d|jv rqt|j}t |j}t|d |j t	k s%J t|d |j
 t	k s2J |j|j
|j ks=J qd S )Nry   r   r"   )r:   r{   r}   r?   ZGetDiskFreeSpaceExr|   r   rf   rg   r   rb   rj   )r<   ZdiskrA   rB   r   r   r   test_disk_usage   s   
zTestSystemAPIs.test_disk_usagec                 C   s>   dd t  dD }dd tjddD }||ksJ d S )Nc                 S   s"   g | ]}|r| d s|d qS )rz   rx   )r~   r%   r   r   r   
<listcomp>   s    z7TestSystemAPIs.test_disk_partitions.<locals>.<listcomp>z\ c                 S   s   g | ]}|j d s|j qS )rz   )r|   r~   r%   r   r   r   r      s    
Trv   )r?   ZGetLogicalDriveStringsr/   r:   r{   r@   r   r   r   test_disk_partitions   s   
z#TestSystemAPIs.test_disk_partitionsc                 C   s   d}d}d}d}d}||i}t jd|jd"}tj||ks!J tj||ks+J |js0J W d    d S 1 s;w   Y  d S )NzC:\Windows\TempzC:z\Device\HarddiskVolume1z\??\C:\Windows\Tempz$\Device\HarddiskVolume1\Windows\Tempz%psutil._pswindows.cext.QueryDosDeviceZside_effect)r   patchgetr:   
_pswindowsconvert_dos_pathcalled)r<   winpathZdriveletterZ
devicepathntpath1ntpath2Zdevicesmr   r   r   test_convert_dos_path_drive  s   "z*TestSystemAPIs.test_convert_dos_path_drivec                 C   sL   d}d}d}t j||ksJ t j||ksJ t j||ks$J d S )Nz\\localhost\C$\Windows\Tempz!\??\UNC\localhost\C$\Windows\Tempz%\Device\Mup\localhost\C$\Windows\Temp)r:   r   r   )r<   r   r   r   r   r   r   test_convert_dos_path_unc  s   z(TestSystemAPIs.test_convert_dos_path_uncc                 C   s`   t t }t  }t  }|D ]}||j ||j q||@ s.J d| d| d S )Nzno common entries in z, )	rs   cextZnet_if_statsrH   rI   ZWin32_NetworkAdapteraddrl   ZNetConnectionID)r<   Zps_namesZwmi_adaptersZ	wmi_namesZwmi_adapterr   r   r   test_net_if_stats"  s   z TestSystemAPIs.test_net_if_statsc                 C   sf   t   }|d jdd }tj|d}tjt	 }t
||  }|dks1J ||fd S )Nr   .%Y%m%d%H%M%Sr>   )rH   rI   ZWin32_OperatingSystemZLastBootUpTimer/   datetimestrptimefromtimestampr:   Z	boot_timerf   total_seconds)r<   Zwmi_osZwmi_btime_strZwmi_btime_dtZ	psutil_dtdiffr   r   r   test_boot_time-  s   zTestSystemAPIs.test_boot_timec                 C   s0   t jj }|d }tt | dk sJ d S )Ng     @@g      ?)ctypeswindllkernel32ZGetTickCount64rf   r   Zuptime)r<   msZsecsr   r   r   test_uptime7  s   zTestSystemAPIs.test_uptimeN)r   r   r   r`   rd   rh   rk   ro   r   ru   r   r   r   r   r   r   r   r   r   r   r   r   rY      s$    




rY   c                   @   sh   e Zd Zdd Zejje dddd Zejje dddd Z	d	d
 Z
dd Zdd Zdd ZdS )TestSensorsBatteryc                 C   s4   t  d rt d usJ d S t d u sJ d S )NZSystemBatteriesPresent)r?   ZGetPwrCapabilitiesr:   sensors_batteryrQ   r   r   r   test_has_batteryE  s   z#TestSensorsBattery.test_has_batteryz
no batteryr   c                 C   s:   t  }|dd }t }t|j|j dk sJ d S )Nselect * from Win32_Batteryr   r"   )rH   rI   queryr:   r   rf   rn   ZEstimatedChargeRemainingr<   rL   Zbattery_wmiZbattery_psutilr   r   r   test_percentK  s   zTestSensorsBattery.test_percentc                 C   s6   t  }|dd }t }|j|jdkksJ d S )Nr   r      )rH   rI   r   r:   r   Zpower_pluggedZBatteryStatusr   r   r   r   test_power_pluggedU  s   z%TestSensorsBattery.test_power_pluggedc                 C   sN   t jddd}t d u sJ |jsJ W d    d S 1 s w   Y  d S )N&psutil._pswindows.cext.sensors_battery)r      r   r   return_value)r   r   r:   r   r   r<   r   r   r   r   test_emulate_no_battery^  s   "z*TestSensorsBattery.test_emulate_no_batteryc                 C   R   t jddd}t jtjksJ |jsJ W d    d S 1 s"w   Y  d S )Nr   )r"   r   r   r   r   r   r   r:   r   secsleftZPOWER_TIME_UNLIMITEDr   r   r   r   r   test_emulate_power_connectedf     "z/TestSensorsBattery.test_emulate_power_connectedc                 C   r   )Nr   )r      r   r   r   r   r   r   r   r   test_emulate_power_chargingp  r   z.TestSensorsBattery.test_emulate_power_chargingc                 C   r   )Nr   )r   r   r   r   )r   r   r:   r   r   ZPOWER_TIME_UNKNOWNr   r   r   r   r   test_emulate_secs_left_unknownz  s   "z1TestSensorsBattery.test_emulate_secs_left_unknownN)r   r   r   r   r   rW   rX   r   r   r   r   r   r   r   r   r   r   r   r   D  s    
	


r   c                   @   s   e Zd Zedd Zedd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#S )$TestProcessc                 C      t  j| _d S r$   r   pidclsr   r   r   
setUpClass     zTestProcess.setUpClassc                 C      t | j d S r$   r   r   r   r   r   r   tearDownClass  r   zTestProcess.tearDownClassc                 C   sD   t d}tt j |  W d    d S 1 sw   Y  d S rS   )r:   Processr   raisesAccessDeniedkillr<   pr   r   r   test_issue_24  s   

"zTestProcess.test_issue_24c                 C   s   t d}| dksJ t| |  | dksJ z| d d \}}W n t jy=   t	 d dvr: Y d S w |dksDJ d S )N   Systemg        r   r"   >   zwin-7ZvistaZwin7r   )
r:   r   namestrusernamecreate_timememory_infor   platformuname)r<   r   rssZ_vmsr   r   r   test_special_pid  s   
zTestProcess.test_special_pidc                 C   sH   t | j}tt |tj W d    d S 1 sw   Y  d S r$   )	r:   r   r   r   r   
ValueErrorsend_signalsignalSIGINTr   r   r   r   test_send_signal  s   "zTestProcess.test_send_signalc                 C   sb   t t }| }ttjtj	t }| }||d ks"J t
| | |ks/J d S )Nr"   )r:   r   r8   getpidnum_handlesr?   OpenProcesswin32conPROCESS_QUERY_INFORMATIONFALSECloseHandle)r<   r   beforehandleafterr   r   r   test_num_handles_increment  s   
z&TestProcess.test_num_handles_incrementc                 C   s   t |  j}|tj |tj |  |	  t
t j |tj W d    n1 s3w   Y  t
t j |tj W d    d S 1 sPw   Y  d S r$   )r:   r   r   r   r   r   ZCTRL_C_EVENTZCTRL_BREAK_EVENTr   waitr   r   NoSuchProcessr   r   r   r   test_ctrl_signals  s   "zTestProcess.test_ctrl_signalsc                 C   s8   t tj}|drtdS t 	 |ksJ d S )N$zrunning as service account)
r?   ZGetUserNameExr   ZNameSamCompatibleendswithr   r   r:   r   r   )r<   r   r   r   r   test_username  s   

zTestProcess.test_usernamec                 C   sP   t ddt  }dt  }|	dd}|	dd}||ks&J d S )Nz[ ]+rZ   r   r!   )
resubr?   ZGetCommandLiner+   r,   r:   r   r   r]   r@   r   r   r   test_cmdline  s
   zTestProcess.test_cmdlinec                 C   sJ   t tjtjt }| t j| t	
|}t  }||ks#J d S r$   )r?   r   r   r   r   r8   r   
addCleanupr   win32processZGetPriorityClassr:   r   nicer<   r   rA   rB   r   r   r   	test_nice  s   
zTestProcess.test_nicec                 C   s   t tjtj| j}| t j| t	|}t
| j }|d |jks'J |d |jks0J |d |jks9J |d |jksBJ |d |jksKJ |d |jksTJ |d |jks]J |d |jksfJ |j|jksnJ |j|jksvJ d S )	NZPeakWorkingSetSizeWorkingSetSizeZQuotaPeakPagedPoolUsageZQuotaPagedPoolUsageZQuotaPeakNonPagedPoolUsageZQuotaNonPagedPoolUsageZPagefileUsageZPeakPagefileUsage)r?   r   r   r   r   r   r   r   r   ZGetProcessMemoryInfor:   r   r   Z	peak_wsetZwsetZpeak_paged_poolZ
paged_poolZpeak_nonpaged_poolZnonpaged_poolZpagefileZpeak_pagefiler   vmsr   r   r   r   test_memory_info  s(   
zTestProcess.test_memory_infoc                 C   sX   t tjtj| j}| t j| t	| j}|
  | }t|}||ks*J d S r$   )r?   r   r   r   r   r   r   r   r:   r   r   r   r   ZGetExitCodeProcess)r<   r   r   rB   rA   r   r   r   	test_wait  s   
zTestProcess.test_waitc                 C   s\   dd }t tjtj| j}| t j| |t	|d }t
| j }||ks,J d S )Nc                    s    fddt dD S )Nc                    s   g | ]
}d |>  @ r|qS )r"   r   )r&   ir'   r   r   r     s    zGTestProcess.test_cpu_affinity.<locals>.from_bitmask.<locals>.<listcomp>@   )ranger   r   r   r   from_bitmask  s   z3TestProcess.test_cpu_affinity.<locals>.from_bitmaskr   )r?   r   r   r   r   r   r   r   r   ZGetProcessAffinityMaskr:   r   Zcpu_affinity)r<   r   r   rA   rB   r   r   r   test_cpu_affinity  s   zTestProcess.test_cpu_affinityc                 C   s   t tjtjt }| t j| t	
|}t  }|j|d ks&J |j|d ks/J |j|d ks8J |j|d ksAJ |j|d ksJJ |j|d ksSJ d S )NZReadOperationCountZWriteOperationCountZReadTransferCountZWriteTransferCountZOtherOperationCountZOtherTransferCount)r?   r   r   r   r   r8   r   r   r   r   ZGetProcessIoCountersr:   r   io_countersZ
read_countZwrite_count
read_byteswrite_bytesother_countZother_bytesr   r   r   r   test_io_counters!  s   
zTestProcess.test_io_countersc                 C   s   dd l }dd l}d}|jj|d| j}| |jjj| |j	 }|jj
||| |j}t| j }||ks>J d S )Nr      )r   Zctypes.wintypesr   r   r   r   r   r   ZwintypesZDWORDZGetProcessHandleCountZbyrefvaluer:   r   r   )r<   r   r   r   ZhndcntrA   rB   r   r   r   test_num_handles/  s   

zTestProcess.test_num_handlesc              
   C   s   t  }d|_tjd|d; td&}t }ttj |	  W d    n1 s-w   Y  W d    n1 s<w   Y  W d    n1 sKw   Y  |j
dksWJ d S )Ni+  z psutil._psplatform.cext.proc_cwdr   z
time.sleepr>   )OSErrorwinerrorr   r   r:   r   r   r   r   cwdZ
call_count)r<   excr   r   r   r   r   test_error_partial_copyA  s   
z#TestProcess.test_error_partial_copyc                 C   sV   t  d d }t j|}tt j |  W d    d S 1 s$w   Y  d S )Nr   i )r:   rt   _psplatformr   r   r   r   exe)r<   r   rF   r   r   r   test_exeL  s
   
"zTestProcess.test_exeN)r   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r   r   r   r   r     s(    

		r   c                   @   s|   e Zd ZdZedd Zedd Zdd Zej	j
edd	d
d Zdd Zdd Ze dd Ze dd Zdd ZdS )TestProcessWMIz%Compare Process API results with WMI.c                 C   r   r$   r   r   r   r   r   r   X  r   zTestProcessWMI.setUpClassc                 C   r   r$   r   r   r   r   r   r   \  r   zTestProcessWMI.tearDownClassc                 C   s8   t  j| jdd }t| j}| |jksJ d S Nrp   r   )rH   rI   rr   r   r:   r   r   ZCaptionr<   rL   r   r   r   r   	test_name`  s   zTestProcessWMI.test_namez!unreliable path on GITHUB_ACTIONSr   c                 C   s@   t  j| jdd }t| j}|  |j ksJ d S r  )	rH   rI   rr   r   r:   r   r  r^   ZExecutablePathr  r   r   r   r  f  s   zTestProcessWMI.test_exec                 C   sF   t  j| jdd }t| j}d| |j	ddks!J d S )Nrp   r   rZ   r   r!   )
rH   rI   rr   r   r:   r   r,   r   ZCommandLiner]   r  r   r   r   r   p  s   $zTestProcessWMI.test_cmdlinec                 C   sR   t  j| jdd }t| j}| \}}}| d| }| |ks'J d S )Nrp   r   rx   )rH   rI   rr   r   r:   r   ZGetOwnerr   )r<   rL   r   domain_r   r   r   r   r   u  s
   zTestProcessWMI.test_usernamec                 C   sB   t  j| jdd }t| j}| j}|t|j	ksJ d S r  )
rH   rI   rr   r   r:   r   r   r   r7   r   )r<   rL   r   r   r   r   r   test_memory_rss|  s   
zTestProcessWMI.test_memory_rssc                 C   s`   t  j| jdd }t| j}| j}t|j	}|||d hvr.t
d| d| S d S )Nrp   r   r  zwmi=z	, psutil=)rH   rI   rr   r   r:   r   r   r   r7   ZPageFileUsager   r_   )r<   rL   r   r   Z	wmi_usager   r   r   test_memory_vms  s   

zTestProcessWMI.test_memory_vmsc                 C   s\   t  j| jdd }t| j}t|jdd }t	
dt	| }||ks,J d S )Nrp   r   r   r   )rH   rI   rr   r   r:   r   r   ZCreationDater/   timestrftime	localtimer   )r<   rL   r   Zwmic_createZpsutil_creater   r   r   test_create_time  s   zTestProcessWMI.test_create_timeN)r   r   r   __doc__r  r   r   r  r   rW   rX   r   r  r   r   r   r  r  r  r   r   r   r   r  U  s$    




r  c                   @   sX   e Zd ZdZedd Zedd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd ZdS )TestDualProcessImplementationaw  Certain APIs on Windows have 2 internal implementations, one
    based on documented Windows APIs, another one based
    NtQuerySystemInformation() which gets called as fallback in
    case the first fails because of limited permission error.
    Here we test that the two methods return the exact same value,
    see:
    https://github.com/giampaolo/psutil/issues/304.
    c                 C   r   r$   r   r   r   r   r   r     r   z(TestDualProcessImplementation.setUpClassc                 C   r   r$   r   r   r   r   r   r     r   z+TestDualProcessImplementation.tearDownClassc                 C   s   t | j }tjdtdG}t | j }t|t|ks"J tt|D ] }|| dks2J || dks:J t	|| ||  dk sHJ q(|j
sNJ W d    d S 1 sYw   Y  d S )Nz(psutil._psplatform.cext.proc_memory_infor   r   i   )r:   r   r   r   r   r   PermissionErrorrO   r   rf   r   )r<   Zmem_1funZmem_2r   r   r   r   r     s   "z.TestDualProcessImplementation.test_memory_infoc                 C   f   t | j }tjdtd}t | j |ksJ |js!J W d    d S 1 s,w   Y  d S )N"psutil._psplatform.cext.proc_timesr   )r:   r   r   r   r   r   r  r   )r<   ctimer   r   r   r   r       "z.TestDualProcessImplementation.test_create_timec                 C   s   t | j }tjdtd.}t | j }|jsJ t|j	|j	 dk s)J t|j
|j
 dk s5J W d    d S 1 s@w   Y  d S )Nr"  r   g{Gz?)r:   r   r   rP   r   r   r  r   rf   usersystem)r<   Zcpu_times_1r   Zcpu_times_2r   r   r   test_cpu_times  s   
"z,TestDualProcessImplementation.test_cpu_timesc                 C   s   t | j }tjdtd-}t | j }tt|D ]}t	|| ||  dk s.J q|j
s4J W d    d S 1 s?w   Y  d S )Nz(psutil._psplatform.cext.proc_io_countersr   r>   )r:   r   r   r   r   r   r  r   rO   rf   r   )r<   Zio_counters_1r   Zio_counters_2r   r   r   r   r    s   "z.TestDualProcessImplementation.test_io_countersc                 C   r!  )Nz(psutil._psplatform.cext.proc_num_handlesr   )r:   r   r   r   r   r   r  r   )r<   r   r   r   r   r   r    r$  z.TestDualProcessImplementation.test_num_handlesc                 C   s   t  D ]9}ztj|dd}tj|dd}W n  ty6 } zt|}t|t jt jfs, W Y d }~qd }~ww ||ks=J qd S )NT)Zuse_pebF)	r:   rt   r   Zproc_cmdliner  r   
isinstancer   r   )r<   r   aberrr   r   r   r     s   z*TestDualProcessImplementation.test_cmdlineN)r   r   r   r  r  r   r   r   r  r'  r  r  r   r   r   r   r   r    s    	

		r  c                       sp   e Zd ZdZedd ZddgZ fddZ fdd	Zd
d Z	dd Z
dd Zdd Zdd Zdd Z  ZS )RemoteProcessTestCasezCertain functions require calling ReadProcessMemory.
    This trivially works when called on the current process.
    Check that this works on other processes, especially when they
    have a different bitness.
    c                  C   s\   d} t  dD ]$}tj|d| gtjtjd}| \}}|  |tt kr+|  S qd S )Nz6import sys; sys.stdout.write(str(sys.maxsize > 2**32))zC:\Python*\python.exe-c)argsstdoutstderr)	glob
subprocessPopenPIPESTDOUTcommunicater   r   r   )codefilenamerF   outputr  r   r   r   find_other_interpreter  s   z,RemoteProcessTestCase.find_other_interpreterr-  zimport sys; sys.stdin.read()c                    s   t    |  }|d u rtdS trtj| _|| _	n|| _tj| _	t
j }tt
 |d< | j| j	g| j |tjd| _| j| jg| j |tjd| _d S )Nz0could not find interpreter with opposite bitnessTHINK_OF_A_NUMBER)envstdin)supersetUpr:  r   r   r   sys
executableZpython64Zpython32r8   r9   copyr   r   r   	test_argsr2  r4  proc32proc64)r<   Zother_pythonr<  	__class__r   r   r?    s&   

zRemoteProcessTestCase.setUpc                    s"   t    | j  | j  d S r$   )r>  tearDownrD  r6  rE  rQ   rF  r   r   rH  )  s   

zRemoteProcessTestCase.tearDownc                 C   @   t | jj}t| dksJ | dd  | jksJ d S N   r"   )r:   r   rD  r   rO   r   rC  r   r   r   r   test_cmdline_32.     z%RemoteProcessTestCase.test_cmdline_32c                 C   rI  rJ  )r:   r   rE  r   rO   r   rC  r   r   r   r   test_cmdline_643  rM  z%RemoteProcessTestCase.test_cmdline_64c                 C   &   t | jj}| t ksJ d S r$   )r:   r   rD  r   r
  r8   getcwdr   r   r   r   test_cwd_328     z!RemoteProcessTestCase.test_cwd_32c                 C   rO  r$   )r:   r   rE  r   r
  r8   rP  r   r   r   r   test_cwd_64<  rR  z!RemoteProcessTestCase.test_cwd_64c                 C   s>   t | jj}| }d|v sJ |d tt ksJ d S )Nr;  )r:   r   rD  r   r9   r   r8   r   )r<   r   er   r   r   test_environ_32@  s   z%RemoteProcessTestCase.test_environ_32c                 C   s4   t | jj}z|  W d S  t jy   Y d S w r$   )r:   r   rE  r   r9   r   r   r   r   r   test_environ_64F  s   z%RemoteProcessTestCase.test_environ_64)r   r   r   r  staticmethodr:  rC  r?  rH  rL  rN  rQ  rS  rU  rV  __classcell__r   r   rF  r   r,    s    
r,  c                   @   s   e Zd Zdd Zdd ZdS )TestServicesc                 C   sX  h d}h d}h d}t  D ]}| dkrq| }t|d ts&J |d  s.J t|d ts7J t|d ts@J |d |v sHJ |d	 d urUt |d	  t|d
 ts^J t|d tsgJ t|d tspJ |d |v sxJ |d |v sJ t|d tsJ | }|d urt |}|	 sJ t 
| }||ksJ qd S )N>   stoppausestartpausedrunningcontinuestopped>   disabledZmanualZ	automatic>   Zstop_pendingZstart_pendingZpause_pendingr]  r`  r^  Zcontinue_pendingZWaaSMedicSvcr   display_namer   statusr   ZbinpathZ
start_typedescription)r:   win_service_iterr   as_dictr(  r   r+   r   r   
is_runningwin_service_get)r<   Zvalid_statusesZvalid_start_typesZservr3   r   r   sr   r   r   test_win_service_iterU  s6   		
z"TestServices.test_win_service_iterc              	   C   s  t jjj}t jjj}tt   }t	t j
}t |d  W d    n1 s*w   Y  |jj|d ks9J t |}tddd}||_tjd|d" t	t j
 |  W d    n1 sdw   Y  W d    n1 ssw   Y  tjd|d" t	t j
 |  W d    n1 sw   Y  W d    n1 sw   Y  tddd}||_tjd|d" t	t j |  W d    n1 sw   Y  W d    n1 sw   Y  tjd|d# t	t j |  W d    n	1 sw   Y  W d    n	1 sw   Y  | t|v s!J | t|v s,J | t|v s7J | t|v sBJ d S )Nz???r   msgz/psutil._psplatform.cext.winservice_query_statusr   z/psutil._psplatform.cext.winservice_query_config)r:   r  r   ERROR_SERVICE_DOES_NOT_EXISTERROR_ACCESS_DENIEDnextre  r   r   r   r   rh  r  r  r	  r   r   rc  r   r   r   rb  repr)r<   rl  rm  r   cmZservicer  r   r   r   test_win_service_get  sb   





z!TestServices.test_win_service_getN)r   r   r   rj  rq  r   r   r   r   rY  S  s    1rY  )6r  r   r   r1  r8   r   r   r   r   r2  r@  r  warningsZunittestr   r:   r   Zpsutil.testsr   r   r   r   r   r	   r
   r   r   r   r   r   catch_warningssimplefilterr?   r   r   rH   Zpsutil._pswindowsr   r  r   rW   rX   r   r    r7   r4   r5   rY   r   r   r  r  r,  rY  r   r   r   r   <module>   sr   


( =F LHW]