classification
Title: __bytes__ doesn't work in subclass of int and str
Type: behavior Stage:
Components: Documentation Versions: Python 3.2, Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: docs@python Nosy List: benjamin.peterson, docs@python, pkoning, terry.reedy
Priority: normal Keywords:

Created on 2013-02-27 15:41 by pkoning, last changed 2013-03-02 18:42 by benjamin.peterson. This issue is now closed.

Files
File name Uploaded Description Edit
bytes.py pkoning, 2013-02-27 15:41
Messages (4)
msg183155 - (view) Author: Paul Koning (pkoning) Date: 2013-02-27 15:41
The __bytes__ special method has no effect in a subclass of "int" because the bytes() builtin checks for int or int subclass before it gets around to looking for that special method.  The attached example shows it.
msg183300 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-03-01 22:42
The library doc says that if the source argument for bytes() or bytearray() is a string or integer it is treated a certain way. I believe 'string' and 'integer' are meant to include subclasses of str and int, else it should have said str or int. You have verified that int subclass instances are indeed treated as integers. The following shows that str subclass instances are treated as strings.

class mystr(str):
    def __bytes__(self): return b'hello'
print(bytes(mystr('a')))
raises TypeError: string argument without an encoding

The language reference says "object.__bytes__(self): Called by bytes() to compute a byte-string representation of an object." No exception is given. I see this as a contradiction in the manual, in which case the current behavior rules and the contradiction is removed by changing the doc part that conflicts with behavior. That would mean extending the doc sentence with 'that is not a string or integer'.

However, I am waiting for other opinions for two reasons.

1. The doc also specified special treatment for buffer interface and iterable objects. However, __bytes__ *is* checked first for those objects. 

class myit(list):
    def __bytes__(self): return b'hello'
print (bytes (myit([1,2,3])))
# b'hello'

class mybit(bytes):  # example 
    def __bytes__(self): return b'hello'
print (bytes (mybit(b'a')))
# b'hello'

So the exception is limited to 'strings' and 'integers', making it somewhat inconsistent.

2. If someone gives an str or int subclass a __bytes__ method, they can only mean for it to be called by bytes(), as there is no other use of that special method.

On the third hand, a user of a class who tests its instances before calling bytes with "isinstance(x, (str, int))" instead of "type(x) is str or type(x) is int", likely expects getting normal string or integer behavior. Of course, the same might be true if the test is 'isiterable(x)'.

If behavior were changed, I think it should wait for 3.4 since the current behavior is not clearly a bug to me.
msg183301 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-03-01 23:03
More data:

class myit(list):
    def __bytes__(self): return b'hello'
print (bytes(b'a'))

class myit(list):
    def __bytes__(self): return b'hello'
print (bytearray (myit([1,2,3])))

# bytearray(b'a')
# bytearray(b'\x01\x02\x03')

class by:
    def __bytes__(self): return b'hello'
# TypeError: 'by' object is not iterable
(Error message is incomplete.)

So bytearray *always* treats objects as specified in its library entry and never calls __bytes__, making its value sometimes unequal as a sequence of bytes from bytes with the same input.
msg183340 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2013-03-02 18:42
This has been fixed recently:

 $ ./python bytes.py 
b'\x00'
b'hello'
History
Date User Action Args
2013-03-02 18:42:21benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg183340

resolution: out of date
2013-03-01 23:03:39terry.reedysetmessages: + msg183301
2013-03-01 22:42:31terry.reedysetassignee: docs@python

components: + Documentation, - Interpreter Core
title: __bytes__ doesn't work in subclass of int -> __bytes__ doesn't work in subclass of int and str
nosy: + docs@python, terry.reedy
versions: + Python 2.7, Python 3.3, Python 3.4
messages: + msg183300
2013-02-27 15:41:26pkoningcreate