[New-bugs-announce] [issue25766] __bytes__ doesn't work for str subclasses

Serhiy Storchaka report at bugs.python.org
Sun Nov 29 15:26:42 EST 2015


New submission from Serhiy Storchaka:

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

----------
components: Interpreter Core
files: str___bytes__.patch
keywords: patch
messages: 255593
nosy: serhiy.storchaka
priority: normal
severity: normal
stage: patch review
status: open
title: __bytes__ doesn't work for str subclasses
type: behavior
versions: Python 3.4, Python 3.5, Python 3.6
Added file: http://bugs.python.org/file41192/str___bytes__.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25766>
_______________________________________


More information about the New-bugs-announce mailing list