[Python-Dev] Need to hook Py_FatalError

Jeff Epler jepler at unpythonic.net
Wed May 4 18:12:26 CEST 2005


On Wed, May 04, 2005 at 03:29:33PM +0000, M.Utku K. wrote:
> James William Pye <python at jwp.name> wrote in
> news:1115218734.62180.119.camel at localhost: 
> > Why should reinitialization be allowed at all? Seems to me that this
> > feature should be exclusively reserved for an embedding application to
> > handle the fatal in an application specific way; ie ereport(FATAL,()) in
> > PostgreSQL, which quickly exits after some cleanup. Why should an
> > extension module be allowed to set this, or reset it?
> 
> What if more than one extension needs it ?

I agree with James; As I imagine this feature, it is for programs that
embed Python, not for extensions.  Whether the hook would be written to
prevent this from being done, or whether it would just be documented as
"for embedders only", I don't care.

In my own application, I didn't use a setter function, I just created a
new global variable.  This works fine for me.  It doesn't prevent the
(abusive, in my view) hooking of the error handler by any old extension,
but since my application doesn't currently import shared modules it
doesn't matter.

--- /tmp/Python-2.3/Python/pythonrun.c	2003-07-15 20:54:38.000000000 -0500
+++ ./pythonrun.c	2005-04-11 13:32:39.000000000 -0500
@@ -1435,9 +1435,14 @@
 
 /* Print fatal error message and abort */
 
+void (*Py_FatalErrorHandler)(const char *msg) = NULL;
 void
 Py_FatalError(const char *msg)
 {
+        if(Py_FatalErrorHandler != NULL) { 
+                Py_FatalErrorHandler(msg);
+                fprintf(stderr, "PyFatalErrorHandler returned\n");
+        }
 	fprintf(stderr, "Fatal Python error: %s\n", msg);
 #ifdef MS_WINDOWS
 	OutputDebugString("Fatal Python error: ");
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-dev/attachments/20050504/3c3b1daf/attachment.pgp


More information about the Python-Dev mailing list