about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsterni <sternenseemann@systemli.org>2021-07-06 23:26:31 +0200
committerSören Tempel <soeren+git@soeren-tempel.net>2021-07-15 13:28:33 +0200
commite89e51d599d4d2e2e9893c2fbade762f991525f9 (patch)
treea97c9fc5e7a691c5720420b226345ab0df742138
parent7bbcd16197e2c1851bb506db7911cd2ab20173bb (diff)
pty.parse_extended_color: convert TypeError into AssertionError
A TypeError is raised in Color.__init__() to show that the color
constructed is invalid. In parse_extended_color() this is a parse
failure and should cause an AssertionError instead.

Take this opportunity to refactor the (parse) error handling in this
function to use a single try … except statement.
-rw-r--r--saneterm/pty.py75
1 files changed, 41 insertions, 34 deletions
diff --git a/saneterm/pty.py b/saneterm/pty.py
index 6940c6e..72e7fbd 100644
--- a/saneterm/pty.py
+++ b/saneterm/pty.py
@@ -264,50 +264,57 @@ def parse_extended_color(iterator):
     """
     args = list(iterator)
 
-    if len(args) == 0:
-        raise AssertionError("too few arguments")
-
-    if args[0] == '5':
-        # 256 color
-        assert len(args) == 2
+    try:
+        if args[0] == '5':
+            # 256 color
+            assert len(args) == 2
 
-        try:
             return Color(
                 ColorType.NUMBERED_256,
                 int(args[1])
             )
-        except ValueError:
-            raise AssertionError("unexpected non-integer")
-    elif args[0] == '2':
-        # truecolor
-        if len(args) == 4:
-            channels = tuple(args[1:4])
-        elif len(args) >= 5:
-            # TODO: handle color space id and tolerance values
-            channels = tuple(args[2:5])
-        else:
-            raise AssertionError("too few arguments")
+        elif args[0] == '2':
+            # truecolor
+            if len(args) == 4:
+                channels = tuple(args[1:4])
+            elif len(args) >= 5:
+                # TODO: handle color space id and tolerance values
+                channels = tuple(args[2:5])
+            else:
+                raise AssertionError("too few arguments")
 
-        try:
             return Color(
                 ColorType.TRUECOLOR,
                 tuple(int(c) for c in channels)
             )
-        except ValueError:
-            raise AssertionError("unexpected non-integer")
-    elif args[0] == '0':
-        # The standard specifies this as “implementation defined”,
-        # so we define this as color reset
-        return None
-    else:
-        # TODO: support
-        #
-        #   1   transparent
-        #   3   CMY
-        #   4   CMYK
-        #
-        # … but who needs these?
-        raise AssertionError("unsupported extended color")
+        elif args[0] == '0':
+            # The standard specifies this as “implementation defined”,
+            # so we define this as color reset
+            return None
+        else:
+            # TODO: support
+            #
+            #   1   transparent
+            #   3   CMY
+            #   4   CMYK
+            #
+            # … but who needs these?
+            raise AssertionError("unsupported extended color")
+
+    # convert a few exceptions that can happen while parsing
+    # into AssertionErrors to indicate that they are
+    # “expected” parse failures
+    except IndexError:
+        # args[0] out of range
+        raise AssertionError("too few arguments")
+
+    except TypeError:
+        # raised in Color.__init__()
+        raise AssertionError("malformed color")
+
+    except ValueError:
+        # raised by usage of int()
+        raise AssertionError("unexpected non-integer")
 
 
 def parse_sgr_sequence(params, special_evs):