git » msnlib » commit c149f1c

Do not use string exceptions

author Alberto Bertogli
2010-06-25 00:19:26 UTC
committer Alberto Bertogli
2010-06-25 00:19:26 UTC
parent 9739a6bfc6937e7b342d65ce5149e2200af21309

Do not use string exceptions

String exceptions have been deprecated for a long long time, and will be
no longer accepted in future Python releases.

This patch removes all string exceptions and replaces them with proper
classes.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

msn +4 -6
msncb.py +24 -4
msnlib.py +23 -8
utils/msnbot +1 -1
utils/msncd +6 -6
utils/msnsbot +1 -1
utils/msntk +2 -2

diff --git a/msn b/msn
index 1ea8a0b..2bc2558 100755
--- a/msn
+++ b/msn
@@ -1776,7 +1776,7 @@ printl('Logging in... ', c.green, 1)
 try:
 	m.login()
 	printl('done\n', c.green, 1)
-except 'AuthError', info:
+except msnlib.AuthError, info:
 	errno = int(info[0])
 	if not msncb.error_table.has_key(errno):
 		desc = 'Unknown'
@@ -1786,7 +1786,7 @@ except 'AuthError', info:
 	quit(1)
 except KeyboardInterrupt:
 	quit()
-except ('SocketError', socket.error), info:
+except (msnlib.SocketError, socket.error), info:
 	perror('Network error: ' + str(info) + '\n')
 	quit(1)
 except:
@@ -1859,9 +1859,7 @@ while 1:
 					else:
 						perror('\rError setting status: unknown status %s\n' % config['initial status'])
 
-
-
-			except ('SocketError', socket.error), err:
+			except (msnlib.SocketError, socket.error), err:
 				if i != m:
 					if i.msgqueue:
 						nick = email2nick(i.emails[0])
@@ -1872,7 +1870,7 @@ while 1:
 				else:
 					printl('\nMain socket closed (%s)\n' % str(err), c.red)
 					quit(1)
-			except 'XFRError', err:
+			except msncb.XFRError, err:
 				printl("\rXFR Error: %s\n" % str(err))
 
 			# always redraw after network event
diff --git a/msncb.py b/msncb.py
index 72099b6..7f8b016 100644
--- a/msncb.py
+++ b/msncb.py
@@ -7,7 +7,7 @@ import socket
 # introduced in python 2.5
 try:
 	from hashlib import md5
-except:
+except ImportError:
 	from md5 import md5
 
 import msnlib
@@ -119,6 +119,25 @@ error_table = {
 	920: 'Not accepting new users',
 }
 
+
+#
+# Possible exceptions
+#
+
+class CallbackMess (Exception):
+	pass
+
+class SYNError (Exception):
+	pass
+
+class XFRError (Exception):
+	pass
+
+
+#
+# Callbacks
+#
+
 def cb_err(md, errno, params):
 	"Handle server errors"
 	if not error_table.has_key(errno):
@@ -142,7 +161,8 @@ def cb_unk(md, type, tid, params):
 
 def cb_chl(md, type, tid, params):
 	"Handles the challenges"
-	if type != 'CHL': raise 'CallbackMess', (md, type, params)
+	if type != 'CHL':
+		raise CallbackMess, (md, type, params)
 	hash = params + 'VT6PX?UQTM4WM%YR' # magic from www.hypothetic.org
 	hash = md5(hash).hexdigest()
 	md._send('QRY', 'PROD0038W!61ZTF9 32')
@@ -224,7 +244,7 @@ def cb_syn(md, type, tid, params):
 	"Receive a SYN notification"
 	t = params.split()
 	if len(t) != 3:
-		raise "SYNError"
+		raise SYNError
 
 	lver = int(t[0])
 	total = int(t[1])
@@ -429,7 +449,7 @@ def cb_xfr(md, type, tid, params):
 			break
 	if not sbd:
 		debug('AIEEE: XFR without sbd!')
-		raise 'XFRError', (type, tid, params)
+		raise XFRError, (type, tid, params)
 
 	sbd.fd = fd
 	sbd.block = 0
diff --git a/msnlib.py b/msnlib.py
index 9d9952e..6e87856 100644
--- a/msnlib.py
+++ b/msnlib.py
@@ -40,6 +40,21 @@ reverse_status = {
 }
 
 
+# Possible exceptions
+class SocketError (Exception):
+	pass
+
+class VersionError (Exception):
+	pass
+
+class NSError (Exception):
+	pass
+
+class AuthError (Exception):
+	pass
+
+
+
 def debug(s):
 	sys.stderr.write('\r' + str(s) + '\n')
 	sys.stderr.flush()
@@ -280,7 +295,7 @@ class msnd:
 			c = fd.recv(1)
 
 		if c == '':
-			raise 'SocketError'
+			raise SocketError
 
 		buf = buf.strip()
 		pbuf = buf.split(' ')
@@ -457,7 +472,7 @@ class msnd:
 
 		r = self._recv()
 		if r[0] != 'VER' and r[2][0:4] != 'MSNP8':
-			raise 'VersionError', r
+			raise VersionError, r
 
 		# lie the version, just in case
 		self._send('CVR', '0x0409 win 4.10 i386 MSNMSGR 5.0.0544 MSMSGS ' + self.email)
@@ -468,7 +483,7 @@ class msnd:
 
 		r = self._recv()
 		if r[0] != 'XFR' and r[2][0:2] != 'NS':
-			raise 'NSError', r
+			raise NSError, r
 
 		# parse the notification server ip and port (as int)
 		ns = string.split(r[2])[1]
