(state_file)
return session
Read state from a compressed pickle.
Read state from a compressed pickle.
Read state from a compressed pickle .
def load_session(state_file):
    """Read state from a compressed pickle."""
    with open(state_file, 'rb') as in_file:
        header = in_file.read(struct.calcsize(HEADER_FORMAT))
        blob = in_file.read()
    # mask checksum to deal with different signs on Py2/Py3
    # see https://docs.python.org/3.5/library/zlib.html#zlib.crc32
    checksum = zlib.crc32(blob) & 0xffffffff
    try:
        header_dict = dict(zip(HEADER_KEYS, struct.unpack(HEADER_FORMAT, header)))
    except struct.error:
        raise ValueError('session file header corrupted')
    # check blob integrity
    if checksum != header_dict['checksum']:
        raise ValueError('session file corrupted')
    if (
            HEADER['python_major'] != header_dict['python_major']
            or HEADER['python_minor'] != header_dict['python_minor']
        ):
        raise ValueError('session file stored with different Python version')
    if (
            HEADER['pcbasic_major'] != header_dict['pcbasic_major']
            or HEADER['pcbasic_minor'] != header_dict['pcbasic_minor']
        ):
        raise ValueError('session file stored with different PC-BASIC version')
    session = pickle.loads(zlib.decompress(blob))
    return session
def load_session ( state_file ) : with open ( state_file , 'rb' ) as in_file : header = in_file . read ( struct . calcsize ( HEADER_FORMAT ) ) blob = in_file . read ( ) # mask checksum to deal with different signs on Py2/Py3 # see https://docs.python.org/3.5/library/zlib.html#zlib.crc32 checksum = zlib . crc32 ( blob ) & 0xffffffff try : header_dict = dict ( zip ( HEADER_KEYS , struct . unpack ( HEADER_FORMAT , header ) ) ) except struct . error : raise ValueError ( 'session file header corrupted' ) # check blob integrity if checksum != header_dict [ 'checksum' ] : raise ValueError ( 'session file corrupted' ) if ( HEADER [ 'python_major' ] != header_dict [ 'python_major' ] or HEADER [ 'python_minor' ] != header_dict [ 'python_minor' ] ) : raise ValueError ( 'session file stored with different Python version' ) if ( HEADER [ 'pcbasic_major' ] != header_dict [ 'pcbasic_major' ] or HEADER [ 'pcbasic_minor' ] != header_dict [ 'pcbasic_minor' ] ) : raise ValueError ( 'session file stored with different PC-BASIC version' ) session = pickle . loads ( zlib . decompress ( blob ) ) return session