Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0166a28

Browse files
committed
modernize some modules' code by replacing OSError->ENOENT/ENOTDIR/EPERM/EEXIST occurrences with the corresponding pep-3151 exceptions (FileNotFoundError, NotADirectoryError, etc.)
1 parent b071d4f commit 0166a28

6 files changed

Lines changed: 27 additions & 51 deletions

File tree

Lib/ctypes/util.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,8 @@ def _findLib_gcc(name):
102102
finally:
103103
try:
104104
os.unlink(ccout)
105-
except OSError as e:
106-
if e.errno != errno.ENOENT:
107-
raise
105+
except FileNotFoundError:
106+
pass
108107
if rv == 10:
109108
raise OSError('gcc or cc command not found')
110109
res = re.search(expr, trace)

Lib/logging/handlers.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,8 @@ def emit(self, record):
438438
try:
439439
# stat the file by path, checking for existence
440440
sres = os.stat(self.baseFilename)
441-
except OSError as err:
442-
if err.errno == errno.ENOENT:
443-
sres = None
444-
else:
445-
raise
441+
except FileNotFoundError:
442+
sres = None
446443
# compare file system stat with that of our stream file handle
447444
if not sres or sres[ST_DEV] != self.dev or sres[ST_INO] != self.ino:
448445
if self.stream is not None:

Lib/mailbox.py

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,8 @@ def discard(self, key):
334334
# This overrides an inapplicable implementation in the superclass.
335335
try:
336336
self.remove(key)
337-
except KeyError:
337+
except (KeyError, FileNotFoundError):
338338
pass
339-
except OSError as e:
340-
if e.errno != errno.ENOENT:
341-
raise
342339

343340
def __setitem__(self, key, message):
344341
"""Replace the keyed message; raise KeyError if it doesn't exist."""
@@ -493,16 +490,12 @@ def _create_tmp(self):
493490
path = os.path.join(self._path, 'tmp', uniq)
494491
try:
495492
os.stat(path)
496-
except OSError as e:
497-
if e.errno == errno.ENOENT:
498-
Maildir._count += 1
499-
try:
500-
return _create_carefully(path)
501-
except OSError as e:
502-
if e.errno != errno.EEXIST:
503-
raise
504-
else:
505-
raise
493+
except FileNotFoundError:
494+
Maildir._count += 1
495+
try:
496+
return _create_carefully(path)
497+
except FileExistsError:
498+
pass
506499

507500
# Fall through to here if stat succeeded or open raised EEXIST.
508501
raise ExternalClashError('Name clash prevented file creation: %s' %
@@ -700,12 +693,9 @@ def flush(self):
700693
os.chmod(new_file.name, mode)
701694
try:
702695
os.rename(new_file.name, self._path)
703-
except OSError as e:
704-
if e.errno == errno.EEXIST:
705-
os.remove(self._path)
706-
os.rename(new_file.name, self._path)
707-
else:
708-
raise
696+
except FileExistsError:
697+
os.remove(self._path)
698+
os.rename(new_file.name, self._path)
709699
self._file = open(self._path, 'rb+')
710700
self._toc = new_toc
711701
self._pending = False
@@ -2081,13 +2071,10 @@ def _lock_file(f, dotlock=True):
20812071
else:
20822072
os.rename(pre_lock.name, f.name + '.lock')
20832073
dotlock_done = True
2084-
except OSError as e:
2085-
if e.errno == errno.EEXIST:
2086-
os.remove(pre_lock.name)
2087-
raise ExternalClashError('dot lock unavailable: %s' %
2088-
f.name)
2089-
else:
2090-
raise
2074+
except FileExistsError:
2075+
os.remove(pre_lock.name)
2076+
raise ExternalClashError('dot lock unavailable: %s' %
2077+
f.name)
20912078
except:
20922079
if fcntl:
20932080
fcntl.lockf(f, fcntl.LOCK_UN)

Lib/os.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,9 @@ def makedirs(name, mode=0o777, exist_ok=False):
232232
if head and tail and not path.exists(head):
233233
try:
234234
makedirs(head, mode, exist_ok)
235-
except OSError as e:
235+
except FileExistsError:
236236
# be happy if someone already created the path
237-
if e.errno != errno.EEXIST:
238-
raise
237+
pass
239238
cdir = curdir
240239
if isinstance(tail, bytes):
241240
cdir = bytes(curdir, 'ASCII')

Lib/smtpd.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,8 +850,7 @@ def parseargs():
850850
nobody = pwd.getpwnam('nobody')[2]
851851
try:
852852
os.setuid(nobody)
853-
except OSError as e:
854-
if e.errno != errno.EPERM: raise
853+
except PermissionError:
855854
print('Cannot setuid "nobody"; try running with -n option.', file=sys.stderr)
856855
sys.exit(1)
857856
try:

Lib/test/support.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -291,25 +291,20 @@ def _rmtree_inner(path):
291291
def unlink(filename):
292292
try:
293293
_unlink(filename)
294-
except OSError as error:
295-
# The filename need not exist.
296-
if error.errno not in (errno.ENOENT, errno.ENOTDIR):
297-
raise
294+
except (FileNotFoundError, NotADirectoryError):
295+
pass
298296

299297
def rmdir(dirname):
300298
try:
301299
_rmdir(dirname)
302-
except OSError as error:
303-
# The directory need not exist.
304-
if error.errno != errno.ENOENT:
305-
raise
300+
except FileNotFoundError:
301+
pass
306302

307303
def rmtree(path):
308304
try:
309305
_rmtree(path)
310-
except OSError as error:
311-
if error.errno != errno.ENOENT:
312-
raise
306+
except FileNotFoundError:
307+
pass
313308

314309
def make_legacy_pyc(source):
315310
"""Move a PEP 3147 pyc/pyo file to its legacy pyc/pyo location.

0 commit comments

Comments
 (0)