deprecation.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """
  2. A module that implements tooling to enable easy warnings about deprecations.
  3. """
  4. from __future__ import absolute_import
  5. import logging
  6. import warnings
  7. class PipDeprecationWarning(Warning):
  8. pass
  9. class Pending(object):
  10. pass
  11. class RemovedInPip10Warning(PipDeprecationWarning):
  12. pass
  13. class RemovedInPip11Warning(PipDeprecationWarning, Pending):
  14. pass
  15. class Python26DeprecationWarning(PipDeprecationWarning):
  16. pass
  17. # Warnings <-> Logging Integration
  18. _warnings_showwarning = None
  19. def _showwarning(message, category, filename, lineno, file=None, line=None):
  20. if file is not None:
  21. if _warnings_showwarning is not None:
  22. _warnings_showwarning(
  23. message, category, filename, lineno, file, line,
  24. )
  25. else:
  26. if issubclass(category, PipDeprecationWarning):
  27. # We use a specially named logger which will handle all of the
  28. # deprecation messages for pip.
  29. logger = logging.getLogger("pip.deprecations")
  30. # This is purposely using the % formatter here instead of letting
  31. # the logging module handle the interpolation. This is because we
  32. # want it to appear as if someone typed this entire message out.
  33. log_message = "DEPRECATION: %s" % message
  34. # PipDeprecationWarnings that are Pending still have at least 2
  35. # versions to go until they are removed so they can just be
  36. # warnings. Otherwise, they will be removed in the very next
  37. # version of pip. We want these to be more obvious so we use the
  38. # ERROR logging level.
  39. if issubclass(category, Pending):
  40. logger.warning(log_message)
  41. else:
  42. logger.error(log_message)
  43. else:
  44. _warnings_showwarning(
  45. message, category, filename, lineno, file, line,
  46. )
  47. def install_warning_logger():
  48. # Enable our Deprecation Warnings
  49. warnings.simplefilter("default", PipDeprecationWarning, append=True)
  50. global _warnings_showwarning
  51. if _warnings_showwarning is None:
  52. _warnings_showwarning = warnings.showwarning
  53. warnings.showwarning = _showwarning