Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/numpy/core/__init__.py: 80%

89 statements  

« prev     ^ index     » next       coverage.py v7.0.5, created at 2023-01-17 06:27 +0000

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 

9from numpy.version import version as __version__ 

10 

11import os 

12import warnings 

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 

72multiarray.set_typeDict(nt.sctypeDict) 

73from . import numeric 

74from .numeric import * 

75from . import fromnumeric 

76from .fromnumeric import * 

77from . import defchararray as char 

78from . import records 

79from . import records as rec 

80from .records import record, recarray, format_parser 

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

82from .memmap import * 

83from .defchararray import chararray 

84from . import function_base 

85from .function_base import * 

86from . import _machar 

87from . import getlimits 

88from .getlimits import * 

89from . import shape_base 

90from .shape_base import * 

91from . import einsumfunc 

92from .einsumfunc import * 

93del nt 

94 

95from .fromnumeric import amax as max, amin as min, round_ as round 

96from .numeric import absolute as abs 

97 

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

99# appearing in an import-time traceback 

100from . import _add_newdocs 

101from . import _add_newdocs_scalars 

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

103from . import _dtype_ctypes 

104from . import _internal 

105from . import _dtype 

106from . import _methods 

107 

108__all__ = ['char', 'rec', 'memmap'] 

109__all__ += numeric.__all__ 

110__all__ += ['record', 'recarray', 'format_parser'] 

111__all__ += ['chararray'] 

112__all__ += function_base.__all__ 

113__all__ += getlimits.__all__ 

114__all__ += shape_base.__all__ 

115__all__ += einsumfunc.__all__ 

116 

117# We used to use `np.core._ufunc_reconstruct` to unpickle. This is unnecessary, 

118# but old pickles saved before 1.20 will be using it, and there is no reason 

119# to break loading them. 

120def _ufunc_reconstruct(module, name): 

121 # The `fromlist` kwarg is required to ensure that `mod` points to the 

122 # inner-most module rather than the parent package when module name is 

123 # nested. This makes it possible to pickle non-toplevel ufuncs such as 

124 # scipy.special.expit for instance. 

125 mod = __import__(module, fromlist=[name]) 

126 return getattr(mod, name) 

127 

128 

129def _ufunc_reduce(func): 

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

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

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

133 # explicitly (Numba has ufuncs as attributes). 

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

135 return func.__name__ 

136 

137 

138def _DType_reconstruct(scalar_type): 

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

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

141 # DTypes become HeapTypes. 

142 return type(dtype(scalar_type)) 

143 

144 

145def _DType_reduce(DType): 

146 # To pickle a DType without having to add top-level names, pickle the 

147 # scalar type for now (and assume that reconstruction will be possible). 

148 if DType is dtype: 

149 return "dtype" # must pickle `np.dtype` as a singleton. 

150 scalar_type = DType.type # pickle the scalar type for reconstruction 

151 return _DType_reconstruct, (scalar_type,) 

152 

153 

154def __getattr__(name): 

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

156 if name == "MachAr": 

157 warnings.warn( 

158 "The `np.core.MachAr` is considered private API (NumPy 1.24)", 

159 DeprecationWarning, stacklevel=2, 

160 ) 

161 return _machar.MachAr 

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

163 

164 

165import copyreg 

166 

167copyreg.pickle(ufunc, _ufunc_reduce) 

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

169 

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

171del copyreg 

172del _ufunc_reduce 

173del _DType_reduce 

174 

175from numpy._pytesttester import PytestTester 

176test = PytestTester(__name__) 

177del PytestTester