Skip to content

Commit

Permalink
Further cleaned up the exception handling here to make sense on both
Browse files Browse the repository at this point in the history
windows and non-windows, Python 2 and 3.

On Python 3 EnvironmentError is an alias for OSError.  On Python 2
WindowsError is a subclass of EnvironmentError.  So this will correctly
check the windows error, if it exists, in all cases.

On Travis we also need to catch ENXIO on Linux, and for some reason
EOPNOTSUPP on OSX.
  • Loading branch information
Erik M. Bray committed Sep 29, 2016
1 parent 3898f5b commit 7d0a323
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions psutil/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,16 +396,19 @@ def wait_for_file(fname, timeout=GLOBAL_TIMEOUT, empty=False,
if delete_file:
os.remove(fname)
return data
except (IOError, OSError) as exc:
if not ((sys.platform.startswith('win') and
exc.winerror == ERROR_SHARING_VIOLATION) or
exc.errno == errno.ENOENT):
except EnvironmentError as exc:
posix_errno = exc.errno
win_errno = getattr(exc, 'winerror', None)

if (posix_errno in (errno.ENOENT, errno.ENXIO, errno.EOPNOTSUPP) or
win_errno == ERROR_SHARING_VIOLATION):
# In Windows deleting the temporary file can fail if some
# process is still holding it open, so retry in that case
time.sleep(sleep_for)
sleep_for = min(sleep_for * 2, 0.01)
else:
raise

time.sleep(sleep_for)
sleep_for = min(sleep_for * 2, 0.01)
raise RuntimeError(
"timed out after %s secs (couldn't read file)" % timeout)

Expand Down

0 comments on commit 7d0a323

Please sign in to comment.