Skip to content

Commit aca1467

Browse files
committed
Update test_bytes.py from CPython v3.11.2
1 parent a2e0abf commit aca1467

File tree

1 file changed

+63
-8
lines changed

1 file changed

+63
-8
lines changed

Lib/test/test_bytes.py

+63-8
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,24 @@ def test_mod(self):
721721
self.assertEqual(b, b'hello,\x00world!')
722722
self.assertIs(type(b), self.type2test)
723723

724+
def check(fmt, vals, result):
725+
b = self.type2test(fmt)
726+
b = b % vals
727+
self.assertEqual(b, result)
728+
self.assertIs(type(b), self.type2test)
729+
730+
# A set of tests adapted from test_unicode:UnicodeTest.test_formatting
731+
check(b'...%(foo)b...', {b'foo':b"abc"}, b'...abc...')
732+
check(b'...%(f(o)o)b...', {b'f(o)o':b"abc", b'foo':b'bar'}, b'...abc...')
733+
check(b'...%(foo)b...', {b'foo':b"abc",b'def':123}, b'...abc...')
734+
check(b'%*b', (5, b'abc',), b' abc')
735+
check(b'%*b', (-5, b'abc',), b'abc ')
736+
check(b'%*.*b', (5, 2, b'abc',), b' ab')
737+
check(b'%*.*b', (5, 3, b'abc',), b' abc')
738+
check(b'%i %*.*b', (10, 5, 3, b'abc',), b'10 abc')
739+
check(b'%i%b %*.*b', (10, b'3', 5, 3, b'abc',), b'103 abc')
740+
check(b'%c', b'a', b'a')
741+
724742
def test_imod(self):
725743
b = self.type2test(b'hello, %b!')
726744
orig = b
@@ -991,6 +1009,18 @@ def test_sq_item(self):
9911009
class BytesTest(BaseBytesTest, unittest.TestCase):
9921010
type2test = bytes
9931011

1012+
def test__bytes__(self):
1013+
foo = b'foo\x00bar'
1014+
self.assertEqual(foo.__bytes__(), foo)
1015+
self.assertEqual(type(foo.__bytes__()), self.type2test)
1016+
1017+
class bytes_subclass(bytes):
1018+
pass
1019+
1020+
bar = bytes_subclass(b'bar\x00foo')
1021+
self.assertEqual(bar.__bytes__(), bar)
1022+
self.assertEqual(type(bar.__bytes__()), self.type2test)
1023+
9941024
def test_getitem_error(self):
9951025
b = b'python'
9961026
msg = "byte indices must be integers or slices"
@@ -1658,8 +1688,8 @@ def delslice():
16581688

16591689
@test.support.cpython_only
16601690
def test_obsolete_write_lock(self):
1661-
from _testcapi import getbuffer_with_null_view
1662-
self.assertRaises(BufferError, getbuffer_with_null_view, bytearray())
1691+
_testcapi = import_helper.import_module('_testcapi')
1692+
self.assertRaises(BufferError, _testcapi.getbuffer_with_null_view, bytearray())
16631693

16641694
def test_iterator_pickling2(self):
16651695
orig = bytearray(b'abc')
@@ -1718,6 +1748,23 @@ def test_repeat_after_setslice(self):
17181748
self.assertEqual(b1, b)
17191749
self.assertEqual(b3, b'xcxcxc')
17201750

1751+
def test_mutating_index(self):
1752+
class Boom:
1753+
def __index__(self):
1754+
b.clear()
1755+
return 0
1756+
1757+
with self.subTest("tp_as_mapping"):
1758+
b = bytearray(b'Now you see me...')
1759+
with self.assertRaises(IndexError):
1760+
b[0] = Boom()
1761+
1762+
with self.subTest("tp_as_sequence"):
1763+
_testcapi = import_helper.import_module('_testcapi')
1764+
b = bytearray(b'Now you see me...')
1765+
with self.assertRaises(IndexError):
1766+
_testcapi.sequence_setitem(b, 0, Boom())
1767+
17211768

17221769
class AssortedBytesTest(unittest.TestCase):
17231770
#
@@ -1948,28 +1995,30 @@ def test_join(self):
19481995
def test_pickle(self):
19491996
a = self.type2test(b"abcd")
19501997
a.x = 10
1951-
a.y = self.type2test(b"efgh")
1998+
a.z = self.type2test(b"efgh")
19521999
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
19532000
b = pickle.loads(pickle.dumps(a, proto))
19542001
self.assertNotEqual(id(a), id(b))
19552002
self.assertEqual(a, b)
19562003
self.assertEqual(a.x, b.x)
1957-
self.assertEqual(a.y, b.y)
2004+
self.assertEqual(a.z, b.z)
19582005
self.assertEqual(type(a), type(b))
1959-
self.assertEqual(type(a.y), type(b.y))
2006+
self.assertEqual(type(a.z), type(b.z))
2007+
self.assertFalse(hasattr(b, 'y'))
19602008

19612009
def test_copy(self):
19622010
a = self.type2test(b"abcd")
19632011
a.x = 10
1964-
a.y = self.type2test(b"efgh")
2012+
a.z = self.type2test(b"efgh")
19652013
for copy_method in (copy.copy, copy.deepcopy):
19662014
b = copy_method(a)
19672015
self.assertNotEqual(id(a), id(b))
19682016
self.assertEqual(a, b)
19692017
self.assertEqual(a.x, b.x)
1970-
self.assertEqual(a.y, b.y)
2018+
self.assertEqual(a.z, b.z)
19712019
self.assertEqual(type(a), type(b))
1972-
self.assertEqual(type(a.y), type(b.y))
2020+
self.assertEqual(type(a.z), type(b.z))
2021+
self.assertFalse(hasattr(b, 'y'))
19732022

19742023
def test_fromhex(self):
19752024
b = self.type2test.fromhex('1a2B30')
@@ -2002,6 +2051,9 @@ def __init__(me, *args, **kwargs):
20022051
class ByteArraySubclass(bytearray):
20032052
pass
20042053

2054+
class ByteArraySubclassWithSlots(bytearray):
2055+
__slots__ = ('x', 'y', '__dict__')
2056+
20052057
class BytesSubclass(bytes):
20062058
pass
20072059

@@ -2022,6 +2074,9 @@ def __init__(me, newarg=1, *args, **kwargs):
20222074
x = subclass(newarg=4, source=b"abcd")
20232075
self.assertEqual(x, b"abcd")
20242076

2077+
class ByteArraySubclassWithSlotsTest(SubclassTest, unittest.TestCase):
2078+
basetype = bytearray
2079+
type2test = ByteArraySubclassWithSlots
20252080

20262081
class BytesSubclassTest(SubclassTest, unittest.TestCase):
20272082
basetype = bytes

0 commit comments

Comments
 (0)