Skip to content

unicode(x) for weakref.proxy objects invokes __str__ instead of __unicode__ #49287

Closed

Description

@Taldor
mannequin
BPO 5037
Nosy @ncoghlan, @benjaminp
Files
  • weakref_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 2009-11-19.03:01:08.967>
    created_at = <Date 2009-01-23.15:02:41.692>
    labels = ['interpreter-core', 'type-bug']
    title = 'unicode(x) for weakref.proxy objects invokes __str__ instead of __unicode__'
    updated_at = <Date 2009-11-19.03:01:08.966>
    user = 'https://bugs.python.org/Taldor'

    bugs.python.org fields:

    activity = <Date 2009-11-19.03:01:08.966>
    actor = 'benjamin.peterson'
    assignee = 'none'
    closed = True
    closed_date = <Date 2009-11-19.03:01:08.967>
    closer = 'benjamin.peterson'
    components = ['Interpreter Core']
    creation = <Date 2009-01-23.15:02:41.692>
    creator = 'Taldor'
    dependencies = []
    files = ['12847']
    hgrepos = []
    issue_num = 5037
    keywords = ['patch', 'needs review']
    message_count = 7.0
    messages = ['80416', '80423', '80433', '80455', '80468', '95422', '95463']
    nosy_count = 4.0
    nosy_names = ['ncoghlan', 'ggenellina', 'benjamin.peterson', 'Taldor']
    pr_nums = []
    priority = 'high'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue5037'
    versions = ['Python 2.6', 'Python 2.7']

    Activity

    1. Taldor commented on Jan 23, 2009

      Taldormannequin
      MannequinAuthor

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

    2. added
      type-bugAn unexpected behavior, bug, or error
      on Jan 23, 2009
    3. ggenellina commented on Jan 24, 2009

      ggenellinamannequin
      Mannequin

      Same results on trunk.

    4. changed the title [-]unexpected unicode behavior for proxy objects[/-] [+]unicode(x) for weakref.proxy objects invokes __str__ instead of __unicode__[/+] on Jan 24, 2009
    5. benjaminp commented on Jan 24, 2009

      @benjaminp
      Contributor

      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.

    6. ncoghlan commented on Jan 24, 2009

      @ncoghlan
      Contributor

      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
    7. benjaminp commented on Jan 24, 2009

      @benjaminp
      Contributor

      Here's a patch.

    8. ncoghlan commented on Nov 18, 2009

      @ncoghlan
      Contributor

      Patch looks good to me.

    9. benjaminp commented on Nov 19, 2009

      @benjaminp
      Contributor

      Fixed in r76395.

    10. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions