Issue 5037: unicode(x) for weakref.proxy objects invokes str instead of unicode (original) (raw)
Issue5037
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.
This issue has been migrated to GitHub: https://github.com/python/cpython/issues/49287
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) * ![]() |
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: Alyssa Coghlan (ncoghlan) * ![]() |
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) * ![]() |
Date: 2009-01-24 18:43 |
Here's a patch. | ||
msg95422 - (view) | Author: Alyssa Coghlan (ncoghlan) * ![]() |
Date: 2009-11-18 12:01 |
Patch looks good to me. | ||
msg95463 - (view) | Author: Benjamin Peterson (benjamin.peterson) * ![]() |
Date: 2009-11-19 03:01 |
Fixed in r76395. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:56:44 | admin | set | github: 49287 |
2009-11-19 03:01:08 | benjamin.peterson | set | status: open -> closedresolution: fixedmessages: + |
2009-11-18 12:01:11 | ncoghlan | set | messages: + |
2009-01-24 18:43:10 | benjamin.peterson | set | priority: highkeywords: + needs review, patchmessages: + files: + weakref_unicode.patch |
2009-01-24 15:34:20 | ncoghlan | set | messages: + |
2009-01-24 02:05:58 | benjamin.peterson | set | nosy: + ncoghlan, benjamin.petersonmessages: + |
2009-01-24 00:06:07 | ggenellina | set | nosy: + ggenellinatitle: unexpected unicode behavior for proxy objects -> unicode(x) for weakref.proxy objects invokes __str__ instead of __unicode__messages: + components: + Interpreter Core, - Noneversions: + Python 2.7 |
2009-01-23 15:02:41 | Taldor | create |