This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: unicode(x) for weakref.proxy objects invokes __str__ instead of __unicode__
Type: behavior Stage:
Components: Interpreter Core Versions: Python 2.7, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Taldor, benjamin.peterson, ggenellina, ncoghlan
Priority: high Keywords: needs review, patch

Created on 2009-01-23 15:02 by Taldor, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
weakref_unicode.patch benjamin.peterson, 2009-01-24 18:43
Messages (7)
msg80416 - (view) Author: (Taldor) Date: 2009-01-23 15:02
Calling the unicode function on a proxy object, doesn't use the proxi-ed
object's __unicode__ method, but its __str__ method.

>>> class A(object):
...   def __str__(self):
...     return "str"    
...   def __unicode__(self):
...     return "unicode"    
...                         
>>> a = A()
>>> unicode(a)
u'unicode'
>>> import weakref
>>> b = weakref.proxy(a)
>>> unicode(b)
u'str'

I expected the last result to be u'unicode'. I did get u'unicode' in
Python 2.5 (but not in 2.6).
msg80423 - (view) Author: Gabriel Genellina (ggenellina) Date: 2009-01-24 00:06
Same results on trunk.
msg80433 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-01-24 02:05
This was broken by r64791. The problem is that PyObject_Unicode now uses
_PyType_Lookup instead of PyObject_GetItem, so that the proxy's custom
__getattr__ implementation is bypassed.
msg80455 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2009-01-24 15:34
weakref.proxy needs to be fixed to delegate the unicode slot correctly.

>>> from weakref import proxy
>>> "__unicode__" in dir(type(proxy(Exception)))
False

That predicate must return true in order for the delegation to do the
right thing (this is actually the case for all of the slots and
pseudo-slots that can bypass __getattribute__ on the instance object -
it's just that most of them are already handled correctly).

This need to explicitly delegate all supported slots is the reason why
weakref proxy instances add so many magic method stubs when compared to
the actual interface of the underlying class:

>>> len(set(dir(type(proxy(Exception)))) - set(dir(Exception)))
53
msg80468 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-01-24 18:43
Here's a patch.
msg95422 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2009-11-18 12:01
Patch looks good to me.
msg95463 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-11-19 03:01
Fixed in r76395.
History
Date User Action Args
2022-04-11 14:56:44adminsetgithub: 49287
2009-11-19 03:01:08benjamin.petersonsetstatus: open -> closed
resolution: fixed
messages: + msg95463
2009-11-18 12:01:11ncoghlansetmessages: + msg95422
2009-01-24 18:43:10benjamin.petersonsetpriority: high
keywords: + needs review, patch
messages: + msg80468
files: + weakref_unicode.patch
2009-01-24 15:34:20ncoghlansetmessages: + msg80455
2009-01-24 02:05:58benjamin.petersonsetnosy: + ncoghlan, benjamin.peterson
messages: + msg80433
2009-01-24 00:06:07ggenellinasetnosy: + ggenellina
title: unexpected unicode behavior for proxy objects -> unicode(x) for weakref.proxy objects invokes __str__ instead of __unicode__
messages: + msg80423
components: + Interpreter Core, - None
versions: + Python 2.7
2009-01-23 15:02:41Taldorcreate