Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/numpy/core/__init__.py: 74%

89 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

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: 18 ↛ 17line 18 didn't jump to line 17, because the condition on line 18 was never false

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 61 ↛ 63line 61 didn't jump to line 63, because the condition on line 61 was never true

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 as rec 

79from .records import record, recarray, format_parser 

80from .memmap import * 

81from .defchararray import chararray 

82from . import function_base 

83from .function_base import * 

84from . import _machar 

85from ._machar import * 

86from . import getlimits 

87from .getlimits import * 

88from . import shape_base 

89from .shape_base import * 

90from . import einsumfunc 

91from .einsumfunc import * 

92del nt 

93 

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

95from .numeric import absolute as abs 

96 

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

98# appearing in an import-time traceback 

99from . import _add_newdocs 

100from . import _add_newdocs_scalars 

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

102from . import _dtype_ctypes 

103from . import _internal 

104from . import _dtype 

105from . import _methods 

106 

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

108__all__ += numeric.__all__ 

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

110__all__ += ['chararray'] 

111__all__ += function_base.__all__ 

112__all__ += getlimits.__all__ 

113__all__ += shape_base.__all__ 

114__all__ += einsumfunc.__all__ 

115 

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

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

118# to break loading them. 

119def _ufunc_reconstruct(module, name): 

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

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

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

123 # scipy.special.expit for instance. 

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

125 return getattr(mod, name) 

126 

127 

128def _ufunc_reduce(func): 

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

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

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

132 # explicitly (Numba has ufuncs as attributes). 

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

134 return func.__name__ 

135 

136 

137def _DType_reconstruct(scalar_type): 

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

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

140 # DTypes become HeapTypes. 

141 return type(dtype(scalar_type)) 

142 

143 

144def _DType_reduce(DType): 

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

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

147 if DType is dtype: 

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

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

150 return _DType_reconstruct, (scalar_type,) 

151 

152 

153def __getattr__(name): 

154 # Deprecated 2021-10-20, NumPy 1.22 

155 if name == "machar": 

156 warnings.warn( 

157 "The `np.core.machar` module is deprecated (NumPy 1.22)", 

158 DeprecationWarning, stacklevel=2, 

159 ) 

160 return _machar 

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

162 

163 

164import copyreg 

165 

166copyreg.pickle(ufunc, _ufunc_reduce) 

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

168 

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

170del copyreg 

171del _ufunc_reduce 

172del _DType_reduce 

173 

174from numpy._pytesttester import PytestTester 

175test = PytestTester(__name__) 

176del PytestTester