Opened 5 years ago

Closed 4 years ago

#13531 closed (invalid)

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

Reported by: aarond10 Owned by: tobias
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: UI/UX:

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 5 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 Changed 5 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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 Changed 5 years ago by douglasbagnall

  • Resolution invalid deleted
  • Status changed from closed to reopened

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

Changed 5 years ago by douglasbagnall

comment:3 Changed 5 years ago by douglasbagnall

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 Changed 4 years ago by tobias

  • Owner changed from nobody to tobias
  • Status changed from reopened to new

attempting to verify this bug...

comment:5 Changed 4 years ago by tobias

  • Resolution set to invalid
  • Status changed from new to closed

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