From 34d4897596edd2c517ab57c68d55a55fde1bbcd8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nikola=20Forr=C3=B3?= <nforro@redhat.com>
Date: Wed, 4 Jan 2017 16:59:54 +0100
Subject: [PATCH] Override LAPACK XERBLA

Backported upstream commits:
374e0b4d2c6f44fdccccfefe0546b607e5291e64
f0b2dd7d5151878f2b4b3ea20ff551b27243f27d
---
 numpy/core/blasdot/python_xerbla.c | 51 +++++++++++++++++++++++++++++++++++
 numpy/core/setup.py                |  3 ++-
 numpy/linalg/lapack_litemodule.c   | 54 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 107 insertions(+), 1 deletion(-)
 create mode 100644 numpy/core/blasdot/python_xerbla.c

diff --git a/numpy/core/blasdot/python_xerbla.c b/numpy/core/blasdot/python_xerbla.c
new file mode 100644
index 0000000..bdf0b90
--- /dev/null
+++ b/numpy/core/blasdot/python_xerbla.c
@@ -0,0 +1,51 @@
+#include "Python.h"
+
+/*
+ * From f2c.h, this should be safe unless fortran is set to use 64
+ * bit integers. We don't seem to have any good way to detect that.
+ */
+typedef int integer;
+
+/*
+  From the original manpage:
+  --------------------------
+  XERBLA is an error handler for the LAPACK routines.
+  It is called by an LAPACK routine if an input parameter has an invalid value.
+  A message is printed and execution stops.
+
+  Instead of printing a message and stopping the execution, a
+  ValueError is raised with the message.
+
+  Parameters:
+  -----------
+  srname: Subroutine name to use in error message, maximum six characters.
+          Spaces at the end are skipped.
+  info: Number of the invalid parameter.
+*/
+
+int xerbla_(char *srname, integer *info)
+{
+        static const char format[] = "On entry to %.*s" \
+                " parameter number %d had an illegal value";
+        char buf[sizeof(format) + 6 + 4];   /* 6 for name, 4 for param. num. */
+
+        int len = 0; /* length of subroutine name*/
+#ifdef WITH_THREAD
+        PyGILState_STATE save;
+#endif
+
+        while( len<6 && srname[len]!='\0' )
+                len++;
+        while( len && srname[len-1]==' ' )
+                len--;
+#ifdef WITH_THREAD
+        save = PyGILState_Ensure();
+#endif
+        PyOS_snprintf(buf, sizeof(buf), format, len, srname, *info);
+        PyErr_SetString(PyExc_ValueError, buf);
+#ifdef WITH_THREAD
+        PyGILState_Release(save);
+#endif
+
+        return 0;
+}
diff --git a/numpy/core/setup.py b/numpy/core/setup.py
index a1000ae..495e163 100644
--- a/numpy/core/setup.py
+++ b/numpy/core/setup.py
@@ -899,12 +899,13 @@ def configuration(parent_package='',top_path=None):
         if blas_info:
             if ('NO_ATLAS_INFO',1) in blas_info.get('define_macros',[]):
                 return None # dotblas needs ATLAS, Fortran compiled blas will not be sufficient.
