docs: def _write_bits(cls, array):
          """Generator that fills a `bytearray` with provided bits. After
          creating the generator, call `send(None)` to initialise it, and
          thereafter call `send((value, bits))` to add that many bits into
          the array. You must call `close()` to flush any partial bytes."""
          pos = 0
          current = 0    # bits not yet written into array
          current_bits = 0
          try:
              while True:
                  (value, bits) = yield
                  if value < 0 or value >= (1 << bits):
                      raise ValueError('Value is out of range for number of bits')
                  current = (current << bits) | value
                  current_bits += bits
                  while current_bits >= 8:
                      array[pos] = current >> (current_bits - 8)
                      current &= (1 << (current_bits - 8)) - 1
                      current_bits -= 8
                      pos += 1
          except GeneratorExit:
              if current_bits > 0:
                  current <<= (8 - current_bits)
                  array[pos] = current