Exemplo n.º 1
0
def open_bz2file_as_stream(space,
                           w_path,
                           mode="r",
                           buffering=-1,
                           compresslevel=9):
    from pypy.rlib.streamio import decode_mode, open_path_helper
    from pypy.rlib.streamio import construct_stream_tower
    os_flags, universal, reading, writing, basemode, binary = decode_mode(mode)
    if reading and writing:
        raise OperationError(space.w_ValueError,
                             space.wrap("cannot open in read-write mode"))
    if basemode == "a":
        raise OperationError(space.w_ValueError,
                             space.wrap("cannot append to bz2 file"))
    stream = open_path_helper(space.str0_w(w_path), os_flags, False)
    if reading:
        bz2stream = ReadBZ2Filter(space, stream, buffering)
        buffering = 0  # by construction, the ReadBZ2Filter acts like
        # a read buffer too - no need for another one
    else:
        assert writing
        bz2stream = WriteBZ2Filter(space, stream, compresslevel)
    stream = construct_stream_tower(bz2stream, buffering, universal, reading,
                                    writing, binary)
    return stream
Exemplo n.º 2
0
def open_file_as_stream(space, path, mode="r", buffering=-1, compresslevel=9):
    from pypy.rlib.streamio import decode_mode, open_path_helper
    from pypy.rlib.streamio import construct_stream_tower
    from pypy.module._file.interp_file import wrap_oserror_as_ioerror, W_Stream
    from pypy.module._file.interp_file import is_mode_ok
    is_mode_ok(space, mode)
    os_flags, universal, reading, writing, basemode = decode_mode(mode)
    if reading and writing:
        raise OperationError(space.w_ValueError,
                             space.wrap("cannot open in read-write mode"))
    if basemode == "a":
        raise OperationError(space.w_ValueError,
                             space.wrap("cannot append to bz2 file"))
    try:
        stream = open_path_helper(path, os_flags, False)
    except OSError, exc:
        raise wrap_oserror_as_ioerror(space, exc)
Exemplo n.º 3
0
def open_bz2file_as_stream(space, w_path, mode="r", buffering=-1, compresslevel=9):
    from pypy.rlib.streamio import decode_mode, open_path_helper
    from pypy.rlib.streamio import construct_stream_tower

    os_flags, universal, reading, writing, basemode, binary = decode_mode(mode)
    if reading and writing:
        raise OperationError(space.w_ValueError, space.wrap("cannot open in read-write mode"))
    if basemode == "a":
        raise OperationError(space.w_ValueError, space.wrap("cannot append to bz2 file"))
    stream = open_path_helper(space.str0_w(w_path), os_flags, False)
    if reading:
        bz2stream = ReadBZ2Filter(space, stream, buffering)
        buffering = 0  # by construction, the ReadBZ2Filter acts like
        # a read buffer too - no need for another one
    else:
        assert writing
        bz2stream = WriteBZ2Filter(space, stream, compresslevel)
    stream = construct_stream_tower(bz2stream, buffering, universal, reading, writing, binary)
    return stream