def check_reader(unicode_string, encoding, n=1000): bytestr = unicode_string.encode(encoding) strlen = len(unicode_string) stream = BytesIO(bytestr) reader = SeekableUnicodeStreamReader(stream, encoding) # Find all character positions chars = [] while True: pos = reader.tell() chars.append( (pos, reader.read(1)) ) if chars[-1][1] == '': break # Find all strings strings = dict( (pos,'') for (pos,c) in chars ) for pos1, char in chars: for pos2, _ in chars: if pos2 <= pos1: strings[pos2] += char while True: op = random.choice('tsrr') # Check our position? if op == 't': # tell reader.tell() # Perform a seek? if op == 's': # seek new_pos = random.choice([p for (p,c) in chars]) reader.seek(new_pos) # Perform a read? if op == 'r': # read if random.random() < .3: pos = reader.tell() else: pos = None if random.random() < .2: size = None elif random.random() < .8: size = random.randint(0, int(strlen/6)) else: size = random.randint(0, strlen+20) if random.random() < .8: s = reader.read(size) else: s = reader.readline(size) # check that everything's consistent if pos is not None: assert pos in strings assert strings[pos].startswith(s) n -= 1 if n == 0: return 'passed'
def check_reader(unicode_string, encoding): bytestr = unicode_string.encode(encoding) stream = BytesIO(bytestr) reader = SeekableUnicodeStreamReader(stream, encoding) # Should open at the start of the file assert reader.tell() == 0 # Compare original string to contents from `.readlines()` assert unicode_string == "".join(reader.readlines()) # Should be at the end of the file now stream.seek(0, os.SEEK_END) assert reader.tell() == stream.tell() reader.seek(0) # go back to start # Compare original string to contents from `.read()` contents = "" char = None while char != "": char = reader.read(1) contents += char assert unicode_string == contents
def check_reader(unicode_string, encoding, n=1000): bytestr = unicode_string.encode(encoding) strlen = len(unicode_string) stream = BytesIO(bytestr) reader = SeekableUnicodeStreamReader(stream, encoding) # Find all character positions chars = [] while True: pos = reader.tell() chars.append((pos, reader.read(1))) if chars[-1][1] == '': break # Find all strings strings = dict((pos, '') for (pos, c) in chars) for pos1, char in chars: for pos2, _ in chars: if pos2 <= pos1: strings[pos2] += char while True: op = random.choice('tsrr') # Check our position? if op == 't': # tell reader.tell() # Perform a seek? if op == 's': # seek new_pos = random.choice([p for (p, c) in chars]) reader.seek(new_pos) # Perform a read? if op == 'r': # read if random.random() < 0.3: pos = reader.tell() else: pos = None if random.random() < 0.2: size = None elif random.random() < 0.8: size = random.randint(0, int(strlen / 6)) else: size = random.randint(0, strlen + 20) if random.random() < 0.8: s = reader.read(size) else: s = reader.readline(size) # check that everything's consistent if pos is not None: assert pos in strings assert strings[pos].startswith(s) n -= 1 if n == 0: return 'passed'
def test_reader_stream_is_closed(): reader = SeekableUnicodeStreamReader(BytesIO(b''), 'ascii') assert not reader.stream.closed reader.__del__() assert reader.stream.closed
def test_reader_stream_closes_when_deleted(): reader = SeekableUnicodeStreamReader(BytesIO(b""), "ascii") assert not reader.stream.closed reader.__del__() assert reader.stream.closed