Opened 3 years ago

Closed 3 years ago

#22307 closed Bug (fixed)

tempfile.SpooledTemporaryFile passed to File causes exception

Reported by: hrlawrenz@… Owned by: nobody
Component: Core (Other) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When a File object is created from a tempfile.SpooledTemporaryFile, an exception occurs when the File.size method is called and the tempfile.SpooledTemporaryFile isn't large enough to have been written to disk. The exception is caused by an attempt to call os.path.exists on a None value. The name attribute of the tempfile.SpooledTemporaryFile in this case is None in this case because it doesn't exist on disk.

Patch in pull request at

The patch just adds another condition before calling os.path.exists to verify that the file name isn't None.

Traceback (most recent call last):

File "/Users/hrwl/Documents/Projects/django/tests/files/", line 215, in test_in_memory_spooled_temp

self.assertTrue(django_file.size, 17)

File "/Users/hrwl/Documents/Projects/django/django/core/files/", line 43, in _get_size

elif hasattr(self.file, 'name') and os.path.exists(

File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 18, in exists


TypeError: coercing to Unicode: need string or buffer, NoneType found

Change History (6)

comment:1 Changed 3 years ago by Baptiste Mispelon

Needs documentation: unset
Needs tests: unset
Patch needs improvement: set
Triage Stage: UnreviewedAccepted
Version: 1.6master


There's some trailing whitespace and the added test doesn't work on Python3.

Looks good otherwise.

comment:2 Changed 3 years ago by Baptiste Mispelon <bmispelon@…>

Resolution: fixed
Status: newclosed

In 918a16bc4c099ab0cae72a231de3e99e2a9d02cb:

Fixed #22307 -- Fixed SpooledTemporaryFile bug in File class.

Added condition to prevent checking the existence of a file name of a
file like object when the name attribute is None. This is necessary
because a SpooledTemporaryFile won't exist on the file system or have a
name until it has reached its max_size. Also added tests.

comment:3 Changed 3 years ago by Aymeric Augustin

Resolution: fixed
Status: closednew

comment:4 Changed 3 years ago by Aymeric Augustin

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

Baptiste, can you review and merge

comment:5 Changed 3 years ago by Baptiste Mispelon <bmispelon@…>

comment:6 Changed 3 years ago by Baptiste Mispelon

Resolution: fixed
Status: newclosed

That fixed it.

Thanks for the heads up.

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