#36975: SimpleUploadedFile cannot be re-opened
-------------------------------------+-------------------------------------
Reporter: Denis Washington | Type:
| Uncategorized
Status: new | Component:
| Uncategorized
Version: 6.0 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
=== Summary
When a `SimpleUploadedFile` is closed, it calls `close()` on the
underlying `BytesIO`, which means that the next call to `open()` fails
(`ValueError: I/O operation on closed file.`). This is unlike the
conceptually similar `ContentFile`, which explicitly overrides `close()`
to do nothing, avoiding this issue.
=== How to Reproduce
The following script reproduces the issue:
{{{#!python
from django.core.files.base import ContentFile
from django.core.files.uploadedfile import SimpleUploadedFile
def read_twice(file):
with file.open() as f:
print(f.read())
with file.open() as f:
print(f.read())
# Works as expected
read_twice(ContentFile(b"test data"))
# ValueError: I/O operation on closed file.
read_twice(SimpleUploadedFile("test.txt", b"test data"))
}}}
=== Expected Behavior
`SimpleUploadedFile` should follow `ContentFile` in making `close()` a no-
op.
Ideally, the same should be done by its superclass `InMemoryUploadedFile`,
at least if its `file` is a `BytesIO` or `StringIO`. (Skipping closing
unconditionally could be risky here because the init method accepts any
file-like object in principle.)
--
Ticket URL: <
https://code.djangoproject.com/ticket/36975>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.