8000 Merge pull request #7853 from charris/fix-novalue-import · numpy/numpy@eb2c1db · GitHub
[go: up one dir, main page]

Skip to content

Commit eb2c1db

Browse files
authored
Merge pull request #7853 from charris/fix-novalue-import
BUG: Make sure numpy globals keep identity after reload.
2 parents 1b16cd8 + b9b42ee commit eb2c1db

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 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
@@ -209,7 +218,7 @@ def pkgload(*packages, **options):
209218
from .compat import long
210219

211220
# Make these accessible from numpy name-space
212-
# but not imported in from numpy import *
221+
# but not imported in from numpy import *
213222
if sys.version_info[0] >= 3:
214223
from builtins import bool, int, float, complex, object, str
215224
unicode = str
@@ -225,8 +234,8 @@ def pkgload(*packages, **options):
225234
__all__.extend(lib.__all__)
226235
__all__.extend(['linalg', 'fft', 'random', 'ctypeslib', 'ma'])
227236

237+
228238
# Filter annoying Cython warnings that serve no good purpose.
229-
import warnings
230239
warnings.filterwarnings("ignore", message="numpy.dtype size changed")
231240
warnings.filterwarnings("ignore", message="numpy.ufunc size changed")
232241
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