Mageia Bugzilla – Attachment 2856 Details for
Bug 6514
Python module "pynotify" produces warning messages when imported
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
New Account
|
Forgot Password
Work around solution to suppress pynotify warning messages
suproutp.py (text/x-python), 1.67 KB, created by
R Topics
on 2012-09-25 16:02:44 CEST
(
hide
)
Description:
Work around solution to suppress pynotify warning messages
Filename:
MIME Type:
Creator:
R Topics
Created:
2012-09-25 16:02:44 CEST
Size:
1.67 KB
patch
obsolete
>import os >from contextlib import contextmanager > >@contextmanager >def suppress_output(fd): > """ > Suppress output to the given ``fd``:: > > with suppress_output(sys.stderr): > # in this block any output to standard error is suppressed > > ``fd`` is an integral file descriptor, or any object with a ``fileno()`` > method. > """ > if hasattr(fd, 'fileno'): > # we were given a file-like object with an underlying fd > if hasattr(fd, 'flush'): > # flush Python-side buffers before redirecting > fd.flush() > # get the fd to redirect > fd = fd.fileno() > > # duplicate the file descriptor to restore it eventually > oldfd = os.dup(fd) > try: > # open the trash can > devnull = os.open(os.devnull, os.O_WRONLY) > try: > # point the file descriptor to the trash can > os.dup2(devnull, fd) > finally: > # close the old trash can descriptor, we don't need it anymore > # since the fd now points to the trash can > os.close(devnull) > # enter the callers block > yield > # restore the file descriptor > os.dup2(oldfd, fd) > finally: > # close the duplicated copy of the original fd, we don't need it > # anymore now that fd is restored > os.close(oldfd) > >if __name__ == "__main__": > > import sys > import warnings > > print "\nSupress output, no warnings should display on importing pynotify" > print " " > > with suppress_output(sys.stderr): > import pynotify > > # generate a warning to show output has been restored > def fxn(): > warnings.warn("Warnings have been restored", Warning) > > fxn() >
import os from contextlib import contextmanager @contextmanager def suppress_output(fd): """ Suppress output to the given ``fd``:: with suppress_output(sys.stderr): # in this block any output to standard error is suppressed ``fd`` is an integral file descriptor, or any object with a ``fileno()`` method. """ if hasattr(fd, 'fileno'): # we were given a file-like object with an underlying fd if hasattr(fd, 'flush'): # flush Python-side buffers before redirecting fd.flush() # get the fd to redirect fd = fd.fileno() # duplicate the file descriptor to restore it eventually oldfd = os.dup(fd) try: # open the trash can devnull = os.open(os.devnull, os.O_WRONLY) try: # point the file descriptor to the trash can os.dup2(devnull, fd) finally: # close the old trash can descriptor, we don't need it anymore # since the fd now points to the trash can os.close(devnull) # enter the callers block yield # restore the file descriptor os.dup2(oldfd, fd) finally: # close the duplicated copy of the original fd, we don't need it # anymore now that fd is restored os.close(oldfd) if __name__ == "__main__": import sys import warnings print "\nSupress output, no warnings should display on importing pynotify" print " " with suppress_output(sys.stderr): import pynotify # generate a warning to show output has been restored def fxn(): warnings.warn("Warnings have been restored", Warning) fxn()
View Attachment As Raw
Actions:
View
Attachments on
bug 6514
: 2856