Re: InMemoryUploadedFile no `encoding` Attribute

653 views
Skip to first unread message

Pedro J. Aramburu

unread,
Jan 6, 2013, 9:41:22 AM1/6/13
to django...@googlegroups.com
I believe that as InMemoryUploadedFile is "InMemory", internally uses a cStringIO as the "container" for the file. So basically the class it's just a wrapper that returns the StringO object, a string representation of the file. You should check for the attributes of StringO or maybe use SimpleUploadedFile class, maybe that works.

El sábado, 5 de enero de 2013 22:46:02 UTC-3, Braden Walters escribió:
I've had a bit of an issue with InMemoryUploadedFile instances that come in request.FILES to a view. I passed one of them to another constructor to initialise it, and then I tried to access the `encoding` attribute (which appears in the help() for the class). I followed the class hierarchy and found that it leads to FileProxyMixin, where the `encoding` attribute is defined. This is where my mind is blown though:


  def __init__(self, f, *args, **kwargs):
    print type(f) # => <class 'django.core.files.uploadedfile.InMemoryUploadedFile'>
    print type(f.encoding) # => AttributeError: 'cStringIO.StringO' object has no attribute 'encoding'


How can it in the first line claim that it is an InMemoryUploadedFile, but in the second line, where I try to access an attribute on it, claim to be a StringO? To test that this issue is not the result of the instance being passed too far into my code (where my code messes something up), I tried the same test in the view directly with the request.FILES['file'] instance, and the same issue occurs. I should be able to continue without this attribute, but now I'm just curious. If anyone knows what's going on, please let me know. Thanks.

nkryptic

unread,
Jan 6, 2013, 11:07:20 AM1/6/13
to django...@googlegroups.com
The InMemoryUloadedFile has the following inheritance:
django.core.files.uploadedfile.InMemoryUploadedFile
- django.core.files.uploadedfile.UploadedFile
--- django.core.files.base.File
------django.core.files.utils.FileProxyMixin

FileProxyMixin defines a propery "encoding", that simply calls self.file.encoding.  The "file" attribute, in your case, is the cStringIO.StringIO object.  The StringIO object doesn't have an encoding attribute, so that is why you're seeing that error.

Braden Walters

unread,
Jan 15, 2013, 11:47:56 PM1/15/13
to django...@googlegroups.com
Thanks to both of you. That actually makes a lot more sense now.
Reply all
Reply to author
Forward
0 new messages