You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

69 lines
2.1 KiB

  1. """
  2. A module that implments 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 RemovedInPip8Warning(PipDeprecationWarning, PendingDeprecationWarning):
  10. pass
  11. class RemovedInPip9Warning(PipDeprecationWarning, PendingDeprecationWarning):
  12. pass
  13. DEPRECATIONS = [RemovedInPip8Warning, RemovedInPip9Warning]
  14. # Warnings <-> Logging Integration
  15. _warnings_showwarning = None
  16. def _showwarning(message, category, filename, lineno, file=None, line=None):
  17. if file is not None:
  18. if _warnings_showwarning is not None:
  19. _warnings_showwarning(
  20. message, category, filename, lineno, file, line,
  21. )
  22. else:
  23. if issubclass(category, PipDeprecationWarning):
  24. # We use a specially named logger which will handle all of the
  25. # deprecation messages for pip.
  26. logger = logging.getLogger("pip.deprecations")
  27. # This is purposely using the % formatter here instead of letting
  28. # the logging module handle the interpolation. This is because we
  29. # want it to appear as if someone typed this entire message out.
  30. log_message = "DEPRECATION: %s" % message
  31. # Things that are DeprecationWarnings will be removed in the very
  32. # next version of pip. We want these to be more obvious so we
  33. # use the ERROR logging level while the PendingDeprecationWarnings
  34. # are still have at least 2 versions to go until they are removed
  35. # so they can just be warnings.
  36. if issubclass(category, DeprecationWarning):
  37. logger.error(log_message)
  38. else:
  39. logger.warning(log_message)
  40. else:
  41. _warnings_showwarning(
  42. message, category, filename, lineno, file, line,
  43. )
  44. def install_warning_logger():
  45. global _warnings_showwarning
  46. if _warnings_showwarning is None:
  47. _warnings_showwarning = warnings.showwarning
  48. warnings.showwarning = _showwarning