commands: Ignore exceptions during READ while UPDATE

If we are reading a file to check if we can skip the write to conserve
writes, don't treat exceptions as fatal.  The file may well have the
access mode in a way that permits us to UPDATE but not to READ.  Simply
fall-back to unconditional UPDATE in this case.

Change-Id: I7bffdaa7596e63c8f0ab04a3cb3ebe12f137d3a8
diff --git a/pySim/commands.py b/pySim/commands.py
index 336cf0c..d785251 100644
--- a/pySim/commands.py
+++ b/pySim/commands.py
@@ -279,9 +279,16 @@
 
         # Save write cycles by reading+comparing before write
         if conserve:
-            data_current, sw = self.read_binary(ef, data_length, offset)
-            if data_current == data:
-                return None, sw
+            try:
+                data_current, sw = self.read_binary(ef, data_length, offset)
+                if data_current == data:
+                    return None, sw
+            except Exception:
+                # cannot read data. This is not a fatal error, as reading is just done to
+                # conserve the amount of smart card writes.  The access conditions of the file
+                # may well permit us to UPDATE but not permit us to READ.  So let's ignore
+                # any such exception during READ.
+                pass
 
         self.select_path(ef)
         total_data = ''
@@ -363,10 +370,17 @@
 
         # Save write cycles by reading+comparing before write
         if conserve:
-            data_current, sw = self.read_record(ef, rec_no)
-            data_current = data_current[0:rec_length*2]
-            if data_current == data:
-                return None, sw
+            try:
+                data_current, sw = self.read_record(ef, rec_no)
+                data_current = data_current[0:rec_length*2]
+                if data_current == data:
+                    return None, sw
+            except Exception:
+                # cannot read data. This is not a fatal error, as reading is just done to
+                # conserve the amount of smart card writes.  The access conditions of the file
+                # may well permit us to UPDATE but not permit us to READ.  So let's ignore
+                # any such exception during READ.
+                pass
 
         pdu = (self.cla_byte + 'dc%02x04%02x' % (rec_no, rec_length)) + data
         res = self._tp.send_apdu_checksw(pdu)