_globals.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """
  2. Module defining global singleton classes.
  3. This module raises a RuntimeError if an attempt to reload it is made. In that
  4. way the identities of the classes defined here are fixed and will remain so
  5. even if numpy itself is reloaded. In particular, a function like the following
  6. will still work correctly after numpy is reloaded::
  7. def foo(arg=np._NoValue):
  8. if arg is np._NoValue:
  9. ...
  10. That was not the case when the singleton classes were defined in the numpy
  11. ``__init__.py`` file. See gh-7844 for a discussion of the reload problem that
  12. motivated this module.
  13. """
  14. from __future__ import division, absolute_import, print_function
  15. __ALL__ = [
  16. 'ModuleDeprecationWarning', 'VisibleDeprecationWarning', '_NoValue'
  17. ]
  18. # Disallow reloading this module so as to preserve the identities of the
  19. # classes defined here.
  20. if '_is_loaded' in globals():
  21. raise RuntimeError('Reloading numpy._globals is not allowed')
  22. _is_loaded = True
  23. class ModuleDeprecationWarning(DeprecationWarning):
  24. """Module deprecation warning.
  25. The nose tester turns ordinary Deprecation warnings into test failures.
  26. That makes it hard to deprecate whole modules, because they get
  27. imported by default. So this is a special Deprecation warning that the
  28. nose tester will let pass without making tests fail.
  29. """
  30. ModuleDeprecationWarning.__module__ = 'numpy'
  31. class VisibleDeprecationWarning(UserWarning):
  32. """Visible deprecation warning.
  33. By default, python will not show deprecation warnings, so this class
  34. can be used when a very visible warning is helpful, for example because
  35. the usage is most likely a user bug.
  36. """
  37. VisibleDeprecationWarning.__module__ = 'numpy'
  38. class _NoValueType(object):
  39. """Special keyword value.
  40. The instance of this class may be used as the default value assigned to a
  41. deprecated keyword in order to check if it has been given a user defined
  42. value.
  43. """
  44. __instance = None
  45. def __new__(cls):
  46. # ensure that only one instance exists
  47. if not cls.__instance:
  48. cls.__instance = super(_NoValueType, cls).__new__(cls)
  49. return cls.__instance
  50. # needed for python 2 to preserve identity through a pickle
  51. def __reduce__(self):
  52. return (self.__class__, ())
  53. def __repr__(self):
  54. return "<no value>"
  55. _NoValue = _NoValueType()