Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit edf5f0d

Browse files
committed
Strengthen BytesWarning tests.
1 parent 764d612 commit edf5f0d

1 file changed

Lines changed: 54 additions & 45 deletions

File tree

Lib/test/test_bytes.py

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,28 @@
99
import re
1010
import sys
1111
import copy
12-
import operator
12+
import functools
1313
import pickle
1414
import tempfile
1515
import unittest
16-
import warnings
1716
import test.support
1817
import test.string_tests
1918
import test.buffer_tests
2019

20+
21+
if sys.flags.bytes_warning:
22+
def check_bytes_warnings(func):
23+
@functools.wraps(func)
24+
def wrapper(*args, **kw):
25+
with test.support.check_warnings(('', BytesWarning)):
26+
return func(*args, **kw)
27+
return wrapper
28+
else:
29+
# no-op
30+
def check_bytes_warnings(func):
31+
return func
32+
33+
2134
class Indexable:
2235
def __init__(self, value=0):
2336
self.value = value
@@ -121,20 +134,19 @@ def test_compare(self):
121134
self.assertFalse(b3 < b2)
122135
self.assertFalse(b3 <= b2)
123136

137+
@check_bytes_warnings
124138
def test_compare_to_str(self):
125-
with test.support.check_warnings():
126-
warnings.simplefilter('ignore', BytesWarning)
127-
# Byte comparisons with unicode should always fail!
128-
# Test this for all expected byte orders and Unicode character
129-
# sizes.
130-
self.assertEqual(self.type2test(b"\0a\0b\0c") == "abc", False)
131-
self.assertEqual(self.type2test(b"\0\0\0a\0\0\0b\0\0\0c") == "abc",
132-
False)
133-
self.assertEqual(self.type2test(b"a\0b\0c\0") == "abc", False)
134-
self.assertEqual(self.type2test(b"a\0\0\0b\0\0\0c\0\0\0") == "abc",
135-
False)
136-
self.assertEqual(self.type2test() == str(), False)
137-
self.assertEqual(self.type2test() != str(), True)
139+
# Byte comparisons with unicode should always fail!
140+
# Test this for all expected byte orders and Unicode character
141+
# sizes.
142+
self.assertEqual(self.type2test(b"\0a\0b\0c") == "abc", False)
143+
self.assertEqual(self.type2test(b"\0\0\0a\0\0\0b\0\0\0c") == "abc",
144+
False)
145+
self.assertEqual(self.type2test(b"a\0b\0c\0") == "abc", False)
146+
self.assertEqual(self.type2test(b"a\0\0\0b\0\0\0c\0\0\0") == "abc",
147+
False)
148+
self.assertEqual(self.type2test() == str(), False)
149+
self.assertEqual(self.type2test() != str(), True)
138150

139151
def test_reversed(self):
140152
input = list(map(ord, "Hello"))
@@ -823,17 +835,16 @@ class AssortedBytesTest(unittest.TestCase):
823835
# Test various combinations of bytes and bytearray
824836
#
825837

838+
@check_bytes_warnings
826839
def test_repr_str(self):
827-
with test.support.check_warnings():
828-
warnings.simplefilter('ignore', BytesWarning)
829-
for f in str, repr:
830-
self.assertEqual(f(bytearray()), "bytearray(b'')")
831-
self.assertEqual(f(bytearray([0])), "bytearray(b'\\x00')")
832-
self.assertEqual(f(bytearray([0, 1, 254, 255])),
833-
"bytearray(b'\\x00\\x01\\xfe\\xff')")
834-
self.assertEqual(f(b"abc"), "b'abc'")
835-
self.assertEqual(f(b"'"), '''b"'"''') # '''
836-
self.assertEqual(f(b"'\""), r"""b'\'"'""") # '
840+
for f in str, repr:
841+
self.assertEqual(f(bytearray()), "bytearray(b'')")
842+
self.assertEqual(f(bytearray([0])), "bytearray(b'\\x00')")
843+
self.assertEqual(f(bytearray([0, 1, 254, 255])),
844+
"bytearray(b'\\x00\\x01\\xfe\\xff')")
845+
self.assertEqual(f(b"abc"), "b'abc'")
846+
self.assertEqual(f(b"'"), '''b"'"''') # '''
847+
self.assertEqual(f(b"'\""), r"""b'\'"'""") # '
837848

838849
def test_compare_bytes_to_bytearray(self):
839850
self.assertEqual(b"abc" == bytes(b"abc"), True)
@@ -876,15 +887,14 @@ def test_from_bytearray(self):
876887
b = bytearray(buf)
877888
self.assertEqual(b, bytearray(sample))
878889

890+
@check_bytes_warnings
879891
def test_to_str(self):
880-
with test.support.check_warnings():
881-
warnings.simplefilter('ignore', BytesWarning)
882-
self.assertEqual(str(b''), "b''")
883-
self.assertEqual(str(b'x'), "b'x'")
884-
self.assertEqual(str(b'\x80'), "b'\\x80'")
885-
self.assertEqual(str(bytearray(b'')), "bytearray(b'')")
886-
self.assertEqual(str(bytearray(b'x')), "bytearray(b'x')")
887-
self.assertEqual(str(bytearray(b'\x80')), "bytearray(b'\\x80')")
892+
self.assertEqual(str(b''), "b''")
893+
self.assertEqual(str(b'x'), "b'x'")
894+
self.assertEqual(str(b'\x80'), "b'\\x80'")
895+
self.assertEqual(str(bytearray(b'')), "bytearray(b'')")
896+
self.assertEqual(str(bytearray(b'x')), "bytearray(b'x')")
897+
self.assertEqual(str(bytearray(b'\x80')), "bytearray(b'\\x80')")
888898

889899
def test_literal(self):
890900
tests = [
@@ -930,19 +940,18 @@ def test_return_self(self):
930940

931941
def test_compare(self):
932942
if sys.flags.bytes_warning:
933-
with test.support.check_warnings():
934-
warnings.simplefilter('error', BytesWarning)
935-
with self.assertRaises(BytesWarning):
936-
b'' == ''
937-
with self.assertRaises(BytesWarning):
938-
b'' != ''
939-
with self.assertRaises(BytesWarning):
940-
bytearray(b'') == ''
941-
with self.assertRaises(BytesWarning):
942-
bytearray(b'') != ''
943+
def bytes_warning():
944+
return test.support.check_warnings(('', BytesWarning))
945+
with bytes_warning():
946+
b'' == ''
947+
with bytes_warning():
948+
b'' != ''
949+
with bytes_warning():
950+
bytearray(b'') == ''
951+
with bytes_warning():
952+
bytearray(b'') != ''
943953
else:
944-
# self.skipTest("BytesWarning is needed for this test: use -bb option")
945-
pass
954+
self.skipTest("BytesWarning is needed for this test: use -bb option")
946955

947956
# Optimizations:
948957
# __iter__? (optimization)

0 commit comments

Comments
 (0)