Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#27716 closed Bug (duplicate)

FieldFile incorrectly returns bytes the first time it is opened with 'r'

Reported by: Tom Forbes Owned by: nobody
Component: File uploads/storage Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tom Forbes)

This is strange. On the latest 1.10 release with Python 3.5.2 if I have this model, created in a fresh project with no non-default settings (other than 'MEDIA_ROOT' set to a directory and a single app in INSTALLED_APPS with this model):

class SomeModel(models.Model):
    fd = models.FileField()

Then I run this in manage.py shell:

>>> s = SomeModel.objects.create(fd='test.txt')
>>> s.fd.size
5
>>> s.fd.closed
True
>>> s.fd.open('r')
>>> s.fd.read()
b'hello'
>>> s.fd.close()
>>> s.fd.open('r')
>>> s.fd.read()
'hello'

The first time a FileField is opened it appears to disregard the encoding given and always returns bytes. However the second time it correctly returns a string. If you re-fetch the model then the behavior happens again.

Just to mention that I'm on Linux, but my co-worker is on Windows and both appear to have this behavior.

Change History (3)

comment:1 by Tom Forbes, 7 years ago

Description: modified (diff)

comment:2 by Tim Graham, 7 years ago

Resolution: duplicate
Status: newclosed

Please test with Django's master branch. This looks like a duplicate of #13809 which is fixed there.

comment:3 by Tom Forbes, 7 years ago

Ahh yes it is fixed in master, my apologies.

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