-            return ext.depends[:1]
+            return ext.depends[:2]
         return None # no extension module will be built
 
     config.add_extension('_dotblas',
                          sources = [get_dotblas_sources],
                          depends = [join('blasdot','_dotblas.c'),
+                                  join('blasdot', 'python_xerbla.c'),
                                   join('blasdot','cblas.h'),
                                   ],
                          include_dirs = ['blasdot'],
diff --git a/numpy/linalg/lapack_litemodule.c b/numpy/linalg/lapack_litemodule.c
index cc62382..4f8cab9 100644
--- a/numpy/linalg/lapack_litemodule.c
+++ b/numpy/linalg/lapack_litemodule.c
@@ -171,6 +171,9 @@ lapack_lite_dgeev(PyObject *NPY_UNUSED(self), PyObject *args)
             FNAME(dgeev)(&jobvl,&jobvr,&n,DDATA(a),&lda,DDATA(wr),DDATA(wi),
                          DDATA(vl),&ldvl,DDATA(vr),&ldvr,DDATA(work),&lwork,
                          &info);
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
 
     return Py_BuildValue("{s:i,s:c,s:c,s:i,s:i,s:i,s:i,s:i,s:i}","dgeev_",
                          lapack_lite_status__,"jobvl",jobvl,"jobvr",jobvr,
@@ -254,6 +257,9 @@ lapack_lite_dsyevd(PyObject *NPY_UNUSED(self), PyObject *args)
     lapack_lite_status__ = \
             FNAME(dsyevd)(&jobz,&uplo,&n,DDATA(a),&lda,DDATA(w),DDATA(work),
                           &lwork,IDATA(iwork),&liwork,&info);
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
 
     return Py_BuildValue("{s:i,s:c,s:c,s:i,s:i,s:i,s:i,s:i}","dsyevd_",
                          lapack_lite_status__,"jobz",jobz,"uplo",uplo,
@@ -341,6 +347,9 @@ lapack_lite_zheevd(PyObject *NPY_UNUSED(self), PyObject *args)
     lapack_lite_status__ = \
     FNAME(zheevd)(&jobz,&uplo,&n,ZDATA(a),&lda,DDATA(w),ZDATA(work),
                   &lwork,DDATA(rwork),&lrwork,IDATA(iwork),&liwork,&info);
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
 
     return Py_BuildValue("{s:i,s:c,s:c,s:i,s:i,s:i,s:i,s:i,s:i}","zheevd_",
                          lapack_lite_status__,"jobz",jobz,"uplo",uplo,"n",n,
@@ -380,6 +389,9 @@ lapack_lite_dgelsd(PyObject *NPY_UNUSED(self), PyObject *args)
             FNAME(dgelsd)(&m,&n,&nrhs,DDATA(a),&lda,DDATA(b),&ldb,
                           DDATA(s),&rcond,&rank,DDATA(work),&lwork,
                           IDATA(iwork),&info);
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
 
     return Py_BuildValue("{s:i,s:i,s:i,s:i,s:i,s:i,s:d,s:i,s:i,s:i}","dgelsd_",
                          lapack_lite_status__,"m",m,"n",n,"nrhs",nrhs,
@@ -407,6 +419,9 @@ lapack_lite_dgesv(PyObject *NPY_UNUSED(self), PyObject *args)
 
     lapack_lite_status__ = \
     FNAME(dgesv)(&n,&nrhs,DDATA(a),&lda,IDATA(ipiv),DDATA(b),&ldb,&info);
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
 
     return Py_BuildValue("{s:i,s:i,s:i,s:i,s:i,s:i}","dgesv_",
                          lapack_lite_status__,"n",n,"nrhs",nrhs,"lda",lda,
@@ -446,6 +461,9 @@ lapack_lite_dgesdd(PyObject *NPY_UNUSED(self), PyObject *args)
             FNAME(dgesdd)(&jobz,&m,&n,DDATA(a),&lda,DDATA(s),DDATA(u),&ldu,
                           DDATA(vt),&ldvt,DDATA(work),&lwork,IDATA(iwork),
                           &info);
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
 
     if (info == 0 && lwork == -1) {
             /* We need to check the result because
@@ -496,6 +514,9 @@ lapack_lite_dgetrf(PyObject *NPY_UNUSED(self), PyObject *args)
 
     lapack_lite_status__ = \
             FNAME(dgetrf)(&m,&n,DDATA(a),&lda,IDATA(ipiv),&info);
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
 
     return Py_BuildValue("{s:i,s:i,s:i,s:i,s:i}","dgetrf_",lapack_lite_status__,
                          "m",m,"n",n,"lda",lda,"info",info);
@@ -516,6 +537,9 @@ lapack_lite_dpotrf(PyObject *NPY_UNUSED(self), PyObject *args)
 
     lapack_lite_status__ = \
             FNAME(dpotrf)(&uplo,&n,DDATA(a),&lda,&info);
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
 
     return Py_BuildValue("{s:i,s:i,s:i,s:i}","dpotrf_",lapack_lite_status__,
                          "n",n,"lda",lda,"info",info);
@@ -540,6 +564,9 @@ lapack_lite_dgeqrf(PyObject *NPY_UNUSED(self), PyObject *args)
         lapack_lite_status__ = \
                 FNAME(dgeqrf)(&m, &n, DDATA(a), &lda, DDATA(tau),
                               DDATA(work), &lwork, &info);
+	if (PyErr_Occurred()) {
+            return NULL;
+	}
 
         return Py_BuildValue("{s:i,s:i,s:i,s:i,s:i,s:i}","dgeqrf_",
                              lapack_lite_status__,"m",m,"n",n,"lda",lda,
@@ -562,6 +589,9 @@ lapack_lite_dorgqr(PyObject *NPY_UNUSED(self), PyObject *args)
         TRY(check_object(work,NPY_DOUBLE,"work","NPY_DOUBLE","dorgqr"));
         lapack_lite_status__ = \
         FNAME(dorgqr)(&m, &n, &k, DDATA(a), &lda, DDATA(tau), DDATA(work), &lwork, &info);
+	if (PyErr_Occurred()) {
+            return NULL;
+	}
 
         return Py_BuildValue("{s:i,s:i}","dorgqr_",lapack_lite_status__,
                              "info",info);
@@ -601,6 +631,9 @@ lapack_lite_zgeev(PyObject *NPY_UNUSED(self), PyObject *args)
             FNAME(zgeev)(&jobvl,&jobvr,&n,ZDATA(a),&lda,ZDATA(w),ZDATA(vl),
                          &ldvl,ZDATA(vr),&ldvr,ZDATA(work),&lwork,
                          DDATA(rwork),&info);
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
 
     return Py_BuildValue("{s:i,s:c,s:c,s:i,s:i,s:i,s:i,s:i,s:i}","zgeev_",
                          lapack_lite_status__,"jobvl",jobvl,"jobvr",jobvr,
@@ -641,6 +674,9 @@ lapack_lite_zgelsd(PyObject *NPY_UNUSED(self), PyObject *args)
     lapack_lite_status__ = \
     FNAME(zgelsd)(&m,&n,&nrhs,ZDATA(a),&lda,ZDATA(b),&ldb,DDATA(s),&rcond,
                   &rank,ZDATA(work),&lwork,DDATA(rwork),IDATA(iwork),&info);
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
 
     return Py_BuildValue("{s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i}","zgelsd_",
                          lapack_lite_status__,"m",m,"n",n,"nrhs",nrhs,"lda",lda,
@@ -667,6 +703,9 @@ lapack_lite_zgesv(PyObject *NPY_UNUSED(self), PyObject *args)
 
     lapack_lite_status__ = \
     FNAME(zgesv)(&n,&nrhs,ZDATA(a),&lda,IDATA(ipiv),ZDATA(b),&ldb,&info);
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
 
     return Py_BuildValue("{s:i,s:i,s:i,s:i,s:i,s:i}","zgesv_",
                          lapack_lite_status__,"n",n,"nrhs",nrhs,"lda",lda,
@@ -708,6 +747,9 @@ lapack_lite_zgesdd(PyObject *NPY_UNUSED(self), PyObject *args)
     FNAME(zgesdd)(&jobz,&m,&n,ZDATA(a),&lda,DDATA(s),ZDATA(u),&ldu,
                   ZDATA(vt),&ldvt,ZDATA(work),&lwork,DDATA(rwork),
                   IDATA(iwork),&info);
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
 
     return Py_BuildValue("{s:i,s:c,s:i,s:i,s:i,s:i,s:i,s:i,s:i}","zgesdd_",
                          lapack_lite_status__,"jobz",jobz,"m",m,"n",n,
@@ -732,6 +774,9 @@ lapack_lite_zgetrf(PyObject *NPY_UNUSED(self), PyObject *args)
 
     lapack_lite_status__ = \
     FNAME(zgetrf)(&m,&n,ZDATA(a),&lda,IDATA(ipiv),&info);
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
 
     return Py_BuildValue("{s:i,s:i,s:i,s:i,s:i}","zgetrf_",
                          lapack_lite_status__,"m",m,"n",n,"lda",lda,"info",info);
@@ -751,6 +796,9 @@ lapack_lite_zpotrf(PyObject *NPY_UNUSED(self), PyObject *args)
     TRY(check_object(a,NPY_CDOUBLE,"a","NPY_CDOUBLE","zpotrf"));
     lapack_lite_status__ = \
     FNAME(zpotrf)(&uplo,&n,ZDATA(a),&lda,&info);
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
 
     return Py_BuildValue("{s:i,s:i,s:i,s:i}","zpotrf_",
                          lapack_lite_status__,"n",n,"lda",lda,"info",info);
@@ -774,6 +822,9 @@ lapack_lite_zgeqrf(PyObject *NPY_UNUSED(self), PyObject *args)
 
         lapack_lite_status__ = \
         FNAME(zgeqrf)(&m, &n, ZDATA(a), &lda, ZDATA(tau), ZDATA(work), &lwork, &info);
+	if (PyErr_Occurred()) {
+            return NULL;
+	}
 
         return Py_BuildValue("{s:i,s:i,s:i,s:i,s:i,s:i}","zgeqrf_",lapack_lite_status__,"m",m,"n",n,"lda",lda,"lwork",lwork,"info",info);
 }
@@ -797,6 +848,9 @@ lapack_lite_zungqr(PyObject *NPY_UNUSED(self), PyObject *args)
         lapack_lite_status__ = \
         FNAME(zungqr)(&m, &n, &k, ZDATA(a), &lda, ZDATA(tau), ZDATA(work),
                       &lwork, &info);
+	if (PyErr_Occurred()) {
+            return NULL;
+	}
 
         return Py_BuildValue("{s:i,s:i}","zungqr_",lapack_lite_status__,
                              "info",info);
-- 
2.7.4

