zope security proxy doesn't handle __unicode__

Bug #1367566 reported by Zach Cashero
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
zope.security
Won't Fix
Undecided
Unassigned

Bug Description

There is no implementation of the __unicode__ method to handle objects that are wrapped in the zope.security._proxy._Proxy. This causes a UnicodeDecodeError when trying to get the unicode representation of a custom object that has a security proxy wrapped around it.

For example, this class implementation:

class ICustomClass(Interface):
    def __unicode__():
        pass

    def __str__():
        pass

@implementer(ICustomClass)
class CustomClass(object):
    def __unicode__(self):
        print "In __unicode__"
        return u'Español con caracteres especiales'

    def __str__(self):
        print "In __str__"
        return unicode(self).encode('utf-8')

With the associated zcml:
  <class class=".test.CustomClass">
    <allow
        interface=".test.ICustomClass" />
  </class>

And a simple test:
>>> from zope.security.checker import ProxyFactory
>>> obj = CustomClass()
>>> str(obj)
In __str__
In __unicode__
'Espa\xc3\xb1ol con caracteres especiales'
>>> unicode(obj)
In __unicode__
u'Espa\xf1ol con caracteres especiales'
>>> prox = ProxyFactory(obj)
>>> str(prox)
In __str__
In __unicode__
'Espa\xc3\xb1ol con caracteres especiales'
>>> unicode(prox)
In __str__
In __unicode__
*** UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4: ordinal not in range(128)

The object, that should return a unicode string with special characters works fine on the original object, but causes an error on the proxied object because it actually calls the __str__ method which returns a 'utf-8' encoded byte string and when trying to make it into a unicode string, fails since it is trying to decode it with the system default of 'ascii.' Essentially, it is the equivalent of calling:
>>> unicode(prox.__str__())
In __str__
In __unicode__
*** UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4: ordinal not in range(128)

Revision history for this message
Tres Seaver (tseaver) wrote :
Changed in zope.security:
status: New → Won't Fix
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.