Help me to catch this exception :-)

M

Mardy

Hi all,
I have written a slightly modified version of the CGIHTTPServer; the
difference with the module included in the standard python distrubution is
that, since I know that the CGI to be executed is a python script, I
always execute it via the execfile() function, instead of opening a
separate process.
But there's a problem: in the CGI, I'm using the cgitb module to catch
exceptions and send them to the browser. This doesn't work. Here is what
happens when a exception is raised from the CGI:

1) the client (HTTP browser) waits for data, which don't came (while I'd
like the cgitb module to display the exception);
2) the exception is propagated to the CGIHTTPServer, which shows it on
the standard error (where noone will ever see it);
3) when I interrupt the HTTP server with Ctrl+C, the KeyboardInterrupt
exception is sent (beautified by cgitb) to the HTTP browser!!!

I can correct (3) by saving the value of sys.excepthook before the call to
execfile and restoring it in the finally clause, but this doesn't solve my
problem. Is there a way to run a python file as if it were a separate
process? That is, forbidding execfile() to modify anything in the caller?
 
M

Mardy

Le die Thu, 24 Nov 2005 07:09:26 -0800, Fuzzyman ha scribite:
I don't know the answer, but I like the problem. :)

What happens with the standard CGIHttpServer ?

The standard CGIHttpServer uses fork() on Unix and popen() on Windows;
since both these functions run the module in a separate process, the
problem doesn't show up. But on Mac, it uses execfile(), so I guess the
problem is the same.

For the moment, I've come up with a hybrid solution: in
CGIHTTPServer, doing "import cgitb; cgitb.enable()" before the try block
which calls execfile(), and add an empty except clause which prints the
exception:

except:
cgitb.handler()

There's still something not working perfectly, but I think this is close
to the solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top