本文整理匯總了Python中os.dup方法的典型用法代碼示例。如果您正苦於以下問題:Python os.dup方法的具體用法?Python os.dup怎麽用?Python os.dup使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類os
的用法示例。
在下文中一共展示了os.dup方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: discard_stderr
# 需要導入模塊: import os [as 別名]
# 或者: from os import dup [as 別名]
def discard_stderr():
"""
Discards error output of a routine if invoked as:
with discard_stderr():
...
"""
with open(os.devnull, 'w') as bit_bucket:
try:
stderr_fileno = sys.stderr.fileno()
old_stderr = os.dup(stderr_fileno)
try:
os.dup2(bit_bucket.fileno(), stderr_fileno)
yield
finally:
os.dup2(old_stderr, stderr_fileno)
except AttributeError:
# On some systems is stderr not a file descriptor but actually a virtual pipeline
# that can not be copied
yield
示例2: __init__
# 需要導入模塊: import os [as 別名]
# 或者: from os import dup [as 別名]
def __init__(self, targetfd, tmpfile=None, now=True, patchsys=False):
""" save targetfd descriptor, and open a new
temporary file there. If no tmpfile is
specified a tempfile.Tempfile() will be opened
in text mode.
"""
self.targetfd = targetfd
if tmpfile is None and targetfd != 0:
f = tempfile.TemporaryFile('wb+')
tmpfile = dupfile(f, encoding="UTF-8")
f.close()
self.tmpfile = tmpfile
self._savefd = os.dup(self.targetfd)
if patchsys:
self._oldsys = getattr(sys, patchsysdict[targetfd])
if now:
self.start()
示例3: clear_scene_and_import_fbx
# 需要導入模塊: import os [as 別名]
# 或者: from os import dup [as 別名]
def clear_scene_and_import_fbx(filepath):
"""
Clear the whole scene and import fbx file into the empty scene.
:param filepath: filepath for fbx file
"""
# redirect blender output info
logfile = 'blender_render.log'
open(logfile, 'w').close()
old = os.dup(1)
sys.stdout.flush()
os.close(1)
os.open(logfile, os.O_WRONLY)
bpy.ops.wm.read_homefile(filepath=HOME_FILE_PATH)
bpy.ops.import_scene.fbx(filepath=filepath)
os.close(1)
os.dup(old)
os.close(old)
示例4: render_without_output
# 需要導入模塊: import os [as 別名]
# 或者: from os import dup [as 別名]
def render_without_output(use_antialiasing=True):
# redirect output to log file
logfile = 'blender_render.log'
open(logfile, 'a').close()
old = os.dup(1)
sys.stdout.flush()
os.close(1)
os.open(logfile, os.O_WRONLY)
# Render
bpy.context.scene.render.use_antialiasing = use_antialiasing
bpy.ops.render.render(write_still=True)
# disable output redirection
os.close(1)
os.dup(old)
os.close(old)
# Creating a lamp with an appropriate energy
示例5: stdchannel_redirected
# 需要導入模塊: import os [as 別名]
# 或者: from os import dup [as 別名]
def stdchannel_redirected(stdchannel, dest_filename):
"""
A context manager to temporarily redirect stdout or stderr
e.g.:
with stdchannel_redirected(sys.stderr, os.devnull):
if compiler.has_function('clock_gettime', libraries=['rt']):
libraries.append('rt')
"""
try:
oldstdchannel = os.dup(stdchannel.fileno())
dest_file = open(dest_filename, 'w')
os.dup2(dest_file.fileno(), stdchannel.fileno())
yield
finally:
if oldstdchannel is not None:
os.dup2(oldstdchannel, stdchannel.fileno())
if dest_file is not None:
dest_file.close()
示例6: callSolver
# 需要導入模塊: import os [as 別名]
# 或者: from os import dup [as 別名]
def callSolver(self, lp, callback = None):
"""Solves the problem with yaposib
"""
if self.msg == 0:
#close stdout to get rid of messages
tempfile = open(mktemp(),'w')
savestdout = os.dup(1)
os.close(1)
if os.dup(tempfile.fileno()) != 1:
raise PulpSolverError("couldn't redirect stdout - dup() error")
self.solveTime = -clock()
lp.solverModel.solve(self.mip)
self.solveTime += clock()
if self.msg == 0:
#reopen stdout
os.close(1)
os.dup(savestdout)
os.close(savestdout)
示例7: pytest_addoption
# 需要導入模塊: import os [as 別名]
# 或者: from os import dup [as 別名]
def pytest_addoption(parser):
group = parser.getgroup("general")
group._addoption(
"--capture",
action="store",
default="fd" if hasattr(os, "dup") else "sys",
metavar="method",
choices=["fd", "sys", "no"],
help="per-test capturing method: one of fd|sys|no.",
)
group._addoption(
"-s",
action="store_const",
const="no",
dest="capture",
help="shortcut for --capture=no.",
)
示例8: safe_text_dupfile
# 需要導入模塊: import os [as 別名]
# 或者: from os import dup [as 別名]
def safe_text_dupfile(f, mode, default_encoding="UTF8"):
""" return an open text file object that's a duplicate of f on the
FD-level if possible.
"""
encoding = getattr(f, "encoding", None)
try:
fd = f.fileno()
except Exception:
if "b" not in getattr(f, "mode", "") and hasattr(f, "encoding"):
# we seem to have a text stream, let's just use it
return f
else:
newfd = os.dup(fd)
if "b" not in mode:
mode += "b"
f = os.fdopen(newfd, mode, 0) # no buffering
return EncodedFile(f, encoding or default_encoding)
示例9: __init__
# 需要導入模塊: import os [as 別名]
# 或者: from os import dup [as 別名]
def __init__(self, targetfd, tmpfile=None):
self.targetfd = targetfd
try:
self.targetfd_save = os.dup(self.targetfd)
except OSError:
self.start = lambda: None
self.done = lambda: None
else:
if targetfd == 0:
assert not tmpfile, "cannot set tmpfile with stdin"
tmpfile = open(os.devnull, "r")
self.syscapture = SysCapture(targetfd)
else:
if tmpfile is None:
f = TemporaryFile()
with f:
tmpfile = safe_text_dupfile(f, mode="wb+")
if targetfd in patchsysdict:
self.syscapture = SysCapture(targetfd, tmpfile)
else:
self.syscapture = NoCapture()
self.tmpfile = tmpfile
self.tmpfile_fd = tmpfile.fileno()
示例10: Pipe
# 需要導入模塊: import os [as 別名]
# 或者: from os import dup [as 別名]
def Pipe(duplex=True):
'''
Returns pair of connection objects at either end of a pipe
'''
if duplex:
s1, s2 = socket.socketpair()
s1.setblocking(True)
s2.setblocking(True)
c1 = _multiprocessing.Connection(os.dup(s1.fileno()))
c2 = _multiprocessing.Connection(os.dup(s2.fileno()))
s1.close()
s2.close()
else:
fd1, fd2 = os.pipe()
c1 = _multiprocessing.Connection(fd1, writable=False)
c2 = _multiprocessing.Connection(fd2, readable=False)
return c1, c2
示例11: test_closerange
# 需要導入模塊: import os [as 別名]
# 或者: from os import dup [as 別名]
def test_closerange(self):
first = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR)
# We must allocate two consecutive file descriptors, otherwise
# it will mess up other file descriptors (perhaps even the three
# standard ones).
second = os.dup(first)
try:
retries = 0
while second != first + 1:
os.close(first)
retries += 1
if retries > 10:
# XXX test skipped
self.skipTest("couldn't allocate two consecutive fds")
first, second = second, os.dup(second)
finally:
os.close(second)
# close a fd that is open, and one that isn't
os.closerange(first, first + 2)
self.assertRaises(OSError, os.write, first, "a")
示例12: html_save
# 需要導入模塊: import os [as 別名]
# 或者: from os import dup [as 別名]
def html_save(html):
'''
Save as HTML file and open in the browser
'''
hide = os.dup(1)
os.close(1)
os.open(os.devnull, os.O_RDWR)
try:
s = Template(open('%s/template.html' % sys.path[0], 'r').read())
text_file = open(args.output, "wb")
text_file.write(s.substitute(content=html).encode('utf8'))
text_file.close()
print("URL to access output: file://%s" % os.path.abspath(args.output))
file = "file:///%s" % os.path.abspath(args.output)
if sys.platform == 'linux' or sys.platform == 'linux2':
subprocess.call(["xdg-open", file])
else:
webbrowser.open(file)
except Exception as e:
print("Output can't be saved in %s \
due to exception: %s" % (args.output, e))
finally:
os.dup2(hide, 1)
示例13: __enter__
# 需要導入模塊: import os [as 別名]
# 或者: from os import dup [as 別名]
def __enter__(self):
sys.stdout.flush()
self._contents = None
self.old_stdout_fileno = sys.stdout.fileno()
self.bak_stdout_fd = os.dup(self.old_stdout_fileno)
self.ftmp = tempfile.NamedTemporaryFile(dir=param.TMPDIR)
os.dup2(self.ftmp.file.fileno(), self.old_stdout_fileno)
return self
示例14: dupfile
# 需要導入模塊: import os [as 別名]
# 或者: from os import dup [as 別名]
def dupfile(f, mode=None, buffering=0, raising=False, encoding=None):
""" return a new open file object that's a duplicate of f
mode is duplicated if not given, 'buffering' controls
buffer size (defaulting to no buffering) and 'raising'
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)
"""
try:
fd = f.fileno()
mode = mode or f.mode
except AttributeError:
if raising:
raise
return f
newfd = os.dup(fd)
if sys.version_info >= (3,0):
if encoding is not None:
mode = mode.replace("b", "")
buffering = True
return os.fdopen(newfd, mode, buffering, encoding, closefd=True)
else:
f = os.fdopen(newfd, mode, buffering)
if encoding is not None:
return EncodedFile(f, encoding)
return f
示例15: __init__
# 需要導入模塊: import os [as 別名]
# 或者: from os import dup [as 別名]
def __init__(self):
# Open a pair of null files
self.null_fds = [os.open(os.devnull, os.O_RDWR) for _ in range(2)]
# Save the actual stdout (1) and stderr (2) file descriptors.
self.save_fds = (os.dup(1), os.dup(2))