Exemple #1
0
def test_read_write_i64():
    stream = BytesIO()
    write_i64(stream, 10)
    assert stream.tell() == 8
    stream.seek(0)
    assert read_i64(stream) == 10
    write_i64(stream, 11, 12, 13)
    assert stream.tell() == 32
    stream.seek(8)
    assert read_i64(stream, 2) == (11, 12)
    assert read_i64(stream, 1) == (13,)
    assert read_i64(stream, 0) == ()
Exemple #2
0
 def _read_index(self):
     try:
         self._handle.seek(-8, os.SEEK_END)
         self._handle.seek(read_i64(self._handle))
         self._index = read_json(self._handle)
     except:
         raise IOError('unable to read index')
Exemple #3
0
 def _try_read_and_verify_version(cls, byte_stream):
     if byte_stream.read(8) != FORMAT_STRING:
         raise IOError('unrecognized file format')
     try:
         version = read_i64(byte_stream)
     except:
         raise IOError('unable to read file version')
     if version != FORMAT_VERSION:  # this could be later relaxed to a range of versions
         raise IOError('incompatible version: {} (expected {})'.format(version, FORMAT_VERSION))
     return version
Exemple #4
0
def test_read_write_json(data):
    stream = BytesIO()
    total = write_json(stream, data)
    assert stream.tell() == total
    stream.seek(0)
    length = read_i64(stream)
    assert stream.tell() == total - length
    assert json.loads(stream.read(length).decode('utf-8')) == data
    stream.seek(0)
    assert read_json(stream) == data