On Fri, 2008-09-26 at 11:34:16 -0500, Ian Bicking wrote:
> Dunk Fordyce wrote:
> > Hello,
> > 
> > When uploading files to my pylons app files are being put in /tmp. Is it 
> > possible to change this location? Im not 100% sure if this is a pylons 
> > thing or an apache setting ( or maybe even mod_wsgi? ). Unfortunately 
> > the /tmp dir on our server is mounted on quite a small partition.
> 
> I think the cgi module actually saving those files, and it is probably 
> just using tempfile or something which has its own mechanism to figure 
> out the temp dir (and I don't really know what it is).  If you look at 
> the source there might be an environmental variable or something that 
> you can set to control that.

It's cgi.FieldStorage.make_file method which is responsible for creating
temporary files.  If you look at it, it contains only these two lines:

  import tempfile
  return tempfile.TemporaryFile("w+b")

TemporaryFile creates file in system temp directory and immediately
unlinks it (this trick makes file accessible only to a process which has
opened it before unlinking).

As far as I know, the only way to change this is subclass
cgi.FieldStorage and override make_file method.  Even its docstring
suggests this ("Overridable: ...").

You can do this somewhere in the beginning of paste.request module:

  class MyFieldStorage(cgi.FieldStorage):
    def make_file(): ... # Your code here

  cgi.FieldStorage = MyFieldStorage

It's a bit hacky, but it works (I tested it myself).

BTW, maybe Paste author (it's Ian Bicking, right? :-) could come with
more convenient way (public API calls?) to control this without the need
of code modification in paste.request?

-- 
Audrius Kažukauskas

Attachment: pgp1YVwojXNCR.pgp
Description: PGP signature

Reply via email to