io.py 866 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import contextlib
  2. import os
  3. import sys
  4. _DEBUG = "_ARC_DEBUG" in os.environ
  5. debug_stream = sys.stderr
  6. def debug(*args):
  7. if _DEBUG:
  8. print(file=debug_stream, *args)
  9. @contextlib.contextmanager
  10. def mute_stdout():
  11. stdout = sys.stdout
  12. sys.stdout = open(os.devnull, "w")
  13. try:
  14. yield
  15. finally:
  16. sys.stdout = stdout
  17. @contextlib.contextmanager
  18. def mute_stderr():
  19. stderr = sys.stderr
  20. sys.stderr = open(os.devnull, "w")
  21. try:
  22. yield
  23. finally:
  24. sys.stderr.close()
  25. sys.stderr = stderr
  26. def warn(*args):
  27. """
  28. Prints **args** to standard error when running completions. This will interrupt the user's command line interaction;
  29. use it to indicate an error condition that is preventing your completer from working.
  30. """
  31. print(file=debug_stream)
  32. print(file=debug_stream, *args)