Opened 15 years ago

Closed 11 years ago

Last modified 11 years ago

#11857 closed Bug (fixed)

TemporaryFile class is missing the closed - attribute

Reported by: andi100 Owned by: Christopher Adams <christopher.r.adams@…>
Component: File uploads/storage Version:
Severity: Normal Keywords: TemporaryFile closed windows
Cc: petar.maric@…, jdunck@…, pombredanne@…, christopher.r.adams@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Hi,

the TemporaryFile-class (core/files/temp.py), is missing the 'closed'-attribute. It inherits from FileProxyMixin, but this class also doesn't have a closed-attribute.

If I want to access the 'width' attribute of an ImageFieldFile, I get an "'TemporaryFile' object has no attribute 'closed'" - error.

Traceback:

File "D:\websites\picality\src\picalityproject\picality\models\image.py" in save
  98.         print self.image.width
File "D:\websites\django-trunk\django\core\files\images.py" in _get_width
  15.         return self._get_image_dimensions()[0]
File "D:\websites\django-trunk\django\core\files\images.py" in _get_image_dimensions
  24.             close = self.closed
File "D:\websites\django-trunk\django\db\models\fields\files.py" in _get_closed
  126.         return file is None or file.closed
File "D:\websites\django-trunk\django\core\files\base.py" in _get_closed
  51.         return not self.file or self.file.closed

Exception Type: AttributeError at /upload/
Exception Value: 'TemporaryFile' object has no attribute 'closed'

But somehow this error occurs only if I want to upload 2 image files in the same form -> because if I upload only one image, the self.image.file.file is a StringIO object, which has a 'closed' attribute.

Attachments (1)

quickfix patch for 11857 against Django 1.2 pre-alpha SVN-11477.diff (497 bytes ) - added by Petar Marić 15 years ago.

Download all attachments as: .zip

Change History (36)

comment:1 by Petar Marić, 15 years ago

Has patch: set
Needs tests: set
Owner: changed from nobody to Petar Marić
Patch needs improvement: set
Status: newassigned

Run into the same error, under different conditions.

I made a quick fix (works for me!) as I'm a bit busy these days to do things properly. When I catch a breath I'll improve the patch with comments and tests.

comment:2 by Petar Marić, 15 years ago

Cc: petar.maric@… added

comment:3 by Russell Keith-Magee, 15 years ago

Triage Stage: UnreviewedAccepted

comment:4 by danostrowski, 15 years ago

This issue is still present, as I've had the same problem.

However, the patch attached will not work if inserted as presented. 'temp.py' has had a lot of it's contents moved into 'tempfile.py' and there's apparently different classes now for temporary files based on whether you're using 'nt' or not. I am developing on Windows so this is of concern to me.

comment:5 by Peter Baumgartner, 14 years ago

Severity: Normal
Type: Bug

comment:6 by homeip127@…, 13 years ago

Easy pickings: unset
UI/UX: unset
Version: SVN

Bug still present! Django 1.3

File "C:\Python26\lib\site-packages\django\forms\forms.py" in full_clean
  269.         self._post_clean()
File "C:\Python26\lib\site-packages\django\forms\models.py" in _post_clean
  308.         self.instance = construct_instance(self, self.instance, opts.fields, opts.exclude)
File "C:\Python26\lib\site-packages\django\forms\models.py" in construct_instance
  53.         f.save_form_data(instance, cleaned_data[f.name])
File "C:\Python26\lib\site-packages\django\db\models\fields\files.py" in save_form_data
  281.             setattr(instance, self.name, data)
File "C:\Python26\lib\site-packages\django\db\models\fields\files.py" in __set__
  314.             self.field.update_dimension_fields(instance, force=True)
File "C:\Python26\lib\site-packages\django\db\models\fields\files.py" in update_dimension_fields
  382.             width = file.width
File "C:\Python26\lib\site-packages\django\core\files\images.py" in _get_width
  15.         return self._get_image_dimensions()[0]
File "C:\Python26\lib\site-packages\django\core\files\images.py" in _get_image_dimensions
  24.             close = self.closed
File "C:\Python26\lib\site-packages\django\db\models\fields\files.py" in _get_closed
  127.         return file is None or file.closed
File "C:\Python26\lib\site-packages\django\core\files\base.py" in _get_closed
  51.         return not self.file or self.file.closed

Exception Type: AttributeError at /photo/upload/
Exception Value: 'TemporaryFile' object has no attribute 'closed'

comment:7 by Karen Tracey, 13 years ago

Note it's is not necessary or or particularly helpful to post that still-open bugs are still recreatable. This bug is open, there is no indication anywhere in it that I can see that it has been fixed, so it's expected that it's still present. If you find an open bug that can no longer be recreated, that would be something to post (and possibly close the ticket), but posting that open bugs are still there is just noise.

in reply to:  4 comment:8 by Michael Silver, 13 years ago

Easy pickings: set
Patch needs improvement: unset

Replying to danostrowski:

This issue is still present, as I've had the same problem.

However, the patch attached will not work if inserted as presented. 'temp.py' has had a lot of it's contents moved into 'tempfile.py' and there's apparently different classes now for temporary files based on whether you're using 'nt' or not. I am developing on Windows so this is of concern to me.

The patch works fine for me, and I don't see why it wouldn't for you. What issues are you having with it? Also, tempfile is not part of Django, it is imported from the Python Standard Library.

temp.py just tells Django what class to use for temporary files. If you are using a Windows NT OS, temp.py will define a custom TemporaryFile class for Django to use. If you are not using a Windows NT OS, temp.py will tell Django to just use the tempfile.NamedTemporaryFile class. The reason for this is that NamedTemporaryFile cannot be reopened with a Windows NT OS.

