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__ doesn't work for str subclasses #69952

Closed
serhiy-storchaka opened this issue Nov 29, 2015 · 3 comments
Closed

__bytes__ doesn't work for str subclasses #69952

serhiy-storchaka opened this issue Nov 29, 2015 · 3 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

BPO 25766
Nosy @mdickinson, @skrah, @serhiy-storchaka
Files
  • str___bytes__.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 = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2015-12-20.17:06:24.769>
    created_at = <Date 2015-11-29.20:26:42.923>
    labels = ['interpreter-core', 'type-bug']
    title = "__bytes__ doesn't work for str subclasses"
    updated_at = <Date 2015-12-20.17:06:24.768>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2015-12-20.17:06:24.768>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2015-12-20.17:06:24.769>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2015-11-29.20:26:42.923>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['41192']
    hgrepos = []
    issue_num = 25766
    keywords = ['patch']
    message_count = 3.0
    messages = ['255593', '256375', '256769']
    nosy_count = 4.0
    nosy_names = ['mark.dickinson', 'skrah', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue25766'
    versions = ['Python 3.5', 'Python 3.6']

    @serhiy-storchaka
    Copy link
    Member Author

    Special __bytes__ method is purposed for converting to bytes with bytes constructor (as well as __str__ and __float__ for str and float). But this doesn't work if the class is a subclass of str.

    >>> class X:
    ...     def __bytes__(self):
    ...         return b'abc'
    ... 
    >>> bytes(X())
    b'abc'
    >>> class Y(str):
    ...     def __bytes__(self):
    ...         return b'abc'
    ... 
    >>> bytes(Y())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: string argument without an encoding

    @serhiy-storchaka serhiy-storchaka added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Nov 29, 2015
    @serhiy-storchaka
    Copy link
    Member Author

    Other conversion special methods work in str subclass.

    >>> class I(str):
    ...     def __int__(self):
    ...         return 42
    ... 
    >>> int(I('35'))
    42
    >>> int(I('35'), 8)
    29

    @serhiy-storchaka serhiy-storchaka self-assigned this Dec 20, 2015
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Dec 20, 2015

    New changeset 1e54adef4064 by Serhiy Storchaka in branch '3.5':
    Issue bpo-25766: Special method __bytes__() now works in str subclasses.
    https://hg.python.org/cpython/rev/1e54adef4064

    New changeset af0b95fb1c19 by Serhiy Storchaka in branch 'default':
    Issue bpo-25766: Special method __bytes__() now works in str subclasses.
    https://hg.python.org/cpython/rev/af0b95fb1c19

    @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) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant