# Buggy code
def __get_mandatory_stats(self, proc, procstat):
    """
    Get mandatory_stats: for all processes.
    Needed for the sorting/filter step.

    Stats grabbed inside this method:
    * 'name', 'cpu_times', 'status', 'ppid'
    * 'username', 'cpu_percent', 'memory_percent'
    """
    procstat["mandatory_stats"] = True

    # Name, cpu_times, status and ppid stats are in the same /proc file
    # Optimisation fir issue #958
    try:
        procstat.update(
            proc.as_dict(attrs=["name", "cpu_times", "status", "ppid"], ad_value="")
        )
    except psutil.NoSuchProcess:
        # Try/catch for issue #432 (process no longer exist)
        return None
    else:
        procstat["status"] = str(procstat["status"])[:1].upper()

    try:
        procstat.update(
            proc.as_dict(
                attrs=["username", "cpu_percent", "memory_percent"], ad_value=""
            )
        )
    except psutil.NoSuchProcess:
        # Try/catch for issue #432 (process no longer exist)
        return None

    if procstat["cpu_percent"] == "" or procstat["memory_percent"] == "":
        # Do not display process if we cannot get the basic
        # cpu_percent or memory_percent stats
        return None

    # Compute the maximum value for cpu_percent and memory_percent
    for k in self._max_values_list:
        if procstat[k] > self.get_max_values(k):
            self.set_max_values(k, procstat[k])

    # Process command line (cached with internal cache)
    if procstat["pid"] not in self.cmdline_cache:
        # Patch for issue #391
        try:
            self.cmdline_cache[procstat["pid"]] = proc.cmdline()
        except (
            AttributeError,
            EnvironmentError,
            UnicodeDecodeError,
            psutil.AccessDenied,
            psutil.NoSuchProcess,
        ):
            self.cmdline_cache[procstat["pid"]] = ""
    procstat["cmdline"] = self.cmdline_cache[procstat["pid"]]

    # Process IO
    # procstat['io_counters'] is a list:
    # [read_bytes, write_bytes, read_bytes_old, write_bytes_old, io_tag]
    # If io_tag = 0 > Access denied (display "?")
    # If io_tag = 1 > No access denied (display the IO rate)
    # Availability: all platforms except macOS and Illumos/Solaris
    try:
        # Get the process IO counters
        proc_io = proc.io_counters()
        io_new = [proc_io.read_bytes, proc_io.write_bytes]
    except (psutil.AccessDenied, psutil.NoSuchProcess, NotImplementedError):
        # Access denied to process IO (no root account)
        # NoSuchProcess (process die between first and second grab)
        # Put 0 in all values (for sort) and io_tag = 0 (for display)
        procstat["io_counters"] = [0, 0] + [0, 0]
        io_tag = 0
    except AttributeError:
        return procstat
    else:
        # For IO rate computation
        # Append saved IO r/w bytes
        try:
            procstat["io_counters"] = io_new + self.io_old[procstat["pid"]]
        except KeyError:
            procstat["io_counters"] = io_new + [0, 0]
        # then save the IO r/w bytes
        self.io_old[procstat["pid"]] = io_new
        io_tag = 1

    # Append the IO tag (for display)
    procstat["io_counters"] += [io_tag]

    return procstat


