[Python-checkins] cpython: Simplify and remove few dependencies on 'errno', thanks to PEP 3151.

florent.xicluna python-checkins at python.org
Fri Oct 28 16:07:21 CEST 2011


http://hg.python.org/cpython/rev/e4d44c2e8e81
changeset:   73169:e4d44c2e8e81
user:        Florent Xicluna <florent.xicluna at gmail.com>
date:        Fri Oct 28 16:06:23 2011 +0200
summary:
  Simplify and remove few dependencies on 'errno', thanks to PEP 3151.

files:
  Lib/importlib/__init__.py   |   3 +-
  Lib/importlib/_bootstrap.py |  16 ++------
  Lib/tarfile.py              |   6 +--
  Lib/tempfile.py             |  42 ++++++++++--------------
  Lib/test/regrtest.py        |   6 +--
  5 files changed, 28 insertions(+), 45 deletions(-)


diff --git a/Lib/importlib/__init__.py b/Lib/importlib/__init__.py
--- a/Lib/importlib/__init__.py
+++ b/Lib/importlib/__init__.py
@@ -81,11 +81,10 @@
         except ImportError:
             raise ImportError('posix, nt, or os2 module required for importlib')
 _bootstrap._os = _os
-import imp, sys, marshal, errno, _io
+import imp, sys, marshal, _io
 _bootstrap.imp = imp
 _bootstrap.sys = sys
 _bootstrap.marshal = marshal
-_bootstrap.errno = errno
 _bootstrap._io = _io
 import _warnings
 _bootstrap._warnings = _warnings
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -7,7 +7,7 @@
 
 """
 
-# Injected modules are '_warnings', 'imp', 'sys', 'marshal', 'errno', '_io',
+# Injected modules are '_warnings', 'imp', 'sys', 'marshal', '_io',
 # and '_os' (a.k.a. 'posix', 'nt' or 'os2').
 # Injected attribute is path_sep.
 #
@@ -503,19 +503,13 @@
             parent = _path_join(parent, part)
             try:
                 _os.mkdir(parent)
-            except OSError as exc:
+            except FileExistsError:
                 # Probably another Python process already created the dir.
-                if exc.errno == errno.EEXIST:
-                    continue
-                else:
-                    raise
-            except IOError as exc:
+                continue
+            except PermissionError:
                 # If can't get proper access, then just forget about writing
                 # the data.
-                if exc.errno == errno.EACCES:
-                    return
-                else:
-                    raise
+                return
         try:
             _write_atomic(path, data)
         except (PermissionError, FileExistsError):
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -42,7 +42,6 @@
 import os
 import shutil
 import stat
-import errno
 import time
 import struct
 import copy
@@ -2281,9 +2280,8 @@
             # Use a safe mode for the directory, the real mode is set
             # later in _extract_member().
             os.mkdir(targetpath, 0o700)
-        except EnvironmentError as e:
-            if e.errno != errno.EEXIST:
-                raise
+        except FileExistsError:
+            pass
 
     def makefile(self, tarinfo, targetpath):
         """Make a file called targetpath.
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -31,7 +31,6 @@
 import sys as _sys
 import io as _io
 import os as _os
-import errno as _errno
 from random import Random as _Random
 
 try:
@@ -43,7 +42,7 @@
     def _set_cloexec(fd):
         try:
             flags = _fcntl.fcntl(fd, _fcntl.F_GETFD, 0)
-        except IOError:
+        except OSError:
             pass
         else:
             # flags read successfully, modify
@@ -85,19 +84,19 @@
 elif hasattr(_os, "stat"):
     _stat = _os.stat
 else:
-    # Fallback.  All we need is something that raises os.error if the
+    # Fallback.  All we need is something that raises OSError if the
     # file doesn't exist.
     def _stat(fn):
         try:
             f = open(fn)
-        except IOError:
-            raise _os.error
+        except OSError:
+            raise OSError
         f.close()
 
 def _exists(fn):
     try:
         _stat(fn)
-    except _os.error:
+    except OSError:
         return False
     else:
         return True
@@ -144,7 +143,7 @@
     # As a last resort, the current directory.
     try:
         dirlist.append(_os.getcwd())
-    except (AttributeError, _os.error):
+    except (AttributeError, OSError):
         dirlist.append(_os.curdir)
 
     return dirlist
@@ -176,12 +175,11 @@
                 _os.unlink(filename)
                 del fp, fd
                 return dir
-            except (OSError, IOError) as e:
-                if e.args[0] != _errno.EEXIST:
-                    break # no point trying more names in this directory
+            except FileExistsError:
                 pass
-    raise IOError(_errno.ENOENT,
-                  "No usable temporary directory found in %s" % dirlist)
+            except OSError:
+                break   # no point trying more names in this directory
+    raise FileNotFoundError("No usable temporary directory found in %s" % dirlist)
 
 _name_sequence = None
 
@@ -211,12 +209,10 @@
             fd = _os.open(file, flags, 0o600)
             _set_cloexec(fd)
             return (fd, _os.path.abspath(file))
-        except OSError as e:
-            if e.errno == _errno.EEXIST:
-                continue # try again
-            raise
+        except FileExistsError:
+            continue    # try again
 
-    raise IOError(_errno.EEXIST, "No usable temporary file name found")
+    raise FileExistsError("No usable temporary file name found")
 
 
 # User visible interfaces.
@@ -300,12 +296,10 @@
         try:
             _os.mkdir(file, 0o700)
             return file
-        except OSError as e:
-            if e.errno == _errno.EEXIST:
-                continue # try again
-            raise
+        except FileExistsError:
+            continue    # try again
 
-    raise IOError(_errno.EEXIST, "No usable temporary directory name found")
+    raise FileExistsError("No usable temporary directory name found")
 
 def mktemp(suffix="", prefix=template, dir=None):
     """User-callable function to return a unique temporary file name.  The
@@ -334,7 +328,7 @@
         if not _exists(file):
             return file
 
-    raise IOError(_errno.EEXIST, "No usable temporary filename found")
+    raise FileExistsError("No usable temporary filename found")
 
 
 class _TemporaryFileWrapper:
@@ -664,7 +658,7 @@
     _islink = staticmethod(_os.path.islink)
     _remove = staticmethod(_os.remove)
     _rmdir = staticmethod(_os.rmdir)
-    _os_error = _os.error
+    _os_error = OSError
     _warn = _warnings.warn
 
     def _rmtree(self, path):
diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py
--- a/Lib/test/regrtest.py
+++ b/Lib/test/regrtest.py
@@ -166,7 +166,6 @@
 """
 
 import builtins
-import errno
 import faulthandler
 import getopt
 import io
@@ -1721,9 +1720,8 @@
         TEMPDIR = os.path.abspath(TEMPDIR)
         try:
             os.mkdir(TEMPDIR)
-        except OSError as e:
-            if e.errno != errno.EEXIST:
-                raise
+        except FileExistsError:
+            pass
 
     # Define a writable temp dir that will be used as cwd while running
     # the tests. The name of the dir includes the pid to allow parallel

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list