Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/numpy/_core/__init__.py: 22%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

89 statements  

1""" 

2Contains the core of NumPy: ndarray, ufuncs, dtypes, etc. 

3 

4Please note that this module is private. All functions and objects 

5are available in the main ``numpy`` namespace - use that instead. 

6 

7""" 

8 

9import os 

10 

11from numpy.version import version as __version__ 

12 

13# disables OpenBLAS affinity setting of the main thread that limits 

14# python threads or processes to one core 

15env_added = [] 

16for envkey in ['OPENBLAS_MAIN_FREE', 'GOTOBLAS_MAIN_FREE']: 

17 if envkey not in os.environ: 

18 os.environ[envkey] = '1' 

19 env_added.append(envkey) 

20 

21try: 

22 from . import multiarray 

23except ImportError as exc: 

24 import sys 

25 

26 # Bypass for the module re-initialization opt-out 

27 if exc.msg == "cannot load module more than once per process": 

28 raise 

29 

30 # Basically always, the problem should be that the C module is wrong/missing... 

31 if ( 

32 isinstance(exc, ModuleNotFoundError) 

33 and exc.name == "numpy._core._multiarray_umath" 

34 ): 

35 import sys 

36 candidates = [] 

37 for path in __path__: 

38 candidates.extend( 

39 f for f in os.listdir(path) if f.startswith("_multiarray_umath")) 

40 if len(candidates) == 0: 

41 bad_c_module_info = ( 

42 "We found no compiled module, did NumPy build successfully?\n") 

43 else: 

44 candidate_str = '\n * '.join(candidates) 

45 # cache_tag is documented to be possibly None, so just use name if it is 

46 # this guesses at cache_tag being the same as the extension module scheme 

47 tag = sys.implementation.cache_tag or sys.implementation.name 

48 bad_c_module_info = ( 

49 f"The following compiled module files exist, but seem incompatible\n" 

50 f"with with either python '{tag}' or the " 

51 f"platform '{sys.platform}':\n\n * {candidate_str}\n" 

52 ) 

53 else: 

54 bad_c_module_info = "" 

55 

56 major, minor, *_ = sys.version_info 

57 msg = f""" 

58 

59IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE! 

60 

61Importing the numpy C-extensions failed. This error can happen for 

62many reasons, often due to issues with your setup or how NumPy was 

63installed. 

64{bad_c_module_info} 

65We have compiled some common reasons and troubleshooting tips at: 

66 

67 https://numpy.org/devdocs/user/troubleshooting-importerror.html 

68 

69Please note and check the following: 

70 

71 * The Python version is: Python {major}.{minor} from "{sys.executable}" 

72 * The NumPy version is: "{__version__}" 

73 

74and make sure that they are the versions you expect. 

75 

76Please carefully study the information and documentation linked above. 

77This is unlikely to be a NumPy issue but will be caused by a bad install 

78or environment on your machine. 

79 

80Original error was: {exc} 

81""" 

82 

83 raise ImportError(msg) from exc 

84finally: 

85 for envkey in env_added: 

86 del os.environ[envkey] 

87del envkey 

88del env_added 

89del os 

90 

91from . import umath 

92 

93# Check that multiarray,umath are pure python modules wrapping 

94# _multiarray_umath and not either of the old c-extension modules 

95if not (hasattr(multiarray, '_multiarray_umath') and 

96 hasattr(umath, '_multiarray_umath')): 

97 import sys 

98 path = sys.modules['numpy'].__path__ 

99 msg = ("Something is wrong with the numpy installation. " 

100 "While importing we detected an older version of " 

101 "numpy in {}. One method of fixing this is to repeatedly uninstall " 

102 "numpy until none is found, then reinstall this version.") 

103 raise ImportError(msg.format(path)) 

104 

105from . import numerictypes as nt 

106from .numerictypes import sctypeDict, sctypes 

107 

108multiarray.set_typeDict(nt.sctypeDict) 

109from . import einsumfunc, fromnumeric, function_base, getlimits, numeric, shape_base 

110from .einsumfunc import * 

111from .fromnumeric import * 

112from .function_base import * 

113from .getlimits import * 

114 

115# Note: module name memmap is overwritten by a class with same name 

116from .memmap import * 

117from .numeric import * 

118from .records import recarray, record 

119from .shape_base import * 

120 

121del nt 

122 

123# do this after everything else, to minimize the chance of this misleadingly 

124# appearing in an import-time traceback 

125# add these for module-freeze analysis (like PyInstaller) 

126from . import ( 

127 _add_newdocs, 

128 _add_newdocs_scalars, 

129 _dtype, 

130 _dtype_ctypes, 

131 _internal, 

132 _methods, 

133) 

134from .numeric import absolute as abs 

135 

136acos = numeric.arccos 

137acosh = numeric.arccosh 

138asin = numeric.arcsin 

139asinh = numeric.arcsinh 

140atan = numeric.arctan 

141atanh = numeric.arctanh 

142atan2 = numeric.arctan2 

143concat = numeric.concatenate 

144bitwise_left_shift = numeric.left_shift 

145bitwise_invert = numeric.invert 

146bitwise_right_shift = numeric.right_shift 

147permute_dims = numeric.transpose 

148pow = numeric.power 

149 

150__all__ = [ 

151 "abs", "acos", "acosh", "asin", "asinh", "atan", "atanh", "atan2", 

152 "bitwise_invert", "bitwise_left_shift", "bitwise_right_shift", "concat", 

153 "pow", "permute_dims", "memmap", "sctypeDict", "record", "recarray" 

154] 

155__all__ += numeric.__all__ 

156__all__ += function_base.__all__ 

157__all__ += getlimits.__all__ 

158__all__ += shape_base.__all__ 

159__all__ += einsumfunc.__all__ 

160 

161 

162def _ufunc_reduce(func): 

163 # Report the `__name__`. pickle will try to find the module. Note that 

164 # pickle supports for this `__name__` to be a `__qualname__`. It may 

165 # make sense to add a `__qualname__` to ufuncs, to allow this more 

166 # explicitly (Numba has ufuncs as attributes). 

167 # See also: https://github.com/dask/distributed/issues/3450 

168 return func.__name__ 

169 

170 

171def _DType_reconstruct(scalar_type): 

172 # This is a work-around to pickle type(np.dtype(np.float64)), etc. 

173 # and it should eventually be replaced with a better solution, e.g. when 

174 # DTypes become HeapTypes. 

175 return type(dtype(scalar_type)) 

176 

177 

178def _DType_reduce(DType): 

179 # As types/classes, most DTypes can simply be pickled by their name: 

180 if not DType._legacy or DType.__module__ == "numpy.dtypes": 

181 return DType.__name__ 

182 

183 # However, user defined legacy dtypes (like rational) do not end up in 

184 # `numpy.dtypes` as module and do not have a public class at all. 

185 # For these, we pickle them by reconstructing them from the scalar type: 

186 scalar_type = DType.type 

187 return _DType_reconstruct, (scalar_type,) 

188 

189 

190import copyreg 

191 

192copyreg.pickle(ufunc, _ufunc_reduce) 

193copyreg.pickle(type(dtype), _DType_reduce, _DType_reconstruct) 

194 

195# Unclutter namespace (must keep _*_reconstruct for unpickling) 

196del copyreg, _ufunc_reduce, _DType_reduce 

197 

198from numpy._pytesttester import PytestTester 

199 

200test = PytestTester(__name__) 

201del PytestTester