@@ -485,7 +500,7 @@ class msnd:
 		self._send('VER', 'MSNP8 CVR0')
 		r = self._recv()
 		if r[0] != 'VER' and r[2][0:4] != 'MSNP8':
-			raise 'VersionError', r
+			raise VersionError, r
 
 		# lie the version, just in case
 		self._send('CVR', '0x0409 win 4.10 i386 MSNMSGR 5.0.0544 MSMSGS	' + self.email)
@@ -496,7 +511,7 @@ class msnd:
 
 		r = self._recv()
 		if r[0] != 'USR':
-			raise 'AuthError', r
+			raise AuthError, r
 		hash = string.split(r[2])[2]
 
 		# get and use the passport id
@@ -505,7 +520,7 @@ class msnd:
 
 		r = self._recv()
 		if r[0] != 'USR' and r[2][0:2] != 'OK':
-			raise 'AuthError', r
+			raise AuthError, r
 		self.nick = string.split(r[2])[2]
 		self.nick = urllib.unquote(self.nick)
 
@@ -572,7 +587,7 @@ class msnd:
 			# for now we raise 911, which means authentication
 			# failed; but maybe we can get more detailed
 			# information
-			raise 'AuthError', [911, 'SSL Auth failed']
+			raise AuthError, [911, 'SSL Auth failed']
 
 		# and parse the headers to get the passport id
 		try:
@@ -606,7 +621,7 @@ class msnd:
 				r = nd.fd.getsockopt(socket.SOL_SOCKET,
 					socket.SO_ERROR)
 				if r != 0:
-					raise 'SocketError', 'ConnectFailed'
+					raise SocketError, 'Connect failed'
 				nd.fd.setblocking(1)
 				nd.block = 1
 				nd.state = 're'
diff --git a/utils/msnbot b/utils/msnbot
index cd20735..2f6f309 100755
--- a/utils/msnbot
+++ b/utils/msnbot
@@ -111,7 +111,7 @@ while 1:
 	for i in fds[0] + fds[1]:       # see msnlib.msnd.pollable.__doc__
 		try:
 			m.read(i)
-		except ('SocketError', socket.error), err:
+		except (msnlib.SocketError, socket.error), err:
 			if i != m:
 				# user closed a connection
 				# note that messages can be lost here
diff --git a/utils/msncd b/utils/msncd
index 82a7e7e..7aabf56 100755
--- a/utils/msncd
+++ b/utils/msncd
@@ -161,7 +161,7 @@ def login(email, password):
 	try:
 		m.login()
 		printl('done\n', c.green, 1)
-	except 'AuthError', info:
+	except msnlib.AuthError, info:
 		errno = int(info[0])
 		if not msncb.error_table.has_key(errno):
 			desc = 'Unknown'
@@ -171,7 +171,7 @@ def login(email, password):
 		quit(1)
 	except KeyboardInterrupt:
 		quit()
-	except ('SocketError', socket.error), info:
+	except (msnlib.SocketError, socket.error), info:
 		perror('Network error: ' + str(info) + '\n')
 		quit(1)
 	except:
@@ -216,7 +216,7 @@ def pipe_read(c):
 	except:
 		psend(c, 'ERR EINVAL\n')
 		return
-	
+
 	if cmd == 'LOGIN':
 		if len(params) != 2:
 			psend(c, 'ERR PARAMS\n')
@@ -227,7 +227,7 @@ def pipe_read(c):
 			m.pwd = pwd
 			m.login()
 			m.sync()
-		except 'AuthError', info:
+		except msnlib.AuthError, info:
 			errno = int(info[0])
 			if not msncb.error_table.has_key(errno):
 				desc = 'Unknown'
@@ -235,7 +235,7 @@ def pipe_read(c):
 				desc = msncb.error_table[errno]
 			psend(c, 'ERR MSN %d %s\n' % (errno, desc))
 			return
-		except ('SocketError', socket.error), info:
+		except (msnlib.SocketError, socket.error), info:
 			psend(c, 'ERR SOCK %s\n' % str(info))
 			return
 		psend(c, 'OK\n')
@@ -401,7 +401,7 @@ while 1:
 		else:
 			try:
 				m.read(i)
-			except ('SocketError', socket.error), err:
+			except (msnlib.SocketError, socket.error), err:
 				if i != m:
 					# user closed a connection
 					# note that messages can be
diff --git a/utils/msnsbot b/utils/msnsbot
index 8e790a1..aaf839f 100755
--- a/utils/msnsbot
+++ b/utils/msnsbot
@@ -136,7 +136,7 @@ class bot:
 			for i in fds[0] + fds[1]:
 				try:
 					self.m.read(i)
-				except ('SocketError', socket.error), err:
+				except (msnlib.SocketError, socket.error), err:
 					traceback.print_last()
 					if i != self.m:
 						# the user closed a connection
diff --git a/utils/msntk b/utils/msntk
index cd6bb36..dd15e4c 100755
--- a/utils/msntk
+++ b/utils/msntk
@@ -423,7 +423,7 @@ root.update()
 try:
 	m.login()
 	m.sync()
-except 'AuthError':
+except msnlib.AuthError:
 	tkMessageBox.showerror("Login", "Error logging in: wrong password")
 	quit()
 
@@ -449,7 +449,7 @@ while 1:
 	for i in fds[0] + fds[1]:
 		try:
 			m.read(i)
-		except ('SocketError', socket.error), err:
+		except (msnlib.SocketError, socket.error), err:
 			if i != m:
 				m.close(i)
 			else: