# Buggy code
def main():
    ad_pids = []
    procs = []
    for p in psutil.process_iter():
        with p.oneshot():
            try:
                mem = p.memory_full_info()
                info = p.as_dict(["cmdline", "username"])
            except psutil.AccessDenied:
                ad_pids.append(p.pid)
            except psutil.NoSuchProcess:
                pass
            else:
                p._uss = mem.uss
                p._rss = mem.rss
                if not p._uss:
                    continue
                p._pss = getattr(mem, "pss", "")
                p._swap = getattr(mem, "swap", "")
                p._info = info
                procs.append(p)

    procs.sort(key=lambda p: p._uss)
    templ = "%-7s %-7s %-30s %7s %7s %7s %7s"
    print(templ % ("PID", "User", "Cmdline", "USS", "PSS", "Swap", "RSS"))
    print("=" * 78)
    for p in procs[:86]:
        line = templ % (
            p.pid,
            p._info["username"][:7] if p._info["username"] else "",
            " ".join(p._info["cmdline"])[:30],
            convert_bytes(p._uss),
            convert_bytes(p._pss) if p._pss != "" else "",
            convert_bytes(p._swap) if p._swap != "" else "",
            convert_bytes(p._rss),
        )
        print(line)
    if ad_pids:
        print("warning: access denied for %s pids" % (len(ad_pids)), file=sys.stderr)


# Traceback (OSError)
Traceback (most recent call last):
File ".\teste.py", line 6, in <module>
for proc in psutil.process_iter():
File "C:\Users\user10\AppData\Roaming\Python\Python38\site-packages\psutil\__init__.py", line 1457, in process_iter
yield add(pid)
File "C:\Users\user10\AppData\Roaming\Python\Python38\site-packages\psutil\__init__.py", line 1432, in add
proc = Process(pid)
File "C:\Users\user10\AppData\Roaming\Python\Python38\site-packages\psutil\__init__.py", line 346, in __init__
self._init(pid)
File "C:\Users\user10\AppData\Roaming\Python\Python38\site-packages\psutil\__init__.py", line 373, in _init
self.create_time()
File "C:\Users\user10\AppData\Roaming\Python\Python38\site-packages\psutil\__init__.py", line 723, in create_time
self._create_time = self._proc.create_time()
File "C:\Users\user10\AppData\Roaming\Python\Python38\site-packages\psutil\_pswindows.py", line 681, in wrapper
raise convert_oserror(err, pid=self.pid, name=self._name)
File "C:\Users\user10\AppData\Roaming\Python\Python38\site-packages\psutil\_pswindows.py", line 671, in convert_oserr
or
raise exc
File "C:\Users\user10\AppData\Roaming\Python\Python38\site-packages\psutil\_pswindows.py", line 679, in wrapper
return fun(self, *args, **kwargs)
File "C:\Users\user10\AppData\Roaming\Python\Python38\site-packages\psutil\_pswindows.py", line 933, in create_time
user, system, created = cext.proc_times(self.pid)
OSError: [WinError 0] The operation completed successfully: '(originated from OpenProcess)'
}

# Fixed code
def main():
    ad_pids = []
    procs = []
    for p in psutil.process_iter():
        with p.oneshot():
            try:
                mem = p.memory_full_info()
                info = p.as_dict(["cmdline", "username"])
            except psutil.AccessDenied:
                ad_pids.append(p.pid)
            except psutil.NoSuchProcess:
                pass
            else:
                p._uss = mem.uss
                p._rss = mem.rss
                if not p._uss:
                    continue
                p._pss = getattr(mem, "pss", "")
                p._swap = getattr(mem, "swap", "")
                p._info = info
                procs.append(p)

    procs.sort(key=lambda p: p._uss)
    templ = "%-7s %-7s %7s %7s %7s %7s %7s"
    print(templ % ("PID", "User", "USS", "PSS", "Swap", "RSS", "Cmdline"))
    print("=" * 78)
    for p in procs[:86]:
        cmd = " ".join(p._info["cmdline"])[:50] if p._info["cmdline"] else ""
        line = templ % (
            p.pid,
            p._info["username"][:7] if p._info["username"] else "",
            convert_bytes(p._uss),
            convert_bytes(p._pss) if p._pss != "" else "",
            convert_bytes(p._swap) if p._swap != "" else "",
            convert_bytes(p._rss),
            cmd,
        )
        print(line)
    if ad_pids:
        print("warning: access denied for %s pids" % (len(ad_pids)), file=sys.stderr)