warn.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # encoding: utf-8
  2. """
  3. Utilities for warnings. Shoudn't we just use the built in warnings module.
  4. """
  5. # Copyright (c) IPython Development Team.
  6. # Distributed under the terms of the Modified BSD License.
  7. from __future__ import print_function
  8. import sys
  9. import warnings
  10. warnings.warn("The module IPython.utils.warn is deprecated since IPython 4.0, use the standard warnings module instead", DeprecationWarning)
  11. def warn(msg,level=2,exit_val=1):
  12. """Deprecated
  13. Standard warning printer. Gives formatting consistency.
  14. Output is sent to sys.stderr.
  15. Options:
  16. -level(2): allows finer control:
  17. 0 -> Do nothing, dummy function.
  18. 1 -> Print message.
  19. 2 -> Print 'WARNING:' + message. (Default level).
  20. 3 -> Print 'ERROR:' + message.
  21. 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).
  22. -exit_val (1): exit value returned by sys.exit() for a level 4
  23. warning. Ignored for all other levels."""
  24. warnings.warn("The module IPython.utils.warn is deprecated since IPython 4.0, use the standard warnings module instead", DeprecationWarning)
  25. if level>0:
  26. header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
  27. print(header[level], msg, sep='', file=sys.stderr)
  28. if level == 4:
  29. print('Exiting.\n', file=sys.stderr)
  30. sys.exit(exit_val)
  31. def info(msg):
  32. """Deprecated
  33. Equivalent to warn(msg,level=1)."""
  34. warn(msg,level=1)
  35. def error(msg):
  36. """Deprecated
  37. Equivalent to warn(msg,level=3)."""
  38. warn(msg,level=3)
  39. def fatal(msg,exit_val=1):
  40. """Deprecated
  41. Equivalent to warn(msg,exit_val=exit_val,level=4)."""
  42. warn(msg,exit_val=exit_val,level=4)