This bug is caused because the custom TemporaryFile object Django defines for NT does not have a closed property, but self.closed is accessed when using the inherited open method. If you are not using NT, this bug won't affect you.

The patch seems to add the property with the expected behavior. In addition to the changes made by the patch, it seems like it would be cleaner to only use the custom TemporaryFile object regardless of OS. It's a pretty simple class and I don't see any platform specific notes for the Standard Library functionality used by it.

Either way, just applying the patch should work. It just needs a test.

I'll try to get to it if I have time, but I'm pretty busy at the moment.

comment:9 by anonymous, 13 years ago

The patch works fine for me.

Environment - Windows 2008 server, Apache + mod_wsgi, Django 1.3

comment:10 by Paul Garner, 13 years ago

Patch worked for me: Windows 7 64 bit, Django 1.3 dev server

comment:11 by Claude Paroz, 13 years ago

Keywords: windows added

comment:12 by anonymous, 13 years ago

Patch doesn't work on Win 8

comment:13 by anonymous, 13 years ago

Sorry it does work on win8 django 1.3.1 devserver

comment:14 by AnkitBagaria, 12 years ago

Owner: Petar Marić removed
Status: assignednew

comment:15 by Susan Tan, 12 years ago

Owner: set to Susan Tan
Status: newassigned

comment:16 by Jeremy Dunck, 12 years ago

Cc: jdunck@… added

comment:17 by Philippe Ombredanne, 12 years ago

I experienced the same issue on win7 and django 1.5.1.
Susan, do you want a patch?

comment:18 by pombredanne@…, 12 years ago

Cc: pombredanne@… added

comment:19 by Susan Tan, 12 years ago

Sure, I'll appreciate. Since Im running macOS, and the bug appears only on windowsOS, I think I may have difficulty replicating this bug.

comment:20 by pombredanne@…, 12 years ago

susan, let me craft that and reference a pull request here.

comment:21 by Philippe Ombredanne, 12 years ago

Owner: changed from Susan Tan to Philippe Ombredanne

comment:22 by Philippe Ombredanne, 12 years ago

Since Python 2.6, tempfile.NamedTemporaryFile has a new arg: delete=True/False which likely removes the needs entirely to have a django.core.temp.NamedTemporaryFile wrapper specifically for 'nt'. I will investigate this since 2.6 is a requirement for 1.5.x. The lack of a closed attribute is something that happens only in some contexts and is something that is injected elsewhere and I am still trying to understand where/when exactly.

Last edited 12 years ago by Philippe Ombredanne (previous) (diff)

comment:23 by Philippe Ombredanne, 11 years ago

Owner: Philippe Ombredanne removed
Status: assignednew

comment:24 by Philippe Ombredanne, 11 years ago

I am unassigning this ticket from me, as I am not able to work on it in the short term ... but this is still on my radar

comment:25 by Christopher Adams, 11 years ago

Owner: set to anonymous
Status: newassigned

comment:26 by anonymous, 11 years ago

Needs tests: unset

comment:27 by Christopher Adams, 11 years ago

Submitted a pull request that should solve this at https://github.com/django/django/pull/1566. This could probably easily be made into a bug fix for previous versions of Django, as it's just a few lines of code and the issue seems to be very old.

comment:28 by Christopher Adams <christopher.r.adams@…>, 11 years ago

  • TemporaryFile now minimally mocks the API of the Python standard library class tempfile.NamedTemporaryFile to avoid AttributeError exceptions.
  • The symbol django.core.files.NamedTemporaryFile is actually assigned as a different class on different operating systems.
  • The bug only occurred if Django is running on Windows, hence why it was hard to diagnose.
  • All tests pass with ./runtests.py --settings=test_sqlite

comment:29 by Christopher Adams <christopher.r.adams@…>, 11 years ago

Owner: anonymous removed
Status: assignednew

comment:30 by Christopher Adams <christopher.r.adams@…>, 11 years ago

Resolution: fixed
Status: newclosed
Triage Stage: AcceptedUnreviewed

comment:31 by Tim Graham, 11 years ago

Resolution: fixed
Status: closednew
Triage Stage: UnreviewedAccepted

Please don't close a ticket until the fix has been committed to master.

comment:32 by Christopher Adams <christopher.r.adams@…>, 11 years ago

Cc: christopher.r.adams@… added

Gotcha. Sorry about that - I'm a n00b!

comment:33 by Christopher Adams <christopher.r.adams@…>, 11 years ago

Owner: set to Christopher Adams <christopher.r.adams@…>
Resolution: fixed
Status: newclosed

In b2f5ac16565605f20a0c4e90acc6beed5a5ac1ce:

Fixed #11857 -- Added missing 'closed' property on TemporaryFile class.

  • TemporaryFile now minimally mocks the API of the Python standard library class tempfile.NamedTemporaryFile to avoid AttributeError exceptions.
  • The symbol django.core.files.NamedTemporaryFile is actually assigned as a different class on different operating systems.
  • The bug only occurred if Django is running on Windows, hence why it was hard to diagnose.

comment:34 by Russell Keith-Magee <russell@…>, 11 years ago

In 926bc421d9bcf04a79f0026a60d3d4b0570b7fe2:

Merge pull request #1566 from adamsc64/ticket_11857

Fixed #11857 -- Added missing 'closed' property on TemporaryFile class.

comment:35 by Russell Keith-Magee <russell@…>, 11 years ago

In 2a2ac5c1400c67f25388621a39749c918a4efe98:

Merge pull request #1566 from adamsc64/ticket_11857

Fixed #11857 -- Added missing 'closed' property on TemporaryFile class.

Backport of 926bc42 from trunk.

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