Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e3bf553

Browse filesBrowse files
authored
Merge pull request #4746 from MegasKomnenos/test_bytes
Update test_bytes.py from CPython v3.11.2
2 parents db2fa98 + f63e6f3 commit e3bf553
Copy full SHA for e3bf553

File tree

1 file changed

+67
-8
lines changed
Filter options

1 file changed

+67
-8
lines changed

‎Lib/test/test_bytes.py

Copy file name to clipboardExpand all lines: Lib/test/test_bytes.py
+67-8Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ def test_rindex(self):
704704
self.assertEqual(b.rindex(i, 3, 9), 7)
705705
self.assertRaises(ValueError, b.rindex, w, 1, 3)
706706

707+
# TODO: RUSTPYTHON
708+
@unittest.expectedFailure
707709
def test_mod(self):
708710
b = self.type2test(b'hello, %b!')
709711
orig = b
@@ -721,6 +723,24 @@ def test_mod(self):
721723
self.assertEqual(b, b'hello,\x00world!')
722724
self.assertIs(type(b), self.type2test)
723725

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

1014+
def test__bytes__(self):
1015+
foo = b'foo\x00bar'
1016+
self.assertEqual(foo.__bytes__(), foo)
1017+
self.assertEqual(type(foo.__bytes__()), self.type2test)
1018+
1019+
class bytes_subclass(bytes):
1020+
pass
1021+
1022+
bar = bytes_subclass(b'bar\x00foo')
1023+
self.assertEqual(bar.__bytes__(), bar)
1024+
self.assertEqual(type(bar.__bytes__()), self.type2test)
1025+
9941026
def test_getitem_error(self):
9951027
b = b'python'
9961028
msg = "byte indices must be integers or slices"
@@ -1658,8 +1690,8 @@ def delslice():
16581690

16591691
@test.support.cpython_only
16601692
def test_obsolete_write_lock(self):
1661-
from _testcapi import getbuffer_with_null_view
1662-
self.assertRaises(BufferError, getbuffer_with_null_view, bytearray())
1693+
_testcapi = import_helper.import_module('_testcapi')
1694+
self.assertRaises(BufferError, _testcapi.getbuffer_with_null_view, bytearray())
16631695

16641696
def test_iterator_pickling2(self):
16651697
orig = bytearray(b'abc')
@@ -1718,6 +1750,23 @@ def test_repeat_after_setslice(self):
17181750
self.assertEqual(b1, b)
17191751
self.assertEqual(b3, b'xcxcxc')
17201752

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

17221771
class AssortedBytesTest(unittest.TestCase):
17231772
#
@@ -1945,31 +1994,35 @@ def test_join(self):
19451994
s3 = s1.join([b"abcd"])
19461995
self.assertIs(type(s3), self.basetype)
19471996

1997+
@unittest.skip("TODO: RUSTPYHON, Fails on ByteArraySubclassWithSlotsTest")
19481998
def test_pickle(self):
19491999
a = self.type2test(b"abcd")
19502000
a.x = 10
1951-
a.y = self.type2test(b"efgh")
2001+
a.z = self.type2test(b"efgh")
19522002
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
19532003
b = pickle.loads(pickle.dumps(a, proto))
19542004
self.assertNotEqual(id(a), id(b))
19552005
self.assertEqual(a, b)
19562006
self.assertEqual(a.x, b.x)
1957-
self.assertEqual(a.y, b.y)
2007+
self.assertEqual(a.z, b.z)
19582008
self.assertEqual(type(a), type(b))
1959-
self.assertEqual(type(a.y), type(b.y))
2009+
self.assertEqual(type(a.z), type(b.z))
2010+
self.assertFalse(hasattr(b, 'y'))
19602011

2012+
@unittest.skip("TODO: RUSTPYHON, Fails on ByteArraySubclassWithSlotsTest")
19612013
def test_copy(self):
19622014
a = self.type2test(b"abcd")
19632015
a.x = 10
1964-
a.y = self.type2test(b"efgh")
2016+
a.z = self.type2test(b"efgh")
19652017
for copy_method in (copy.copy, copy.deepcopy):
19662018
b = copy_method(a)
19672019
self.assertNotEqual(id(a), id(b))
19682020
self.assertEqual(a, b)
19692021
self.assertEqual(a.x, b.x)
1970-
self.assertEqual(a.y, b.y)
2022+
self.assertEqual(a.z, b.z)
19712023
self.assertEqual(type(a), type(b))
1972-
self.assertEqual(type(a.y), type(b.y))
2024+
self.assertEqual(type(a.z), type(b.z))
2025+
self.assertFalse(hasattr(b, 'y'))
19732026

19742027
def test_fromhex(self):
19752028
b = self.type2test.fromhex('1a2B30')
@@ -2002,6 +2055,9 @@ def __init__(me, *args, **kwargs):
20022055
class ByteArraySubclass(bytearray):
20032056
pass
20042057

2058+
class ByteArraySubclassWithSlots(bytearray):
2059+
__slots__ = ('x', 'y', '__dict__')
2060+
20052061
class BytesSubclass(bytes):
20062062
pass
20072063

@@ -2022,6 +2078,9 @@ def __init__(me, newarg=1, *args, **kwargs):
20222078
x = subclass(newarg=4, source=b"abcd")
20232079
self.assertEqual(x, b"abcd")
20242080

2081+
class ByteArraySubclassWithSlotsTest(SubclassTest, unittest.TestCase):
2082+
basetype = bytearray
2083+
type2test = ByteArraySubclassWithSlots
20252084

20262085
class BytesSubclassTest(SubclassTest, unittest.TestCase):
20272086
basetype = bytes

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.