From b5686d01cad68b1e91281ec8a77879dd4670c8d5 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Thu, 29 Jul 2021 22:47:34 +0900 Subject: [PATCH 1/2] Update test_bytes.py to 3.9.6 --- Lib/test/test_bytes.py | 86 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index e7d78a1a4..32b3b6bae 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -12,12 +12,14 @@ import copy import functools import pickle import tempfile +import textwrap import unittest import test.support import test.string_tests import test.list_tests from test.support import bigaddrspacetest, MAX_Py_ssize_t +from test.support.script_helper import assert_python_failure if sys.flags.bytes_warning: @@ -319,6 +321,62 @@ class BaseBytesTest: # Default encoding is utf-8 self.assertEqual(self.type2test(b'\xe2\x98\x83').decode(), '\u2603') + def test_check_encoding_errors(self): + # bpo-37388: bytes(str) and bytes.encode() must check encoding + # and errors arguments in dev mode + invalid = 'Boom, Shaka Laka, Boom!' + encodings = ('ascii', 'utf8', 'latin1') + code = textwrap.dedent(f''' + import sys + type2test = {self.type2test.__name__} + encodings = {encodings!r} + + for data in ('', 'short string'): + try: + type2test(data, encoding={invalid!r}) + except LookupError: + pass + else: + sys.exit(21) + + for encoding in encodings: + try: + type2test(data, encoding=encoding, errors={invalid!r}) + except LookupError: + pass + else: + sys.exit(22) + + for data in (b'', b'short string'): + data = type2test(data) + print(repr(data)) + try: + data.decode(encoding={invalid!r}) + except LookupError: + sys.exit(10) + else: + sys.exit(23) + + try: + data.decode(errors={invalid!r}) + except LookupError: + pass + else: + sys.exit(24) + + for encoding in encodings: + try: + data.decode(encoding=encoding, errors={invalid!r}) + except LookupError: + pass + else: + sys.exit(25) + + sys.exit(10) + ''') + proc = assert_python_failure('-X', 'dev', '-c', code) + self.assertEqual(proc.rc, 10, proc) + def test_from_int(self): b = self.type2test(0) self.assertEqual(b, self.type2test()) @@ -494,9 +552,13 @@ class BaseBytesTest: self.assertEqual(dot_join([bytearray(b"ab"), b"cd"]), b"ab.:cd") self.assertEqual(dot_join([b"ab", bytearray(b"cd")]), b"ab.:cd") # Stress it with many items - seq = [b"abc"] * 1000 - expected = b"abc" + b".:abc" * 999 + seq = [b"abc"] * 100000 + expected = b"abc" + b".:abc" * 99999 self.assertEqual(dot_join(seq), expected) + # Stress test with empty separator + seq = [b"abc"] * 100000 + expected = b"abc" * 100000 + self.assertEqual(self.type2test(b"").join(seq), expected) self.assertRaises(TypeError, self.type2test(b" ").join, None) # Error handling and cleanup when some item in the middle of the # sequence has the wrong type. @@ -917,6 +979,15 @@ class BaseBytesTest: c = b.translate(None, delete=b'e') self.assertEqual(c, b'hllo') + def test_sq_item(self): + _testcapi = test.support.import_module('_testcapi') + obj = self.type2test((42,)) + with self.assertRaises(IndexError): + _testcapi.sequence_getitem(obj, -2) + with self.assertRaises(IndexError): + _testcapi.sequence_getitem(obj, 1) + self.assertEqual(_testcapi.sequence_getitem(obj, 0), 42) + class BytesTest(BaseBytesTest, unittest.TestCase): type2test = bytes @@ -978,6 +1049,7 @@ class BytesTest(BaseBytesTest, unittest.TestCase): c_char_p) PyBytes_FromFormat = pythonapi.PyBytes_FromFormat + PyBytes_FromFormat.argtypes = (c_char_p,) PyBytes_FromFormat.restype = py_object # basic tests @@ -1615,6 +1687,16 @@ class ByteArrayTest(BaseBytesTest, unittest.TestCase): # Shouldn't raise an error self.assertEqual(list(it), []) + def test_repeat_after_setslice(self): + # bpo-42924: * used to copy from the wrong memory location + b = bytearray(b'abc') + b[:2] = b'x' + b1 = b * 1 + b3 = b * 3 + self.assertEqual(b1, b'xc') + self.assertEqual(b1, b) + self.assertEqual(b3, b'xcxcxc') + class AssortedBytesTest(unittest.TestCase): # From 2d3b85b28c81c92b7aba4adb0eea3090a233fa6b Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Thu, 29 Jul 2021 22:54:23 +0900 Subject: [PATCH 2/2] Mark unexpected failre of new test_bytes tests --- Lib/test/test_bytes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 32b3b6bae..05ab7bdaa 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -321,6 +321,8 @@ class BaseBytesTest: # Default encoding is utf-8 self.assertEqual(self.type2test(b'\xe2\x98\x83').decode(), '\u2603') + # TODO: RUSTPYTHON + @unittest.expectedFailure def test_check_encoding_errors(self): # bpo-37388: bytes(str) and bytes.encode() must check encoding # and errors arguments in dev mode