I have a Python script that is using some closed-box Python functions (i.e. I can't edit these functions) provided by my employer. When I call these functions, they are printing output to my linux terminal that I would like to suppress. I've tried redirecting stdout / stderr via;
我有一个Python脚本,它使用我的雇主提供的一些闭箱Python函数(即我无法编辑这些函数)。当我调用这些函数时,它们正在打印输出到我想要压制的linux终端。我尝试过重定向stdout / stderr;
orig_out = sys.stdout
sys.stdout = StringIO()
rogue_function()
sys.stdout = orig_out
but this fails to catch the output. I think the functions I'm calling via-Python (rogue_function() from above) are really wrappers for compiled C-code, which are actually doing the printing.
但这无法捕捉到输出。我认为我通过Python调用的函数(上面的rogue_function())实际上是编译C代码的包装器,它实际上正在进行打印。
Does anyone know of a way I can do a "deep-capture" of any print handed to stdout / stderr by a function (and any sub-functions that function calls)?
有没有人知道我可以通过函数(以及函数调用的任何子函数)对stdout / stderr的任何打印进行“深度捕获”?
UPDATE:
I ended up taking the method outlined in the selected answer below and writing a context manager to supress stdout and stderr:
我最终采用了下面选定答案中概述的方法并编写了一个上下文管理器来压制stdout和stderr:
# Define a context manager to suppress stdout and stderr.
class suppress_stdout_stderr(object):
'''
A context manager for doing a "deep suppression" of stdout and stderr in
Python, i.e. will suppress all print, even if the print originates in a
compiled C/Fortran sub-function.
This will not suppress raised exceptions, since exceptions are printed
to stderr just before a script exits, and after the context manager has
exited (at least, I think that is why it lets exceptions through).
'''
def __init__(self):
# Open a pair of null files
self.null_fds = [os.open(os.devnull,os.O_RDWR) for x in range(2)]
# Save the actual stdout (1) and stderr (2) file descriptors.
self.save_fds = [os.dup(1), os.dup(2)]
def __enter__(self):
# Assign the null pointers to stdout and stderr.
os.dup2(self.null_fds[0],1)
os.dup2(self.null_fds[1],2)
def __exit__(self, *_):
# Re-assign the real stdout/stderr back to (1) and (2)
os.dup2(self.save_fds[0],1)
os.dup2(self.save_fds[1],2)
# Close all file descriptors
for fd in self.null_fds + self.save_fds:
os.close(fd)
To use this you just:
要使用它你只需:
with suppress_stdout_stderr():
rogue_function()
This works "pretty good". It does suppress the printout from the rogue functions that were cluttering up my script. I noticed in testing it that it lets through raised exceptions as well as some logger print, and I'm not entirely clear why. I think it has something to do with when these messages get sent to stdout / stderr (I think it happens after my context manager exits). If anyone can confirm this, I'd be interested in hearing the details ...
这工作“非常好”。它确实抑制了使我的脚本混乱的流氓功能的打印输出。我在测试时注意到它允许通过凸起的异常以及一些记录器打印,我并不完全清楚为什么。我认为这与将这些消息发送到stdout / stderr有关(我认为它发生在我的上下文管理器退出之后)。如果有人可以证实这一点,我有兴趣听取细节......
7 个解决方案
#1
5
This approach (found through the related sidebar) might work. It reassigns the file descriptors rather than just the wrappers to them in sys.stdout, etc.
这种方法(通过相关侧栏找到)可能有效。它在sys.stdout等中重新分配文件描述符而不仅仅是它们的包装器。
#2
1
Did you try to redirect stderr too? e.g.
您是否尝试重定向stderr?例如
sys.stdout = StringIO();
sys.stderr = StringIO();
foo(bar);
sys.stdout = sys.__stdout__; # These are provided by python
sys.stderr = sys.__stderr__;
Also using StringIO might use extra memory. You can use a dummy device instead (e.g. http://coreygoldberg.blogspot.com/2009/05/python-redirect-or-turn-off-stdout-and.html).
使用StringIO也可能使用额外的内存。您可以使用虚拟设备(例如http://coreygoldberg.blogspot.com/2009/05/python-redirect-or-turn-off-stdout-and.html)。
#3
1
My solution is similar to yours but uses contextlib
and is a little shorter and easier to understand (IMHO).
我的解决方案与您的解决方案类似但使用了contextlib,并且更短,更容易理解(恕我直言)。
import contextlib
@contextlib.contextmanager
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()
The context for why I created this is at this blog post. Similar to yours I think.
我创建此文章的背景是在这篇博客文章中。我认为与你的相似。
I use it like this in a setup.py
:
我在setup.py中使用它:
with stdchannel_redirected(sys.stderr, os.devnull):
if compiler.has_function('clock_gettime', libraries=['rt']):
libraries.append('rt')
#4
1
Not really requested by the OP, but I needed to hide and store the output, and did like follows:
OP并没有真正要求,但我需要隐藏和存储输出,并且如下所示:
from io import StringIO
import sys
class Hider:
def __init__(self, channels=('stdout',)):
self._stomach = StringIO()
self._orig = {ch : None for ch in channels}
def __enter__(self):
for ch in self._orig:
self._orig[ch] = getattr(sys, ch)
setattr(sys, ch, self)
return self
def write(self, string):
self._stomach.write(string)
def flush(self):
pass
def autopsy(self):
return self._stomach.getvalue()
def __exit__(self, *args):
for ch in self._orig:
setattr(sys, ch, self._orig[ch])
Usage:
with Hider() as h:
spammy_function()
result = h.autopsy()
(tested only with Python 3)
(仅使用Python 3测试)
EDIT: now allows to select stderr
, stdout
or both, as in Hider([stdout, stderr])
编辑:现在允许选择stderr,stdout或两者,如Hider([stdout,stderr])
#5
0
python 3.6 working version, tested with million suppressions without any errors
python 3.6工作版,经过百万次抑制测试,没有任何错误
import os
import sys
class suppress_stdout_stderr(object):
def __enter__(self):
self.outnull_file = open(os.devnull, 'w')
self.errnull_file = open(os.devnull, 'w')
self.old_stdout_fileno_undup = sys.stdout.fileno()
self.old_stderr_fileno_undup = sys.stderr.fileno()
self.old_stdout_fileno = os.dup ( sys.stdout.fileno() )
self.old_stderr_fileno = os.dup ( sys.stderr.fileno() )
self.old_stdout = sys.stdout
self.old_stderr = sys.stderr
os.dup2 ( self.outnull_file.fileno(), self.old_stdout_fileno_undup )
os.dup2 ( self.errnull_file.fileno(), self.old_stderr_fileno_undup )
sys.stdout = self.outnull_file
sys.stderr = self.errnull_file
return self
def __exit__(self, *_):
sys.stdout = self.old_stdout
sys.stderr = self.old_stderr
os.dup2 ( self.old_stdout_fileno, self.old_stdout_fileno_undup )
os.dup2 ( self.old_stderr_fileno, self.old_stderr_fileno_undup )
os.close ( self.old_stdout_fileno )
os.close ( self.old_stderr_fileno )
self.outnull_file.close()
self.errnull_file.close()
#6
0
As of python 3.5 we can do this with minimal work using built-ins in contextlib
, namely redirect_stdout
and redirect_stderr
. We only need to combine these two built-in context managers in a custom context manager of ours, which can be easily done using the nice pattern in Martijn's answer here. Redirecting both outputs to os.devnull
should be safe and portable enough.
从python 3.5开始,我们可以使用contextlib中的内置函数(即redirect_stdout和redirect_stderr)进行最少的工作。我们只需要在我们的自定义上下文管理器中组合这两个内置的上下文管理器,这可以使用Martijn的答案中的漂亮模式轻松完成。将两个输出重定向到os.devnull应该足够安全和便携。
from contextlib import contextmanager,redirect_stderr,redirect_stdout
from os import devnull
@contextmanager
def suppress_stdout_stderr():
"""A context manager that redirects stdout and stderr to devnull"""
with open(devnull, 'w') as fnull:
with redirect_stderr(fnull) as err, redirect_stdout(fnull) as out:
yield (err, out)
Note that suppressing stderr
will still give you full tracebacks when something breaks, which is a good thing:
请注意,当出现问题时,抑制stderr仍然会给你完整的回溯,这是一件好事:
import sys
def rogue_function():
print('spam to stdout')
print('important warning', file=sys.stderr)
1 + 'a'
return 42
with suppress_stdout_stderr():
rogue_function()
When run the above only prints
当运行上面只打印
Traceback (most recent call last):
File "tmp.py", line 20, in <module>
rogue_function()
File "foo.py", line 16, in rogue_function
1 + 'a'
TypeError: unsupported operand type(s) for +: 'int' and 'str'
to the terminal. Unhandled exceptions should never go unnoticed.
到终点站。未处理的异常永远不会被忽视。
#7
-2
If you are running this script on a linux based machine, you should be able to:
如果您在基于Linux的计算机上运行此脚本,您应该能够:
$> ./runscript.py > output.txt
#1
5
This approach (found through the related sidebar) might work. It reassigns the file descriptors rather than just the wrappers to them in sys.stdout, etc.
这种方法(通过相关侧栏找到)可能有效。它在sys.stdout等中重新分配文件描述符而不仅仅是它们的包装器。
#2
1
Did you try to redirect stderr too? e.g.
您是否尝试重定向stderr?例如
sys.stdout = StringIO();
sys.stderr = StringIO();
foo(bar);
sys.stdout = sys.__stdout__; # These are provided by python
sys.stderr = sys.__stderr__;
Also using StringIO might use extra memory. You can use a dummy device instead (e.g. http://coreygoldberg.blogspot.com/2009/05/python-redirect-or-turn-off-stdout-and.html).
使用StringIO也可能使用额外的内存。您可以使用虚拟设备(例如http://coreygoldberg.blogspot.com/2009/05/python-redirect-or-turn-off-stdout-and.html)。
#3
1
My solution is similar to yours but uses contextlib
and is a little shorter and easier to understand (IMHO).
我的解决方案与您的解决方案类似但使用了contextlib,并且更短,更容易理解(恕我直言)。
import contextlib
@contextlib.contextmanager
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()
The context for why I created this is at this blog post. Similar to yours I think.
我创建此文章的背景是在这篇博客文章中。我认为与你的相似。
I use it like this in a setup.py
:
我在setup.py中使用它:
with stdchannel_redirected(sys.stderr, os.devnull):
if compiler.has_function('clock_gettime', libraries=['rt']):
libraries.append('rt')
#4
1
Not really requested by the OP, but I needed to hide and store the output, and did like follows:
OP并没有真正要求,但我需要隐藏和存储输出,并且如下所示:
from io import StringIO
import sys
class Hider:
def __init__(self, channels=('stdout',)):
self._stomach = StringIO()
self._orig = {ch : None for ch in channels}
def __enter__(self):
for ch in self._orig:
self._orig[ch] = getattr(sys, ch)
setattr(sys, ch, self)
return self
def write(self, string):
self._stomach.write(string)
def flush(self):
pass
def autopsy(self):
return self._stomach.getvalue()
def __exit__(self, *args):
for ch in self._orig:
setattr(sys, ch, self._orig[ch])
Usage:
with Hider() as h:
spammy_function()
result = h.autopsy()
(tested only with Python 3)
(仅使用Python 3测试)
EDIT: now allows to select stderr
, stdout
or both, as in Hider([stdout, stderr])
编辑:现在允许选择stderr,stdout或两者,如Hider([stdout,stderr])
#5
0
python 3.6 working version, tested with million suppressions without any errors
python 3.6工作版,经过百万次抑制测试,没有任何错误
import os
import sys
class suppress_stdout_stderr(object):
def __enter__(self):
self.outnull_file = open(os.devnull, 'w')
self.errnull_file = open(os.devnull, 'w')
self.old_stdout_fileno_undup = sys.stdout.fileno()
self.old_stderr_fileno_undup = sys.stderr.fileno()
self.old_stdout_fileno = os.dup ( sys.stdout.fileno() )
self.old_stderr_fileno = os.dup ( sys.stderr.fileno() )
self.old_stdout = sys.stdout
self.old_stderr = sys.stderr
os.dup2 ( self.outnull_file.fileno(), self.old_stdout_fileno_undup )
os.dup2 ( self.errnull_file.fileno(), self.old_stderr_fileno_undup )
sys.stdout = self.outnull_file
sys.stderr = self.errnull_file
return self
def __exit__(self, *_):
sys.stdout = self.old_stdout
sys.stderr = self.old_stderr
os.dup2 ( self.old_stdout_fileno, self.old_stdout_fileno_undup )
os.dup2 ( self.old_stderr_fileno, self.old_stderr_fileno_undup )
os.close ( self.old_stdout_fileno )
os.close ( self.old_stderr_fileno )
self.outnull_file.close()
self.errnull_file.close()
#6
0
As of python 3.5 we can do this with minimal work using built-ins in contextlib
, namely redirect_stdout
and redirect_stderr
. We only need to combine these two built-in context managers in a custom context manager of ours, which can be easily done using the nice pattern in Martijn's answer here. Redirecting both outputs to os.devnull
should be safe and portable enough.
从python 3.5开始,我们可以使用contextlib中的内置函数(即redirect_stdout和redirect_stderr)进行最少的工作。我们只需要在我们的自定义上下文管理器中组合这两个内置的上下文管理器,这可以使用Martijn的答案中的漂亮模式轻松完成。将两个输出重定向到os.devnull应该足够安全和便携。
from contextlib import contextmanager,redirect_stderr,redirect_stdout
from os import devnull
@contextmanager
def suppress_stdout_stderr():
"""A context manager that redirects stdout and stderr to devnull"""
with open(devnull, 'w') as fnull:
with redirect_stderr(fnull) as err, redirect_stdout(fnull) as out:
yield (err, out)
Note that suppressing stderr
will still give you full tracebacks when something breaks, which is a good thing:
请注意,当出现问题时,抑制stderr仍然会给你完整的回溯,这是一件好事:
import sys
def rogue_function():
print('spam to stdout')
print('important warning', file=sys.stderr)
1 + 'a'
return 42
with suppress_stdout_stderr():
rogue_function()
When run the above only prints
当运行上面只打印
Traceback (most recent call last):
File "tmp.py", line 20, in <module>
rogue_function()
File "foo.py", line 16, in rogue_function
1 + 'a'
TypeError: unsupported operand type(s) for +: 'int' and 'str'
to the terminal. Unhandled exceptions should never go unnoticed.
到终点站。未处理的异常永远不会被忽视。
#7
-2
If you are running this script on a linux based machine, you should be able to:
如果您在基于Linux的计算机上运行此脚本,您应该能够:
$> ./runscript.py > output.txt