Rev 6342: (gz) Add is_environment_error() to check if exceptions are due to the in file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/%2Btrunk/

Patch Queue Manager pqm at pqm.ubuntu.com
Mon Dec 5 11:33:00 UTC 2011


At file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 6342 [merge]
revision-id: pqm at pqm.ubuntu.com-20111205113259-ias55e1z6dsn2ksp
parent: pqm at pqm.ubuntu.com-20111205105956-2sswur26splv04w8
parent: martin.packman at canonical.com-20111205110616-i7v6fgkzvwvntndz
committer: Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Mon 2011-12-05 11:32:59 +0000
message:
  (gz) Add is_environment_error() to check if exceptions are due to the
   process environment (Martin Packman)
modified:
  bzrlib/osutils.py              osutils.py-20050309040759-eeaff12fbf77ac86
  bzrlib/tests/test_osutils.py   test_osutils.py-20051201224856-e48ee24c12182989
  bzrlib/trace.py                trace.py-20050309040759-c8ed824bdcd4748a
  bzrlib/win32utils.py           win32console.py-20051021033308-123c6c929d04973d
=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2011-11-25 17:54:52 +0000
+++ b/bzrlib/osutils.py	2011-12-02 12:49:48 +0000
@@ -28,6 +28,7 @@
 import getpass
 import ntpath
 import posixpath
+import select
 # We need to import both shutil and rmtree as we export the later on posix
 # and need the former on windows
 import shutil
@@ -2518,3 +2519,16 @@
     fn = getattr(os, 'fdatasync', getattr(os, 'fsync', None))
     if fn is not None:
         fn(fileno)
+
+
+def is_environment_error(evalue):
+    """True if exception instance is due to a process environment issue
+
+    This includes OSError and IOError, but also other errors that come from
+    the operating system or core libraries but are not subclasses of those.
+    """
+    if isinstance(evalue, (EnvironmentError, select.error)):
+        return True
+    if sys.platform == "win32" and win32utils._is_pywintypes_error(evalue):
+        return True
+    return False

=== modified file 'bzrlib/tests/test_osutils.py'
--- a/bzrlib/tests/test_osutils.py	2011-10-06 07:43:13 +0000
+++ b/bzrlib/tests/test_osutils.py	2011-12-02 12:49:48 +0000
@@ -20,6 +20,7 @@
 import errno
 import os
 import re
+import select
 import socket
 import sys
 import time
@@ -2179,3 +2180,29 @@
         self.assertTrue(osutils.find_executable_on_path('sh') is not None)
         self.assertTrue(
             osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
+
+
+class TestEnvironmentErrors(tests.TestCase):
+    """Test handling of environmental errors"""
+
+    def test_is_oserror(self):
+        self.assertTrue(osutils.is_environment_error(
+            OSError(errno.EINVAL, "Invalid parameter")))
+
+    def test_is_ioerror(self):
+        self.assertTrue(osutils.is_environment_error(
+            IOError(errno.EINVAL, "Invalid parameter")))
+
+    def test_is_socket_error(self):
+        self.assertTrue(osutils.is_environment_error(
+            socket.error(errno.EINVAL, "Invalid parameter")))
+
+    def test_is_select_error(self):
+        self.assertTrue(osutils.is_environment_error(
+            select.error(errno.EINVAL, "Invalid parameter")))
+
+    def test_is_pywintypes_error(self):
+        self.requireFeature(features.pywintypes)
+        import pywintypes
+        self.assertTrue(osutils.is_environment_error(
+            pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))

=== modified file 'bzrlib/trace.py'
--- a/bzrlib/trace.py	2011-09-23 20:24:51 +0000
+++ b/bzrlib/trace.py	2011-12-02 12:49:48 +0000
@@ -491,11 +491,7 @@
         print_exception(exc_info, err_file)
         return errors.EXIT_ERROR
     exc_type, exc_object, exc_tb = exc_info
-    if (isinstance(exc_object, IOError)
-        and getattr(exc_object, 'errno', None) == errno.EPIPE):
-        err_file.write("bzr: broken pipe\n")
-        return errors.EXIT_ERROR
-    elif isinstance(exc_object, KeyboardInterrupt):
+    if isinstance(exc_object, KeyboardInterrupt):
         err_file.write("bzr: interrupted\n")
         return errors.EXIT_ERROR
     elif isinstance(exc_object, MemoryError):
@@ -513,9 +509,10 @@
     elif not getattr(exc_object, 'internal_error', True):
         report_user_error(exc_info, err_file)
         return errors.EXIT_ERROR
-    elif isinstance(exc_object, (OSError, IOError)) or (
-        # GZ 2010-05-20: Like (exc_type is pywintypes.error) but avoid import
-        exc_type.__name__ == "error" and exc_type.__module__ == "pywintypes"):
+    elif osutils.is_environment_error(exc_object):
+        if getattr(exc_object, 'errno', None) == errno.EPIPE:
+            err_file.write("bzr: broken pipe\n")
+            return errors.EXIT_ERROR
         # Might be nice to catch all of these and show them as something more
         # specific, but there are too many cases at the moment.
         report_user_error(exc_info, err_file)

=== modified file 'bzrlib/win32utils.py'
--- a/bzrlib/win32utils.py	2011-09-19 15:59:40 +0000
+++ b/bzrlib/win32utils.py	2011-12-05 11:06:16 +0000
@@ -68,16 +68,21 @@
         create_buffer = ctypes.create_unicode_buffer
         suffix = 'W'
 try:
-    import win32file
     import pywintypes
-    has_win32file = True
-except ImportError:
-    has_win32file = False
-try:
-    import win32api
-    has_win32api = True
-except ImportError:
-    has_win32api = False
+    has_pywintypes = True
+except ImportError:
+    has_pywintypes = has_win32file = has_win32api = False
+else:
+    try:
+        import win32file
+        has_win32file = True
+    except ImportError:
+        has_win32file = False
+    try:
+        import win32api
+        has_win32api = True
+    except ImportError:
+        has_win32api = False
 
 # pulling in win32com.shell is a bit of overhead, and normally we don't need
 # it as ctypes is preferred and common.  lazy_imports and "optional"
@@ -615,3 +620,10 @@
         _CloseHandle(handle)
         return False
     is_local_pid_dead = _ctypes_is_local_pid_dead
+
+
+def _is_pywintypes_error(evalue):
+    """True if exception instance is an error from pywin32"""
+    if has_pywintypes and isinstance(evalue, pywintypes.error):
+        return True
+    return False




More information about the bazaar-commits mailing list