diff -r a6afd26caa8a Doc/library/cgi.rst --- a/Doc/library/cgi.rst Fri Jul 29 14:24:29 2011 -0500 +++ b/Doc/library/cgi.rst Sat Jul 30 00:34:21 2011 +0200 @@ -150,6 +150,21 @@ if not line: break linecount = linecount + 1 +If what you want is only to save the uploaded file, you may find more convenient +just to rename the temporary file created during the upload, using +:func:`os.link`:: + + fileitem = form["userfile"] + if fileitem.file: + fn = os.path.basename(fileitem.filename) + os.link(fileitem.file.name, os.path.join('uploaded_files', fn)) + +This is especially interesting if the uploaded file is rather large since +it will not be read in memory then written down a second time on disk. +Note that in this case it may be worth considering changing the default +temporary directory (using the commodities offered by :mod:`tempfile`) so that +there is really no need to copy the file contents across different filesystems. + If an error is encountered when obtaining the contents of an uploaded file (for example, when the user interrupts the form submission by clicking on a Back or Cancel button) the :attr:`done` attribute of the object for the diff -r a6afd26caa8a Lib/cgi.py --- a/Lib/cgi.py Fri Jul 29 14:24:29 2011 -0500 +++ b/Lib/cgi.py Sat Jul 30 00:34:21 2011 +0200 @@ -745,7 +745,7 @@ def __write(self, line): """line is always bytes, not string""" if self.__file is not None: - if self.__file.tell() + len(line) > 1000: + if self._binary_file or self.__file.tell() + len(line) > 1000: self.file = self.make_file() data = self.__file.getvalue() self.file.write(data) @@ -854,7 +854,7 @@ """ if self._binary_file: - return tempfile.TemporaryFile("wb+") + return tempfile.NamedTemporaryFile("wb+") else: return tempfile.TemporaryFile("w+", encoding=self.encoding, newline = '\n') diff -r a6afd26caa8a Lib/test/test_cgi.py --- a/Lib/test/test_cgi.py Fri Jul 29 14:24:29 2011 -0500 +++ b/Lib/test/test_cgi.py Sat Jul 30 00:34:21 2011 +0200 @@ -212,6 +212,7 @@ for k, exp in expect[x].items(): got = getattr(fs.list[x], k) self.assertEqual(got, exp) + self.assertTrue(fs['file'].file.name.startswith(tempfile.tempdir)) def test_fieldstorage_multipart_non_ascii(self): #Test basic FieldStorage multipart parsing