Mailing List Archive

Extension types as exceptions
Should it be possible to use extension types (implemented in C) as
exceptions?

Currently, this works partly, as you can 'raise' and 'except' them.
It doesn't work in the following code:

try:
... code raising exception
except EOFException, err:
pass

When the exception doesn't match any 'except' clause, it seems to be
reraised through the following code in ceval.c:

case END_FINALLY:
v = POP();
if (PyInt_Check(v)) {
why = (enum why_code) PyInt_AsLong(v);
if (why == WHY_RETURN)
retval = POP();
}
else if (PyString_Check(v) || PyClass_Check(v)) {
w = POP();
u = POP();
PyErr_Restore(v, w, u);
why = WHY_RERAISE;
break;
}
else if (v != Py_None) {
PyErr_SetString(PyExc_SystemError,
"'finally' pops bad exception");
why = WHY_EXCEPTION;
}
Py_DECREF(v);
break;

Only strings and python classes seem to be allowed.

--
Daniel Dittmar
daniel.dittmar@sap-ag.de
SAP AG, Basis Entwicklung Berlin
Extension types as exceptions [ In reply to ]
Daniel Dittmar <daniel.dittmar@sap-ag.de> wrote:

: Should it be possible to use extension types (implemented in C) as
: exceptions?

You do not want to use types as exceptions, you want to use either
strings or exceptions.

: Currently, this works partly, as you can 'raise' and 'except' them.
: It doesn't work in the following code:

: try:
: ... code raising exception
: except EOFException, err:
: pass

: Only strings and python classes seem to be allowed.

And C exception classes created with PyErr_NewException(). This
creates a subclass of "Exception" which can be used to raise and catch
exceptions.

You might want to read:
http://www.python.org/doc/current/api/exceptionHandling.html and
http://starship.python.net/crew/arcege/extwriting/pyext.html (under the
section "Making an exception").

-Arcege
Extension types as exceptions [ In reply to ]
Daniel Dittmar wrote:
>
> Should it be possible to use extension types (implemented in C) as
> exceptions?

Good question. Even though it makes more sense to subclass the
ones in exceptions.py (this is possible from within C too) and
then include any additional information in the parameters, it should
basically be possible to "raise" any object as exception just like
in C++.

> Currently, this works partly, as you can 'raise' and 'except' them.
> It doesn't work in the following code:
>
> try:
> ... code raising exception
> except EOFException, err:
> pass

The code you show here doesn't have anything to do with this. The
functionality would have to be added to Python/errors.c and ceval.c
(plus maybe other files).

> When the exception doesn't match any 'except' clause, it seems to be
> reraised through the following code in ceval.c:
>
> case END_FINALLY:
> ...
>
> Only strings and python classes seem to be allowed.

Strings, classes and instances are allowed.

Hope that helps.
--
Marc-Andre Lemburg
______________________________________________________________________
Y2000: 141 days left
Business: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/