Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#20020 closed Bug (invalid)

ValueError for FileField if the file is closed in clean method

Reported by: whitews@… Owned by: nobody
Component: File uploads/storage Version: 1.4
Severity: Normal Keywords: FileField ValueError
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no



Say you have a Model with a FileField, and you need to override clean to do some validation on the file before it is saved. If the file is closed within clean and the file is over 2.5MB (or larger than FILE_UPLOAD_MAX_MEMORY_SIZE) a "ValueError: I/O operation on closed file" will result. If you attempt to re-open the file in clean to avoid this error a different ValueError will result stating "The file cannot be reopened." This is particularly a problem if the verification of a file uses some other library that may open and close the file.

I'm using Django 1.4.3, and have seen the issue under the Django web development server as well as under Apache w/mod_wsgi. Here's an example to illustrate the issue:

from string import join
from django.db import models
from FileUploadExample.settings import MEDIA_ROOT

def some_file_operation(file):

def upload_file_path(instance, filename):
    upload_dir = join([MEDIA_ROOT, str(filename)], '')
    return upload_dir

class UploadedFile(models.Model):
    uploaded_file = models.FileField(upload_to=upload_file_path)

    def clean(self):
        # some call to somewhere to validate the file

Change History (5)

comment:1 Changed 4 years ago by whitews@…

Keywords: FileField ValueError added
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Type: UncategorizedBug

comment:2 Changed 4 years ago by Aymeric Augustin

Resolution: invalid
Status: newclosed

TemporaryUploadedFile relies on Python's tempfile.NamedTemporaryFile to make sure the temporary file will be eventually deleted; in fact, it's deleted as soon as it's closed.

That's why you cannot reopen it, and that isn't going to change — unless you come up with another way to guarantee that the temporary file gets deleted eventually.

At least on Unix it seems that the file can be re-opened by name. Use self.uploaded_file.temporary_file_path.

comment:3 Changed 4 years ago by whitews@…

I'm not quite following, if it is deleted how can the file be re-opened?

This is the issue I am seeing, once the file is closed there is no way to proceed to save the model instance.

comment:4 Changed 4 years ago by Aymeric Augustin

Sorry I wasn't clear -- you can re-open it by name as long as you haven't closed the original.

comment:5 Changed 4 years ago by whitews@…

Got it, this works. I just create another file handle to give to a routine that closes files. Thanks

Note: See TracTickets for help on using tickets.
Back to Top