aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/libshiboken/sbknumpy.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'sources/shiboken6/libshiboken/sbknumpy.cpp')
-rw-r--r--sources/shiboken6/libshiboken/sbknumpy.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/sources/shiboken6/libshiboken/sbknumpy.cpp b/sources/shiboken6/libshiboken/sbknumpy.cpp
index 46b3af702..b6422e73f 100644
--- a/sources/shiboken6/libshiboken/sbknumpy.cpp
+++ b/sources/shiboken6/libshiboken/sbknumpy.cpp
@@ -7,17 +7,40 @@
# include <numpy/arrayobject.h>
#endif
+#include "helper.h"
#include "sbknumpycheck.h"
+#include "sbkcpptonumpy.h"
#include "sbknumpyview.h"
+#include <algorithm>
+
namespace Shiboken::Numpy
{
+#ifdef HAVE_NUMPY
+static void initNumPy()
+{
+ // PYSIDE-2404: Delay-initialize numpy from check() as it causes a
+ // significant startup delay (~770 allocations in memray)
+ static bool initialized = false;
+ if (initialized)
+ return;
+ initialized = true;
+ // Expanded from macro "import_array" in __multiarray_api.h
+ // Make sure to read about the magic defines PY_ARRAY_UNIQUE_SYMBOL etc.,
+ // when changing this or spreading the code over several source files.
+ if (_import_array() < 0)
+ PyErr_Print();
+}
+#endif // HAVE_NUMPY
+
bool check(PyObject *pyIn)
{
#ifdef HAVE_NUMPY
+ initNumPy();
return PyArray_Check(pyIn);
#else
+ SBK_UNUSED(pyIn);
return false;
#endif
}
@@ -28,6 +51,7 @@ bool check(PyObject *pyIn)
// translation unit (see comment at initNumPyArrayConverters()).
#include "sbknumpyview.cpp"
+#include "sbkcpptonumpy.cpp"
#ifdef HAVE_NUMPY
# include "sbknumpyarrayconverter.cpp"
#endif