mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
Merge pull request #2761 from youknowone/test-bytes
Update test_bytes.py
This commit is contained in:
@@ -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,64 @@ 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
|
||||
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 +554,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 +981,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 +1051,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 +1689,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):
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user