#33023: InMemoryUploadedFile - opening with context manager multiple time is
throwing error
--------------------------------+--------------------------------------
     Reporter:  Harsh Bhikadia  |                    Owner:  nobody
         Type:  Bug             |                   Status:  closed
    Component:  Core (Other)    |                  Version:  3.2
     Severity:  Normal          |               Resolution:  fixed
     Keywords:                  |             Triage Stage:  Unreviewed
    Has patch:  0               |      Needs documentation:  0
  Needs tests:  0               |  Patch needs improvement:  0
Easy pickings:  0               |                    UI/UX:  0
--------------------------------+--------------------------------------
Changes (by Harsh Bhikadia):

 * status:  new => closed
 * resolution:   => fixed


Old description:

> **What is the issue:**
> when "opening" the "uploaded file" with "context manager (like `with
> file.open('rb') as f:`) multiple times it is throwing `` exception
>
> **Explaination**
> Digging into the code, I realised that `InMemoryUploadedFile` overrides
> the `open` method from `File` instance - the implementation just "seeks
> to 0". Since the context manager will call the `close` method on "exit".
> When "opening" the file again (with or without context manager) it fails
> with "cannot seek(0) of already closed file"
>

> ** Potential solution**
> - Not sure why the "open" method was overridden - if not required then
> just should remove the "overridden method"
> - or making sure the file is "re-opened" (like in `File`) if closed
>

> **To conclude**
>
> Since
> [https://docs.djangoproject.com/en/3.2/ref/files/file/#django.core.files.File.open
> the doc on `File`] mentions that it could be used with "context manager"
> - I think this is unexpected behaviour and should be fixed.
>
> I have been using Django for many years but new to contribution/issue
> reporting, so forgive me when I am being ignorant.
>
> I am interested in fixing this myself if someone here gives me a green
> signal for it.

New description:

 **What is the issue:**
 when "opening" the "uploaded file" with "context manager (like `with
 file.open('rb') as f:`) multiple times it is throwing `ValueError: I/O
 operation on closed file.` exception

 **Explaination**
 Digging into the code, I realised that `InMemoryUploadedFile` overrides
 the `open` method from `File` instance - the implementation just "seeks to
 0". Since the context manager will call the `close` method on "exit". When
 "opening" the file again (with or without context manager) it fails with
 "cannot seek(0) of already closed file"


 ** Potential solution**
 - Not sure why the "open" method was overridden - if not required then
 just should remove the "overridden method"
 - or making sure the file is "re-opened" (like in `File`) if closed


 **To conclude**

 Since
 
[https://docs.djangoproject.com/en/3.2/ref/files/file/#django.core.files.File.open
 the doc on `File`] mentions that it could be used with "context manager"
 - I think this is unexpected behaviour and should be fixed.

 I have been using Django for many years but new to contribution/issue
 reporting, so forgive me when I am being ignorant.

 I am interested in fixing this myself if someone here gives me a green
 signal for it.

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33023#comment:1>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.c2673ce005605d768de05e7e0d6ea8d4%40djangoproject.com.

Reply via email to