#27150: Wrong File object behavior if file object has no name attribute
------------------------------+--------------------
Reporter: pacahon | Owner: nobody
Type: Bug | Status: new
Component: Core (Other) | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------
[https://docs.djangoproject.com/en/1.10/ref/files/file/#the-file-class
django.core.files.base.File] supports file-like objects (synonym to file
object in python glossary) and in fact can't rely on additional `name`
attribute, but in current implementation of `__bool__` method it does:
{{{
def __bool__(self):
return bool(self.name)
}}}
For example, until `tempfile.SpooledTemporaryFile` isn't large enough to
have been written to disk, his `name` attribute is `None` and `if`
statement will return False.
{{{
import tempfile
from django.core.files.base import File
python_file_like_object = tempfile.SpooledTemporaryFile()
file_obj = File(python_file_like_object) # name keyword is None by
default
if file_object: # Falsy, because python_file_like_object.name is None
print("42")
}}}
The same behavior with streams (BytesIO, StringIO).
Maybe `__bool__` method should rely on `self.file` attribute to avoid this
behavior?
--
Ticket URL: <https://code.djangoproject.com/ticket/27150>
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/050.e5bc90439ab77408e6e13a3729ddf965%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.