Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2Module defining global singleton classes. 

3 

4This module raises a RuntimeError if an attempt to reload it is made. In that 

5way the identities of the classes defined here are fixed and will remain so 

6even if numpy itself is reloaded. In particular, a function like the following 

7will still work correctly after numpy is reloaded:: 

8 

9 def foo(arg=np._NoValue): 

10 if arg is np._NoValue: 

11 ... 

12 

13That was not the case when the singleton classes were defined in the numpy 

14``__init__.py`` file. See gh-7844 for a discussion of the reload problem that 

15motivated this module. 

16 

17""" 

18__ALL__ = [ 

19 'ModuleDeprecationWarning', 'VisibleDeprecationWarning', '_NoValue' 

20 ] 

21 

22 

23# Disallow reloading this module so as to preserve the identities of the 

24# classes defined here. 

25if '_is_loaded' in globals(): 

26 raise RuntimeError('Reloading numpy._globals is not allowed') 

27_is_loaded = True 

28 

29 

30class ModuleDeprecationWarning(DeprecationWarning): 

31 """Module deprecation warning. 

32 

33 The nose tester turns ordinary Deprecation warnings into test failures. 

34 That makes it hard to deprecate whole modules, because they get 

35 imported by default. So this is a special Deprecation warning that the 

36 nose tester will let pass without making tests fail. 

37 

38 """ 

39 

40 

41ModuleDeprecationWarning.__module__ = 'numpy' 

42 

43 

44class VisibleDeprecationWarning(UserWarning): 

45 """Visible deprecation warning. 

46 

47 By default, python will not show deprecation warnings, so this class 

48 can be used when a very visible warning is helpful, for example because 

49 the usage is most likely a user bug. 

50 

51 """ 

52 

53 

54VisibleDeprecationWarning.__module__ = 'numpy' 

55 

56 

57class _NoValueType: 

58 """Special keyword value. 

59 

60 The instance of this class may be used as the default value assigned to a 

61 deprecated keyword in order to check if it has been given a user defined 

62 value. 

63 """ 

64 __instance = None 

65 def __new__(cls): 

66 # ensure that only one instance exists 

67 if not cls.__instance: 

68 cls.__instance = super(_NoValueType, cls).__new__(cls) 

69 return cls.__instance 

70 

71 # needed for python 2 to preserve identity through a pickle 

72 def __reduce__(self): 

73 return (self.__class__, ()) 

74 

75 def __repr__(self): 

76 return "<no value>" 

77 

78 

79_NoValue = _NoValueType()