# Traceback (PermissionError)
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/bin/glances", line 11, in <module>
sys.exit(main())
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/glances/__init__.py", line 134, in main
start(config=config, args=args)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/glances/__init__.py", line 103, in start
mode.serve_forever()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/glances/standalone.py", line 117, in serve_forever
self.schedule.run()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/sched.py", line 147, in run
action(*argument, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/glances/standalone.py", line 100, in __serve_forever
self.stats.update()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/glances/stats.py", line 192, in update
self._plugins[p].update()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/glances/plugins/glances_processcount.py", line 57, in update
glances_processes.update()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/glances/processes.py", line 428, in update
standard_stats=self.max_processes is None)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/glances/processes.py", line 387, in __get_process_stats
procstat = self.__get_mandatory_stats(proc, procstat)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/glances/processes.py", line 242, in __get_mandatory_stats
ad_value=''))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/psutil/__init__.py", line 563, in as_dict
ret = meth()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/psutil/_common.py", line 318, in wrapper
ret = cache[fun] = fun(self)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/psutil/__init__.py", line 1097, in cpu_times
return self._proc.cpu_times()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/psutil/_psosx.py", line 293, in wrapper
return fun(self, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/psutil/_psosx.py", line 416, in cpu_times
rawtuple = self._get_pidtaskinfo()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/psutil/_common.py", line 318, in wrapper
ret = cache[fun] = fun(self)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/psutil/_psosx.py", line 331, in _get_pidtaskinfo
ret = cext.proc_pidtaskinfo_oneshot(self.pid)
PermissionError: [Errno 1] Operation not permitted

# Fixed code
def __get_mandatory_stats(self, proc, procstat):
    """
    Get mandatory_stats: for all processes.
    Needed for the sorting/filter step.

    Stats grabbed inside this method:
    * 'name', 'cpu_times', 'status', 'ppid'
    * 'username', 'cpu_percent', 'memory_percent'
    """
    procstat["mandatory_stats"] = True

    # Name, cpu_times, status and ppid stats are in the same /proc file
    # Optimisation fir issue #958
    try:
        procstat.update(
            proc.as_dict(attrs=["name", "cpu_times", "status", "ppid"], ad_value="")
        )
    except (psutil.NoSuchProcess, psutil.AccessDenied):
        # Try/catch for issue #432 (process no longer exist)
        # Try/catch for issue #1120 (only see on Macos)
        return None
    else:
        procstat["status"] = str(procstat["status"])[:1].upper()

    try:
        procstat.update(
            proc.as_dict(
                attrs=["username", "cpu_percent", "memory_percent"], ad_value=""
            )
        )
    except (psutil.NoSuchProcess, psutil.AccessDenied):
        # Try/catch for issue #432 (process no longer exist)
        return None

    if procstat["cpu_percent"] == "" or procstat["memory_percent"] == "":
        # Do not display process if we cannot get the basic
        # cpu_percent or memory_percent stats
        return None

    # Compute the maximum value for cpu_percent and memory_percent
    for k in self._max_values_list:
        if procstat[k] > self.get_max_values(k):
            self.set_max_values(k, procstat[k])

    # Process command line (cached with internal cache)
    if procstat["pid"] not in self.cmdline_cache:
        # Patch for issue #391
        try:
            self.cmdline_cache[procstat["pid"]] = proc.cmdline()
        except (
            AttributeError,
            EnvironmentError,
            UnicodeDecodeError,
            psutil.AccessDenied,
            psutil.NoSuchProcess,
        ):
            self.cmdline_cache[procstat["pid"]] = ""
    procstat["cmdline"] = self.cmdline_cache[procstat["pid"]]

    # Process IO
    # procstat['io_counters'] is a list:
    # [read_bytes, write_bytes, read_bytes_old, write_bytes_old, io_tag]
    # If io_tag = 0 > Access denied (display "?")
    # If io_tag = 1 > No access denied (display the IO rate)
    # Availability: all platforms except macOS and Illumos/Solaris
    try:
        # Get the process IO counters
        proc_io = proc.io_counters()
        io_new = [proc_io.read_bytes, proc_io.write_bytes]
    except (psutil.AccessDenied, psutil.NoSuchProcess, NotImplementedError):
        # Access denied to process IO (no root account)
        # NoSuchProcess (process die between first and second grab)
        # Put 0 in all values (for sort) and io_tag = 0 (for display)
        procstat["io_counters"] = [0, 0] + [0, 0]
        io_tag = 0
    except AttributeError:
        return procstat
    else:
        # For IO rate computation
        # Append saved IO r/w bytes
        try:
            procstat["io_counters"] = io_new + self.io_old[procstat["pid"]]
        except KeyError:
            procstat["io_counters"] = io_new + [0, 0]
        # then save the IO r/w bytes
        self.io_old[procstat["pid"]] = io_new
        io_tag = 1

    # Append the IO tag (for display)
    procstat["io_counters"] += [io_tag]

    return procstat