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

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

91 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 

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

15# python threads or processes to one core 

16env_added = [] 

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

18 if envkey not in os.environ: 

19 os.environ[envkey] = '1' 

20 env_added.append(envkey) 

21 

22try: 

23 from . import multiarray 

24except ImportError as exc: 

25 import sys 

26 msg = """ 

27 

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

29 

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

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

32installed. 

33 

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

35 

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

37 

38Please note and check the following: 

39 

40 * The Python version is: Python%d.%d from "%s" 

41 * The NumPy version is: "%s" 

42 

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

44Please carefully study the documentation linked above for further help. 

45 

46Original error was: %s 

47""" % (sys.version_info[0], sys.version_info[1], sys.executable, 

48 __version__, exc) 

49 raise ImportError(msg) 

50finally: 

51 for envkey in env_added: 

52 del os.environ[envkey] 

53del envkey 

54del env_added 

55del os 

56 

57from . import umath 

58 

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

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

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

62 hasattr(umath, '_multiarray_umath')): 

63 import sys 

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

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

66 "While importing we detected an older version of " 

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

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

69 raise ImportError(msg.format(path)) 

70 

71from . import numerictypes as nt 

72from .numerictypes import sctypes, sctypeDict 

73multiarray.set_typeDict(nt.sctypeDict) 

74from . import numeric 

75from .numeric import * 

76from . import fromnumeric 

77from .fromnumeric import * 

78from .records import record, recarray 

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

80from .memmap import * 

81from . import function_base 

82from .function_base import * 

83from . import _machar 

84from . import getlimits 

85from .getlimits import * 

86from . import shape_base 

87from .shape_base import * 

88from . import einsumfunc 

89from .einsumfunc import * 

90del nt 

91 

92from .numeric import absolute as abs 

93 

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

95# appearing in an import-time traceback 

96from . import _add_newdocs 

97from . import _add_newdocs_scalars 

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

99from . import _dtype_ctypes 

100from . import _internal 

101from . import _dtype 

102from . import _methods 

103 

104acos = numeric.arccos 

105acosh = numeric.arccosh 

106asin = numeric.arcsin 

107asinh = numeric.arcsinh 

108atan = numeric.arctan 

109atanh = numeric.arctanh 

110atan2 = numeric.arctan2 

111concat = numeric.concatenate 

112bitwise_left_shift = numeric.left_shift 

113bitwise_invert = numeric.invert 

114bitwise_right_shift = numeric.right_shift 

115permute_dims = numeric.transpose 

116pow = numeric.power 

117 

118__all__ = [ 

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

120 "bitwise_invert", "bitwise_left_shift", "bitwise_right_shift", "concat", 

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

122] 

123__all__ += numeric.__all__ 

124__all__ += function_base.__all__ 

125__all__ += getlimits.__all__ 

126__all__ += shape_base.__all__ 

127__all__ += einsumfunc.__all__ 

128 

129 

130def _ufunc_reduce(func): 

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

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

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

134 # explicitly (Numba has ufuncs as attributes). 

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

136 return func.__name__ 

137 

138 

139def _DType_reconstruct(scalar_type): 

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

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

142 # DTypes become HeapTypes. 

143 return type(dtype(scalar_type)) 

144 

145 

146def _DType_reduce(DType): 

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

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

149 return DType.__name__ 

150 

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

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

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

154 scalar_type = DType.type 

155 return _DType_reconstruct, (scalar_type,) 

156 

157 

158def __getattr__(name): 

159 # Deprecated 2022-11-22, NumPy 1.25. 

160 if name == "MachAr": 

161 import warnings 

162 warnings.warn( 

163 "The `np._core.MachAr` is considered private API (NumPy 1.24)", 

164 DeprecationWarning, stacklevel=2, 

165 ) 

166 return _machar.MachAr 

167 raise AttributeError(f"Module {__name__!r} has no attribute {name!r}") 

168 

169 

170import copyreg 

171 

172copyreg.pickle(ufunc, _ufunc_reduce) 

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

174 

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

176del copyreg, _ufunc_reduce, _DType_reduce 

177 

178from numpy._pytesttester import PytestTester 

179test = PytestTester(__name__) 

180del PytestTester