New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IDLE: printing unicode subclasses broken (again) #67771
Comments
This is a regression or recurrence of issue bpo-19481. To reproduce, create a subclass of unicode and try and print an instance of that class: class Foo(unicode): pass print Foo() results in a traceback: Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
print Foo()
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/idlelib/PyShell.py", line 1353, in write
s = unicode.__getslice__(s, None, None)
TypeError: an integer is required because unicode.__getslice__ does not accept None for the start and end indices. |
Proposed fix, replace line 1352-1353 in PseudoOutputFile.write(): if isinstance(s, unicode):
s = unicode.__getslice__(s, None, None) with if isinstance(s, unicode):
s = unicode.__getslice__(s, 0, len(s)) |
I can propose two ways to fix this: s = unicode.__getslice__(s, 0, unicode.__len__(s)) and s = unicode.__getitem__(s, slice(None)) |
Just len() doesn't work when the subclass overrides __len__. |
I like the unicode.__getitem__(s, slice(None)) approach, it has the advantage of not having to rely on len(s). |
What about just 's = unicode(s)'? The doc says "object.__unicode__(self)
but the latter part seems not to be true for unicode subclasses. >>> class F(unicode):
def __str__(self): raise TypeError()
>>> f = F()
>>> u = unicode(f)
>>> type(u)
<type 'unicode'> |
See example in msg205740. The same works with overridden __unicode__. I'm writing tests now. |
I don't see what you have in mind. Translating my example class S(str):
def __str__(self):
return 'S: ' + str.__str__(self) to class U(unicode):
def __unicode__(self):
return u'U: ' + unicode.__unicode__(self) does not work because there is no unicode.__unicode__. |
Yes, this is why unicode.__getslice__ or unicode.__getitem__ should be used. Here is a patch for 2.7 that fixes this issue and adds tests for PseudoOutputFile and PseudoInputFile. Tests should be ported also to 3.x. |
New changeset cd9e4f73b5ce by Serhiy Storchaka in branch '2.7': New changeset 0c72cdf3ff22 by Serhiy Storchaka in branch '2.7': New changeset 2c680d7d8ca6 by Serhiy Storchaka in branch '3.4': New changeset f860915e5705 by Serhiy Storchaka in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: