8000 Merge pull request #7870 from charris/backport-7853 · numpy/numpy@9760096 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9760096

Browse files
authored
Merge pull request #7870 from charris/backport-7853
Backport 7853, BUG: Raise RuntimeError when reloading numpy is attempted.
2 parents 3c26d43 + 5361d69 commit 9760096

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

numpy/__init__.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,21 @@
107107
from __future__ import division, absolute_import, print_function
108108

109109
import sys
110+
import warnings
110111

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

120+
121+
# Define some global warnings and the _NoValue sentinal. Defining them here
122+
# means that their identity will change if numpy is reloaded, hence if that is
123+
# to be allowed they should be moved into their own, non-reloadable module.
124+
# Note that these should be defined (or imported) before the other imports.
112125
class ModuleDeprecationWarning(DeprecationWarning):
113126
"""Module deprecation warning.
114127
@@ -135,9 +148,8 @@ class VisibleDeprecationWarning(UserWarning):
135148
class _NoValue:
136149
"""Special keyword value.
137150
138-
This class may be used as the default value assigned to a
139-
deprecated keyword in order to check if it has been given a user
140-
defined 8000 value.
151+
This class may be used as the default value assigned to a deprecated
152+
keyword in order to check if it has been given a user defined value.
141153
"""
142154
pass
143155

@@ -155,11 +167,8 @@ class _NoValue:
155167
except NameError:
156168
__NUMPY_SETUP__ = False
157169

158-
159170
if __NUMPY_SETUP__:
160-
import sys as _sys
161-
_sys.stderr.write('Running from numpy source directory.\n')
162-
del _sys
171+
sys.stderr.write('Running from numpy source directory.\n')
163172
else:
164173
try:
165174
from numpy.__config__ import show as show_config
@@ -206,7 +215,7 @@ def pkgload(*packages, **options):
206215
from .compat import long
207216

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

234+
225235
# Filter annoying Cython warnings that serve no good purpose.
226-
import warnings
227236
warnings.filterwarnings("ignore", message="numpy.dtype size changed")
228237
warnings.filterwarnings("ignore", message="numpy.ufunc size changed")
229238
warnings.filterwarnings("ignore", message="numpy.ndarray size changed")

numpy/tests/test_reloading.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from __future__ import division, absolute_import, print_function
2+
3+
import sys
4+
5+
import numpy as np
6+
from numpy.testing import assert_raises, assert_, run_module_suite
7+
8+
if sys.version_info[:2] >= (3, 4):
9+
from importlib import reload
10+
else:
11+
from imp import reload
12+
13+
def test_reloading_exception():
14+
# gh-7844. Also check that relevant globals retain their identity.
15+
_NoValue = np._NoValue
16+
VisibleDeprecationWarning = np.VisibleDeprecationWarning
17+
ModuleDeprecationWarning = np.ModuleDeprecationWarning
18+
19+
assert_raises(RuntimeError, reload, np)
20+
assert_(_NoValue is np._NoValue)
21+
assert_(ModuleDeprecationWarning is np.ModuleDeprecationWarning)
22+
assert_(VisibleDeprecationWarning is np.VisibleDeprecationWarning)
23+
24+
25+
if __name__ == "__main__":
26+
run_module_suite()

0 commit comments

Comments
 (0)
0