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

bytes() should respect __bytes__ #46667

Closed
warsaw opened this issue Mar 19, 2008 · 9 comments
Closed

bytes() should respect __bytes__ #46667

warsaw opened this issue Mar 19, 2008 · 9 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) release-blocker type-feature A feature request or enhancement

Comments

@warsaw
Copy link
Member

warsaw commented Mar 19, 2008

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

    @warsaw
    Copy link
    Member Author

    warsaw commented Mar 19, 2008

    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'

    @warsaw warsaw added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Mar 19, 2008
    @benjaminp
    Copy link
    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

    @benjaminp
    Copy link
    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.

    @pitrou
    Copy link
    Member

    pitrou commented Aug 21, 2008

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

    @benjaminp
    Copy link
    Contributor

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

    @warsaw
    Copy link
    Member Author

    warsaw commented Aug 26, 2008

    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?

    @pitrou
    Copy link
    Member

    pitrou commented Aug 26, 2008

    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?

    @warsaw
    Copy link
    Member Author

    warsaw commented Aug 26, 2008

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

    @benjaminp
    Copy link
    Contributor

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

    @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) release-blocker type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants