$65 GRAYBYTE WORDPRESS FILE MANAGER $39

SERVER : premium201.web-hosting.com #1 SMP Wed Mar 26 12:08:09 UTC 2025
SERVER IP : 172.67.162.162 | ADMIN IP 216.73.216.174
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/opt/hc_python/lib/python3.12/site-packages/psutil/tests/

HOME
Current File : /opt/hc_python/lib/python3.12/site-packages/psutil/tests//test_sudo.py
#!/usr/bin/env python3

# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests which are meant to be run as root.

NOTE: keep this module compatible with unittest: we want to run this
file with the unittest runner, since pytest may not be installed for
the root user.
"""

import datetime
import time
import unittest

import psutil
from psutil import FREEBSD
from psutil import LINUX
from psutil import OPENBSD
from psutil import WINDOWS
from psutil.tests import CI_TESTING
from psutil.tests import PsutilTestCase


def get_systime():
    if hasattr(time, "clock_gettime") and hasattr(time, "CLOCK_REALTIME"):
        return time.clock_gettime(time.CLOCK_REALTIME)
    return time.time()


def set_systime(secs):  # secs since the epoch
    if hasattr(time, "clock_settime") and hasattr(time, "CLOCK_REALTIME"):
        try:
            time.clock_settime(time.CLOCK_REALTIME, secs)
        except PermissionError:
            raise unittest.SkipTest("needs root")
    elif WINDOWS:
        import pywintypes
        import win32api

        dt = datetime.datetime.fromtimestamp(secs, datetime.timezone.utc)
        try:
            win32api.SetSystemTime(
                dt.year,
                dt.month,
                dt.isoweekday() % 7,
                dt.day,
                dt.hour,
                dt.minute,
                dt.second,
                int(dt.microsecond / 1000),
            )
        except pywintypes.error as err:
            if err.winerror == 1314:
                raise unittest.SkipTest("needs Administrator user")
            raise
    else:
        raise unittest.SkipTest("setting systime not supported")


class TestUpdatedSystemTime(PsutilTestCase):
    """Tests which update the system clock."""

    def setUp(self):
        self.time_updated = False
        self.orig_time = get_systime()
        self.time_started = time.monotonic()

    def tearDown(self):
        if self.time_updated:
            extra_t = time.monotonic() - self.time_started
            set_systime(self.orig_time + extra_t)

    def update_systime(self):
        # set system time 1 hour later
        set_systime(self.orig_time + 3600)
        self.time_updated = True

    def test_boot_time(self):
        # Test that boot_time() reflects system clock updates.
        t1 = psutil.boot_time()
        self.update_systime()
        t2 = psutil.boot_time()
        self.assertGreater(t2, t1)
        diff = int(t2 - t1)
        self.assertAlmostEqual(diff, 3600, delta=1)

    @unittest.skipIf(WINDOWS, "broken on WINDOWS")  # TODO: fix it
    def test_proc_create_time(self):
        # Test that Process.create_time() reflects system clock
        # updates. On systems such as Linux this is added on top of the
        # process monotonic time returned by the kernel.
        t1 = psutil.Process().create_time()
        self.update_systime()
        t2 = psutil.Process().create_time()
        diff = int(t2 - t1)
        self.assertAlmostEqual(diff, 3600, delta=1)

    @unittest.skipIf(CI_TESTING, "skipped on CI for now")  # TODO: fix it
    @unittest.skipIf(OPENBSD, "broken on OPENBSD")  # TODO: fix it
    @unittest.skipIf(FREEBSD, "broken on FREEBSD")  # TODO: fix it
    def test_proc_ident(self):
        p1 = psutil.Process()
        self.update_systime()
        p2 = psutil.Process()
        self.assertEqual(p1._get_ident(), p2._get_ident())
        self.assertEqual(p1, p2)

    @unittest.skipIf(not LINUX, "LINUX only")
    def test_linux_monotonic_proc_time(self):
        t1 = psutil.Process()._proc.create_time(monotonic=True)
        self.update_systime()
        time.sleep(0.05)
        t2 = psutil.Process()._proc.create_time(monotonic=True)
        self.assertEqual(t1, t2)


Current_dir [ NOT WRITEABLE ] Document_root [ WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
3 Dec 2025 9.30 AM
root / root
0755
__pycache__
--
3 Dec 2025 9.30 AM
root / root
0755
__init__.py
61.429 KB
3 Dec 2025 9.30 AM
root / root
0644
__main__.py
0.298 KB
3 Dec 2025 9.30 AM
root / root
0644
test_aix.py
4.305 KB
3 Dec 2025 9.30 AM
root / root
0644
test_bsd.py
19.528 KB
3 Dec 2025 9.30 AM
root / root
0644
test_connections.py
20.742 KB
3 Dec 2025 9.30 AM
root / root
0644
test_contracts.py
11.897 KB
3 Dec 2025 9.30 AM
root / root
0644
test_linux.py
86.486 KB
3 Dec 2025 9.30 AM
root / root
0644
test_memleaks.py
14.723 KB
3 Dec 2025 9.30 AM
root / root
0644
test_misc.py
28.725 KB
3 Dec 2025 9.30 AM
root / root
0644
test_osx.py
6.512 KB
3 Dec 2025 9.30 AM
root / root
0644
test_posix.py
16.983 KB
3 Dec 2025 9.30 AM
root / root
0644
test_process.py
58.705 KB
3 Dec 2025 9.30 AM
root / root
0644
test_process_all.py
18.199 KB
3 Dec 2025 9.30 AM
root / root
0644
test_scripts.py
7.692 KB
3 Dec 2025 9.30 AM
root / root
0644
test_sudo.py
3.747 KB
3 Dec 2025 9.30 AM
root / root
0644
test_sunos.py
1.162 KB
3 Dec 2025 9.30 AM
root / root
0644
test_system.py
34.7 KB
3 Dec 2025 9.30 AM
root / root
0644
test_testutils.py
18.348 KB
3 Dec 2025 9.30 AM
root / root
0644
test_unicode.py
10.472 KB
3 Dec 2025 9.30 AM
root / root
0644
test_windows.py
33.614 KB
3 Dec 2025 9.30 AM
root / root
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2025 CONTACT ME
Static GIF