mirror of
https://github.com/python/cpython.git
synced 2025-08-14 13:59:20 +00:00
Strengthen BytesWarning tests.
This commit is contained in:
parent
764d612f5e
commit
edf5f0ddc0
1 changed files with 54 additions and 45 deletions
|
@ -9,15 +9,28 @@ import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import copy
|
import copy
|
||||||
import operator
|
import functools
|
||||||
import pickle
|
import pickle
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
import warnings
|
|
||||||
import test.support
|
import test.support
|
||||||
import test.string_tests
|
import test.string_tests
|
||||||
import test.buffer_tests
|
import test.buffer_tests
|
||||||
|
|
||||||
|
|
||||||
|
if sys.flags.bytes_warning:
|
||||||
|
def check_bytes_warnings(func):
|
||||||
|
@functools.wraps(func)
|
||||||
|
def wrapper(*args, **kw):
|
||||||
|
with test.support.check_warnings(('', BytesWarning)):
|
||||||
|
return func(*args, **kw)
|
||||||
|
return wrapper
|
||||||
|
else:
|
||||||
|
# no-op
|
||||||
|
def check_bytes_warnings(func):
|
||||||
|
return func
|
||||||
|
|
||||||
|
|
||||||
class Indexable:
|
class Indexable:
|
||||||
def __init__(self, value=0):
|
def __init__(self, value=0):
|
||||||
self.value = value
|
self.value = value
|
||||||
|
@ -121,20 +134,19 @@ class BaseBytesTest(unittest.TestCase):
|
||||||
self.assertFalse(b3 < b2)
|
self.assertFalse(b3 < b2)
|
||||||
self.assertFalse(b3 <= b2)
|
self.assertFalse(b3 <= b2)
|
||||||
|
|
||||||
|
@check_bytes_warnings
|
||||||
def test_compare_to_str(self):
|
def test_compare_to_str(self):
|
||||||
with test.support.check_warnings():
|
# Byte comparisons with unicode should always fail!
|
||||||
warnings.simplefilter('ignore', BytesWarning)
|
# Test this for all expected byte orders and Unicode character
|
||||||
# Byte comparisons with unicode should always fail!
|
# sizes.
|
||||||
# Test this for all expected byte orders and Unicode character
|
self.assertEqual(self.type2test(b"\0a\0b\0c") == "abc", False)
|
||||||
# sizes.
|
self.assertEqual(self.type2test(b"\0\0\0a\0\0\0b\0\0\0c") == "abc",
|
||||||
self.assertEqual(self.type2test(b"\0a\0b\0c") == "abc", False)
|
False)
|
||||||
self.assertEqual(self.type2test(b"\0\0\0a\0\0\0b\0\0\0c") == "abc",
|
self.assertEqual(self.type2test(b"a\0b\0c\0") == "abc", False)
|
||||||
False)
|
self.assertEqual(self.type2test(b"a\0\0\0b\0\0\0c\0\0\0") == "abc",
|
||||||
self.assertEqual(self.type2test(b"a\0b\0c\0") == "abc", False)
|
False)
|
||||||
self.assertEqual(self.type2test(b"a\0\0\0b\0\0\0c\0\0\0") == "abc",
|
self.assertEqual(self.type2test() == str(), False)
|
||||||
False)
|
self.assertEqual(self.type2test() != str(), True)
|
||||||
self.assertEqual(self.type2test() == str(), False)
|
|
||||||
self.assertEqual(self.type2test() != str(), True)
|
|
||||||
|
|
||||||
def test_reversed(self):
|
def test_reversed(self):
|
||||||
input = list(map(ord, "Hello"))
|
input = list(map(ord, "Hello"))
|
||||||
|
@ -823,17 +835,16 @@ class AssortedBytesTest(unittest.TestCase):
|
||||||
# Test various combinations of bytes and bytearray
|
# Test various combinations of bytes and bytearray
|
||||||
#
|
#
|
||||||
|
|
||||||
|
@check_bytes_warnings
|
||||||
def test_repr_str(self):
|
def test_repr_str(self):
|
||||||
with test.support.check_warnings():
|
for f in str, repr:
|
||||||
warnings.simplefilter('ignore', BytesWarning)
|
self.assertEqual(f(bytearray()), "bytearray(b'')")
|
||||||
for f in str, repr:
|
self.assertEqual(f(bytearray([0])), "bytearray(b'\\x00')")
|
||||||
self.assertEqual(f(bytearray()), "bytearray(b'')")
|
self.assertEqual(f(bytearray([0, 1, 254, 255])),
|
||||||
self.assertEqual(f(bytearray([0])), "bytearray(b'\\x00')")
|
"bytearray(b'\\x00\\x01\\xfe\\xff')")
|
||||||
self.assertEqual(f(bytearray([0, 1, 254, 255])),
|
self.assertEqual(f(b"abc"), "b'abc'")
|
||||||
"bytearray(b'\\x00\\x01\\xfe\\xff')")
|
self.assertEqual(f(b"'"), '''b"'"''') # '''
|
||||||
self.assertEqual(f(b"abc"), "b'abc'")
|
self.assertEqual(f(b"'\""), r"""b'\'"'""") # '
|
||||||
self.assertEqual(f(b"'"), '''b"'"''') # '''
|
|
||||||
self.assertEqual(f(b"'\""), r"""b'\'"'""") # '
|
|
||||||
|
|
||||||
def test_compare_bytes_to_bytearray(self):
|
def test_compare_bytes_to_bytearray(self):
|
||||||
self.assertEqual(b"abc" == bytes(b"abc"), True)
|
self.assertEqual(b"abc" == bytes(b"abc"), True)
|
||||||
|
@ -876,15 +887,14 @@ class AssortedBytesTest(unittest.TestCase):
|
||||||
b = bytearray(buf)
|
b = bytearray(buf)
|
||||||
self.assertEqual(b, bytearray(sample))
|
self.assertEqual(b, bytearray(sample))
|
||||||
|
|
||||||
|
@check_bytes_warnings
|
||||||
def test_to_str(self):
|
def test_to_str(self):
|
||||||
with test.support.check_warnings():
|
self.assertEqual(str(b''), "b''")
|
||||||
warnings.simplefilter('ignore', BytesWarning)
|
self.assertEqual(str(b'x'), "b'x'")
|
||||||
self.assertEqual(str(b''), "b''")
|
self.assertEqual(str(b'\x80'), "b'\\x80'")
|
||||||
self.assertEqual(str(b'x'), "b'x'")
|
self.assertEqual(str(bytearray(b'')), "bytearray(b'')")
|
||||||
self.assertEqual(str(b'\x80'), "b'\\x80'")
|
self.assertEqual(str(bytearray(b'x')), "bytearray(b'x')")
|
||||||
self.assertEqual(str(bytearray(b'')), "bytearray(b'')")
|
self.assertEqual(str(bytearray(b'\x80')), "bytearray(b'\\x80')")
|
||||||
self.assertEqual(str(bytearray(b'x')), "bytearray(b'x')")
|
|
||||||
self.assertEqual(str(bytearray(b'\x80')), "bytearray(b'\\x80')")
|
|
||||||
|
|
||||||
def test_literal(self):
|
def test_literal(self):
|
||||||
tests = [
|
tests = [
|
||||||
|
@ -930,19 +940,18 @@ class AssortedBytesTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_compare(self):
|
def test_compare(self):
|
||||||
if sys.flags.bytes_warning:
|
if sys.flags.bytes_warning:
|
||||||
with test.support.check_warnings():
|
def bytes_warning():
|
||||||
warnings.simplefilter('error', BytesWarning)
|
return test.support.check_warnings(('', BytesWarning))
|
||||||
with self.assertRaises(BytesWarning):
|
with bytes_warning():
|
||||||
b'' == ''
|
b'' == ''
|
||||||
with self.assertRaises(BytesWarning):
|
with bytes_warning():
|
||||||
b'' != ''
|
b'' != ''
|
||||||
with self.assertRaises(BytesWarning):
|
with bytes_warning():
|
||||||
bytearray(b'') == ''
|
bytearray(b'') == ''
|
||||||
with self.assertRaises(BytesWarning):
|
with bytes_warning():
|
||||||
bytearray(b'') != ''
|
bytearray(b'') != ''
|
||||||
else:
|
else:
|
||||||
# self.skipTest("BytesWarning is needed for this test: use -bb option")
|
self.skipTest("BytesWarning is needed for this test: use -bb option")
|
||||||
pass
|
|
||||||
|
|
||||||
# Optimizations:
|
# Optimizations:
|
||||||
# __iter__? (optimization)
|
# __iter__? (optimization)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue