# 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,
            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 (WindowsError)
C:\windows\system32>glances
Traceback (most recent call last):
File "c:\python27\lib\runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "c:\python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Python27\Scripts\glances.exe\__main__.py", line 9, in <module>
File "c:\python27\lib\site-packages\glances\__init__.py", line 238, in main
start_webserver(config=config, args=args)
File "c:\python27\lib\site-packages\glances\__init__.py", line 193, in start_w
ebserver
webserver = GlancesWebServer(config=config, args=args)
File "c:\python27\lib\site-packages\glances\webserver.py", line 41, in __init_
_
self.stats.update()
File "c:\python27\lib\site-packages\glances\stats.py", line 164, in update
self._plugins[p].update()
File "c:\python27\lib\site-packages\glances\plugins\glances_processcount.py",
line 57, in update
glances_processes.update()
File "c:\python27\lib\site-packages\glances\processes.py", line 427, in update

standard_stats=self.max_processes is None)
File "c:\python27\lib\site-packages\glances\processes.py", line 386, in __get_
process_stats
procstat = self.__get_mandatory_stats(proc, procstat)
File "c:\python27\lib\site-packages\glances\processes.py", line 271, in __get_
mandatory_stats
self.cmdline_cache[procstat['pid']] = proc.cmdline()
File "c:\python27\lib\site-packages\psutil\__init__.py", line 701, in cmdline
return self._proc.cmdline()
File "c:\python27\lib\site-packages\psutil\_pswindows.py", line 611, in wrappe
r
return fun(self, *args, **kwargs)
File "c:\python27\lib\site-packages\psutil\_pswindows.py", line 681, in cmdlin
e
ret = cext.proc_cmdline(self.pid)
WindowsError: [Error 18] There are no more files

# 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:
        # 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,
            UnicodeDecodeError,
            psutil.AccessDenied,
            psutil.NoSuchProcess,
            psutil.WindowsError,
        ):
            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