[ZODB-Dev] Investigating a Zope reference leak... tracking object
creation
Ben Last (ZODBDev)
zodbdev at benlast.com
Mon Jan 31 05:56:57 EST 2005
Chris Withers wrote:
> My gut reaction is that it's this extenal method that's causing your
> problems. Is it short enough to post here?
In slightly bastardized form, yes. This is normally invoked from ZPT using an idiom:
<img tal:replace="structure python:here.scripts.getGraphic(here,environment,'myimage.gif',...)"/>
def getGraphic(here, target, environment, style=None, css_class=None, border=None, wap=False, title=None, alt=None, longdesc=None, **kwargs):
"""Return an object that represents the graphic, with useful
attributes. This is rather like looking up the Image (or flash
file) but is more useful for the site because we can override the tag
generation code and handle flash-replacement.
See sitedesign.doc for the rationale that chose not to extend the
OFS.Image.Image class"""
request = here.REQUEST
assert environment, "An environment is required"
if style:
#TAL doesn't allow ';' in the source, so we use '$' instead.
style = style.replace('$',chr(59))
if flash is None:
flash = getattr(environment,'flash',False)
partner = getattr(environment, 'partner', "")
if target.endswith('.jpg') or target.endswith('.gif'):
swftarget = target[:-4]
else:
swftarget = None
#Create a dictish object to hold the attributes we return
graphic = Products.PythonScripts.standard.Object()
graphic['target'] = target
#Fix up the tag in case of failure.
graphic['tag'] = '<img alt="No image" src="/Images/none.jpg"/>'
img = None
#Try and find the image.
#find the partner folder and look up images from there (so that
#acquisition lets us override stuff).
imgc = None
if partner:
imgc = getattr(here.Images, partner)
else:
imgc = here.Images
#Look up the image.
img = None
try:
graphic['state'] = 'Image not found'
img = getattr(imgc,target)
graphic['state'] = 'Image title not found'
if not title: title = (img.title) or ("Image "+target)
if not alt: alt = title
if not longdesc: longdesc = title
graphic['title'] = title
tag = doTag(img,alt=alt,title=title,style=style,wap=wap,css_class=css_class,**kwargs).replace(request.BASE1,environment.relpath).strip()
graphic['imgtag'] = tag
graphic['tag'] = tag
graphic['state'] = 'Absolute URL not found'
url = img.absolute_url().replace(request.BASE1,environment.relpath)
graphic['imgurl'] = url
graphic['url'] = url
graphic['state'] = 'Image width not found'
graphic['width'] = img.width
graphic['state'] = 'Image width not found'
graphic['height'] = img.height
graphic['state'] = 'Image loaded successfully'
except AttributeError:
pass
#debugging leak, January 05
#be really sure that we don't leave references to acquired objects lying around
if img is not None:
del img
if imgc is not None:
del imgc
if swftarget:
graphic = flashOverride(here,swftarget,graphic)
return graphic
More information about the ZODB-Dev
mailing list