mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
make bytes(o) respect __bytes__ #2415
This adds two new C-API functions: PyObject_Bytes and PyBytes_FromObject. Reviewer: Barry
This commit is contained in:
parent
a786b026c9
commit
c15a07333e
8 changed files with 83 additions and 2 deletions
|
@ -458,6 +458,18 @@ class BytesTest(BaseBytesTest):
|
|||
with open(fd, "rb", buffering=0) as f:
|
||||
self.assertRaises(TypeError, f.readinto, b"")
|
||||
|
||||
def test_custom(self):
|
||||
class A:
|
||||
def __bytes__(self):
|
||||
return b'abc'
|
||||
self.assertEqual(bytes(A()), b'abc')
|
||||
class A: pass
|
||||
self.assertRaises(TypeError, bytes, A())
|
||||
class A:
|
||||
def __bytes__(self):
|
||||
return None
|
||||
self.assertRaises(TypeError, bytes, A())
|
||||
|
||||
|
||||
class ByteArrayTest(BaseBytesTest):
|
||||
type2test = bytearray
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue