[bmap-tools] [PATCH 1/3] TransRead: handle the urllib2.URLError exception

Artem Bityutskiy dedekind1 at gmail.com
Fri Sep 13 06:48:12 EDT 2013


From: Artem Bityutskiy <artem.bityutskiy at intel.com>

When opening an URL with urllib2, handle the URLError exceptions too.

This patch adds a new "except" statement instead of adding the exception object
to the existing array. The reason is that in the next commit we will need to
handle the urllib2.URLError exceptions a bit differently.

This patch also refactors the code a tiny bit as a preparation to the next
commit.

Change-Id: I69856aa8698b495c5a2450979db9384aad1b713a
Signed-off-by: Artem Bityutskiy <artem.bityutskiy at intel.com>
---
 bmaptools/TransRead.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/bmaptools/TransRead.py b/bmaptools/TransRead.py
index a16d0c8..45c243d 100644
--- a/bmaptools/TransRead.py
+++ b/bmaptools/TransRead.py
@@ -466,14 +466,18 @@ class TransRead:
         urllib2.install_opener(opener)
 
         try:
-            self._f_objs.append(opener.open(url))
-            self.is_url = True
+            f_obj = opener.open(url)
+        except urllib2.URLError as err:
+            raise Error("cannot open URL '%s': %s" % (url, err))
         except (IOError, ValueError, httplib.InvalidURL) as err:
             raise Error("cannot open URL '%s': %s" % (url, err))
         except httplib.BadStatusLine:
             raise Error("cannot open URL '%s': server responds with an HTTP "
                         "status code that we don't understand" % url)
 
+        self.is_url = True
+        self._f_objs.append(f_obj)
+
     def _create_local_copy(self):
         """Create a local copy of a remote or compressed file."""
         import tempfile
-- 
1.8.1.4




More information about the Bmap-tools mailing list