Commits

Anonymous committed 5bb8e51

[fix] unihist of empty file or with --only-combining in a file without combining characters raises exception

Comments (0)

Files changed (2)

 def die(fmt, *args):
     print >> sys.stderr, fmt % args
     sys.exit(1)
+
+def _safe_op(op, collection, fallback):
+    try:
+        return op(collection)
+    except ValueError:
+        return fallback
+
+def safe_max(collection, fallback = 0):
+    return _safe_op(max, collection, fallback)
+
+def safe_min(collection, fallback = int(2 ** 31 - 1)):
+    return _safe_op(min, collection, fallback)

src/unihistext.py

         d['hexs'] = " ".join([ "0x%.6X" % ord(unichr) for unichr in d['unistr'] ]) # convert unistr to HEXes
     @staticmethod
     def stat(stats, totals):
-        totals['max_hexs_len'] = max(len(d['hexs']) for d in stats)
+        totals['max_hexs_len'] = safe_max(len(d['hexs']) for d in stats, 0)
 
 class UnistrFmt(Formatter):
     @staticmethod
             d['name'] = "<no base> " + d['name']
     @staticmethod
     def stat(stats, totals):
-        totals['max_name_len'] = max(len(d['name']) for d in stats)
+        totals['max_name_len'] = safe_max(len(d['name']) for d in stats, 0)
     @staticmethod
     def fmt(d, totals):
         return "%-*s" % (totals['max_name_len'] + 3, d['name'])