_globals.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. __ALL__ = [
  15. 'ModuleDeprecationWarning', 'VisibleDeprecationWarning', '_NoValue'
  16. ]
  17. # Disallow reloading this module so as to preserve the identities of the
  18. # classes defined here.
  19. if '_is_loaded' in globals():
  20. raise RuntimeError('Reloading numpy._globals is not allowed')
  21. _is_loaded = True
  22. class ModuleDeprecationWarning(DeprecationWarning):
  23. """Module deprecation warning.
  24. The nose tester turns ordinary Deprecation warnings into test failures.
  25. That makes it hard to deprecate whole modules, because they get
  26. imported by default. So this is a special Deprecation warning that the
  27. nose tester will let pass without making tests fail.
  28. """
  29. ModuleDeprecationWarning.__module__ = 'numpy'
  30. class VisibleDeprecationWarning(UserWarning):
  31. """Visible deprecation warning.
  32. By default, python will not show deprecation warnings, so this class
  33. can be used when a very visible warning is helpful, for example because
  34. the usage is most likely a user bug.
  35. """
  36. VisibleDeprecationWarning.__module__ = 'numpy'
  37. class _NoValueType:
  38. """Special keyword value.
  39. The instance of this class may be used as the default value assigned to a
  40. deprecated keyword in order to check if it has been given a user defined
  41. value.
  42. """
  43. __instance = None
  44. def __new__(cls):
  45. # ensure that only one instance exists
  46. if not cls.__instance:
  47. cls.__instance = super(_NoValueType, cls).__new__(cls)
  48. return cls.__instance
  49. # needed for python 2 to preserve identity through a pickle
  50. def __reduce__(self):
  51. return (self.__class__, ())
  52. def __repr__(self):
  53. return "<no value>"
  54. _NoValue = _NoValueType()