Rev 3598: Factor out the common exception handling looking for ENOTDIR and use it in http://bzr.arbash-meinel.com/branches/bzr/jam-integration

John Arbash Meinel john at arbash-meinel.com
Wed Aug 13 17:07:56 BST 2008


At http://bzr.arbash-meinel.com/branches/bzr/jam-integration

------------------------------------------------------------
revno: 3598
revision-id: john at arbash-meinel.com-20080813160710-ns4og8r4y6xcmp0b
parent: mhammond at skippinet.com.au-20080809050006-5sf16g2b7eijnkwz
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: jam-integration
timestamp: Wed 2008-08-13 11:07:10 -0500
message:
  Factor out the common exception handling looking for ENOTDIR and use it
  for osutils.walkdirs() and tree transform code.
-------------- next part --------------
=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2008-08-09 05:00:06 +0000
+++ b/bzrlib/osutils.py	2008-08-13 16:07:10 +0000
@@ -1119,6 +1119,34 @@
         raise errors.IllegalPath(path)
 
 
+_WIN32_ERROR_DIRECTORY = 267 # Similar to errno.ENOTDIR
+
+def _is_error_enotdir(e):
+    """Check if this exception represents ENOTDIR.
+
+    Unfortunately, python is very inconsistent about the exception
+    here. The cases are:
+      1) Linux, Mac OSX all versions seem to set errno == ENOTDIR
+      2) Windows, Python2.4, uses errno == ERROR_DIRECTORY (267)
+         which is the windows error code.
+      3) Windows, Python2.5 uses errno == EINVAL and
+         winerror == ERROR_DIRECTORY
+
+    :param e: An Exception object (expected to be OSError with an errno
+        attribute, but we should be able to cope with anything)
+    :return: True if this represents an ENOTDIR error. False otherwise.
+    """
+    en = getattr(e, 'errno', None)
+    if (en == errno.ENOTDIR
+        or (sys.platform == 'win32'
+            and (en == _WIN32_ERROR_DIRECTORY
+                 or (en == errno.EINVAL
+                     and getattr(e, 'winerror', None) == _WIN32_ERROR_DIRECTORY)
+        ))):
+        return True
+    return False
+
+
 def walkdirs(top, prefix=""):
     """Yield data about all the directories in a tree.
     
@@ -1170,16 +1198,8 @@
         append = dirblock.append
         try:
             names = sorted(_listdir(top))
-        except EnvironmentError, e:
-            # Py 2.4 and earlier will set errno to EINVAL to 
-            # ERROR_DIRECTORY (267).  Later versions set it to
-            # EINVAL and winerror gets set to ERROR_DIRECTORY.
-            en = getattr(e, 'errno', None)
-            if (en == errno.ENOTDIR or
-                (sys.platform=='win32' and en in (267, errno.EINVAL))):
-                # We have been asked to examine a file, this is fine.
-                pass
-            else:
+        except OSError, e:
+            if not _is_error_enotdir(e):
                 raise
         else:
             for name in names:

=== modified file 'bzrlib/transform.py'
--- a/bzrlib/transform.py	2008-08-09 05:00:06 +0000
+++ b/bzrlib/transform.py	2008-08-13 16:07:10 +0000
@@ -14,7 +14,6 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-import sys
 import os
 import errno
 from stat import S_ISREG, S_IEXEC
@@ -637,12 +636,11 @@
         try:
             children = os.listdir(self._tree.abspath(path))
         except OSError, e:
-            # See comments in osutils regarding winerror etc.
-            if ((e.errno not in (errno.ENOENT, errno.ESRCH, errno.ENOTDIR)) and
-                (sys.platform!='win32' or e.errno not in (267, errno.EINVAL))):
+            if not (osutils._is_error_enotdir(e)
+                    or e.errno in (errno.ENOENT, errno.ESRCH)):
                 raise
             return
-            
+
         for child in children:
             childpath = joinpath(path, child)
             if self._tree.is_control_filename(childpath):



More information about the bazaar-commits mailing list