def make_stream(name,
bin_file,
mode='r',
buffering=-1,
encoding=None,
errors=None,
newline='',
line_buffering=False,
**kwargs):
"""Take a Python 2.x binary file and return an IO Stream.
"""
reading = 'r' in mode
writing = 'w' in mode
appending = 'a' in mode
binary = 'b' in mode
if '+' in mode:
reading = True
writing = True
encoding = None if binary else (encoding or 'utf-8')
io_object = RawWrapper(bin_file, mode=mode, name=name)
if buffering >= 0:
if reading and writing:
io_object = io.BufferedRandom(
io_object,
buffering or io.DEFAULT_BUFFER_SIZE
)
elif reading:
io_object = io.BufferedReader(
io_object,
buffering or io.DEFAULT_BUFFER_SIZE
)
elif writing or appending:
io_object = io.BufferedWriter(
io_object,
buffering or io.DEFAULT_BUFFER_SIZE
)
if not binary:
io_object = io.TextIOWrapper(
io_object,
encoding=encoding,
errors=errors,
newline=newline,
line_buffering=line_buffering,
)
return io_object
评论列表
文章目录