Skip to content

Commit

Permalink
0.7.x: Show Cephes errors as Python warnings instead of fprintfing.
Browse files Browse the repository at this point in the history
This changes cephes/mtherr.c to signal warnings via PyErr_WarnEx rather
than fprintf. (Though error printing is disabled by default, as previously.)

Backported: http://svn.scipy.org/svn/scipy/trunk@5514 d6536bca-fef9-0310-8506-e4c0a848fbcf
(cherry picked from commit 8ea62852f764d1719aa65ebe69684fa597c5c357)
  • Loading branch information
cournape committed May 26, 2009
1 parent 1ea61b1 commit 35a7cb0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 30 deletions.
26 changes: 26 additions & 0 deletions scipy/special/_cephesmodule.c
Expand Up @@ -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"
Expand Down Expand Up @@ -1050,6 +1051,23 @@ static void Cephes_InitOperators(PyObject *dictionary) {

}

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" \
Expand Down Expand Up @@ -1102,6 +1120,14 @@ PyMODINIT_FUNC init_cephes(void) {
/* 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");
Expand Down
58 changes: 28 additions & 30 deletions scipy/special/cephes/mtherr.c
Expand Up @@ -57,6 +57,7 @@ Direct inquiries to 30 Frost Street, Cambridge, MA 02140
#include <stdio.h>
#include "mconf.h"

void scipy_special_raise_warning(char *fmt, ...);
int scipy_special_print_error_messages = 0;

int merror = 0;
Expand All @@ -66,42 +67,39 @@ int merror = 0;
* 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);
}

0 comments on commit 35a7cb0

Please sign in to comment.