changeset 49318:050dc8730858

py3: catch specific OSError subclasses instead of checking errno Contrary to the previous changesets in this series, this covers cases where errno was checked for multiple values. EACCES -> PermissionError ENOENT -> FileNotFoundError ENOTDIR -> NotADirectoryError EISDIR -> IsADirectoryError
author Manuel Jacob <me@manueljacob.de>
date Wed, 01 Jun 2022 00:47:25 +0200
parents d54b213c4380
children defc369d705e
files mercurial/dirstate.py mercurial/fileset.py mercurial/lock.py mercurial/posix.py setup.py
diffstat 5 files changed, 22 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/dirstate.py	Tue May 31 23:45:33 2022 +0200
+++ b/mercurial/dirstate.py	Wed Jun 01 00:47:25 2022 +0200
@@ -8,7 +8,6 @@
 
 import collections
 import contextlib
-import errno
 import os
 import stat
 import uuid
@@ -1035,13 +1034,11 @@
                 try:
                     with tracing.log('dirstate.walk.traverse listdir %s', nd):
                         entries = listdir(join(nd), stat=True, skip=skip)
-                except OSError as inst:
-                    if inst.errno in (errno.EACCES, errno.ENOENT):
-                        match.bad(
-                            self.pathto(nd), encoding.strtolocal(inst.strerror)
-                        )
-                        continue
-                    raise
+                except (PermissionError, FileNotFoundError) as inst:
+                    match.bad(
+                        self.pathto(nd), encoding.strtolocal(inst.strerror)
+                    )
+                    continue
                 for f, kind, st in entries:
                     # Some matchers may return files in the visitentries set,
                     # instead of 'this', if the matcher explicitly mentions them
--- a/mercurial/fileset.py	Tue May 31 23:45:33 2022 +0200
+++ b/mercurial/fileset.py	Wed Jun 01 00:47:25 2022 +0200
@@ -6,7 +6,6 @@
 # GNU General Public License version 2 or any later version.
 
 
-import errno
 import re
 
 from .i18n import _
@@ -575,16 +574,14 @@
                     return False
                 try:
                     return predfn(fctx)
-                except (IOError, OSError) as e:
-                    # open()-ing a directory fails with EACCES on Windows
-                    if e.errno in (
-                        errno.ENOENT,
-                        errno.EACCES,
-                        errno.ENOTDIR,
-                        errno.EISDIR,
-                    ):
-                        return False
-                    raise
+                # open()-ing a directory fails with PermissionError on Windows
+                except (
+                    FileNotFoundError,
+                    PermissionError,
+                    NotADirectoryError,
+                    IsADirectoryError,
+                ):
+                    return False
 
         else:
 
--- a/mercurial/lock.py	Tue May 31 23:45:33 2022 +0200
+++ b/mercurial/lock.py	Wed Jun 01 00:47:25 2022 +0200
@@ -38,9 +38,8 @@
     if pycompat.sysplatform.startswith(b'linux'):
         try:
             result += b'/%x' % os.stat(b'/proc/self/ns/pid').st_ino
-        except OSError as ex:
-            if ex.errno not in (errno.ENOENT, errno.EACCES, errno.ENOTDIR):
-                raise
+        except (FileNotFoundError, PermissionError, NotADirectoryError):
+            pass
     return result
 
 
--- a/mercurial/posix.py	Tue May 31 23:45:33 2022 +0200
+++ b/mercurial/posix.py	Wed Jun 01 00:47:25 2022 +0200
@@ -581,9 +581,7 @@
             st = lstat(nf)
             if getkind(st.st_mode) not in _wantedkinds:
                 st = None
-        except OSError as err:
-            if err.errno not in (errno.ENOENT, errno.ENOTDIR):
-                raise
+        except (FileNotFoundError, NotADirectoryError):
             st = None
         yield st
 
--- a/setup.py	Tue May 31 23:45:33 2022 +0200
+++ b/setup.py	Wed Jun 01 00:47:25 2022 +0200
@@ -95,7 +95,6 @@
 ispypy = "PyPy" in sys.version
 
 import ctypes
-import errno
 import stat, subprocess, time
 import re
 import shutil
@@ -1422,15 +1421,12 @@
             )
         try:
             subprocess.check_call(cargocmd, env=env, cwd=self.rustsrcdir)
-        except OSError as exc:
-            if exc.errno == errno.ENOENT:
-                raise RustCompilationError("Cargo not found")
-            elif exc.errno == errno.EACCES:
-                raise RustCompilationError(
-                    "Cargo found, but permission to execute it is denied"
-                )
-            else:
-                raise
+        except FileNotFoundError:
+            raise RustCompilationError("Cargo not found")
+        except PermissionError:
+            raise RustCompilationError(
+                "Cargo found, but permission to execute it is denied"
+            )
         except subprocess.CalledProcessError:
             raise RustCompilationError(
                 "Cargo failed. Working directory: %r, "