From adb8d089953c5a925b427d065424fe64847d64ec Mon Sep 17 00:00:00 2001 From: MegasKomnenos Date: Sat, 25 Mar 2023 13:39:49 +0900 Subject: [PATCH 1/2] Update test_bytes.py from CPython v3.11.2 --- Lib/test/test_bytes.py | 71 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 727b7645e1..f382081df3 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -721,6 +721,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 @@ -991,6 +1009,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" @@ -1658,8 +1688,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') @@ -1718,6 +1748,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): # @@ -1948,28 +1995,30 @@ def test_join(self): 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')) 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') @@ -2002,6 +2051,9 @@ def __init__(me, *args, **kwargs): class ByteArraySubclass(bytearray): pass +class ByteArraySubclassWithSlots(bytearray): + __slots__ = ('x', 'y', '__dict__') + class BytesSubclass(bytes): pass @@ -2022,6 +2074,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 From f63e6f3e1b490e5250ae647df143e117b5a209f7 Mon Sep 17 00:00:00 2001 From: MegasKomnenos Date: Sat, 25 Mar 2023 14:30:26 +0900 Subject: [PATCH 2/2] Flag/skip failing tests --- Lib/test/test_bytes.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index f382081df3..b632637d38 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -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 def test_mod(self): b = self.type2test(b'hello, %b!') orig = b @@ -1992,6 +1994,7 @@ def test_join(self): s3 = s1.join([b"abcd"]) self.assertIs(type(s3), self.basetype) + @unittest.skip("TODO: RUSTPYHON, Fails on ByteArraySubclassWithSlotsTest") def test_pickle(self): a = self.type2test(b"abcd") a.x = 10 @@ -2006,6 +2009,7 @@ def test_pickle(self): 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