Begin forwarded message:

> From: Ludvig Ericson <lud...@lericson.se>
> Date: February 22, 2009 22:37:16 GMT+01:00
> To: django-developers@googlegroups.com
> Subject: FileField.delete_file opening a file when deleting
>
> django.db.models.fields.files.FileField.delete_file often actually  
> winds up opening a file... to close it.
>
> I find this an "interesting" property - as it causes excessive  
> request processing when using something like Amazon S3 for the  
> storage backend.
>
> Here's the relevant part of a traceback I managed to get, so you can  
> see which path the code takes.
>
>  File "foobar/models.py", line 88, in switch_quux
>    field.delete_file(instance=old, sender=old)
>  File "django/db/models/fields/files.py", line 179, in delete_file
>    file.close()
>  File "django/core/files/base.py", line 147, in close
>    self.file.close()
>  File "/Users/ludvigericson/Library/Python/django/db/models/fields/ 
> files.py", line 43, in _get_file
>    self._file = self.storage.open(self.name, 'rb')
>  File "/Users/ludvigericson/Library/Python/django/core/files/ 
> storage.py", line 29, in open
>    file = self._open(name, mode)
>
> The reason it's reopened is that self.file.close triggers the  
> property's _get_file function, which conveniently opens the file if  
> it's not already opened.
>
> What puzzles me is how self.file can trigger the descriptor.  
> Python's descriptors only trigger when you do `C.x`, not `a = C.x ;  
> years later ; a.z`. self.file should be referring not to some proxy,  
> but to the actual file object. But I might be confused.
>
> Anyway, would people agree that this is a bug, or at least a  
> misfeature? I would really love avoiding this.
>
> I could also add that this particular case only arises if the file  
> is multiply referenced, or the default. At the top we see line 179:
>
>  174      if file and file.name != self.default and \
>  175          not sender._default_manager.filter(**{self.name:  
> file.name}):
>  176              file.delete(save=False)
>  177      elif file:
>  178          # Otherwise, just close the file, so it doesn't tie up  
> resources.
>  179          file.close(
>
> - Ludvig Ericson <lud...@lericson.se>
>

- Ludvig


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to