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

__str__ cannot be overridden on unicode-derived classes #44163

Closed
laughingboy0 mannequin opened this issue Oct 24, 2006 · 5 comments
Closed

__str__ cannot be overridden on unicode-derived classes #44163

laughingboy0 mannequin opened this issue Oct 24, 2006 · 5 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-unicode type-bug An unexpected behavior, bug, or error

Comments

@laughingboy0
Copy link
Mannequin

laughingboy0 mannequin commented Oct 24, 2006

BPO 1583863
Nosy @vstinner, @devdanzin
Files
  • format_unicode.patch
  • 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 = None
    closed_at = <Date 2010-03-22.12:57:04.963>
    created_at = <Date 2006-10-24.15:57:03.000>
    labels = ['interpreter-core', 'type-bug', 'expert-unicode']
    title = '__str__ cannot be overridden on unicode-derived classes'
    updated_at = <Date 2010-03-22.12:57:04.962>
    user = 'https://bugs.python.org/laughingboy0'

    bugs.python.org fields:

    activity = <Date 2010-03-22.12:57:04.962>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-03-22.12:57:04.963>
    closer = 'vstinner'
    components = ['Interpreter Core', 'Unicode']
    creation = <Date 2006-10-24.15:57:03.000>
    creator = 'laughingboy0'
    dependencies = []
    files = ['13467']
    hgrepos = []
    issue_num = 1583863
    keywords = ['patch']
    message_count = 5.0
    messages = ['61028', '84519', '84534', '84535', '101501']
    nosy_count = 3.0
    nosy_names = ['vstinner', 'ajaksu2', 'laughingboy0']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'test needed'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue1583863'
    versions = ['Python 2.6']

    @laughingboy0
    Copy link
    Mannequin Author

    laughingboy0 mannequin commented Oct 24, 2006

    class S(str):
       def __str__(self): return '__str__ overridden'
    
    class U(unicode):
       def __str__(self): return '__str__ overridden'
       def __unicode__(self): return u'__unicode__ overridden'
    
    s = S()
    u = U()

    print 's:', s
    print "str(s):", str(s)
    print 's substitued is "%s"\n' % s
    print 'u:', u
    print "str(u):", str(u)
    print 'u substitued is "%s"' % u

    -----------------------------------------------------

    s: __str__ overridden
    str(s): __str__ overridden
    s substitued is "__str__ overridden"

    u:
    str(u): __str__ overridden
    u substitued is ""

    Results are identical for 2.4.2 and 2.5c2 (running
    under windows).

    @laughingboy0 laughingboy0 mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Oct 24, 2006
    @devdanzin
    Copy link
    Mannequin

    devdanzin mannequin commented Mar 30, 2009

    Confirmed in trunk.

    @devdanzin devdanzin mannequin added topic-unicode type-bug An unexpected behavior, bug, or error labels Mar 30, 2009
    @vstinner
    Copy link
    Member

    Case "S" (type str): "%s" uses _PyObject_Str() which checks the object
    type with PyString_CheckExact().

    Case "U" (type unicode) "%s" uses PyUnicode_Check() and then calls
    PyUnicode_Format(). PyUnicode_Format() uses PyUnicode_Check() to check
    the object object. It should uses PyUnicode_CheckExact() instead.

    xxx_CheckExact() is different than xxx_Check(): exact is only true for
    the base type, whereas the the second is also true for subclass.

    @vstinner
    Copy link
    Member

    Patch: Use PyUnicode_CheckExact() instead of PyUnicode_CheckExact() in
    PyUnicode_Format() to use obj.__unicode__ slot.

    @vstinner
    Copy link
    Member

    Commited: r79278+r79280 (trunk), r79281 (py3k), r79282 (3.1), r79283 (2.6).

    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-unicode type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant