# Buggy code
def wrap_exceptions(fun):
    """Call callable into a try/except clause and translate ENOENT,
    EACCES and EPERM in NoSuchProcess or AccessDenied exceptions.
    """

    def wrapper(self, *args, **kwargs):
        try:
            return fun(self, *args, **kwargs)
        except EnvironmentError as err:
            if self.pid == 0:
                if 0 in pids():
                    raise AccessDenied(self.pid, self._name)
                else:
                    raise
            # ENOENT (no such file or directory) gets raised on open().
            # ESRCH (no such process) can get raised on read() if
            # process is gone in meantime.
            if err.errno in (errno.ENOENT, errno.ESRCH):
                if not pid_exists(self.pid):
                    raise NoSuchProcess(self.pid, self._name)
                else:
                    raise ZombieProcess(self.pid, self._name, self._ppid)
            if err.errno in (errno.EPERM, errno.EACCES):
                raise AccessDenied(self.pid, self._name)
            raise

    return wrapper


# Traceback (AttributeError)
Traceback (most recent call last):
File "/opt/freeware/lib64/python3.7/site-packages/psutil/__init__.py", line 593, in oneshot
self._proc.oneshot_enter()
File "/opt/freeware/lib64/python3.7/site-packages/psutil/_psaix.py", line 369, in oneshot_enter
self._proc_name_and_args.cache_activate(self)
AttributeError: 'function' object has no attribute 'cache_activate'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/freeware/lib64/python3.7/site-packages/psutil/__init__.py", line 1562, in process_iter
yield add(pid)
File "/opt/freeware/lib64/python3.7/site-packages/psutil/__init__.py", line 1539, in add
proc.info = proc.as_dict(attrs=attrs, ad_value=ad_value)
File "/opt/freeware/lib64/python3.7/site-packages/psutil/__init__.py", line 627, in as_dict
with self.oneshot():
File "/opt/freeware/lib64/python3.7/contextlib.py", line 112, in __enter__
return next(self.gen)
File "/opt/freeware/lib64/python3.7/site-packages/psutil/__init__.py", line 601, in oneshot
self._proc.oneshot_exit()
File "/opt/freeware/lib64/python3.7/site-packages/psutil/_psaix.py", line 374, in oneshot_exit
self._proc_name_and_args.cache_deactivate(self)
AttributeError: 'function' object has no attribute 'cache_deactivate'

# Fixed code
def wrap_exceptions(fun):
    """Call callable into a try/except clause and translate ENOENT,
    EACCES and EPERM in NoSuchProcess or AccessDenied exceptions.
    """

    @functools.wraps(fun)
    def wrapper(self, *args, **kwargs):
        try:
            return fun(self, *args, **kwargs)
        except EnvironmentError as err:
            if self.pid == 0:
                if 0 in pids():
                    raise AccessDenied(self.pid, self._name)
                else:
                    raise
            # ENOENT (no such file or directory) gets raised on open().
            # ESRCH (no such process) can get raised on read() if
            # process is gone in meantime.
            if err.errno in (errno.ENOENT, errno.ESRCH):
                if not pid_exists(self.pid):
                    raise NoSuchProcess(self.pid, self._name)
                else:
                    raise ZombieProcess(self.pid, self._name, self._ppid)
            if err.errno in (errno.EPERM, errno.EACCES):
                raise AccessDenied(self.pid, self._name)
            raise

    return wrapper