]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
modernize some modules' code by replacing OSError->ENOENT/ENOTDIR/EPERM/EEXIST occurr...
authorGiampaolo Rodola' <g.rodola@gmail.com>
Tue, 12 Feb 2013 14:14:17 +0000 (15:14 +0100)
committerGiampaolo Rodola' <g.rodola@gmail.com>
Tue, 12 Feb 2013 14:14:17 +0000 (15:14 +0100)
Lib/ctypes/util.py
Lib/logging/handlers.py
Lib/mailbox.py
Lib/os.py
Lib/smtpd.py
Lib/test/support.py

index 151560427272a2496e7d88dbefe4988c09cc61ac..c427d319a7d3ee3c0a729ea9e94cc146950e1ad1 100644 (file)
@@ -102,9 +102,8 @@ elif os.name == "posix":
         finally:
             try:
                 os.unlink(ccout)
-            except OSError as e:
-                if e.errno != errno.ENOENT:
-                    raise
+            except FileNotFoundError:
+                pass
         if rv == 10:
             raise OSError('gcc or cc command not found')
         res = re.search(expr, trace)
index 1491b479879153001fd5e2e770db14e3a86d2cdc..263acc947dab58dd360c1afd14011adb205fb842 100644 (file)
@@ -438,11 +438,8 @@ class WatchedFileHandler(logging.FileHandler):
         try:
             # stat the file by path, checking for existence
             sres = os.stat(self.baseFilename)
-        except OSError as err:
-            if err.errno == errno.ENOENT:
-                sres = None
-            else:
-                raise
+        except FileNotFoundError:
+            sres = None
         # 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 self.stream is not None:
index ab20ff9e7f7e4cd45f2437c7b6b9afffe109175c..2049516b2d6a88512a670506726ab3d06eff7317 100644 (file)
@@ -334,11 +334,8 @@ class Maildir(Mailbox):
         # This overrides an inapplicable implementation in the superclass.
         try:
             self.remove(key)
-        except KeyError:
+        except (KeyError, FileNotFoundError):
             pass
-        except OSError as e:
-            if e.errno != errno.ENOENT:
-                raise
 
     def __setitem__(self, key, message):
         """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)
         try:
             os.stat(path)
-        except OSError as e:
-            if e.errno == errno.ENOENT:
-                Maildir._count += 1
-                try:
-                    return _create_carefully(path)
-                except OSError as e:
-                    if e.errno != errno.EEXIST:
-                        raise
-            else:
-                raise
+        except FileNotFoundError:
+            Maildir._count += 1
+            try:
+                return _create_carefully(path)
+            except FileExistsError:
+                pass
 
         # Fall through to here if stat succeeded or open raised EEXIST.
         raise ExternalClashError('Name clash prevented file creation: %s' %
@@ -700,12 +693,9 @@ class _singlefileMailbox(Mailbox):
         os.chmod(new_file.name, mode)
         try:
             os.rename(new_file.name, self._path)
-        except OSError as e:
-            if e.errno == errno.EEXIST:
-                os.remove(self._path)
-                os.rename(new_file.name, self._path)
-            else:
-                raise
+        except FileExistsError:
+            os.remove(self._path)
+            os.rename(new_file.name, self._path)
         self._file = open(self._path, 'rb+')
         self._toc = new_toc
         self._pending = False
@@ -2081,13 +2071,10 @@ def _lock_file(f, dotlock=True):
                 else:
                     os.rename(pre_lock.name, f.name + '.lock')
                     dotlock_done = True
-            except OSError as e:
-                if e.errno == errno.EEXIST:
-                    os.remove(pre_lock.name)
-                    raise ExternalClashError('dot lock unavailable: %s' %
-                                             f.name)
-                else:
-                    raise
+            except FileExistsError:
+                os.remove(pre_lock.name)
+                raise ExternalClashError('dot lock unavailable: %s' %
+                                         f.name)
     except:
         if fcntl:
             fcntl.lockf(f, fcntl.LOCK_UN)
index 659f3cc047669cffd9b83f7449b321cc7da0174b..5f35f4c1a4295619c7c7b6c5ccf859b7c448390c 100644 (file)
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -232,10 +232,9 @@ def makedirs(name, mode=0o777, exist_ok=False):
     if head and tail and not path.exists(head):
         try:
             makedirs(head, mode, exist_ok)
-        except OSError as e:
+        except FileExistsError:
             # be happy if someone already created the path
-            if e.errno != errno.EEXIST:
-                raise
+            pass
         cdir = curdir
         if isinstance(tail, bytes):
             cdir = bytes(curdir, 'ASCII')
index 1e239a117c14d0fa15eda469bfc966d5e93de725..3cab264df8ba2431fa81e1c9c383b6b5bca41475 100755 (executable)
@@ -850,8 +850,7 @@ if __name__ == '__main__':
         nobody = pwd.getpwnam('nobody')[2]
         try:
             os.setuid(nobody)
-        except OSError as e:
-            if e.errno != errno.EPERM: raise
+        except PermissionError:
             print('Cannot setuid "nobody"; try running with -n option.', file=sys.stderr)
             sys.exit(1)
     try:
index 7f60111712ddc9f64674141334295f9f144575f5..d886ad47c4a27196874791297aab73acc52da88f 100644 (file)
@@ -291,25 +291,20 @@ else:
 def unlink(filename):
     try:
         _unlink(filename)
-    except OSError as error:
-        # The filename need not exist.
-        if error.errno not in (errno.ENOENT, errno.ENOTDIR):
-            raise
+    except (FileNotFoundError, NotADirectoryError):
+        pass
 
 def rmdir(dirname):
     try:
         _rmdir(dirname)
-    except OSError as error:
-        # The directory need not exist.
-        if error.errno != errno.ENOENT:
-            raise
+    except FileNotFoundError:
+        pass
 
 def rmtree(path):
     try:
         _rmtree(path)
-    except OSError as error:
-        if error.errno != errno.ENOENT:
-            raise
+    except FileNotFoundError:
+        pass
 
 def make_legacy_pyc(source):
     """Move a PEP 3147 pyc/pyo file to its legacy pyc/pyo location.