git @ Cat's Eye Technologies
Make `bits` an instance attribute of BytesIO. Chris Pressey 7 months ago
1 changed file(s) with 13 addition(s) and 10 deletion(s).
 324 324 325 325 326 326 class BytesIO(NhohnhehrIO): 327 def units_in(self, bits=[[]]): 327 def __init__(self): 328 self.bits = [[]] 329 330 def units_in(self): 328 331 # get data if necessary 329 if bits[0] == []: 332 if self.bits[0] == []: 330 333 i = sys.stdin.read(1) 331 334 if (i == ''): 332 335 raise IOError() # eof 333 336 else: 334 bits[0] = [int(bool(ord(i) & (1 << b))) for b in range(7, -1, -1)] 337 self.bits[0] = [int(bool(ord(i) & (1 << b))) for b in range(7, -1, -1)] 335 338 336 339 # return data 337 bit = bits[0][0] 338 bits[0] = bits[0][1:] 340 bit = self.bits[0][0] 341 self.bits[0] = self.bits[0][1:] 339 342 return bit 340 343 341 def units_out(self, bit, bits=[[]]): 342 bits[0].append(bit) 344 def units_out(self, bit): 345 self.bits[0].append(bit) 343 346 344 347 # if we have 8 bits, output 345 if len(bits[0]) == 8: 346 sys.stdout.write(chr(sum(bits[0][7 - b] << b for b in range(7, -1, -1)))) 348 if len(self.bits[0]) == 8: 349 sys.stdout.write(chr(sum(self.bits[0][7 - b] << b for b in range(7, -1, -1)))) 347 350 sys.stdout.flush() 348 bits[0] = [] 351 self.bits[0] = [] 349 352 350 353 351 354 def main(argv):