cgi-tb after os.chdir

Donn Cave donn at drizzle.com
Tue Jul 15 00:18:18 EDT 2003


Quoth schull at digitalgoods.com (Jon Schull):
| I find that cgitb fails to show error context if I've changed my
| default directory, so I tried to subclass cgitb and get it to save the
| directory we were in when the program started.  Didn't work.  I'd
| appreciate your eyes.
|
| Problem--this apparently fails to find my sourcefile when #os.chdir is
| uncommented from this cgi program
| .........................
|
| #! /usr/local/bin/python 
| import cgitb
| cgitb.enable()
|
| import os
| #os.chdir('../')
| 1/0
| .........................
| What I thought would work:
| .........................
| from cgitb import Hook
| import os
| import sys
|
| class myHook(Hook):
| 	def __init__(self, display=1, logdir=None, context=5,
| file=None,wkdir='xx'):
| 		self.wkdir=wkdir
| 		Hook.__init__(self, display=1, logdir=None, context=5, file=None)
|
| 	def handle(self, info=None):
| 		os.cwd(self.wkdir)
| 		Hook.handle(self,info)
|
| handler = myHook().handle
| def enable(display=1, logdir=None, context=5):
| 	wkdir=os.getcwd()
| 	sys.excepthook = myHook(display, logdir, context,wkdir=wkdir)
|
| .........................
| But in fact, my exception handler doesn't even seem to get installed.
| (I get python's usual tracebacks)
|
| I'm probably over my head here, but...

Not too badly, I think.

| What's wrong with the program where I attempt to subclass myHook?

Hm, did you call your enable() function?  Looks to me like the
handler = ... line is a decoy that doesn't do anything, incidentally.
If it were up to me, I might be tempted to make enable() a method
of the Hook class, only for convenience though.

| Is this a reasonable way to get cgitb to survive directory changes?

Yes, it could work.  I would use chdir() instead of cwd(), when
returning to the original working directory.  As you have probably
already found out, chdir also can compromise module imports;  if
your only problem is the quality of tracebacks, you're getting off
easy.

	Donn Cave, donn at drizzle.com




More information about the Python-list mailing list