FINANCIAL MODELLING PACKAGE IN PYTHON?
Michael P. Reilly
arcege at shore.net
Sat Jan 8 12:25:41 EST 2000
Robin Becker <robin at jessikat.demon.co.uk> wrote:
: In article <1265040530-3892638 at hypernet.com>, Gordon McMillan
: <gmcm at hypernet.com> writes
:>Robin Becker wrote:
:> > > |Also under
:>> certain extreme circumstances my code would like to report a >
:>> |total failure eg attempt to divide by error. I would like to ask
:>> more > |experienced pythoneers how arithmetic errors are handled
:>> using built in > |IEEE exceptions and how does one gracefully
:>> crash a python script? (eg > |an abend/abort).
:>
:>> >raise
:>> ArithmeticError, "Oops" >raise ZeroDivisionError, "Ouch" > >? >
:>
:>> yes that's python, but what's the best way to do it in an
:>> existing C library? -- Robin Becker
:>
:>PyErr_SetString(PyExc_ZeroDivisionError, "Ouch");
:>return NULL;
:>
:>(see bltinmodule.c for the complete list).
:>
:>> --
:>> http://www.python.org/mailman/listinfo/python-list
:>
:>
:>
:>- Gordon
:>
: I guess I could have made my query a bit clearer. When I call my
: extension in C I can pass inputs which may cause unexpected arithmetic
: exceptions. That's an efficiency saving. I could check every arithmetic
: op. Assuming I want speed is there a standard pythonic way eg
: signal+setjmp/longjmp to trap these errors?
You will want to check that Python C/API functions return NULL and
PyErr_Occurred() returns non-NULL (the "pythonic way").
/* add two numbers and check any exception */
if ((c = PyNumber_Add(a, b)) == NULL &&
PyErr_Occurred() != NULL) { /* exception raised */
if (PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
PyErr_SetString(PyExc_SystemError,
"how can we get a division by zero on an add");
return NULL;
} else if (!PyErr_ExceptionMatches(PyExc_ArithmeticError))
return NULL; /* propogate the exception */
else /* arithmetic error, try something else? */
PyErr_Clear();
}
}
<URL: http://www.python.org/doc/current/api/exceptionHandling.html>
: In the VBA interface I have implemented exception trapping using VC++
: __try blocks (I seem to remember this was because of Excel somehow).
: The current C uses a pair of global functions to handle I/O to
: stdout/err. What's a good way to provide pythonic output for a C
: extension module?
Which pair of global functions are you talking about?
If you mean output to sys.stdout and sys.stderr, then you can use
PySys_WriteStdout() and PySys_WriteStderr().
<URL: file:///sources/Python-1.5.2/Python/sysmodule.c>
If you are talking about real file descriptors (the term you've been
using in this thread), then you can create a Python file object wrapped
around it.
PyObject *opened_file;
{ FILE *fp;
fp = fdopen(opened_fd, "w+");
/* the NULL tells PyFile_FromFile not to close implicitly */
opened_file = PyFile_FromFile(fp, "<internal>", "w+", NULL);
}
PyFile_WriteString("This is to some file\n", opened_file);
Py_DECREF(opened_file); /* leaves file open, but reclaims obj */
<URL: http://www.python.org/doc/current/api/FileObject.html>
: One other thing I am a bit uncertain about is threading. If I call
: python inside my C extension must I worry about threading issues for the
: extension?
There are some things you need to worry about, yes - mostly about
access to global variables in the module itself. As long as you do
that within the interpreter lock, things should be fine.
More information about the Python-list
mailing list