Django

Code

Ticket #8156 (closed: fixed)

Opened 4 months ago

Last modified 3 months ago

UnicodeEncodeError on repr of UploadedFile with unicode name

Reported by: kmtracey Assigned to: marco
Milestone: 1.0 Component: File uploads/storage
Version: SVN Keywords:
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

If I create an UploadedFile with a unicode name that contains non-ASCII chars, I get a UnicodeEncodeError attempting to display its representation in a Python shell:

>>> from django.core.files.uploadedfile import UploadedFile
>>> uf = UploadedFile(name=u'¿Cómo',content_type='text')
>>> uf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xbf' in position 15: ordinal not in range(128)
>>> uf.__repr__()
u'<UploadedFile: \xbfC\xf3mo (text)>'
 

The implementation of __repr__ here is:

    def __repr__(self):
        return "<%s: %s (%s)>" % (self.__class__.__name__, self.name, self.content_type)

I think __repr__ for UploadedFile needs to take into account the fact that the file name may be Unicode with non-ASCII chars and convert it into an ASCII-only representation before including it in the return value. As it is this implementation returns unicode, where I thought __repr__ was supposed to return only a string (per http://docs.python.org/ref/customization.html "The return value must be a string object.")?

Attachments

uploadfile_repr.diff (0.6 kB) - added by marco on 08/21/08 14:26:27.
uploadedfile.diff (0.5 kB) - added by julianb on 08/21/08 15:23:22.
encode to ascii

Change History

08/12/08 12:11:46 changed by jacob

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • component changed from Core framework to File uploads/storage.
  • needs_tests changed.
  • needs_docs changed.

08/21/08 13:45:46 changed by marco

  • owner changed from nobody to marco.
  • status changed from new to assigned.

08/21/08 14:25:58 changed by marco

hi, this is my first patch :)

this patch checks if the name is unicode, and if it is, uses its repr instead.

08/21/08 14:26:27 changed by marco

  • attachment uploadfile_repr.diff added.

08/21/08 14:27:05 changed by marco

  • has_patch set to 1.

08/21/08 15:23:22 changed by julianb

  • attachment uploadedfile.diff added.

encode to ascii

08/27/08 15:53:03 changed by jacob

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [8636]) FIxed #8156: UploadedFile.__repr__ now returns a string, a good __repr__ should.


Add/Change #8156 (UnicodeEncodeError on repr of UploadedFile with unicode name)




Change Properties
Action