Skip to content
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

Closed
mjpieters mannequin opened this issue Mar 4, 2015 · 10 comments
Closed

IDLE: printing unicode subclasses broken (again) #67771

mjpieters mannequin opened this issue Mar 4, 2015 · 10 comments
Assignees
Labels
topic-IDLE topic-unicode type-bug An unexpected behavior, bug, or error

Comments

@mjpieters
Copy link
Mannequin

mjpieters mannequin commented Mar 4, 2015

BPO 23583
Nosy @terryjreedy, @mjpieters, @vstinner, @ned-deily, @ezio-melotti, @serhiy-storchaka
Files
  • idle_test_io-2.7.patch
  • idle_test_io-3.5.patch: Tests for 3.x
  • 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:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2015-03-24.20:04:18.561>
    created_at = <Date 2015-03-04.14:59:48.029>
    labels = ['expert-IDLE', 'type-bug', 'expert-unicode']
    title = 'IDLE: printing unicode subclasses broken (again)'
    updated_at = <Date 2015-03-24.20:04:18.560>
    user = 'https://github.com/mjpieters'

    bugs.python.org fields:

    activity = <Date 2015-03-24.20:04:18.560>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2015-03-24.20:04:18.561>
    closer = 'serhiy.storchaka'
    components = ['IDLE', 'Unicode']
    creation = <Date 2015-03-04.14:59:48.029>
    creator = 'mjpieters'
    dependencies = []
    files = ['38337', '38338']
    hgrepos = []
    issue_num = 23583
    keywords = ['patch']
    message_count = 10.0
    messages = ['237181', '237184', '237186', '237187', '237203', '237209', '237210', '237213', '237245', '239162']
    nosy_count = 7.0
    nosy_names = ['terry.reedy', 'mjpieters', 'vstinner', 'ned.deily', 'ezio.melotti', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue23583'
    versions = ['Python 2.7']

    @mjpieters
    Copy link
    Mannequin Author

    mjpieters mannequin commented Mar 4, 2015

    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.

    @mjpieters mjpieters mannequin added the topic-IDLE label Mar 4, 2015
    @ezio-melotti ezio-melotti added topic-unicode type-bug An unexpected behavior, bug, or error labels Mar 4, 2015
    @mjpieters
    Copy link
    Mannequin Author

    mjpieters mannequin commented Mar 4, 2015

    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))

    @serhiy-storchaka
    Copy link
    Member

    I can propose two ways to fix this:

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

    and

        s = unicode.__getitem__(s, slice(None))

    @serhiy-storchaka
    Copy link
    Member

    Just len() doesn't work when the subclass overrides __len__.

    @mjpieters
    Copy link
    Mannequin Author

    mjpieters mannequin commented Mar 4, 2015

    I like the unicode.__getitem__(s, slice(None)) approach, it has the advantage of not having to rely on len(s).

    @terryjreedy
    Copy link
    Member

    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'>

    @serhiy-storchaka
    Copy link
    Member

    See example in msg205740. The same works with overridden __unicode__.

    I'm writing tests now.

    @terryjreedy
    Copy link
    Member

    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__.

    @serhiy-storchaka
    Copy link
    Member

    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.

    @serhiy-storchaka serhiy-storchaka self-assigned this Mar 5, 2015
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 24, 2015

    New changeset cd9e4f73b5ce by Serhiy Storchaka in branch '2.7':
    Issue bpo-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 bpo-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 bpo-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 bpo-23583: Added tests for standard IO streams in IDLE.
    https://hg.python.org/cpython/rev/f860915e5705

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    topic-IDLE topic-unicode type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants