2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
def dupfile(f, mode=None, buffering=0, raising=False):
|
2007-01-25 17:46:16 +08:00
|
|
|
""" return a new open file object that's a duplicate of f
|
|
|
|
|
2007-02-01 22:35:07 +08:00
|
|
|
mode is duplicated if not given, 'buffering' controls
|
|
|
|
buffer size (defaulting to no buffering) and 'raising'
|
2007-01-25 17:46:16 +08:00
|
|
|
defines whether an exception is raised when an incompatible
|
|
|
|
file object is passed in (if raising is False, the file
|
|
|
|
object itself will be returned)
|
|
|
|
"""
|
2007-01-24 22:24:01 +08:00
|
|
|
try:
|
|
|
|
fd = f.fileno()
|
|
|
|
except AttributeError:
|
|
|
|
if raising:
|
|
|
|
raise
|
|
|
|
return f
|
|
|
|
newfd = os.dup(fd)
|
|
|
|
mode = mode and mode or f.mode
|
|
|
|
return os.fdopen(newfd, mode, buffering)
|
2007-01-25 17:46:16 +08:00
|
|
|
|