Opened 14 years ago

Closed 14 years ago

#13531 closed (invalid)

django.core.files.File throws an exception on _get_size

Reported by: aarond10 Owned by: Tobias McNulty
Component: Uncategorized Version: 1.2-beta
Severity: Keywords: File, Exception
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The File _get_size() function incorrectly uses self.file.name as a path.

When using a custom FileStorage like storage object, the files name and actual path may be different. This will trigger an AttributeError even though the file actually exists on disk and is readable.

Attachments (1)

django-find-size-without-name.diff (1006 bytes ) - added by douglasbagnall 14 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 by Russell Keith-Magee, 14 years ago

Resolution: invalid
Status: newclosed

I'm going to mark this invalid because I can't work out the actual bug that is being reported.

What attribute is causing an AttributeError? Why? What exact conditions cause the problem to occur? This is one of those situations where some sample code would be very helpful.

I don't deny you're seeing a problem, but there's a limit to what we can do to fix a problem when you don't give us the detail required to reproduce, diagnose and fix the problem. Feel free to reopen if you can provide more specifics.

comment:2 by douglasbagnall, 14 years ago

Resolution: invalid
Status: closedreopened

If the file does not have a name (for example, is a StringIO object), the AttributeError is at line 39 in django/core/files/base.py (svn trunk):

    def _get_size(self):
        if not hasattr(self, '_size'):
            if hasattr(self.file, 'size'):
                self._size = self.file.size
            elif os.path.exists(self.file.name):   #<-----------HERE
                self._size = os.path.getsize(self.file.name)
            else:
                raise AttributeError("Unable to determine the file's size.")
        return self._size

by douglasbagnall, 14 years ago

comment:3 by douglasbagnall, 14 years ago

The attached patch shows one way to find the size, though now I think about it, it is broken, because it leaves the file in a different state (you really need to find the initial offset with tell(), seek to the end, get size with tell, then seek back to the original place).

comment:4 by Tobias McNulty, 14 years ago

Owner: changed from nobody to Tobias McNulty
Status: reopenednew

attempting to verify this bug...

comment:5 by Tobias McNulty, 14 years ago

Resolution: invalid
Status: newclosed

The File class is just a wrapper for local file operations and has nothing to do with a custom file storage. file.name /is/ the path, so if you're using it for something else, then you're probably using it wrong. Every place that it's used in that class it's assumed to be the path (e.g., see the open() method). If you're trying to do something else, then you may need to create your own independent File class and use that in your custom FileStorage instead.

Closing this as invalid again...

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