__init__.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # encoding=utf-8
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2015 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import io
  21. import sys
  22. class StdoutBuffer(object):
  23. def __init__(self):
  24. self.orig_stdout = None
  25. if sys.version_info[0] >= 3:
  26. self.stdout = io.StringIO()
  27. else:
  28. self.stdout = io.BytesIO()
  29. def __enter__(self):
  30. self.orig_stdout = sys.stdout
  31. sys.stdout = self.stdout
  32. return self.stdout
  33. def __exit__(self, exc_type, exc_val, exc_tb):
  34. sys.stdout = self.orig_stdout
  35. return False
  36. class StderrBuffer(object):
  37. def __init__(self):
  38. self.orig_stderr = None
  39. if sys.version_info[0] >= 3:
  40. self.stderr = io.StringIO()
  41. else:
  42. self.stderr = io.BytesIO()
  43. def __enter__(self):
  44. self.orig_stderr = sys.stderr
  45. sys.stderr = self.stderr
  46. return self.stderr
  47. def __exit__(self, exc_type, exc_val, exc_tb):
  48. sys.stderr = self.orig_stderr
  49. return False