modernize some modules' code by replacing OSError->ENOENT/ENOTDIR/EPERM/EEXIST occurrences with the corresponding pep-3151 exceptions (FileNotFoundError, NotADirectoryError, etc.)
This commit is contained in:
parent
b071d4f3da
commit
0166a283f6
|
@ -102,9 +102,8 @@ elif os.name == "posix":
|
||||||
finally:
|
finally:
|
||||||
try:
|
try:
|
||||||
os.unlink(ccout)
|
os.unlink(ccout)
|
||||||
except OSError as e:
|
except FileNotFoundError:
|
||||||
if e.errno != errno.ENOENT:
|
pass
|
||||||
raise
|
|
||||||
if rv == 10:
|
if rv == 10:
|
||||||
raise OSError('gcc or cc command not found')
|
raise OSError('gcc or cc command not found')
|
||||||
res = re.search(expr, trace)
|
res = re.search(expr, trace)
|
||||||
|
|
|
@ -438,11 +438,8 @@ class WatchedFileHandler(logging.FileHandler):
|
||||||
try:
|
try:
|
||||||
# stat the file by path, checking for existence
|
# stat the file by path, checking for existence
|
||||||
sres = os.stat(self.baseFilename)
|
sres = os.stat(self.baseFilename)
|
||||||
except OSError as err:
|
except FileNotFoundError:
|
||||||
if err.errno == errno.ENOENT:
|
sres = None
|
||||||
sres = None
|
|
||||||
else:
|
|
||||||
raise
|
|
||||||
# compare file system stat with that of our stream file handle
|
# compare file system stat with that of our stream file handle
|
||||||
if not sres or sres[ST_DEV] != self.dev or sres[ST_INO] != self.ino:
|
if not sres or sres[ST_DEV] != self.dev or sres[ST_INO] != self.ino:
|
||||||
if self.stream is not None:
|
if self.stream is not None:
|
||||||
|
|
|
@ -334,11 +334,8 @@ class Maildir(Mailbox):
|
||||||
# This overrides an inapplicable implementation in the superclass.
|
# This overrides an inapplicable implementation in the superclass.
|
||||||
try:
|
try:
|
||||||
self.remove(key)
|
self.remove(key)
|
||||||
except KeyError:
|
except (KeyError, FileNotFoundError):
|
||||||
pass
|
pass
|
||||||
except OSError as e:
|
|
||||||
if e.errno != errno.ENOENT:
|
|
||||||
raise
|
|
||||||
|
|
||||||
def __setitem__(self, key, message):
|
def __setitem__(self, key, message):
|
||||||
"""Replace the keyed message; raise KeyError if it doesn't exist."""
|
"""Replace the keyed message; raise KeyError if it doesn't exist."""
|
||||||
|
@ -493,16 +490,12 @@ class Maildir(Mailbox):
|
||||||
path = os.path.join(self._path, 'tmp', uniq)
|
path = os.path.join(self._path, 'tmp', uniq)
|
||||||
try:
|
try:
|
||||||
os.stat(path)
|
os.stat(path)
|
||||||
except OSError as e:
|
except FileNotFoundError:
|
||||||
if e.errno == errno.ENOENT:
|
Maildir._count += 1
|
||||||
Maildir._count += 1
|
try:
|
||||||
try:
|
return _create_carefully(path)
|
||||||
return _create_carefully(path)
|
except FileExistsError:
|
||||||
except OSError as e:
|
pass
|
||||||
if e.errno != errno.EEXIST:
|
|
||||||
raise
|
|
||||||
else:
|
|
||||||
raise
|
|
||||||
|
|
||||||
# Fall through to here if stat succeeded or open raised EEXIST.
|
# Fall through to here if stat succeeded or open raised EEXIST.
|
||||||
raise ExternalClashError('Name clash prevented file creation: %s' %
|
raise ExternalClashError('Name clash prevented file creation: %s' %
|
||||||
|
@ -700,12 +693,9 @@ class _singlefileMailbox(Mailbox):
|
||||||
os.chmod(new_file.name, mode)
|
os.chmod(new_file.name, mode)
|
||||||
try:
|
try:
|
||||||
os.rename(new_file.name, self._path)
|
os.rename(new_file.name, self._path)
|
||||||
except OSError as e:
|
except FileExistsError:
|
||||||
if e.errno == errno.EEXIST:
|
os.remove(self._path)
|
||||||
os.remove(self._path)
|
os.rename(new_file.name, self._path)
|
||||||
os.rename(new_file.name, self._path)
|
|
||||||
else:
|
|
||||||
raise
|
|
||||||
self._file = open(self._path, 'rb+')
|
self._file = open(self._path, 'rb+')
|
||||||
self._toc = new_toc
|
self._toc = new_toc
|
||||||
self._pending = False
|
self._pending = False
|
||||||
|
@ -2081,13 +2071,10 @@ def _lock_file(f, dotlock=True):
|
||||||
else:
|
else:
|
||||||
os.rename(pre_lock.name, f.name + '.lock')
|
os.rename(pre_lock.name, f.name + '.lock')
|
||||||
dotlock_done = True
|
dotlock_done = True
|
||||||
except OSError as e:
|
except FileExistsError:
|
||||||
if e.errno == errno.EEXIST:
|
os.remove(pre_lock.name)
|
||||||
os.remove(pre_lock.name)
|
raise ExternalClashError('dot lock unavailable: %s' %
|
||||||
raise ExternalClashError('dot lock unavailable: %s' %
|
f.name)
|
||||||
f.name)
|
|
||||||
else:
|
|
||||||
raise
|
|
||||||
except:
|
except:
|
||||||
if fcntl:
|
if fcntl:
|
||||||
fcntl.lockf(f, fcntl.LOCK_UN)
|
fcntl.lockf(f, fcntl.LOCK_UN)
|
||||||
|
|
|
@ -232,10 +232,9 @@ def makedirs(name, mode=0o777, exist_ok=False):
|
||||||
if head and tail and not path.exists(head):
|
if head and tail and not path.exists(head):
|
||||||
try:
|
try:
|
||||||
makedirs(head, mode, exist_ok)
|
makedirs(head, mode, exist_ok)
|
||||||
except OSError as e:
|
except FileExistsError:
|
||||||
# be happy if someone already created the path
|
# be happy if someone already created the path
|
||||||
if e.errno != errno.EEXIST:
|
pass
|
||||||
raise
|
|
||||||
cdir = curdir
|
cdir = curdir
|
||||||
if isinstance(tail, bytes):
|
if isinstance(tail, bytes):
|
||||||
cdir = bytes(curdir, 'ASCII')
|
cdir = bytes(curdir, 'ASCII')
|
||||||
|
|
|
@ -850,8 +850,7 @@ if __name__ == '__main__':
|
||||||
nobody = pwd.getpwnam('nobody')[2]
|
nobody = pwd.getpwnam('nobody')[2]
|
||||||
try:
|
try:
|
||||||
os.setuid(nobody)
|
os.setuid(nobody)
|
||||||
except OSError as e:
|
except PermissionError:
|
||||||
if e.errno != errno.EPERM: raise
|
|
||||||
print('Cannot setuid "nobody"; try running with -n option.', file=sys.stderr)
|
print('Cannot setuid "nobody"; try running with -n option.', file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -291,25 +291,20 @@ else:
|
||||||
def unlink(filename):
|
def unlink(filename):
|
||||||
try:
|
try:
|
||||||
_unlink(filename)
|
_unlink(filename)
|
||||||
except OSError as error:
|
except (FileNotFoundError, NotADirectoryError):
|
||||||
# The filename need not exist.
|
pass
|
||||||
if error.errno not in (errno.ENOENT, errno.ENOTDIR):
|
|
||||||
raise
|
|
||||||
|
|
||||||
def rmdir(dirname):
|
def rmdir(dirname):
|
||||||
try:
|
try:
|
||||||
_rmdir(dirname)
|
_rmdir(dirname)
|
||||||
except OSError as error:
|
except FileNotFoundError:
|
||||||
# The directory need not exist.
|
pass
|
||||||
if error.errno != errno.ENOENT:
|
|
||||||
raise
|
|
||||||
|
|
||||||
def rmtree(path):
|
def rmtree(path):
|
||||||
try:
|
try:
|
||||||
_rmtree(path)
|
_rmtree(path)
|
||||||
except OSError as error:
|
except FileNotFoundError:
|
||||||
if error.errno != errno.ENOENT:
|
pass
|
||||||
raise
|
|
||||||
|
|
||||||
def make_legacy_pyc(source):
|
def make_legacy_pyc(source):
|
||||||
"""Move a PEP 3147 pyc/pyo file to its legacy pyc/pyo location.
|
"""Move a PEP 3147 pyc/pyo file to its legacy pyc/pyo location.
|
||||||
|
|
Loading…
Reference in New Issue