changeset 49305:53e9422a9b45

py3: catch FileExistsError instead of checking errno == EEXIST
author Manuel Jacob <me@manueljacob.de>
date Tue, 31 May 2022 21:16:17 +0200
parents 48f1b314056b
children 2e726c934fcd
files hgext/mq.py hgext/remotefilelog/remotefilelogserver.py hgext/remotefilelog/shallowutil.py mercurial/cmdutil.py mercurial/hg.py mercurial/posix.py mercurial/win32.py mercurial/windows.py tests/run-tests.py tests/test-verify-repo-operations.py
diffstat 10 files changed, 33 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/mq.py	Tue May 31 16:54:58 2022 +0200
+++ b/hgext/mq.py	Tue May 31 21:16:17 2022 +0200
@@ -2151,8 +2151,8 @@
             raise error.Abort(_(b"patch queue directory already exists"))
         try:
             os.mkdir(self.path)
-        except OSError as inst:
-            if inst.errno != errno.EEXIST or not create:
+        except FileExistsError:
+            if not create:
                 raise
         if create:
             return self.qrepo(create=True)
--- a/hgext/remotefilelog/remotefilelogserver.py	Tue May 31 16:54:58 2022 +0200
+++ b/hgext/remotefilelog/remotefilelogserver.py	Tue May 31 21:16:17 2022 +0200
@@ -5,7 +5,6 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
-import errno
 import os
 import stat
 import time
@@ -255,9 +254,8 @@
             if not os.path.exists(dirname):
                 try:
                     os.makedirs(dirname)
-                except OSError as ex:
-                    if ex.errno != errno.EEXIST:
-                        raise
+                except FileExistsError:
+                    pass
 
             f = None
             try:
--- a/hgext/remotefilelog/shallowutil.py	Tue May 31 16:54:58 2022 +0200
+++ b/hgext/remotefilelog/shallowutil.py	Tue May 31 21:16:17 2022 +0200
@@ -6,7 +6,6 @@
 # GNU General Public License version 2 or any later version.
 
 import collections
-import errno
 import os
 import stat
 import struct
@@ -359,9 +358,8 @@
     if not os.path.exists(dirname):
         try:
             os.makedirs(dirname)
-        except OSError as ex:
-            if ex.errno != errno.EEXIST:
-                raise
+        except FileExistsError:
+            pass
 
     fd, temp = tempfile.mkstemp(prefix=b'.%s-' % filename, dir=dirname)
     os.close(fd)
@@ -519,9 +517,8 @@
         for path in reversed(missingdirs):
             try:
                 os.mkdir(path)
-            except OSError as ex:
-                if ex.errno != errno.EEXIST:
-                    raise
+            except FileExistsError:
+                pass
 
         for path in missingdirs:
             setstickygroupdir(path, gid, ui.warn)
--- a/mercurial/cmdutil.py	Tue May 31 16:54:58 2022 +0200
+++ b/mercurial/cmdutil.py	Tue May 31 21:16:17 2022 +0200
@@ -561,9 +561,8 @@
             backupdir = repo.vfs.join(b'record-backups')
             try:
                 os.mkdir(backupdir)
-            except OSError as err:
-                if err.errno != errno.EEXIST:
-                    raise
+            except FileExistsError:
+                pass
         try:
             # backup continues
             for f in tobackup:
--- a/mercurial/hg.py	Tue May 31 16:54:58 2022 +0200
+++ b/mercurial/hg.py	Tue May 31 21:16:17 2022 +0200
@@ -7,7 +7,6 @@
 # GNU General Public License version 2 or any later version.
 
 
-import errno
 import os
 import posixpath
 import shutil
@@ -528,9 +527,8 @@
     # lock class requires the directory to exist.
     try:
         util.makedir(pooldir, False)
-    except OSError as e:
-        if e.errno != errno.EEXIST:
-            raise
+    except FileExistsError:
+        pass
 
     poolvfs = vfsmod.vfs(pooldir)
     basename = os.path.basename(sharepath)
@@ -893,13 +891,9 @@
                     create=True,
                     createopts=createopts,
                 )
-            except OSError as inst:
-                if inst.errno == errno.EEXIST:
-                    cleandir = None
-                    raise error.Abort(
-                        _(b"destination '%s' already exists") % dest
-                    )
-                raise
+            except FileExistsError:
+                cleandir = None
+                raise error.Abort(_(b"destination '%s' already exists") % dest)
 
             if revs:
                 if not srcpeer.capable(b'lookup'):
