Skip to content

bytes() should respect __bytes__ #46667

Closed

Description

@warsaw
BPO 2415
Nosy @warsaw, @pitrou, @tiran, @benjaminp
Files
  • 2415.diff
  • 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/warsaw'
    closed_at = <Date 2008-08-26.17:08:56.981>
    created_at = <Date 2008-03-19.03:04:14.120>
    labels = ['interpreter-core', 'type-feature', 'release-blocker']
    title = 'bytes() should respect __bytes__'
    updated_at = <Date 2008-08-26.17:08:56.980>
    user = 'https://github.com/warsaw'

    bugs.python.org fields:

    activity = <Date 2008-08-26.17:08:56.980>
    actor = 'benjamin.peterson'
    assignee = 'barry'
    closed = True
    closed_date = <Date 2008-08-26.17:08:56.981>
    closer = 'benjamin.peterson'
    components = ['Interpreter Core']
    creation = <Date 2008-03-19.03:04:14.120>
    creator = 'barry'
    dependencies = []
    files = ['11194']
    hgrepos = []
    issue_num = 2415
    keywords = ['patch', 'needs review']
    message_count = 9.0
    messages = ['64027', '64061', '71660', '71684', '71687', '71972', '71974', '71976', '71987']
    nosy_count = 4.0
    nosy_names = ['barry', 'pitrou', 'christian.heimes', 'benjamin.peterson']
    pr_nums = []
    priority = 'release blocker'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue2415'
    versions = ['Python 3.0']

    Activity

    1. warsaw commented on Mar 19, 2008

      @warsaw
      MemberAuthor

      The bytes() builtin should respect an __bytes__() converter if it exists.
      E.g. instead of

      >>> class Foo:
      ...  def __bytes__(self): return b'foo'
      ... 
      >>> bytes(Foo())
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      TypeError: 'Foo' object is not iterable
      >>> 

      bytes(Foo()) should return b'foo'

      Here's one use case. email.header.Header instances represent email headers
      (naturally) that conceptually are bytes, but also have a string
      representation. Say for example, a Subject header comes across the wire in
      RFC 2033 encoded utf-8. The unicode representation would be the value
      of the
      header decoded according to the RFC. The bytes representation would be the
      raw bytes seen on the wire.

      The most natural way to retrieve each representation would be

      >>> header = msg['subject']
      >>> str(header)
      'some string with non-ascii'
      >>> bytes(header)
      b'the rfc 2033 encoded raw header value'
    2. added
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      type-featureA feature request or enhancement
      on Mar 19, 2008
    3. benjaminp commented on Mar 19, 2008

      @benjaminp
      Contributor

      I took a quick glance at this. It hinges on how the C-API is going to
      look. Currently, bytes is known in C as PyString and gets it's
      representation from __str__. Although we could just change it to
      __bytes__, Christian has said that he is going to rename it to PyBytes
      (and what is now PyBytes -> PyByteArray). [1] Further muddying the
      waters is the fact that PyObject_Str generates the unicode
      representation of an object and should really be called PyObject_Unicode.

      [1] http://mail.python.org/pipermail/python-3000/2008-March/012477.html

    4. benjaminp commented on Aug 21, 2008

      @benjaminp
      Contributor

      Here's a patch. It's only implemented for bytes. Doing this for
      bytearray would require a bit of refactoring, and can I think wait for
      3.1. I added two new C functions. PyObject_Bytes and PyBytes_FromObject.
      You can review it at http://codereview.appspot.com/3245.

    5. pitrou commented on Aug 21, 2008

      @pitrou
      Member

      Isn't it a new feature and, therefore, should wait for 3.1?

    6. benjaminp commented on Aug 21, 2008

      @benjaminp
      Contributor

      Well, yes I suppose. However, I think it's a serious enough deficiency
      that it should block. I'll let Barry decide, though.

    7. warsaw commented on Aug 26, 2008

      @warsaw
      MemberAuthor

      Well, if I figured out how to use Rietveld correctly, I've left some
      questions for you in the review. It looks basically pretty good, so if
      you could answer those questions, you can commit the change.

      Should __bytes__ support be backported to 2.6?

    8. pitrou commented on Aug 26, 2008

      @pitrou
      Member

      Should __bytes__ support be backported to 2.6?

      Isn't it already there in __str__?
      Or do you mean just add support for the alternate method name?

    9. warsaw commented on Aug 26, 2008

      @warsaw
      MemberAuthor

      yep, that's all i meant. it might not be worth it though.

    10. benjaminp commented on Aug 26, 2008

      @benjaminp
      Contributor

      Thanks for the review, Barry! Committed in r66038. Sort of backported in
      r66039 by aliasing PyObject_Bytes to PyObject_Str.

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

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions