New python new syntax for except: as keyword
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Wed, 16 May 2012 15:13:17 +0000 (15:13 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Wed, 16 May 2012 15:13:17 +0000 (15:13 +0000)
14 files changed:
TODO
bin/common.py
bin/djais/models.py
bin/djais/views.py
bin/extras/planeplotter_parse.py
bin/extras/shipplotter_coaa.py
bin/inputs/config.py
bin/inputs/outpeer.py
bin/inputs/serialin.py
bin/inputs/stats.py
bin/inputs/tcpout.py
bin/jobrunner.py
bin/ntime.py
bin/ntools.py

diff --git a/TODO b/TODO
index 6ba34677bd855d45f4ff248243c1eabea89576cc..bb1ff191d9399c29eb1db1e31b7359c324b70a79 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,5 +1,2 @@
 Know bugs:
 - Database restart crashes the acquisition daemon.
-
-Python 2.6 updates:
-       We should use the new except .. *as*
index c13e72ca5711a0ff245c2982fead094be7e935f4..17d2e3c5092a711735ab2d1e3ce87c3979a84f09 100755 (executable)
@@ -542,7 +542,7 @@ def db_lastinfo_setrecord_ifnewer(basefilename, record, timestamp):
 
     try:
         f = open(filename, 'r+b')
-    except IOError, ioerr:
+    except IOError as ioerr:
         if ioerr.errno != 2:
             raise
         # File was not found? Ok, create it. FIXME: we should lock something...
@@ -675,7 +675,7 @@ def add_nmea5_partial(strmmsi, timestamp, imo, name, callsign, type, \
     filename = os.path.join(DBPATH, 'last', _hash3_pathfilename(filename))
     try:
         f = open(filename, 'r+b')
-    except IOError, ioerr:
+    except IOError as ioerr:
         if ioerr.errno != 2:
             raise
         # File was not found? Ok, create it. FIXME: we should lock something...
@@ -1352,7 +1352,7 @@ class BankNmea1(list):
         try:
             file = open(self.get_filename(), 'rb')
             lockf(file, LOCK_SH)
-        except IOError, ioerr:
+        except IOError as ioerr:
             if ioerr.errno == 2: # No file
                 return
             raise
@@ -1373,7 +1373,7 @@ class BankNmea1(list):
         filename = self.get_filename()
         try:
             file = open(filename, 'r+b') # read/write binary
-        except IOError, ioerr:
+        except IOError as ioerr:
             if ioerr.errno != 2: # No file
                 raise
             return self # no data
@@ -1551,7 +1551,7 @@ class BankNmea5(list):
         try:
             file = open(self.get_filename(), 'rb')
             lockf(file, LOCK_SH)
-        except IOError, ioerr:
+        except IOError as ioerr:
             if ioerr.errno == 2: # No file
                 return
             raise
index 7c0c262b16fcb6a615c4a6e710b0683c72cea12d..d291284f8d3e6591a01810cf779b9d7f1d5f2d6d 100644 (file)
@@ -249,7 +249,7 @@ class Job(models.Model):
         filename = '/var/lib/ais/jobs/%s.log' % self.id
         try:
             return os.path.getsize(filename)
-        except OSError, err:
+        except OSError as err:
             if err.errno == 2:
                 return 0
             raise
index f68e9a3fdfa61c00a7ce1ce531fd34258a18b29b..425750e80d14a4e91b380ec5d250fb501f0cc0da 100644 (file)
@@ -208,7 +208,7 @@ class HistoryForm(forms.Form):
             return None
         try:
             clean_lat = clean_latitude(lat)
-        except LatLonFormatError, err:
+        except LatLonFormatError as err:
             if len(err.args) > 1:
                 raise forms.ValidationError(err.args[0] % err.args[1:])
             else:
@@ -221,7 +221,7 @@ class HistoryForm(forms.Form):
             return None
         try:
             clean_lon = clean_longitude(lon)
-        except LatLonFormatError, err:
+        except LatLonFormatError as err:
             if len(err.args) > 1:
                 raise forms.ValidationError(err.args[0] % err.args[1:])
             else:
@@ -962,7 +962,7 @@ def job_log(request, jobid):
     log = u''
     try:
         log = file(jobrunner.RESULT_DIR+unicode(jobid)+'.log').read()
-    except IOError, err:
+    except IOError as err:
         if err.errno != 2: # No such file
             raise
     return render_to_response('job_log.html', {'job': job, 'log': log}, RequestContext(request))
@@ -1059,7 +1059,7 @@ class ChangePasswordForm(forms.Form):
         password = self.cleaned_data[field_name]
         try:
             crack.FascistCheck(password)
-        except ValueError, err:
+        except ValueError as err:
             raise forms.ValidationError(err.message)
         return password
 
index 7b4f191c94005cc77d7509c3a0a66cfbd10e910b..af1ac7620ec8fd182669a011f35a89b89db2309b 100755 (executable)
@@ -100,7 +100,7 @@ def parse_log_line(line):
     #        J = int(eta[3:5])
     #        h = int(eta[6:8])
     #        m = int(eta[9:11])
-    #    except Exception, e:
+    #    except Exception as e:
     #        pass
     #        #print e
     #    else:
index fba2f6457e57907f07ed1aad8686c0ca0596123c..d5f9ac37f466b722c3f7b12b2a612fe14b711024 100755 (executable)
@@ -47,7 +47,7 @@ def save_httpdata(httpdata):
     foldername = 'shipplotter/'+now.strftime('%Y%m%d')
     try:
         os.mkdir(foldername)
-    except OSError, oserr:
+    except OSError as oserr:
         if oserr.errno != 17:
             raise
         # else folder allrady exists, perfect
index 6a3fa33eaae969e12615837c66d556c845ba9963..029a072b1a4d3a81cbc2fdb16ce0258ac6a110c5 100644 (file)
@@ -167,7 +167,7 @@ def get_hidden_mmsi():
         __hidden_mmsi__ = []
         try:
             lines = file(HIDDENMMSI_FILENAME).read().split('\n')
-        except IOError, err:
+        except IOError as err:
             if err.errno == 2: # No such file or directory
                 logging.info('No hidden ship')
                 return __hidden_mmsi__
index 97cc48886bcb6bf465f58b753fd923e269629ee7..03d509e6ee8a3990fe5765e11e6d3c38befdb6ce 100644 (file)
@@ -69,7 +69,7 @@ class UdpOutPeer:
                             len(self.possibles_addrinfo), self.host, self.port)
                         logging.debug('possibles_addrinfo: %s ',
                             self.possibles_addrinfo)
-                    except socket.gaierror, err:
+                    except socket.gaierror as err:
                         logging.error("Can't resolve %s:%s: %s. "
                             "Will retry in a minute.",
                             self.host, self.port, err)
@@ -91,7 +91,7 @@ class UdpOutPeer:
             try:
                 self.sock = socket.socket(af_, socktype, proto)
                 return True
-            except socket.error, msg:
+            except socket.error as msg:
                 logging.error("Can't open socket to %s: %s", self.addr, msg)
         
     def send_line(self, line, decoded_info):
@@ -103,7 +103,7 @@ class UdpOutPeer:
             try:
                 self.sock.sendto(line+'\r\n', self.addr)
                 return
-            except socket.error, err:
+            except socket.error as err:
                 logging.error('socket.sendto(%s) failed: %s', self.addr, err)
                 self.sock = None
 
@@ -121,7 +121,7 @@ class TcpInOutPeer:
     def send_line(self, line, decoded_info):
         try:
             self.sock.send(line+'\r\n')
-        except socket.error, err:
+        except socket.error as err:
             if err.errno == 32: # Broken pipe
                 logging.info('TcpInOutPeer %s:%s disconnect', formataddr(self.addr), self.port)
                 tcpin_outpeers.remove_peer(self)
@@ -151,7 +151,7 @@ class TcpInServiceOut(Thread):
             try:
                 self.sock.bind(('', self.port))
                 break
-            except socket.error, err:
+            except socket.error as err:
                 if err.errno == 98: # Address already in use
                     logging.error('TCP port %s already in use. Waiting...', self.port)
                     sleep(1)
@@ -168,7 +168,7 @@ class TcpInServiceOut(Thread):
         while True:
             try:
                 conn, addr = self.sock.accept()
-            except socket.error, err:
+            except socket.error as err:
                 if err.errno == 22: # Invalid argument
                     logging.info('TcpInServiceOut socket was closed. Exiting thread.')
                     return
@@ -180,7 +180,7 @@ class TcpInServiceOut(Thread):
             try:
                 logging.info('Shuting down tcpinoutpeerlisten socket')
                 self.sock.shutdown(socket.SHUT_RDWR)
-            except socket.error, err:
+            except socket.error as err:
                 logging.info('Ignoring error %s while shuting down TcpInServiceOut socket.', err.errno)
             self.sock.close()       
 
index ab3eaf0e82cd7c26347dab92a993b6e4be1e4b58..1ce596a4f5edaa3403d3c6e6787e2d68e45b0242 100644 (file)
@@ -43,7 +43,7 @@ class SerialChannel(Channel):
         '''
         try:
             waiting = self.serial.inWaiting()
-        except IOError, err:
+        except IOError as err:
             logging.critical('%s: %s', self.name, err)
             return
         if waiting:
index c664e3462833042a1ce3a46e1a11d3bad1855ed7..cbf6ca47fc0bcb17daa8e393b9ab0d3dda341c23 100644 (file)
@@ -72,7 +72,7 @@ class InStatsRate:
                         self.rbytes, self.rbytes_ethernet))
         try:
             self._save(filename)
-        except rrdtool.error, err:
+        except rrdtool.error as err:
             logging.error(err)
             if 'No such file or directory' in err.message:
                 self._create(filename)
index 124e552f11a187532c86459ed0813252006f2f98..e87879bd222edd01dfdcdf92ebafecc426a56914 100644 (file)
@@ -32,12 +32,12 @@ class TcpOutChannel(Channel):
             self.sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
             self.sock.connect(address_out)
             logging.info("Connected to %s using tcp6", address_out)
-        except socket.gaierror, err:
+        except socket.gaierror as err:
             logging.error("Can't connet to %s: %s using tcp6", address_out, err)
             self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
             try:
                 self.sock.connect(address_out)
-            except socket.error, err:
+            except socket.error as err:
                 logging.error("Can't connet to %s: %s using tcp4", address_out, err)
                 raise
             logging.info("Connected to %s using tcp4", address_out)
index 85d2fdae06dc1c14c8dca60bb2d0a61ec2c21735..ff9de5416b0b3d1802d615b1267a76e572834258 100755 (executable)
@@ -105,11 +105,11 @@ def runjob():
         filename, logfilename = jobid_ext_to_filenames(*row)
         try:
             os.unlink(filename)
-        except OSError, err:
+        except OSError as err:
             logging.error('unlinking %s: %s', filename, err)
         try:
             os.unlink(logfilename)
-        except OSError, err:
+        except OSError as err:
             logging.error('unlinking %s: %s', logfilename, err)
         deleted_jobs.append(row[0])
 
index 27741b821717094618ded786d2e710b3120d87e2..d30f50356e9685acff759497c8330e12a49c0c24 100644 (file)
@@ -37,7 +37,7 @@ def __init():
     filename = '/var/lib/ais/leapseconds'
     try:
         history_file = file(filename)
-    except IOError, err:
+    except IOError as err:
         if err[0] != 2: # file not found
             raise
         logging.warning('Leap second file not found. Downloading...')
index 3236319f676c1cb6c06b5c1692991be2a04b9409..29d6bdfcd61aa3c404698903167405ce75beb2e7 100644 (file)
@@ -104,7 +104,7 @@ def clean_alnum_unicode(txt):
 def open_with_mkdirs(filename, mode):
     try:
         return file(filename, mode)
-    except IOError, ioerr:
+    except IOError as ioerr:
         logging.warning("file(%s,%s): errno %s %s", filename, mode, ioerr.errno, ioerr.strerror)
         # FIXME only if doesn't exists ...
         #print 'Creating directory', os.path.dirname(filename)