[Django] #36975: SimpleUploadedFile cannot be re-opened

9 views
Skip to first unread message

Django

unread,
Mar 6, 2026, 9:50:12 AM (20 hours ago) Mar 6
to django-...@googlegroups.com
#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.

Django

unread,
Mar 6, 2026, 9:26:39 PM (8 hours ago) Mar 6
to django-...@googlegroups.com
#36975: SimpleUploadedFile cannot be re-opened
-------------------------------------+-------------------------------------
Reporter: Denis Washington | Owner: Dinesh
| Thumma
Type: Uncategorized | Status: assigned
Component: Uncategorized | Version: 6.0
Severity: Normal | Resolution:
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 Dinesh Thumma):

* owner: (none) => Dinesh Thumma
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36975#comment:1>
Reply all
Reply to author
Forward
0 new messages