summaryrefslogtreecommitdiff
path: root/numpy/lib/function_base.py
diff options
context:
space:
mode:
authorSebastian Berg <sebastianb@nvidia.com>2023-01-18 13:13:45 +0100
committerSebastian Berg <sebastianb@nvidia.com>2023-01-20 13:22:56 +0100
commit8334d57785997b6a3f6a3bae0ffea273632c65f1 (patch)
tree87f0819ca9ea27ba1b53238e9e2913bba9e3ad0d /numpy/lib/function_base.py
parenteb4ed7f649662a0969552036b62f201b45e244e6 (diff)
downloadnumpy-8334d57785997b6a3f6a3bae0ffea273632c65f1.tar.gz
API: Allow SciPy to get away with assuming `trapz` is a Python function
This wraps `trapz` into a proper python function and then copies all attributes expected on a Python function over from the "fake" version to the real one. This allows SciPy to pretend `trapz` is a Python function to create their own version.
Diffstat (limited to 'numpy/lib/function_base.py')
-rw-r--r--numpy/lib/function_base.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 11a5a3ad0..d494aed0f 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -4912,6 +4912,25 @@ def trapz(y, x=None, dx=1.0, axis=-1):
return ret
+if overrides.ARRAY_FUNCTION_ENABLED:
+ # If array-function is enabled (normal), we wrap everything into a C
+ # callable, which has no __code__ or other attributes normal Python funcs
+ # have. SciPy however, tries to "clone" `trapz` into a new Python function
+ # which requires `__code__` and a few other attributes.
+ # So we create a dummy clone and copy over its attributes allowing
+ # SciPy <= 1.10 to work: https://github.com/scipy/scipy/issues/17811
+ assert not hasattr(trapz, "__code__")
+
+ def _fake_trapz(y, x=None, dx=1.0, axis=-1):
+ return trapz(y, x=x, dx=dx, axis=axis)
+
+ trapz.__code__ = _fake_trapz.__code__
+ trapz.__globals__ = _fake_trapz.__globals__
+ trapz.__defaults__ = _fake_trapz.__defaults__
+ trapz.__closure__ = _fake_trapz.__closure__
+ trapz.__kwdefaults__ = _fake_trapz.__kwdefaults__
+
+
def _meshgrid_dispatcher(*xi, copy=None, sparse=None, indexing=None):
return xi