1"""

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

3

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

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

6

7"""

8

9 from numpy.version import version as __version__

10

11 import os

12 import warnings

13

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

15# python threads or processes to one core

16 env_added = []

17 for 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

22 try:

23 from . import multiarray

24 except ImportError as exc:

25 import sys

26 msg = """

27

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

29

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

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

32 installed.

33

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

35

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

37

38 Please note and check the following:

39

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

41 * The NumPy version is: "%s"

42

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

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

45

46 Original error was: %s

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

48 __version__, exc)

49 raise ImportError(msg)

50 finally:

51 for envkey in env_added:

52 del os.environ[envkey]

53 del envkey

54 del env_added

55 del os

56

57 from . 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

61 if 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

71 from . import numerictypes as nt

72 multiarray.set_typeDict(nt.sctypeDict)

73 from . import numeric

74 from .numeric import *

75 from . import fromnumeric

76 from .fromnumeric import *

77 from . import defchararray as char

78 from . import records

79 from . import records as rec

80 from .records import record, recarray, format_parser

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

82 from .memmap import *

83 from .defchararray import chararray

84 from . import function_base

85 from .function_base import *

86 from . import _machar

87 from ._machar import *

88 from . import getlimits

89 from .getlimits import *

90 from . import shape_base

91 from .shape_base import *

92 from . import einsumfunc

93 from .einsumfunc import *

94 del nt

95

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

97 from .numeric import absolute as abs

98

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

100# appearing in an import-time traceback

101 from . import _add_newdocs

102 from . import _add_newdocs_scalars

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

104 from . import _dtype_ctypes

105 from . import _internal

106 from . import _dtype

107 from . import _methods

108

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

110__all__ += numeric.__all__

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

112__all__ += ['chararray']

113__all__ += function_base.__all__

114__all__ += getlimits.__all__

115__all__ += shape_base.__all__

116__all__ += einsumfunc.__all__

117

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

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

120# to break loading them.

121 def _ufunc_reconstruct(module, name):

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

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

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

125 # scipy.special.expit for instance.

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

127 return getattr(mod, name)

128

129

130 def _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

139 def _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

146 def _DType_reduce(DType):

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

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

149 if DType is dtype:

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

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

152 return _DType_reconstruct, (scalar_type,)

153

154

155 def __getattr__(name):

156 # Deprecated 2021-10-20, NumPy 1.22

157 if name == "machar":

158 warnings.warn(

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

160 DeprecationWarning, stacklevel=2,

161 )

162 return _machar

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

164

165

166 import copyreg

167

168 copyreg.pickle(ufunc, _ufunc_reduce)

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

170

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

172 del copyreg

173 del _ufunc_reduce

174 del _DType_reduce

175

176 from numpy._pytesttester import PytestTester

177 test = PytestTester(__name__)

178 del PytestTester