U =_9@sdZddlmZddlZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddlm Z dd lm Z dd l mZdd l mZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$dd lm%Z%ej&j'Z'e(Z)d!Z*d"d#Z+Gd$d%d%e$Z,Gd&d'd'e,Z-e%.e d(Gd)d*d*e$Z/Gd+d,d,e$Z0e1d-krdd.l2m3Z3e3e4dS)/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)create_sockets) get_testfn)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)process_namespace)skip_on_access_denied)spawn_testproc)system_namespace) terminate)TestMemoryLeak)unittestcCs dd}|S)zsDecorator for those Linux functions which are implemented in pure Python, and which we want to run faster. cstfdd}|S)NcsJtr6|jj}zt|j_|f||WS||j_Xn|f||SdSN)r __class__times FEW_TIMES)selfargskwargsZbeforefunM/opt/alt/python38/lib64/python3.8/site-packages/psutil/tests/test_memleaks.pywrapperAs z5fewtimes_if_linux..decorator..wrapper) functoolswraps)r)r,r*r(r+ decorator@s z$fewtimes_if_linux..decoratorr*)r/r*r*r+fewtimes_if_linux<s r0c@seZdZdZeZddZeddZeddZ edd Z ed d Z e e d ed dZe e d eddZeddZddZddZe e dddZe e dddZe e deddZe eddd Zed!d"Zeeed#d$d%Ze e d&d'd(Ze e d ed)d*Z ed+d,Z!eeed#d-d.Z"ed/d0Z#ee e$ dd1d2Z%ed3d4Z&ed5d6Z'e e d ed7d8Z(d9d:Z)ed;d<Z*e e+ dd=d>Z,e e+ dd?d@Z-edAdBZ.e e/ dedCdDZ0e e1 dEe e2 ddFdGZ3e e1 dEe e2 ddHdIZ4ee edJdKdLZ5e e6 ddMdNZ7e e d&dOdPZ8dQS)RTestProcessObjectLeaksz$Test leaks of Process class methods.cCs td}|||j|jdSr!)rtest_class_coverageZgettersZsettersr%nsr*r*r+ test_coverageZsz$TestProcessObjectLeaks.test_coveragecCs||jjdSr!)executeprocnamer%r*r*r+ test_name^sz TestProcessObjectLeaks.test_namecCs||jjdSr!)r6r7Zcmdliner9r*r*r+ test_cmdlinebsz#TestProcessObjectLeaks.test_cmdlinecCs||jjdSr!)r6r7Zexer9r*r*r+test_exefszTestProcessObjectLeaks.test_execCs||jjdSr!)r6r7Zppidr9r*r*r+ test_ppidjsz TestProcessObjectLeaks.test_ppidz POSIX onlycCs||jjdSr!)r6r7Zuidsr9r*r*r+ test_uidsnsz TestProcessObjectLeaks.test_uidscCs||jjdSr!)r6r7Zgidsr9r*r*r+ test_gidsssz TestProcessObjectLeaks.test_gidscCs||jjdSr!)r6r7Zstatusr9r*r*r+ test_statusxsz"TestProcessObjectLeaks.test_statuscCs||jjdSr!)r6r7nicer9r*r*r+ test_nice|sz TestProcessObjectLeaks.test_nicecs tfdddS)Ncs jSr!)r7rAr*Znicenessr%r*r+z6TestProcessObjectLeaks.test_nice_set..)thisprocrAr6r9r*rCr+ test_nice_setsz$TestProcessObjectLeaks.test_nice_set not supportedcCs||jjdSr!)r6r7ionicer9r*r*r+ test_ionicesz"TestProcessObjectLeaks.test_ionicecsZtr"tfddn4fddttjt dd} t |dS)Ncs jSr!)r7rIr*r%valuer*r+rDrEz8TestProcessObjectLeaks.test_ionice_set..csjtjSr!)r7rIpsutilZIOPRIO_CLASS_NONEr*r9r*r+rDrEr) rrFrIr6r-partialcextZproc_ioprio_setosgetpid execute_w_excOSErrorr%r)r*rKr+test_ionice_sets z&TestProcessObjectLeaks.test_ionice_setcCs||jjdSr!)r6r7Z io_countersr9r*r*r+test_io_counterssz'TestProcessObjectLeaks.test_io_counterszworthless on POSIXcCst||jjdSr!)rMProcessZusernamer6r7r9r*r*r+ test_usernames z$TestProcessObjectLeaks.test_usernamecCs||jjdSr!)r6r7Z create_timer9r*r*r+test_create_timesz'TestProcessObjectLeaks.test_create_time)Zonly_ifcCs||jjdSr!)r6r7Z num_threadsr9r*r*r+test_num_threadssz'TestProcessObjectLeaks.test_num_threads WINDOWS onlycCs||jjdSr!)r6r7Z num_handlesr9r*r*r+test_num_handlessz'TestProcessObjectLeaks.test_num_handlescCs||jjdSr!)r6r7Znum_fdsr9r*r*r+ test_num_fdssz#TestProcessObjectLeaks.test_num_fdscCs||jjdSr!)r6r7Znum_ctx_switchesr9r*r*r+test_num_ctx_switchessz,TestProcessObjectLeaks.test_num_ctx_switchescCs||jjdSr!)r6r7Zthreadsr9r*r*r+ test_threadssz#TestProcessObjectLeaks.test_threadscCs||jjdSr!)r6r7 cpu_timesr9r*r*r+test_cpu_timessz%TestProcessObjectLeaks.test_cpu_timescCs||jjdSr!)r6r7Zcpu_numr9r*r*r+ test_cpu_numsz#TestProcessObjectLeaks.test_cpu_numcCs||jjdSr!)r6r7Z memory_infor9r*r*r+test_memory_infosz'TestProcessObjectLeaks.test_memory_infocCs||jjdSr!)r6r7Zmemory_full_infor9r*r*r+test_memory_full_infosz,TestProcessObjectLeaks.test_memory_full_infocCs||jjdSr!)r6r7Zterminalr9r*r*r+ test_terminalsz$TestProcessObjectLeaks.test_terminalcCs$trtn|j}|j|jj|ddS)Nr#)rr$r#r6r7resumer%r#r*r*r+ test_resumesz"TestProcessObjectLeaks.test_resumecCs||jjdSr!)r6r7cwdr9r*r*r+test_cwdszTestProcessObjectLeaks.test_cwdcCs||jjdSr!)r6r7 cpu_affinityr9r*r*r+test_cpu_affinitysz(TestProcessObjectLeaks.test_cpu_affinitycs4tfddtfdddS)Ncs jSr!r7rmr*Zaffinityr%r*r+rDrEz>TestProcessObjectLeaks.test_cpu_affinity_set..csjdgSNrNror*r9r*r+rDrE)rFrmr6rS ValueErrorr9r*rpr+test_cpu_affinity_sets  z,TestProcessObjectLeaks.test_cpu_affinity_setc Cs*ttd||jjW5QRXdS)Nw)openr r6r7Z open_filesr9r*r*r+test_open_filessz&TestProcessObjectLeaks.test_open_filescCs||jjdSr!)r6r7Z memory_mapsr9r*r*r+test_memory_mapssz'TestProcessObjectLeaks.test_memory_mapsz LINUX onlycsfdddS)NcsjtjSr!r7rlimitrM RLIMIT_NOFILEr*r9r*r+rDrEz4TestProcessObjectLeaks.test_rlimit..r6r9r*r9r+ test_rlimitsz"TestProcessObjectLeaks.test_rlimitcs<ttjfddttffdddS)NcsjtjSr!rxr*limitr%r*r+rDrEz8TestProcessObjectLeaks.test_rlimit_set..cs jdSrq)r7ryr*r9r*r+rDrE)rFryrMrzr6rSrTrrr9r*r}r+test_rlimit_sets z&TestProcessObjectLeaks.test_rlimit_setzworthless on WINDOWSc s6t&trdndfddW5QRXdS)NZinetallcs jSr!)r7Z connectionsr*kindr%r*r+rDrEz9TestProcessObjectLeaks.test_connections..)r rr6r9r*rr+test_connectionss z'TestProcessObjectLeaks.test_connectionscCs||jjdSr!)r6r7environr9r*r*r+ test_environsz#TestProcessObjectLeaks.test_environcCs|dddS)NcSsttSr!)rP proc_inforQrRr*r*r*r+rDrEz7TestProcessObjectLeaks.test_proc_info..r{r9r*r*r+test_proc_infosz%TestProcessObjectLeaks.test_proc_infoN)9__name__ __module__ __qualname____doc__rFr7r5r0r:r;r<r=rskipIfrr>r?r@rBrGrrJrVrrWrYrZrrr[rr]r^r_r`rbrrcrdrerfrjrlr rnrsrvrrwrrr|rrrrrr*r*r*r+r1Us                                                  r1cspeZdZdZefddZefddZddZerhdd Z d d Z d d Z ddZ ddZ ddZZS)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. cs:tt|_t|jj|_|j|j dSr!) r setUpClassrsubprMrXpidr7killwaitclsr"r*r+rs   z%TestTerminatedProcessLeaks.setUpClasscstt|jdSr!)r tearDownClassrrrrr*r+rs z(TestTerminatedProcessLeaks.tearDownClasscCs&z |Wntjk r YnXdSr!)rMZ NoSuchProcessrUr*r*r+calls zTestTerminatedProcessLeaks.callcCs||jjdSr!)r6r7rr9r*r*r+ test_kill'sz$TestTerminatedProcessLeaks.test_killcCs||jjdSr!)r6r7rr9r*r*r+test_terminate*sz)TestTerminatedProcessLeaks.test_terminatecCs||jjdSr!)r6r7Zsuspendr9r*r*r+ test_suspend-sz'TestTerminatedProcessLeaks.test_suspendcCs||jjdSr!)r6r7rhr9r*r*r+rj0sz&TestTerminatedProcessLeaks.test_resumecCs||jjdSr!)r6r7rr9r*r*r+ test_wait3sz$TestTerminatedProcessLeaks.test_waitcsfdd}|dS)Ncs*ztjjWStk r$YnXdSr!)rPrr7rr r*r9r*r+r8sz7TestTerminatedProcessLeaks.test_proc_info..callr{)r%rr*r9r+r6s z)TestTerminatedProcessLeaks.test_proc_info)rrrr classmethodrrrrrrrrjrr __classcell__r*r*rr+r srr\c@seZdZddZddZdS)TestProcessDualImplementationcCs|dddS)NcSstjtddS)NTZuse_pebrPZ proc_cmdlinerQrRr*r*r*r+rDErEzETestProcessDualImplementation.test_cmdline_peb_true..r{r9r*r*r+test_cmdline_peb_trueDsz3TestProcessDualImplementation.test_cmdline_peb_truecCs|dddS)NcSstjtddS)NFrrr*r*r*r+rDHrEzFTestProcessDualImplementation.test_cmdline_peb_false..r{r9r*r*r+test_cmdline_peb_falseGsz4TestProcessDualImplementation.test_cmdline_peb_falseN)rrrrrr*r*r*r+rAsrc@seZdZdZddZeddZeddZedd Zed d Z ed d Z ee e dddZe e dddZddZe edddZddZddZddZe eoejd d ed!d"Zed#d$Zee e dd%d&Zee eo*e d'kd(d)d*Z!d+d,Z"d-d.Z#ee e$ dd/d0Z%ee e& dd1d2Z'ee e( dd3d4Z)ed5d6Z*d7d8Z+erd9d:Z,d;d<Z-d=d>Z.d?d@Z/dAdBZ0dCS)DTestModuleFunctionsLeaksz&Test leaks of psutil module functions.cCst}|||jdSr!)rr2rr3r*r*r+r5Ssz&TestModuleFunctionsLeaks.test_coveragecCs|dddS)NcSs tjddS)NTZlogicalrM cpu_countr*r*r*r+rD[rEz9TestModuleFunctionsLeaks.test_cpu_count..r{r9r*r*r+test_cpu_countYsz'TestModuleFunctionsLeaks.test_cpu_countcCs|dddS)NcSs tjddS)NFrrr*r*r*r+rD_rEzBTestModuleFunctionsLeaks.test_cpu_count_physical..r{r9r*r*r+test_cpu_count_physical]sz0TestModuleFunctionsLeaks.test_cpu_count_physicalcCs|tjdSr!)r6rMrar9r*r*r+rbasz'TestModuleFunctionsLeaks.test_cpu_timescCs|dddS)NcSs tjddS)NT)Zpercpu)rMrar*r*r*r+rDgrEz=TestModuleFunctionsLeaks.test_per_cpu_times..r{r9r*r*r+test_per_cpu_timesesz+TestModuleFunctionsLeaks.test_per_cpu_timescCs|tjdSr!)r6rMZ cpu_statsr9r*r*r+test_cpu_statsisz'TestModuleFunctionsLeaks.test_cpu_statsrHcCs|tjdSr!)r6rMZcpu_freqr9r*r*r+ test_cpu_freqmsz&TestModuleFunctionsLeaks.test_cpu_freqr\cCst|tjdSr!)rM getloadavgr6r9r*r*r+test_getloadavgrsz(TestModuleFunctionsLeaks.test_getloadavgcCs|tjdSr!)r6rMZvirtual_memoryr9r*r*r+test_virtual_memoryysz,TestModuleFunctionsLeaks.test_virtual_memoryz&worthless on SUNOS (uses a subprocess)cCs|tjdSr!)r6rMZ swap_memoryr9r*r*r+test_swap_memory}sz)TestModuleFunctionsLeaks.test_swap_memorycCs$trtn|j}|jdd|ddS)NcSsttSr!)rMZ pid_existsrQrRr*r*r*r+rDrEz:TestModuleFunctionsLeaks.test_pid_exists..rgrr$r#r6rir*r*r+test_pid_existssz(TestModuleFunctionsLeaks.test_pid_existscCs$trtn|j}|jdd|ddS)NcSs tdS)N.)rMZ disk_usager*r*r*r+rDrEz:TestModuleFunctionsLeaks.test_disk_usage..rgrrir*r*r+test_disk_usagesz(TestModuleFunctionsLeaks.test_disk_usagecCs|tjdSr!)r6rMZdisk_partitionsr9r*r*r+test_disk_partitionssz-TestModuleFunctionsLeaks.test_disk_partitionsz/proc/diskstatsz3/proc/diskstats not available on this Linux versioncCs|dddS)NcSs tjddSNF)Znowrap)rMZdisk_io_countersr*r*r*r+rDrEz@TestModuleFunctionsLeaks.test_disk_io_counters..r{r9r*r*r+test_disk_io_counterssz.TestModuleFunctionsLeaks.test_disk_io_counterscCs|tjdSr!)r6rMZpidsr9r*r*r+ test_pidssz"TestModuleFunctionsLeaks.test_pidscCs|dddS)NcSs tjddSr)rMZnet_io_countersr*r*r*r+rDrEz?TestModuleFunctionsLeaks.test_net_io_counters..r{r9r*r*r+test_net_io_counterssz-TestModuleFunctionsLeaks.test_net_io_countersrzneed root accessc Cs0tjddt|ddW5QRXdS)NrrcSs tjddS)Nrr)rMnet_connectionsr*r*r*r+rDrEz?TestModuleFunctionsLeaks.test_net_connections..)rMrr r6r9r*r*r+test_net_connectionss z-TestModuleFunctionsLeaks.test_net_connectionscCs"trdn|j}|jtj|ddS)Ni@) tolerance)rrr6rMZ net_if_addrs)r%rr*r*r+test_net_if_addrssz*TestModuleFunctionsLeaks.test_net_if_addrscCs|tjdSr!)r6rMZ net_if_statsr9r*r*r+test_net_if_statssz*TestModuleFunctionsLeaks.test_net_if_statscCs|tjdSr!)r6rMZsensors_batteryr9r*r*r+test_sensors_batterysz-TestModuleFunctionsLeaks.test_sensors_batterycCs|tjdSr!)r6rMZsensors_temperaturesr9r*r*r+test_sensors_temperaturessz2TestModuleFunctionsLeaks.test_sensors_temperaturescCs|tjdSr!)r6rMZ sensors_fansr9r*r*r+test_sensors_fanssz*TestModuleFunctionsLeaks.test_sensors_fanscCs|tjdSr!)r6rMZ boot_timer9r*r*r+test_boot_timesz'TestModuleFunctionsLeaks.test_boot_timecCs|tjdSr!)r6rMZusersr9r*r*r+ test_userssz#TestModuleFunctionsLeaks.test_userscCs|tjdSr!)r6rPZwinservice_enumerater9r*r*r+test_win_service_itersz.TestModuleFunctionsLeaks.test_win_service_itercCsdSr!r*r9r*r*r+test_win_service_getsz-TestModuleFunctionsLeaks.test_win_service_getcs&tt|fdddS)Ncs tSr!)rPZwinservice_query_configr*r8r*r+rDrEzFTestModuleFunctionsLeaks.test_win_service_get_config..nextrMZwin_service_iterr8r6r9r*rr+test_win_service_get_configsz4TestModuleFunctionsLeaks.test_win_service_get_configcs&tt|fdddS)Ncs tSr!)rPZwinservice_query_statusr*rr*r+rDrEzFTestModuleFunctionsLeaks.test_win_service_get_status..rr9r*rr+test_win_service_get_statussz4TestModuleFunctionsLeaks.test_win_service_get_statuscs&tt|fdddS)Ncs tSr!)rPZwinservice_query_descrr*rr*r+rDrEzKTestModuleFunctionsLeaks.test_win_service_get_description..rr9r*rr+ test_win_service_get_descriptionsz9TestModuleFunctionsLeaks.test_win_service_get_descriptionN)1rrrrr5r0rrrbrrrrrrrrrrrrrrrrQpathexistsrrrrrgetuidrrrrrrrrrrrrrrrrr*r*r*r+rPsp                       r__main__) run_from_name)5rZ __future__rr-rQrMZpsutil._commonrrrrrrZpsutil._compatr r Z psutil.testsr r r rrrrrrrrrrrrrrrrrrZ _psplatformrPrXrFr$r0r1rrrrrZpsutil.tests.runnerr__file__r*r*r*r+s`                              76