freemyipod r584 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r583‎ | r584 | r585 >
Date:13:29, 12 February 2011
Author:theseven
Status:new
Tags:
Comment:
misc.py: Fix to_int and to_bool, don't globally modify core objects and provide better error details
Modified paths:
  • /emcore/trunk/tools/misc.py (modified) (history)

Diff [purge]

Index: emcore/trunk/tools/misc.py
@@ -347,18 +347,16 @@
348348 """
349349 try: long
350350 except NameError: long = int
351 - if type(something) == bool:
 351+ if type(something).__name__ in ("bool", "NoneType"):
352352 return something
353 - if something is None:
354 - return False
355 - elif type(something) == int or type(something) == long:
356 - return bool(something)
357 - elif type(something == str):
 353+ elif type(something).__name__ in ("int", "long"):
 354+ return something != 0
 355+ elif type(something) == str:
358356 if something.lower() in ['true', '1', 't', 'y', 'yes']:
359357 return True
360358 elif something.lower() in ['false', '0', 'f', 'n', 'no']:
361359 return False
362 - raise ArgumentTypeError("bool", "'%s'" % something)
 360+ raise ArgumentTypeError("bool", "'%s' (%s)" % (something, type(something).__name__))
363361
364362 def to_int(something):
365363 """
@@ -366,12 +364,8 @@
367365 This works for default arguments too, because it returns
368366 None when it found that it got a NoneType object.
369367 """
370 - try: long
371 - except NameError: long = int
372 - if type(something) == int or type(something) == long:
 368+ if type(something).__name__ in ("int", "long", "NoneType"):
373369 return something
374 - elif something is None:
375 - return None
376370 elif type(something) == str:
377371 try:
378372 if something[:2] == "0x": # Hexadecimal notation
@@ -381,9 +375,9 @@
382376 else: # Decimal notation
383377 return int(something, 10)
384378 except ValueError:
385 - raise ArgumentTypeError("integer", "'%s'" % something)
 379+ raise ArgumentTypeError("integer", "'%s' (%s)" % (something, type(something).__name__))
386380 else:
387 - raise ArgumentTypeError("integer", "'%s'" % something)
 381+ raise ArgumentTypeError("integer", "'%s' (%s)" % (something, type(something).__name__))
388382
389383
390384 def majorver():