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: IDLE: printing unicode subclasses broken (again)
Type: behavior Stage: resolved
Components: IDLE, Unicode Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: ezio.melotti, mjpieters, ned.deily, python-dev, serhiy.storchaka, terry.reedy, vstinner
Priority: normal Keywords: patch

Created on 2015-03-04 14:59 by mjpieters, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
idle_test_io-2.7.patch serhiy.storchaka, 2015-03-05 07:29 review
idle_test_io-3.5.patch serhiy.storchaka, 2015-03-05 07:38 Tests for 3.x review
Messages (10)
msg237181 - (view) Author: Martijn Pieters (mjpieters) * Date: 2015-03-04 14:59
This is a regression or recurrence of issue #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.
msg237184 - (view) Author: Martijn Pieters (mjpieters) * Date: 2015-03-04 15:12
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))
msg237186 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-04 15:29
I can propose two ways to fix this:

    s = unicode.__getslice__(s, 0, unicode.__len__(s))

and

    s = unicode.__getitem__(s, slice(None))
msg237187 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-04 15:31
Just len() doesn't work when the subclass overrides __len__.
msg237203 - (view) Author: Martijn Pieters (mjpieters) * Date: 2015-03-04 19:32
I like the unicode.__getitem__(s, slice(None)) approach, it has the advantage of not having to rely on len(s).
msg237209 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-03-04 20:43
What about just 's = unicode(s)'?  The doc says

"object.__unicode__(self)

    Called to implement unicode() built-in; should return a Unicode object. When this method is not defined, string conversion is attempted, and the result of string conversion is converted to Unicode using the system default encoding."

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'>
msg237210 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-04 20:50
See example in msg205740. The same works with overridden __unicode__.

I'm writing tests now.
msg237213 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-03-04 21:04
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__.
msg237245 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-05 07:29
> 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.
msg239162 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-03-24 17:50
New changeset cd9e4f73b5ce by Serhiy Storchaka in branch '2.7':
Issue #23583: Fixed writing unicode to standard output stream in IDLE.
https://hg.python.org/cpython/rev/cd9e4f73b5ce

New changeset 0c72cdf3ff22 by Serhiy Storchaka in branch '2.7':
Issue #23583: Fixed writing unicode to standard output stream in IDLE.
https://hg.python.org/cpython/rev/0c72cdf3ff22

New changeset 2c680d7d8ca6 by Serhiy Storchaka in branch '3.4':
Issue #23583: Added tests for standard IO streams in IDLE.
https://hg.python.org/cpython/rev/2c680d7d8ca6

New changeset f860915e5705 by Serhiy Storchaka in branch 'default':
Issue #23583: Added tests for standard IO streams in IDLE.
https://hg.python.org/cpython/rev/f860915e5705
History
Date User Action Args
2022-04-11 14:58:13adminsetgithub: 67771
2015-03-24 20:04:18serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2015-03-24 17:50:45python-devsetnosy: + python-dev
messages: + msg239162
2015-03-05 07:38:39serhiy.storchakasetfiles: + idle_test_io-3.5.patch
2015-03-05 07:29:54serhiy.storchakasetfiles: + idle_test_io-2.7.patch
messages: + msg237245

assignee: serhiy.storchaka
keywords: + patch
stage: needs patch -> patch review
2015-03-04 21:04:38terry.reedysetmessages: + msg237213
2015-03-04 20:50:41serhiy.storchakasetmessages: + msg237210
2015-03-04 20:43:13terry.reedysetmessages: + msg237209
stage: needs patch
2015-03-04 19:32:15mjpieterssetmessages: + msg237203
2015-03-04 15:31:24serhiy.storchakasetmessages: + msg237187
2015-03-04 15:29:04serhiy.storchakasetmessages: + msg237186
2015-03-04 15:12:11mjpieterssetmessages: + msg237184
2015-03-04 15:01:25ezio.melottisetnosy: + ezio.melotti, terry.reedy, ned.deily, vstinner, serhiy.storchaka
type: behavior
components: + Unicode
2015-03-04 15:00:49mjpieterssetcomponents: + IDLE
2015-03-04 14:59:48mjpieterscreate