[mod_python] Image handling handler

Terry MacDonald terry.macdonald at dsl.pipex.com
Fri Sep 24 14:17:16 EDT 2004


Thanks for that.

My problem was that I was unaware that the img.save method internally
called a given objects write method.  Is this documented anywhere?

Is it only the write method that is required by an object to be a valid
file object for the img.save method? 

On Thu, 2004-09-23 at 18:31, Damjan wrote:
> >>> I'm probably missing something obvious here, but in the 'handle_thumb'
> >>> and 'handle_size' class methods if the image is not cached (or is older)
> >>> where is the method sending the image file back to the client? 
> >>> It appears only to resize and cache the image not return it.
> >> 
> >> img.save(self.req, format=img.format)
> >> 
> >> I'm "saving" the image to the req object. mod_python's request object is
> >> fine at emulating a file object.
> >
> > I'm probably having one of those dumb moments (It happens often!) but I
> > just don't get this.
> 
> :)
>  
> > How does the request object know its supposed to be a file object and
> > relay the image to the browser.  
> 
> The request object is "file object" compatible, that means that any
> python function that works with file objects can work with the request
> object too. Like this:
> 
> def fill_file_with_zeros(something, howmany):
> 	buff = '\x0' * howmany
> 	something.write(buff)
> 	return
> 
> Now I can call that function like this:
> 
> fp = file('/tmp/zeroed')
> fill_file_with_zeros(fp, 100)
> 
> or in mod_pytnon like this:
> 
> fill_file_with_zeros(req, 100)
> 
> The function doesn't care if "something" is really a file. Its enough if
> "something" has the write method.
> So in the second case, the function fill_file_with_zeros calls "req"s
> write method... what happens is that the client receives a file filled with
> NULLs (well, that's not very usefull ;) ).
> 
> > When/where does the req.write get invoked to send the image?
> 
> Inside img.save(...), img.save is just like the above fill_file_with_zeros,
> it doesn't care if you give it a real file, or a file like object (like
> req)... as long as it supports the write method.
> 
> Btw, I could've done it like this:
> 
> 	buff = StringIO.StringIO()
> 	img.save(buff,...)
> 	req.write(buff.getvalue())
> 
> But that would only use twice the memory for no reason.
-- 
Terry
Registered Linux User # 311806
www.taumu.com




More information about the Mod_python mailing list