Development of an internal social media platform with personalised dashboards for students
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.

compat.py 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from __future__ import absolute_import
  2. import sys
  3. class WarningMessage(object):
  4. """Holds the result of a single showwarning() call."""
  5. _WARNING_DETAILS = ('message', 'category', 'filename', 'lineno', 'file',
  6. 'line')
  7. def __init__(self, message, category, filename, lineno, file=None,
  8. line=None):
  9. local_values = locals()
  10. for attr in self._WARNING_DETAILS:
  11. setattr(self, attr, local_values[attr])
  12. self._category_name = category and category.__name__ or None
  13. def __str__(self):
  14. return ('{message : %r, category : %r, filename : %r, lineno : %s, '
  15. 'line : %r}' % (self.message, self._category_name,
  16. self.filename, self.lineno, self.line))
  17. class catch_warnings(object):
  18. """A context manager that copies and restores the warnings filter upon
  19. exiting the context.
  20. The 'record' argument specifies whether warnings should be captured by a
  21. custom implementation of warnings.showwarning() and be appended to a list
  22. returned by the context manager. Otherwise None is returned by the context
  23. manager. The objects appended to the list are arguments whose attributes
  24. mirror the arguments to showwarning().
  25. The 'module' argument is to specify an alternative module to the module
  26. named 'warnings' and imported under that name. This argument is only
  27. useful when testing the warnings module itself.
  28. """
  29. def __init__(self, record=False, module=None):
  30. """Specify whether to record warnings and if an alternative module
  31. should be used other than sys.modules['warnings'].
  32. For compatibility with Python 3.0, please consider all arguments to be
  33. keyword-only.
  34. """
  35. self._record = record
  36. self._module = module is None and sys.modules['warnings'] or module
  37. self._entered = False
  38. def __repr__(self):
  39. args = []
  40. if self._record:
  41. args.append('record=True')
  42. if self._module is not sys.modules['warnings']:
  43. args.append('module=%r' % self._module)
  44. name = type(self).__name__
  45. return '%s(%s)' % (name, ', '.join(args))
  46. def __enter__(self):
  47. if self._entered:
  48. raise RuntimeError('Cannot enter %r twice' % self)
  49. self._entered = True
  50. self._filters = self._module.filters
  51. self._module.filters = self._filters[:]
  52. self._showwarning = self._module.showwarning
  53. if self._record:
  54. log = []
  55. def showwarning(*args, **kwargs):
  56. log.append(WarningMessage(*args, **kwargs))
  57. self._module.showwarning = showwarning
  58. return log
  59. def __exit__(self, *exc_info):
  60. if not self._entered:
  61. raise RuntimeError('Cannot exit %r without entering first' % self)
  62. self._module.filters = self._filters
  63. self._module.showwarning = self._showwarning