PyFilesystem / pyfilesystem

Python filesystem abstraction layer
http://pyfilesystem.org/
BSD 3-Clause "New" or "Revised" License
288 stars 63 forks source link

fs.errors.FSError(u'a unicode message').__unicode__() raises an exception #184

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Instantiate FSError with a unicode value for msg. Cause __unicode__() to be 
called on the instance.

What is the expected output? What do you see instead?
Expect to see the unicode representation of the error. Instead, it raises 
TypeError for trying to decode Unicode.

What version of the product are you using? On what operating system?
Mac OSX fs==0.5.0

Please provide any additional information below.

Fix is simple:

    def __unicode__(self):
        keys = {}
        for k,v in self.__dict__.iteritems():
            if isinstance(v, six.binary_type):
                v = v.decode(sys.getfilesystemencoding(), 'replace')
            keys[k] = v
        msg = self.msg
        # The next two lines fix the issue
        if isinstance(msg,unicode):
            msg = msg.encode(sys.getfilesystemencoding())
        return unicode(msg, encoding=sys.getfilesystemencoding(), errors='replace') % keys

Original issue reported on code.google.com by jmead...@box.com on 12 Sep 2014 at 11:11