summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorda-woods <dw-git@d-woods.co.uk>2022-07-31 05:42:21 +0100
committerGitHub <noreply@github.com>2022-07-31 06:42:21 +0200
commit66534408d3d8d6132e5b4e4061481661ba2d190e (patch)
tree36b1436f33658b384242902bf21a8d7dd9874ca8
parenta864f9b8830fb80a24abb0d82cd9a28289e532a5 (diff)
downloadcython-66534408d3d8d6132e5b4e4061481661ba2d190e.tar.gz
Don't call __Pyx_ImportNumPyArrayTypeIfAvailable() for every fused def function (GH-4935)
Cython.Utils.OrderedSet did not have a `__bool__` function and so was always treated as "true". The mean that Cython always generated buffer-handling utility code (`if all_buffer_types:` in FusedNode.py) even when just handling scalar types.
-rw-r--r--Cython/Utils.py5
-rw-r--r--tests/compile/fused_buffers.pyx16
-rw-r--r--tests/compile/fused_no_numpy.pyx13
3 files changed, 34 insertions, 0 deletions
diff --git a/Cython/Utils.py b/Cython/Utils.py
index 0c86ca044..fa4801731 100644
--- a/Cython/Utils.py
+++ b/Cython/Utils.py
@@ -579,6 +579,11 @@ class OrderedSet(object):
self._list.append(e)
self._set.add(e)
+ def __bool__(self):
+ return bool(self._set)
+
+ __nonzero__ = __bool__
+
# Class decorator that adds a metaclass and recreates the class with it.
# Copied from 'six'.
diff --git a/tests/compile/fused_buffers.pyx b/tests/compile/fused_buffers.pyx
new file mode 100644
index 000000000..73b0315ed
--- /dev/null
+++ b/tests/compile/fused_buffers.pyx
@@ -0,0 +1,16 @@
+# mode: compile
+
+# cython: test_assert_c_code_has = __Pyx_ImportNumPyArrayTypeIfAvailable
+# cython: test_assert_c_code_has = ndarray
+
+# counterpart test to fused_no_numpy - buffer types are compared against Numpy
+# dtypes as a quick test. fused_no_numpy tests that the mechanism isn't
+# accidentally generated, while this just confirms that the same mechanism is
+# still in use
+
+ctypedef fused IntOrFloat:
+ int
+ float
+
+def f(IntOrFloat[:] x):
+ return x
diff --git a/tests/compile/fused_no_numpy.pyx b/tests/compile/fused_no_numpy.pyx
new file mode 100644
index 000000000..efb49c322
--- /dev/null
+++ b/tests/compile/fused_no_numpy.pyx
@@ -0,0 +1,13 @@
+# mode: compile
+
+# cython: test_fail_if_c_code_has = __Pyx_ImportNumPyArrayTypeIfAvailable
+
+ctypedef fused IntOrFloat:
+ int
+ float
+
+# This function does not use buffers so has no reason to import numpy to
+# look up dtypes. fused_buffers.pyx is the corresponding test for the case
+# where numpy is imported
+def f(IntOrFloat x):
+ return x