[Scipy-svn] r5514 - in trunk/scipy/special: . cephes
scipy-svn at scipy.org
scipy-svn at scipy.org
Fri Jan 23 18:14:07 EST 2009
Author: ptvirtan
Date: 2009-01-23 17:13:54 -0600 (Fri, 23 Jan 2009)
New Revision: 5514
Modified:
trunk/scipy/special/_cephesmodule.c
trunk/scipy/special/cephes/mtherr.c
Log:
Show Cephes errors as Python warnings instead of fprintfing.
This changes cephes/mtherr.c to signal warnings via PyErr_WarnEx rather
than fprintf. (Though error printing is disabled by default, as previously.)
Modified: trunk/scipy/special/_cephesmodule.c
===================================================================
--- trunk/scipy/special/_cephesmodule.c 2009-01-23 09:24:54 UTC (rev 5513)
+++ trunk/scipy/special/_cephesmodule.c 2009-01-23 23:13:54 UTC (rev 5514)
@@ -6,6 +6,7 @@
* Copyright 1999 Travis E. Oliphant
* Revisions 2002 (added functions from cdflib)
*/
+#include <stdarg.h>
#include "Python.h"
#include "numpy/arrayobject.h"
@@ -1050,6 +1051,23 @@
}
+static PyObject *scipy_special_SpecialFunctionWarning = NULL;
+
+void scipy_special_raise_warning(char *fmt, ...)
+{
+ NPY_ALLOW_C_API_DEF
+ char msg[1024];
+ va_list ap;
+
+ va_start(ap, fmt);
+ PyOS_vsnprintf(msg, 1024, fmt, ap);
+ va_end(ap);
+
+ NPY_ALLOW_C_API
+ PyErr_WarnEx(scipy_special_SpecialFunctionWarning, msg, 2);
+ NPY_DISABLE_C_API
+}
+
static char errprint_doc[] = \
"errprint({flag}) sets the error printing flag for special functions\n" \
" (from the cephesmodule). The output is the previous state.\n" \
@@ -1102,6 +1120,14 @@
/* Load the cephes operators into the array module's namespace */
Cephes_InitOperators(d);
+ /* Register and add the warning type object */
+ scipy_special_SpecialFunctionWarning = PyErr_NewException(
+ "scipy.special._cephes.SpecialFunctionWarning",
+ PyExc_RuntimeWarning,
+ NULL);
+ PyModule_AddObject(m, "SpecialFunctionWarning",
+ scipy_special_SpecialFunctionWarning);
+
/* Check for errors */
if (PyErr_Occurred())
Py_FatalError("can't initialize module _cephes");
Modified: trunk/scipy/special/cephes/mtherr.c
===================================================================
--- trunk/scipy/special/cephes/mtherr.c 2009-01-23 09:24:54 UTC (rev 5513)
+++ trunk/scipy/special/cephes/mtherr.c 2009-01-23 23:13:54 UTC (rev 5514)
@@ -57,6 +57,7 @@
#include <stdio.h>
#include "mconf.h"
+void scipy_special_raise_warning(char *fmt, ...);
int scipy_special_print_error_messages = 0;
int merror = 0;
@@ -66,42 +67,39 @@
* in mconf.h.
*/
static char *ermsg[8] = {
-"unknown", /* error code 0 */
-"domain", /* error code 1 */
-"singularity", /* et seq. */
-"overflow",
-"underflow",
-"total loss of precision",
-"partial loss of precision",
-"too many iterations"
+ "unknown", /* error code 0 */
+ "domain", /* error code 1 */
+ "singularity", /* et seq. */
+ "overflow",
+ "underflow",
+ "total loss of precision",
+ "partial loss of precision",
+ "too many iterations"
};
-int mtherr( name, code )
-char *name;
-int code;
+int mtherr(char *name, int code)
{
+ /* Display string passed by calling program,
+ * which is supposed to be the name of the
+ * function in which the error occurred:
+ */
-/* Display string passed by calling program,
- * which is supposed to be the name of the
- * function in which the error occurred:
- */
+ /* Set global error message word */
+ merror = code;
-/* Set global error message word */
-merror = code;
-
-/* Display error message defined
- * by the code argument.
- */
-if( (code <= 0) || (code >= 8) )
+ /* Display error message defined
+ * by the code argument.
+ */
+ if ((code <= 0) || (code >= 8))
code = 0;
-if (scipy_special_print_error_messages) {
- printf( "\n%s ", name );
- printf( "%s error\n", ermsg[code] );
-}
-/* Return to calling
- * program
- */
-return( 0 );
+ if (scipy_special_print_error_messages) {
+ scipy_special_raise_warning("%s: %s error", name, ermsg[code]);
+ }
+
+ /* Return to calling
+ * program
+ */
+ return (0);
}
More information about the Scipy-svn
mailing list