diff options
author | Benjamin Peterson <benjamin@python.org> | 2008-08-26 16:46:47 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2008-08-26 16:46:47 (GMT) |
commit | c15a07333e712e682036d9ecb7230d25ccb1a85f (patch) | |
tree | 52eee04b10bc95047d25c9dd6b82ec9e48efeb97 /Lib | |
parent | a786b026c9992cee195b7a550bab2c70c50f874f (diff) | |
download | cpython-c15a07333e712e682036d9ecb7230d25ccb1a85f.zip cpython-c15a07333e712e682036d9ecb7230d25ccb1a85f.tar.gz cpython-c15a07333e712e682036d9ecb7230d25ccb1a85f.tar.bz2 |
make bytes(o) respect __bytes__ #2415
This adds two new C-API functions: PyObject_Bytes and PyBytes_FromObject.
Reviewer: Barry
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_bytes.py | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 630f862..81d2dad 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -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 |