codekingpro/portable-devtools
114k
1from numpy import ufunc
2from numpy._core import _multiarray_umath
3
4for item in _multiarray_umath.__dir__():
5 # ufuncs appear in pickles with a path in numpy.core._multiarray_umath
6 # and so must import from this namespace without warning or error
7 attr = getattr(_multiarray_umath, item)
8 if isinstance(attr, ufunc):
9 globals()[item] = attr
10
11
12def __getattr__(attr_name):
13 from numpy._core import _multiarray_umath
14
15 from ._utils import _raise_warning
16
17 if attr_name in {"_ARRAY_API", "_UFUNC_API"}:
18 import sys
19 import textwrap
20 import traceback
21
22 from numpy.version import short_version
23
24 msg = textwrap.dedent(f"""
25 A module that was compiled using NumPy 1.x cannot be run in
26 NumPy {short_version} as it may crash. To support both 1.x and 2.x
27 versions of NumPy, modules must be compiled with NumPy 2.0.
28 Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.
29
30 If you are a user of the module, the easiest solution will be to
31 downgrade to 'numpy<2' or try to upgrade the affected module.
32 We expect that some modules will need time to support NumPy 2.
33
34 """)
35 tb_msg = "Traceback (most recent call last):"
36 for line in traceback.format_stack()[:-1]:
37 if "frozen importlib" in line:
38 continue
39 tb_msg += line
40
41 # Also print the message (with traceback). This is because old versions
42 # of NumPy unfortunately set up the import to replace (and hide) the
43 # error. The traceback shouldn't be needed, but e.g. pytest plugins
44 # seem to swallow it and we should be failing anyway...
45 sys.stderr.write(msg + tb_msg)
46 raise ImportError(msg)
47
48 ret = getattr(_multiarray_umath, attr_name, None)
49 if ret is None:
50 raise AttributeError(
51 "module 'numpy.core._multiarray_umath' has no attribute "
52 f"{attr_name}")
53 _raise_warning(attr_name, "_multiarray_umath")
54 return ret
55
56
57del _multiarray_umath, ufunc
58 