Skip to content

Commit e332643

Browse files
embrayeteq
authored andcommitted
Merge pull request #3479 from embray/utils/minversion-2
Ensure that NUMPY_LT_ constants are always defined
1 parent 8ac3d6d commit e332643

3 files changed

Lines changed: 39 additions & 17 deletions

File tree

‎astropy/utils/compat/__init__.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@
1010

1111
from .misc import *
1212

13-
if not _ASTROPY_SETUP_:
14-
# Importing this module will also install monkey-patches defined in it
15-
from .numpycompat import *
13+
# Importing this module will also install monkey-patches defined in it
14+
from .numpycompat import *

‎astropy/utils/compat/numpycompat.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,18 @@
88
from ...extern import six
99
from ...utils import minversion
1010

11-
import numpy as np
12-
1311

1412
__all__ = ['NUMPY_LT_1_6_1', 'NUMPY_LT_1_7', 'NUMPY_LT_1_8', 'NUMPY_LT_1_9',
1513
'NUMPY_LT_1_9_1']
1614

1715
# TODO: It might also be nice to have aliases to these named for specific
1816
# features/bugs we're checking for (ex:
1917
# astropy.table.table._BROKEN_UNICODE_TABLE_SORT)
20-
NUMPY_LT_1_6_1 = not minversion(np, '1.6.1')
21-
NUMPY_LT_1_7 = not minversion(np, '1.7.0')
22-
NUMPY_LT_1_8 = not minversion(np, '1.8.0')
23-
NUMPY_LT_1_9 = not minversion(np, '1.9.0')
24-
NUMPY_LT_1_9_1 = not minversion(np, '1.9.1')
18+
NUMPY_LT_1_6_1 = not minversion('numpy', '1.6.1')
19+
NUMPY_LT_1_7 = not minversion('numpy', '1.7.0')
20+
NUMPY_LT_1_8 = not minversion('numpy', '1.8.0')
21+
NUMPY_LT_1_9 = not minversion('numpy', '1.9.0')
22+
NUMPY_LT_1_9_1 = not minversion('numpy', '1.9.1')
2523

2624

2725
def _monkeypatch_unicode_mask_fill_values():
@@ -33,6 +31,7 @@ def _monkeypatch_unicode_mask_fill_values():
3331
fill value to handle this case.
3432
"""
3533
if NUMPY_LT_1_8 and six.PY2:
34+
import numpy as np
3635
from numpy.ma import core as ma_core
3736
_check_fill_value_original = ma_core._check_fill_value
3837

@@ -46,4 +45,5 @@ def _check_fill_value(fill_value, ndtype):
4645
ma_core._check_fill_value = _check_fill_value
4746

4847

49-
_monkeypatch_unicode_mask_fill_values()
48+
if not _ASTROPY_SETUP_:
49+
_monkeypatch_unicode_mask_fill_values()

‎astropy/utils/introspection.py‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,22 @@ def resolve_name(name):
4949
If the module or named object is not found.
5050
"""
5151

52-
parts = name.split('.')
53-
cursor = len(parts) - 1
52+
# Note: On python 2 these must be str objects and not unicode
53+
parts = [str(part) for part in name.split('.')]
54+
55+
if len(parts) == 1:
56+
# No dots in the name--just a straight up module import
57+
cursor = 1
58+
attr_name = str('') # Must not be unicode on Python 2
59+
else:
60+
cursor = len(parts) - 1
61+
attr_name = parts[-1]
62+
5463
module_name = parts[:cursor]
55-
attr_name = parts[-1]
5664

5765
while cursor > 0:
5866
try:
59-
ret = __import__('.'.join(module_name), fromlist=[attr_name])
67+
ret = __import__(str('.'.join(module_name)), fromlist=[attr_name])
6068
break
6169
except ImportError:
6270
if cursor == 0:
@@ -87,8 +95,10 @@ def minversion(module, version, inclusive=True, version_path='__version__'):
8795
Parameters
8896
----------
8997
90-
module : module
91-
An imported module of which to check the version.
98+
module : module or `str`
99+
An imported module of which to check the version, or the name of
100+
that module (in which case an import of that module is attempted--
101+
if this fails `False` is returned).
92102
93103
version : `str`
94104
The version as a string that this module must have at a minimum (e.g.
@@ -111,6 +121,19 @@ def minversion(module, version, inclusive=True, version_path='__version__'):
111121
True
112122
"""
113123

124+
if isinstance(module, types.ModuleType):
125+
module_name = module.__name__
126+
elif isinstance(module, six.string_types):
127+
module_name = module
128+
try:
129+
module = resolve_name(module_name)
130+
except ImportError:
131+
return False
132+
else:
133+
raise ValueError('module argument must be an actual imported '
134+
'module, or the import name of the module; '
135+
'got {0!r}'.format(module))
136+
114137
if '.' not in version_path:
115138
have_version = getattr(module, version_path)
116139
else:

0 commit comments

Comments
 (0)