freemyipod r341 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r340‎ | r341 | r342 >
Date:21:56, 19 December 2010
Author:farthen
Status:new
Tags:
Comment:
libembios/embios.py: Improve the error handling
Modified paths:
  • /embios/trunk/tools/embios.py (modified) (history)
  • /embios/trunk/tools/libembios.py (modified) (history)

Diff [purge]

Index: embios/trunk/tools/embios.py
@@ -177,7 +177,7 @@
178178 create a function with the name of it in this class and decorate
179179 it with the decorator @command. If you don't want to call the desired
180180 function (wrong arguments etc) just raise ArgumentError with or
181 - without an error message or raise ArgumentCountError
 181+ without an error message.
182182 """
183183 def __init__(self):
184184 self.logger = Logger()
@@ -195,9 +195,9 @@
196196 if func in self.cmddict:
197197 try:
198198 self.cmddict[func](*args)
199 - except ArgumentError, e:
 199+ except (ArgumentError, libembios.ArgumentError), e:
200200 usage(e, specific=func)
201 - except ArgumentError:
 201+ except (ArgumentError, libembios.ArgumentError):
202202 usage("Syntax Error in function '" + func + "'", specific=func)
203203 except ArgumentTypeError, e:
204204 usage(e, specific=func)
@@ -205,15 +205,14 @@
206206 self.logger.error("This function is not implemented yet!")
207207 except libembios.DeviceError, e:
208208 self.logger.error(str(e))
209 - except ValueError:
210 - usage(specific=func)
211209 except TypeError, e:
 210+ # Only act on TypeErrors for the function we called, not on TypeErrors raised by another function.
212211 if str(e).split(" ", 1)[0] == func + "()":
213212 self.logger.error(usage("Argument Error in '" + func + "': Wrong argument count", specific=func))
214213 else:
215214 raise
216215 except libembios.usb.core.USBError:
217 - self.logger.error("Problem with USB connection.")
 216+ self.logger.error("There is a problem with the USB connection.")
218217 else:
219218 usage("No such command")
220219
Index: embios/trunk/tools/libembios.py
@@ -229,7 +229,7 @@
230230 """ Writes data to an i2c slave """
231231 size = len(data)
232232 if size > 256 or size < 1:
233 - raise ValueError("Size must be a number between 1 and 256")
 233+ raise ArgumentError("Size must be a number between 1 and 256")
234234 if size == 256:
235235 size = 0
236236 return self.lib.monitorcommand(struct.pack("IBBBBII%ds" % size, 9, index, slaveaddr, startaddr, size, 0, 0, data), "III", (None, None, None))
@@ -371,15 +371,15 @@
372372 elif threadtype == "system":
373373 threadtype = 1
374374 else:
375 - raise ValueError("Threadtype must be either 'system' or 'user'")
 375+ raise ArgumentError("Threadtype must be either 'system' or 'user'")
376376 if priority > 256 or priority < 0:
377 - raise ValueError("Priority must be a number between 0 and 256")
 377+ raise ArgumentError("Priority must be a number between 0 and 256")
378378 if state == "ready":
379379 state = 0
380380 elif state == "suspended":
381381 state = 1
382382 else:
383 - raise ValueError("State must be either 'ready' or 'suspended'")
 383+ raise ArgumentError("State must be either 'ready' or 'suspended'")
384384 resp = self.lib.monitorcommand(struct.pack("IIIIIIII", 19, nameptr, entrypoint, stackptr, stacksize, threadtype, priority, state), "III", (id, None, None))
385385 if resp.id < 0:
386386 raise DeviceError("The device returned the error code "+str(resp.id))
@@ -398,10 +398,10 @@
399399 try:
400400 appheader = struct.unpack("<8sIIIIIIIIII", app[:48])
401401 except struct.error:
402 - raise ArgumentError("The specified file is not an emBIOS application")
 402+ raise ArgumentError("The specified app is not an emBIOS application")
403403 header = appheader[0]
404404 if header != "emBIexec":
405 - raise ArgumentError("The specified file is not an emBIOS application")
 405+ raise ArgumentError("The specified app is not an emBIOS application")
406406 baseaddr = appheader[2]
407407 threadnameptr = appheader[8]
408408 nameptr = threadnameptr - baseaddr
@@ -412,7 +412,7 @@
413413 if ord(char) == 0:
414414 break
415415 except TypeError:
416 - raise ArgumentError("The specified file is not an emBIOS application")
 416+ raise ArgumentError("The specified app is not an emBIOS application")
417417 name += char
418418 nameptr += 1
419419 usermem = self.getusermemrange()