--- a/mercurial/posix.py	Tue May 31 16:54:58 2022 +0200
+++ b/mercurial/posix.py	Tue May 31 21:16:17 2022 +0200
@@ -329,11 +329,9 @@
                     except OSError:
                         unlink(name)
                 return True
-            except OSError as inst:
+            except FileExistsError:
                 # link creation might race, try again
-                if inst.errno == errno.EEXIST:
-                    continue
-                raise
+                continue
             finally:
                 if fd is not None:
                     fd.close()
--- a/mercurial/win32.py	Tue May 31 16:54:58 2022 +0200
+++ b/mercurial/win32.py	Tue May 31 21:16:17 2022 +0200
@@ -735,11 +735,10 @@
     for tries in range(10):
         temp = b'%s-%08x' % (f, random.randint(0, 0xFFFFFFFF))
         try:
-            os.rename(f, temp)  # raises OSError EEXIST if temp exists
+            os.rename(f, temp)
             break
-        except OSError as e:
-            if e.errno != errno.EEXIST:
-                raise
+        except FileExistsError:
+            pass
     else:
         raise IOError(errno.EEXIST, "No usable temporary filename found")
 
--- a/mercurial/windows.py	Tue May 31 16:54:58 2022 +0200
+++ b/mercurial/windows.py	Tue May 31 21:16:17 2022 +0200
@@ -638,9 +638,7 @@
     '''atomically rename file src to dst, replacing dst if it exists'''
     try:
         os.rename(src, dst)
-    except OSError as e:
-        if e.errno != errno.EEXIST:
-            raise
+    except FileExistsError:
         unlink(dst)
         os.rename(src, dst)
 
--- a/tests/run-tests.py	Tue May 31 16:54:58 2022 +0200
+++ b/tests/run-tests.py	Tue May 31 21:16:17 2022 +0200
@@ -1099,9 +1099,8 @@
 
         try:
             os.mkdir(self._threadtmp)
-        except OSError as e:
-            if e.errno != errno.EEXIST:
-                raise
+        except FileExistsError:
+            pass
 
         name = self._tmpname
         self._testtmp = os.path.join(self._threadtmp, name)
@@ -3277,9 +3276,8 @@
             exceptionsdir = os.path.join(self._outputdir, b'exceptions')
             try:
                 os.makedirs(exceptionsdir)
-            except OSError as e:
-                if e.errno != errno.EEXIST:
-                    raise
+            except FileExistsError:
+                pass
 
             # Remove all existing exception reports.
             for f in os.listdir(exceptionsdir):
@@ -3593,10 +3591,9 @@
                     try:
                         os.symlink(sysexecutable, mypython)
                         self._createdfiles.append(mypython)
-                    except OSError as err:
+                    except FileExistsError:
                         # child processes may race, which is harmless
-                        if err.errno != errno.EEXIST:
-                            raise
+                        pass
         elif WINDOWS and not os.getenv('MSYSTEM'):
             raise AssertionError('cannot run test on Windows without MSYSTEM')
         else:
@@ -3706,9 +3703,8 @@
         def makedirs(p):
             try:
                 os.makedirs(p)
-            except OSError as e:
-                if e.errno != errno.EEXIST:
-                    raise
+            except FileExistsError:
+                pass
 
         makedirs(self._pythondir)
         makedirs(self._bindir)
@@ -3757,9 +3753,8 @@
             covdir = os.path.join(self._installdir, b'..', b'coverage')
             try:
                 os.mkdir(covdir)
-            except OSError as e:
-                if e.errno != errno.EEXIST:
-                    raise
+            except FileExistsError:
+                pass
 
             osenvironb[b'COVERAGE_DIR'] = covdir
 
--- a/tests/test-verify-repo-operations.py	Tue May 31 16:54:58 2022 +0200
+++ b/tests/test-verify-repo-operations.py	Tue May 31 21:16:17 2022 +0200
@@ -36,7 +36,6 @@
 
 import binascii
 from contextlib import contextmanager
-import errno
 import pipes
 import shutil
 import silenttestrunner
@@ -86,9 +85,8 @@
     try:
         os.close(os.open(savefile, os.O_CREAT | os.O_EXCL | os.O_WRONLY))
         break
-    except OSError as e:
-        if e.errno != errno.EEXIST:
-            raise
+    except FileExistsError:
+        pass
 assert os.path.exists(savefile)
 
 hgrc = os.path.join(".hg", "hgrc")