Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Backport 7853, BUG: Raise RuntimeError when reloading numpy is attempted. #7870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions numpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,21 @@
from __future__ import division, absolute_import, print_function

import sys
import warnings

# Disallow reloading numpy. Doing that does nothing to change previously
# loaded modules, which would need to be reloaded separately, but it does
# change the identity of the warnings and sentinal classes defined below
# with dire consequences when checking for identity.
if '_is_loaded' in globals():
raise RuntimeError('Reloading numpy is not supported')
_is_loaded = True


# Define some global warnings and the _NoValue sentinal. Defining them here
# means that their identity will change if numpy is reloaded, hence if that is
# to be allowed they should be moved into their own, non-reloadable module.
# Note that these should be defined (or imported) before the other imports.
class ModuleDeprecationWarning(DeprecationWarning):
"""Module deprecation warning.

Expand All @@ -135,9 +148,8 @@ class VisibleDeprecationWarning(UserWarning):
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.
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

Expand All @@ -155,11 +167,8 @@ class _NoValue:
except NameError:
__NUMPY_SETUP__ = False


if __NUMPY_SETUP__:
import sys as _sys
_sys.stderr.write('Running from numpy source directory.\n')
del _sys
sys.stderr.write('Running from numpy source directory.\n')
else:
try:
from numpy.__config__ import show as show_config
Expand Down Expand Up @@ -206,7 +215,7 @@ def pkgload(*packages, **options):
from .compat import long

# Make these accessible from numpy name-space
# but not imported in from numpy import *
# but not imported in from numpy import *
if sys.version_info[0] >= 3:
from builtins import bool, int, float, complex, object, str
unicode = str
Expand All @@ -222,8 +231,8 @@ def pkgload(*packages, **options):
__all__.extend(lib.__all__)
__all__.extend(['linalg', 'fft', 'random', 'ctypeslib', 'ma'])


# Filter annoying Cython warnings that serve no good purpose.
import warnings
warnings.filterwarnings("ignore", message="numpy.dtype size changed")
warnings.filterwarnings("ignore", message="numpy.ufunc size changed")
warnings.filterwarnings("ignore", message="numpy.ndarray size changed")
26 changes: 26 additions & 0 deletions numpy/tests/test_reloading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from __future__ import division, absolute_import, print_function

import sys

import numpy as np
from numpy.testing import assert_raises, assert_, run_module_suite

if sys.version_info[:2] >= (3, 4):
from importlib import reload
else:
from imp import reload

def test_reloading_exception():
# gh-7844. Also check that relevant globals retain their identity.
_NoValue = np._NoValue
VisibleDeprecationWarning = np.VisibleDeprecationWarning
ModuleDeprecationWarning = np.ModuleDeprecationWarning

assert_raises(RuntimeError, reload, np)
assert_(_NoValue is np._NoValue)
assert_(ModuleDeprecationWarning is np.ModuleDeprecationWarning)
assert_(VisibleDeprecationWarning is np.VisibleDeprecationWarning)


if __name__ == "__main__":
run_module_suite()