Help me to catch this exception :-)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mardy

    #1

    Help me to catch this exception :-)

    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 KeyboardInterru pt
    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?


    --
    Saluti,
    Mardy


  • Fuzzyman

    #2
    Re: Help me to catch this exception :-)

    I don't know the answer, but I like the problem. :-)

    What happens with the standard CGIHttpServer ?

    Fuzzyman
    http://www.voidspace.org.uk/python/index.shtml

    Comment

    • Mardy

      #3
      Re: Help me to catch this exception :-)

      Le die Thu, 24 Nov 2005 07:09:26 -0800, Fuzzyman ha scribite:
      [color=blue]
      > I don't know the answer, but I like the problem. :-)
      >
      > What happens with the standard CGIHttpServer ?[/color]

      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.


      --
      Saluti,
      Mardy


      Comment

      Working...