[Sorry if this is a repeat, but it didn't seem to go through, so I'm
re-sending.]
Yay! I've come up with a fix to a mysterious problem that's been
haunting me for a while!
My TurboGears application was constantly crashing after reloading, so I
had to restart it any time I made a change.
I'm doing some funky stuff with dynamically calling templates from
templates, in addition to the usual way of calling them by returning
dictionaries from controller methods.
But my app would always crash after reloading or encountering an
exception, because it was trying to import sitetemplate from
turbogears.view.sitetemplate instead of
turbogears.view.templates.sitetemplate.
The first time the application runs, when I access one of the normal
administration pages first, it works fine, and then I can access the
dynamic template pages ok, as long as there are no exceptions. But if I
access a dynamic template page first, or if it hits an exception for
any reason, or reloads because any files changed, then it would crash
trying to load sitetemplate from the wrong place.
The cause of the problem turned out to be some code in setuptools'
pkg_resources module. The NullProvider base class (from which other
providers like the EggProvider inherit) had an __init__ method that was
grabbing the __file__ out of the module into fileName, assuming it was
a path ending with the file name of '__init__.pyc', and calling
os.path.dirname(filename) to get the directory name to import. When the
__file__ did end with '.../__init__.pyc', it worked fine. But that
would mysteriously change after reloads or exceptions! In the cases
where it would crash, the __file__ actually contained just the
DIRECTORY name without a trailing slash, instead of '.../__init__.pyc'.
So I inserted a test of os.path.isdir(fileName) to guard the code that
stripped off the file name with dirname, and it works now! I am so
happy!
Here is the old version that has the bug:
def __init__(self, module):
self.loader = getattr(module, '__loader__', None)
self.module_path = os.path.dirname(getattr(module, '__file__',
''))
Here is the version I fixed to test for the directory name in
module.__file__:
def __init__(self, module):
self.loader = getattr(module, '__loader__', None)
fileName = getattr(module, '__file__', '')
if fileName and not os.path.isdir(fileName):
fileName = os.path.dirname(fileName)
self.module_path = filename
I don't know why the __file__ is getting the directory name after
reloading, instead of the __init__.pyc. Maybe this is a symptom of
another problem, but I don't know enough about how that sausage factory
works to say. (I love yummy python sausage, but it's still kinda gross
to see how it's made.)
I'm running the latest version of TurboGears out of subversion (maybe a
few days old at most), and using setuptools-0.6a10-py2.4.egg.
This article by Ben Bangert was very helpful in figuring out what was
going on with setuptools:
http://www.groovie.org/articles/2005/09/29/setuptools-and-python-paste
-Don
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---