diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2016-07-20 11:09:36 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2016-08-16 13:12:32 -0600 |
commit | 0ade4c43f052247a2d12a2c49d3c846e6c429fa9 (patch) | |
tree | 9a392681beeb6e1b297e8563867068fb5fa1106d /numpy/_globals.py | |
parent | 63d30baee2535f03d55a10819ea1e56165d4c030 (diff) | |
download | numpy-0ade4c43f052247a2d12a2c49d3c846e6c429fa9.tar.gz |
BUG: Make sure numpy globals keep identity after reload.
Reloading currently causes problems because global classes defined in
numpy/__init__.py change their identity (a is b) after reload. The
solution taken here is to move those classes to a new non-reloadable
module numpy/_globals and import them into numpy from there.
Classes moved are ModuleDeprecationWarning, VisibleDeprecationWarning,
and _NoValue.
Closes #7844.
Diffstat (limited to 'numpy/_globals.py')
-rw-r--r-- | numpy/_globals.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/numpy/_globals.py b/numpy/_globals.py new file mode 100644 index 000000000..64a84da96 --- /dev/null +++ b/numpy/_globals.py @@ -0,0 +1,62 @@ +""" +Module defining global singleton classes. + +This module raises a RuntimeError if an attempt to reload it is made. In that +way the identities of the classes defined here are fixed and will remain so +even if numpy itself is reloaded. In particular, a function like the following +will still work correctly after numpy is reloaded:: + + def foo(arg=np._NoValue): + if arg is np._NoValue: + ... + +That was not the case when the singleton classes were defined in the numpy +``__init__.py`` file. See gh-7844 for a discussion of the reload problem that +motivated this module. + +""" +from __future__ import division, absolute_import, print_function + + +__ALL__ = [ + 'ModuleDeprecationWarning', 'VisibleDeprecationWarning', '_NoValue' + ] + + +# Disallow reloading this module so as to preserve the identities of the +# classes defined here. +if '_is_loaded' in globals(): + raise RuntimeError('Reloading numpy._globals is not allowed') +_is_loaded = True + + +class ModuleDeprecationWarning(DeprecationWarning): + """Module deprecation warning. + + The nose tester turns ordinary Deprecation warnings into test failures. + That makes it hard to deprecate whole modules, because they get + imported by default. So this is a special Deprecation warning that the + nose tester will let pass without making tests fail. + + """ + pass + + +class VisibleDeprecationWarning(UserWarning): + """Visible deprecation warning. + + By default, python will not show deprecation warnings, so this class + can be used when a very visible warning is helpful, for example because + the usage is most likely a user bug. + + """ + pass + + +class _NoValue: + """Special keyword value. + + This class may be used as the default value assigned to a deprecated + keyword in order to check if it has been given a user defined value. + """ + pass |