> My impression was that it was a bug in Python since the servlet's
> module *is* getting reloaded. Maybe if you can recreate the issue
> in a small standalone example, you can ask comp.lang.python for help.

Thanks for pointing me into the right direction.

I checked this and you're right. The problem is Python, or specifically the linecache module used by traceback. The module is reloaded correctly, but the old source is still in the linecache.

I have attached an example.
* A test_module with an erroneous line is loaded:
  - the traceback is ok
* Then, a line is added to the test_module
  and the module is reloaded:
  - now the traceback displays the wrong line
* Finally, linecache.checkcache() is called
  and the module is reloaded:
  - now the traceback is ok again

I noticed that this problem exists only with Python 2.3. It seems to be fixed in Python 2.4 already, so I think there is no need to discuss it in comp.lang.python.

What I could do is add a linecache.checkcache() as a workaround.

There are four modules where this could be added - I'm unsere where is the most appropriate place:

ServletFactory, ImportSpy, AutoReloadingAppServer or ExceptionHandler?

-- Christoph
import imp, linecache, traceback, time

def import_test():
    print
    fp, pathname, stuff = imp.find_module('test_module')
    try:
        module = imp.load_module('test_module', fp, pathname, stuff)
    except:
        traceback.print_exc()
    fp.close()

script = '''print 'Loading test module...'
print 1/0
# no problem here
'''

file('test_module.py', 'w').write(script)

import_test()

time.sleep(2)

script = '''print 'Added line...'
''' + script

file('test_module.py', 'w').write(script)

import_test()

try:
    linecache.checkcache('test_module.py')
except TypeError: # Python < 2.4 has not filename parameter
    linecache.checkcache()

import_test()

Reply via email to