Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 67 additions & 8 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,8 @@ def test_rindex(self):
self.assertEqual(b.rindex(i, 3, 9), 7)
self.assertRaises(ValueError, b.rindex, w, 1, 3)

# TODO: RUSTPYTHON
@unittest.expectedFailure
Comment on lines +707 to +708
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youknowone This should have been broken out into a separate commit.

def test_mod(self):
b = self.type2test(b'hello, %b!')
orig = b
Expand All @@ -721,6 +723,24 @@ def test_mod(self):
self.assertEqual(b, b'hello,\x00world!')
self.assertIs(type(b), self.type2test)

def check(fmt, vals, result):
b = self.type2test(fmt)
b = b % vals
self.assertEqual(b, result)
self.assertIs(type(b), self.type2test)

# A set of tests adapted from test_unicode:UnicodeTest.test_formatting
check(b'...%(foo)b...', {b'foo':b"abc"}, b'...abc...')
check(b'...%(f(o)o)b...', {b'f(o)o':b"abc", b'foo':b'bar'}, b'...abc...')
check(b'...%(foo)b...', {b'foo':b"abc",b'def':123}, b'...abc...')
check(b'%*b', (5, b'abc',), b' abc')
check(b'%*b', (-5, b'abc',), b'abc ')
check(b'%*.*b', (5, 2, b'abc',), b' ab')
check(b'%*.*b', (5, 3, b'abc',), b' abc')
check(b'%i %*.*b', (10, 5, 3, b'abc',), b'10 abc')
check(b'%i%b %*.*b', (10, b'3', 5, 3, b'abc',), b'103 abc')
check(b'%c', b'a', b'a')

def test_imod(self):
b = self.type2test(b'hello, %b!')
orig = b
Expand Down Expand Up @@ -991,6 +1011,18 @@ def test_sq_item(self):
class BytesTest(BaseBytesTest, unittest.TestCase):
type2test = bytes

def test__bytes__(self):
foo = b'foo\x00bar'
self.assertEqual(foo.__bytes__(), foo)
self.assertEqual(type(foo.__bytes__()), self.type2test)

class bytes_subclass(bytes):
pass

bar = bytes_subclass(b'bar\x00foo')
self.assertEqual(bar.__bytes__(), bar)
self.assertEqual(type(bar.__bytes__()), self.type2test)

def test_getitem_error(self):
b = b'python'
msg = "byte indices must be integers or slices"
Expand Down Expand Up @@ -1658,8 +1690,8 @@ def delslice():

@test.support.cpython_only
def test_obsolete_write_lock(self):
from _testcapi import getbuffer_with_null_view
self.assertRaises(BufferError, getbuffer_with_null_view, bytearray())
_testcapi = import_helper.import_module('_testcapi')
self.assertRaises(BufferError, _testcapi.getbuffer_with_null_view, bytearray())

def test_iterator_pickling2(self):
orig = bytearray(b'abc')
Expand Down Expand Up @@ -1718,6 +1750,23 @@ def test_repeat_after_setslice(self):
self.assertEqual(b1, b)
self.assertEqual(b3, b'xcxcxc')

def test_mutating_index(self):
class Boom:
def __index__(self):
b.clear()
return 0

with self.subTest("tp_as_mapping"):
b = bytearray(b'Now you see me...')
with self.assertRaises(IndexError):
b[0] = Boom()

with self.subTest("tp_as_sequence"):
_testcapi = import_helper.import_module('_testcapi')
b = bytearray(b'Now you see me...')
with self.assertRaises(IndexError):
_testcapi.sequence_setitem(b, 0, Boom())


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

@unittest.skip("TODO: RUSTPYHON, Fails on ByteArraySubclassWithSlotsTest")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youknowone This should have been broken out into a separate commit.

def test_pickle(self):
a = self.type2test(b"abcd")
a.x = 10
a.y = self.type2test(b"efgh")
a.z = self.type2test(b"efgh")
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
b = pickle.loads(pickle.dumps(a, proto))
self.assertNotEqual(id(a), id(b))
self.assertEqual(a, b)
self.assertEqual(a.x, b.x)
self.assertEqual(a.y, b.y)
self.assertEqual(a.z, b.z)
self.assertEqual(type(a), type(b))
self.assertEqual(type(a.y), type(b.y))
self.assertEqual(type(a.z), type(b.z))
self.assertFalse(hasattr(b, 'y'))

@unittest.skip("TODO: RUSTPYHON, Fails on ByteArraySubclassWithSlotsTest")
def test_copy(self):
a = self.type2test(b"abcd")
a.x = 10
a.y = self.type2test(b"efgh")
a.z = self.type2test(b"efgh")
for copy_method in (copy.copy, copy.deepcopy):
b = copy_method(a)
self.assertNotEqual(id(a), id(b))
self.assertEqual(a, b)
self.assertEqual(a.x, b.x)
self.assertEqual(a.y, b.y)
self.assertEqual(a.z, b.z)
self.assertEqual(type(a), type(b))
self.assertEqual(type(a.y), type(b.y))
self.assertEqual(type(a.z), type(b.z))
self.assertFalse(hasattr(b, 'y'))

def test_fromhex(self):
b = self.type2test.fromhex('1a2B30')
Expand Down Expand Up @@ -2002,6 +2055,9 @@ def __init__(me, *args, **kwargs):
class ByteArraySubclass(bytearray):
pass

class ByteArraySubclassWithSlots(bytearray):
__slots__ = ('x', 'y', '__dict__')

class BytesSubclass(bytes):
pass

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

class ByteArraySubclassWithSlotsTest(SubclassTest, unittest.TestCase):
basetype = bytearray
type2test = ByteArraySubclassWithSlots

class BytesSubclassTest(SubclassTest, unittest.TestCase):
basetype = bytes
Expand Down