mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
MuteStdIO: Partly support ctor arguments
ctor now understands stdio='off' or ='something' and mutes stdout in the first case. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
f6e5d25df7
commit
3451ea0d83
1 changed files with 27 additions and 22 deletions
|
|
@ -7,11 +7,15 @@ from fcntl import fcntl, F_GETFL, F_SETFL
|
||||||
|
|
||||||
class MuteStdIO: # export
|
class MuteStdIO: # export
|
||||||
|
|
||||||
def __init__(self, mute_stderr=False, mute_stdout=True):
|
def __init__(self, stderr='on', stdout='off'):
|
||||||
# TODO: arguments not implemented
|
self.__stderr = stderr
|
||||||
|
self.__stdout = stdout
|
||||||
|
# TODO: arguments not fully implemented,
|
||||||
|
# Add support for 'off', 'on', 'stdxxx' (redirection)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
|
if self.__stdout == 'off':
|
||||||
rfd, wfd = os.pipe()
|
rfd, wfd = os.pipe()
|
||||||
flags = fcntl(rfd, F_GETFL)
|
flags = fcntl(rfd, F_GETFL)
|
||||||
fcntl(rfd, F_SETFL, flags | os.O_NONBLOCK)
|
fcntl(rfd, F_SETFL, flags | os.O_NONBLOCK)
|
||||||
|
|
@ -22,6 +26,7 @@ class MuteStdIO: # export
|
||||||
os.dup2(wfd, 1)
|
os.dup2(wfd, 1)
|
||||||
|
|
||||||
def __exit__(self, type, value, traceback):
|
def __exit__(self, type, value, traceback):
|
||||||
|
if self.__stdout == 'off':
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
os.dup2(self.real_stdout_fd, 1)
|
os.dup2(self.real_stdout_fd, 1)
|
||||||
if type is not None:
|
if type is not None:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue