@@ -19,17 +19,17 @@ def _read_16_bit(fh: BinaryIO) -> int:
1919class Node :
2020 __slots__ = ("children" , "is_leaf" , "symbol" )
2121
22- def __init__ (self , symbol : Symbol | None = None , is_leaf : bool = False ):
22+ def __init__ (self , symbol : int = 0 , is_leaf : bool = False ):
2323 self .symbol = symbol
2424 self .is_leaf = is_leaf
25- self .children = [None , None ]
25+ self .children : list [ Node | None ] = [None , None ]
2626
2727
2828def _add_leaf (nodes : list [Node ], idx : int , mask : int , bits : int ) -> int :
2929 node = nodes [0 ]
3030 i = idx + 1
3131
32- while bits > 1 :
32+ while node and bits > 1 :
3333 bits -= 1
3434 childidx = (mask >> bits ) & 1
3535 if node .children [childidx ] is None :
@@ -38,6 +38,7 @@ def _add_leaf(nodes: list[Node], idx: int, mask: int, bits: int) -> int:
3838 i += 1
3939 node = node .children [childidx ]
4040
41+ assert node
4142 node .children [mask & 1 ] = nodes [idx ]
4243 return i
4344
@@ -84,8 +85,9 @@ def _build_tree(buf: bytes) -> Node:
8485
8586
8687class BitString :
88+ source : BinaryIO
89+
8790 def __init__ (self ):
88- self .source = None
8991 self .mask = 0
9092 self .bits = 0
9193
@@ -114,16 +116,18 @@ def skip(self, n: int) -> None:
114116 self .mask += _read_16_bit (self .source ) << (16 - self .bits )
115117 self .bits += 16
116118
117- def decode (self , root : Node ) -> Symbol :
119+ def decode (self , root : Node ) -> int :
118120 node = root
119- while not node .is_leaf :
121+ while node and not node .is_leaf :
120122 bit = self .lookup (1 )
121123 self .skip (1 )
122124 node = node .children [bit ]
125+
126+ assert node
123127 return node .symbol
124128
125129
126- def decompress (src : bytes | BinaryIO ) -> bytes :
130+ def decompress (src : bytes | bytearray | memoryview | BinaryIO ) -> bytes :
127131 """LZXPRESS decompress from a file-like object or bytes.
128132
129133 Decompresses until EOF of the input data.
@@ -134,7 +138,7 @@ def decompress(src: bytes | BinaryIO) -> bytes:
134138 Returns:
135139 The decompressed data.
136140 """
137- if not hasattr (src , "read" ):
141+ if isinstance (src , ( bytes , bytearray , memoryview ) ):
138142 src = io .BytesIO (src )
139143
140144 dst = bytearray ()
0 commit comments