#13750: ImageField accessing height or width and then data results in "I/O
operation on closed file"
--------------------------------------+------------------------------------
     Reporter:  ROsborne              |                    Owner:  aethemba
         Type:  Bug                   |                   Status:  assigned
    Component:  File uploads/storage  |                  Version:  1.2
     Severity:  Normal                |               Resolution:
     Keywords:                        |             Triage Stage:  Accepted
    Has patch:  1                     |      Needs documentation:  0
  Needs tests:  0                     |  Patch needs improvement:  0
Easy pickings:  0                     |                    UI/UX:  0
--------------------------------------+------------------------------------
Changes (by aethemba):

 * needs_better_patch:  1 => 0


Comment:

 I took another look at this issue and found some other interesting stuff.
 I noticed that, when not accessing the width or height property, the call
 to Image.open(model.image) does pass successfully. I may have been looking
 at the wrong thing.

 I dug a little deeper and I found that in django/core/files/images.py the
 function get_dimensions() has a "close" keyword argument. This keyword
 argument comes from a call to self.closed in the ImageFile class (a few
 lines above). In the "get_image_dimensions" function, however, the "close"
 keyword argument determines whether a file should be closed at the end of
 the function. I think that was not correct, because if self.closed returns
 True, the keyword argument to the function is "close=True" and there is a
 call to file.close() at the end of the function get_image_dimensions.

 As a solution, I removed the "close" keyword argument in
 "get_image_dimensions" and added a variable "close" that is either True or
 False depending on whether in the beginning of the function the file is
 explicitly opened.

 I also re-added the regression test that caused the  "I/O operation on
 closed file" error. With the new patch this test now passes and so do all
 the other tests. I updated my PR to the repo and removed the documentation
 bit of my previous patch.

--
Ticket URL: <https://code.djangoproject.com/ticket/13750#comment:17>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.bc6c5ad104fffbfd88da56bce27c2982%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to