Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_bytes.py @ 41455:5dfd2f4bc834
Support name and mode attributes on all file types.
Don't read more than one line when reading text from a tty device.
Add peek() and read1() methods.
Return str instead of unicode when return ASCII characters in text mode.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 13 Apr 2007 18:42:35 +0000 |
| parents | e2d7a4c39360 |
| children | beacf40b5960 |
| rev | line source |
|---|---|
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 """Unit tests for the bytes type.""" |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3 import os |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
4 import re |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 import sys |
|
41425
0486deaaade3
An example of action-at-a-distance: fix the problems I had in test_io.py
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
6 import pickle |
|
0486deaaade3
An example of action-at-a-distance: fix the problems I had in test_io.py
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
7 import cPickle |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
8 import tempfile |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 import unittest |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 import test.test_support |
|
41038
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
11 import test.string_tests |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 class BytesTest(unittest.TestCase): |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 def test_basics(self): |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 b = bytes() |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 self.assertEqual(type(b), bytes) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 self.assertEqual(b.__class__, bytes) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 def test_empty_sequence(self): |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 b = bytes() |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 self.assertEqual(len(b), 0) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 self.assertRaises(IndexError, lambda: b[0]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 self.assertRaises(IndexError, lambda: b[1]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 self.assertRaises(IndexError, lambda: b[sys.maxint]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 self.assertRaises(IndexError, lambda: b[sys.maxint+1]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 self.assertRaises(IndexError, lambda: b[10**100]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 self.assertRaises(IndexError, lambda: b[-1]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 self.assertRaises(IndexError, lambda: b[-2]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 self.assertRaises(IndexError, lambda: b[-sys.maxint]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 self.assertRaises(IndexError, lambda: b[-sys.maxint-1]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 self.assertRaises(IndexError, lambda: b[-sys.maxint-2]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 self.assertRaises(IndexError, lambda: b[-10**100]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 def test_from_list(self): |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 ints = list(range(256)) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 b = bytes(i for i in ints) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 self.assertEqual(len(b), 256) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 self.assertEqual(list(b), ints) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 def test_from_index(self): |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 class C: |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 def __init__(self, i=0): |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 self.i = i |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 def __index__(self): |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 return self.i |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 b = bytes([C(), C(1), C(254), C(255)]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 self.assertEqual(list(b), [0, 1, 254, 255]) |
|
37930
e1252cea439c
Fix a leak and a buglet discovered by Thomas.
Guido van Rossum <guido@python.org>
parents:
37928
diff
changeset
|
50 self.assertRaises(ValueError, bytes, [C(-1)]) |
|
e1252cea439c
Fix a leak and a buglet discovered by Thomas.
Guido van Rossum <guido@python.org>
parents:
37928
diff
changeset
|
51 self.assertRaises(ValueError, bytes, [C(256)]) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 def test_constructor_type_errors(self): |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
54 self.assertRaises(TypeError, bytes, 0.0) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 class C: |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 pass |
|
37930
e1252cea439c
Fix a leak and a buglet discovered by Thomas.
Guido van Rossum <guido@python.org>
parents:
37928
diff
changeset
|
57 self.assertRaises(TypeError, bytes, ["0"]) |
|
e1252cea439c
Fix a leak and a buglet discovered by Thomas.
Guido van Rossum <guido@python.org>
parents:
37928
diff
changeset
|
58 self.assertRaises(TypeError, bytes, [0.0]) |
|
e1252cea439c
Fix a leak and a buglet discovered by Thomas.
Guido van Rossum <guido@python.org>
parents:
37928
diff
changeset
|
59 self.assertRaises(TypeError, bytes, [None]) |
|
e1252cea439c
Fix a leak and a buglet discovered by Thomas.
Guido van Rossum <guido@python.org>
parents:
37928
diff
changeset
|
60 self.assertRaises(TypeError, bytes, [C()]) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 def test_constructor_value_errors(self): |
|
37930
e1252cea439c
Fix a leak and a buglet discovered by Thomas.
Guido van Rossum <guido@python.org>
parents:
37928
diff
changeset
|
63 self.assertRaises(ValueError, bytes, [-1]) |
|
e1252cea439c
Fix a leak and a buglet discovered by Thomas.
Guido van Rossum <guido@python.org>
parents:
37928
diff
changeset
|
64 self.assertRaises(ValueError, bytes, [-sys.maxint]) |
|
e1252cea439c
Fix a leak and a buglet discovered by Thomas.
Guido van Rossum <guido@python.org>
parents:
37928
diff
changeset
|
65 self.assertRaises(ValueError, bytes, [-sys.maxint-1]) |
|
39631
8d71f9a6c1c3
Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
66 self.assertRaises(ValueError, bytes, [-sys.maxint-2]) |
|
8d71f9a6c1c3
Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
67 self.assertRaises(ValueError, bytes, [-10**100]) |
|
37930
e1252cea439c
Fix a leak and a buglet discovered by Thomas.
Guido van Rossum <guido@python.org>
parents:
37928
diff
changeset
|
68 self.assertRaises(ValueError, bytes, [256]) |
|
e1252cea439c
Fix a leak and a buglet discovered by Thomas.
Guido van Rossum <guido@python.org>
parents:
37928
diff
changeset
|
69 self.assertRaises(ValueError, bytes, [257]) |
|
e1252cea439c
Fix a leak and a buglet discovered by Thomas.
Guido van Rossum <guido@python.org>
parents:
37928
diff
changeset
|
70 self.assertRaises(ValueError, bytes, [sys.maxint]) |
|
39631
8d71f9a6c1c3
Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
71 self.assertRaises(ValueError, bytes, [sys.maxint+1]) |
|
8d71f9a6c1c3
Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
72 self.assertRaises(ValueError, bytes, [10**100]) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 def test_repr(self): |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40894
diff
changeset
|
75 self.assertEqual(repr(bytes()), "b''") |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40894
diff
changeset
|
76 self.assertEqual(repr(bytes([0])), "b'\\0'") |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40894
diff
changeset
|
77 self.assertEqual(repr(bytes([0, 1, 254, 255])), "b'\\0\\x01\\xfe\\xff'") |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40894
diff
changeset
|
78 self.assertEqual(repr(bytes('abc')), "b'abc'") |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40894
diff
changeset
|
79 self.assertEqual(repr(bytes("'")), "b'\\''") |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 def test_compare(self): |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 b1 = bytes([1, 2, 3]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 b2 = bytes([1, 2, 3]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 b3 = bytes([1, 3]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 self.failUnless(b1 == b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 self.failUnless(b2 != b3) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 self.failUnless(b1 <= b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 self.failUnless(b1 <= b3) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 self.failUnless(b1 < b3) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 self.failUnless(b1 >= b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 self.failUnless(b3 >= b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 self.failUnless(b3 > b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 self.failIf(b1 != b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 self.failIf(b2 == b3) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 self.failIf(b1 > b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 self.failIf(b1 > b3) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 self.failIf(b1 >= b3) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 self.failIf(b1 < b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 self.failIf(b3 < b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 self.failIf(b3 <= b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
41038
diff
changeset
|
104 def test_compare_to_str(self): |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
41038
diff
changeset
|
105 self.assertEqual(b"abc" == "abc", True) |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
41038
diff
changeset
|
106 self.assertEqual(b"ab" != "abc", True) |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
41038
diff
changeset
|
107 self.assertEqual(b"ab" <= "abc", True) |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
41038
diff
changeset
|
108 self.assertEqual(b"ab" < "abc", True) |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
41038
diff
changeset
|
109 self.assertEqual(b"abc" >= "ab", True) |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
41038
diff
changeset
|
110 self.assertEqual(b"abc" > "ab", True) |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
41038
diff
changeset
|
111 |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
41038
diff
changeset
|
112 self.assertEqual(b"abc" != "abc", False) |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
41038
diff
changeset
|
113 self.assertEqual(b"ab" == "abc", False) |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
41038
diff
changeset
|
114 self.assertEqual(b"ab" > "abc", False) |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
41038
diff
changeset
|
115 self.assertEqual(b"ab" >= "abc", False) |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
41038
diff
changeset
|
116 self.assertEqual(b"abc" < "ab", False) |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
41038
diff
changeset
|
117 self.assertEqual(b"abc" <= "ab", False) |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
41038
diff
changeset
|
118 |
|
41412
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
119 self.assertEqual("abc" == b"abc", True) |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
120 self.assertEqual("ab" != b"abc", True) |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
121 self.assertEqual("ab" <= b"abc", True) |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
122 self.assertEqual("ab" < b"abc", True) |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
123 self.assertEqual("abc" >= b"ab", True) |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
124 self.assertEqual("abc" > b"ab", True) |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
125 |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
126 self.assertEqual("abc" != b"abc", False) |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
127 self.assertEqual("ab" == b"abc", False) |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
128 self.assertEqual("ab" > b"abc", False) |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
129 self.assertEqual("ab" >= b"abc", False) |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
130 self.assertEqual("abc" < b"ab", False) |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
131 self.assertEqual("abc" <= b"ab", False) |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
132 |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
133 # But they should never compare equal to Unicode! |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
134 # Test this for all expected byte orders and Unicode character sizes |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
135 self.assertEqual(b"\0a\0b\0c" == u"abc", False) |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
136 self.assertEqual(b"\0\0\0a\0\0\0b\0\0\0c" == u"abc", False) |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
137 self.assertEqual(b"a\0b\0c\0" == u"abc", False) |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
138 self.assertEqual(b"a\0\0\0b\0\0\0c\0\0\0" == u"abc", False) |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
139 |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 def test_nohash(self): |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 self.assertRaises(TypeError, hash, bytes()) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 def test_doc(self): |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 self.failUnless(bytes.__doc__ != None) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 self.failUnless(bytes.__doc__.startswith("bytes(")) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
147 def test_buffer_api(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
148 short_sample = "Hello world\n" |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
149 sample = short_sample + "x"*(20 - len(short_sample)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
150 tfn = tempfile.mktemp() |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
151 try: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
152 # Prepare |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
153 with open(tfn, "wb") as f: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
154 f.write(short_sample) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
155 # Test readinto |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
156 with open(tfn, "rb") as f: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
157 b = bytes([ord('x')]*20) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
158 n = f.readinto(b) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
159 self.assertEqual(n, len(short_sample)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
160 self.assertEqual(list(b), map(ord, sample)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
161 # Test writing in binary mode |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
162 with open(tfn, "wb") as f: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
163 f.write(b) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
164 with open(tfn, "rb") as f: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
165 self.assertEqual(f.read(), sample) |
|
41455
5dfd2f4bc834
Support name and mode attributes on all file types.
Guido van Rossum <guido@python.org>
parents:
41451
diff
changeset
|
166 # Text mode is ambiguous; don't test |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
167 finally: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
168 try: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
169 os.remove(tfn) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
170 except os.error: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
171 pass |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
172 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
173 def test_reversed(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
174 input = map(ord, "Hello") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
175 b = bytes(input) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
176 output = list(reversed(b)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
177 input.reverse() |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
178 self.assertEqual(output, input) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
179 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
180 def test_reverse(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
181 b = b'hello' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
182 self.assertEqual(b.reverse(), None) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
183 self.assertEqual(b, b'olleh') |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
184 b = b'hello1' # test even number of items |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
185 b.reverse() |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
186 self.assertEqual(b, b'1olleh') |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
187 b = bytes() |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
188 b.reverse() |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
189 self.assertFalse(b) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
190 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
191 def test_getslice(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
192 def by(s): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
193 return bytes(map(ord, s)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
194 b = by("Hello, world") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
195 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
196 self.assertEqual(b[:5], by("Hello")) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
197 self.assertEqual(b[1:5], by("ello")) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
198 self.assertEqual(b[5:7], by(", ")) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
199 self.assertEqual(b[7:], by("world")) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
200 self.assertEqual(b[7:12], by("world")) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
201 self.assertEqual(b[7:100], by("world")) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
202 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
203 self.assertEqual(b[:-7], by("Hello")) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
204 self.assertEqual(b[-11:-7], by("ello")) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
205 self.assertEqual(b[-7:-5], by(", ")) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
206 self.assertEqual(b[-5:], by("world")) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
207 self.assertEqual(b[-5:12], by("world")) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
208 self.assertEqual(b[-5:100], by("world")) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
209 self.assertEqual(b[-100:5], by("Hello")) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
210 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
211 def test_extended_getslice(self): |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
212 # Test extended slicing by comparing with list slicing. |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
213 L = list(range(255)) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
214 b = bytes(L) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
215 indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
216 for start in indices: |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
217 for stop in indices: |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
218 # Skip step 0 (invalid) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
219 for step in indices[1:]: |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
220 self.assertEqual(b[start:stop:step], bytes(L[start:stop:step])) |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
221 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
222 def test_regexps(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
223 def by(s): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
224 return bytes(map(ord, s)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
225 b = by("Hello, world") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
226 self.assertEqual(re.findall(r"\w+", b), [by("Hello"), by("world")]) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
227 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
228 def test_setitem(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
229 b = bytes([1, 2, 3]) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
230 b[1] = 100 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
231 self.assertEqual(b, bytes([1, 100, 3])) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
232 b[-1] = 200 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
233 self.assertEqual(b, bytes([1, 100, 200])) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
234 class C: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
235 def __init__(self, i=0): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
236 self.i = i |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
237 def __index__(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
238 return self.i |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
239 b[0] = C(10) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
240 self.assertEqual(b, bytes([10, 100, 200])) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
241 try: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
242 b[3] = 0 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
243 self.fail("Didn't raise IndexError") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
244 except IndexError: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
245 pass |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
246 try: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
247 b[-10] = 0 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
248 self.fail("Didn't raise IndexError") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
249 except IndexError: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
250 pass |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
251 try: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
252 b[0] = 256 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
253 self.fail("Didn't raise ValueError") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
254 except ValueError: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
255 pass |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
256 try: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
257 b[0] = C(-1) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
258 self.fail("Didn't raise ValueError") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
259 except ValueError: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
260 pass |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
261 try: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
262 b[0] = None |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
263 self.fail("Didn't raise TypeError") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
264 except TypeError: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
265 pass |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
266 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
267 def test_delitem(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
268 b = bytes(range(10)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
269 del b[0] |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
270 self.assertEqual(b, bytes(range(1, 10))) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
271 del b[-1] |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
272 self.assertEqual(b, bytes(range(1, 9))) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
273 del b[4] |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
274 self.assertEqual(b, bytes([1, 2, 3, 4, 6, 7, 8])) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
275 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
276 def test_setslice(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
277 b = bytes(range(10)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
278 self.assertEqual(list(b), list(range(10))) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
279 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
280 b[0:5] = bytes([1, 1, 1, 1, 1]) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
281 self.assertEqual(b, bytes([1, 1, 1, 1, 1, 5, 6, 7, 8, 9])) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
282 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
283 del b[0:-5] |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
284 self.assertEqual(b, bytes([5, 6, 7, 8, 9])) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
285 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
286 b[0:0] = bytes([0, 1, 2, 3, 4]) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
287 self.assertEqual(b, bytes(range(10))) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
288 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
289 b[-7:-3] = bytes([100, 101]) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
290 self.assertEqual(b, bytes([0, 1, 2, 100, 101, 7, 8, 9])) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
291 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
292 b[3:5] = [3, 4, 5, 6] |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
293 self.assertEqual(b, bytes(range(10))) |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
294 |
|
39643
3dd61ffd049e
Fix buglet in slice assignment of bytesobjects: assigning to b[3:0] ('stop'
Thomas Wouters <thomas@python.org>
parents:
39631
diff
changeset
|
295 b[3:0] = [42, 42, 42] |
|
3dd61ffd049e
Fix buglet in slice assignment of bytesobjects: assigning to b[3:0] ('stop'
Thomas Wouters <thomas@python.org>
parents:
39631
diff
changeset
|
296 self.assertEqual(b, bytes([0, 1, 2, 42, 42, 42, 3, 4, 5, 6, 7, 8, 9])) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
297 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
298 def test_extended_set_del_slice(self): |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
299 indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
300 for start in indices: |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
301 for stop in indices: |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
302 # Skip invalid step 0 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
303 for step in indices[1:]: |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
304 L = list(range(255)) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
305 b = bytes(L) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
306 # Make sure we have a slice of exactly the right length, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
307 # but with different data. |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
308 data = L[start:stop:step] |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
309 data.reverse() |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
310 L[start:stop:step] = data |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
311 b[start:stop:step] = data |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
312 self.assertEquals(b, bytes(L)) |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
313 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
314 del L[start:stop:step] |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
315 del b[start:stop:step] |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
316 self.assertEquals(b, bytes(L)) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
317 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
318 def test_setslice_trap(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
319 # This test verifies that we correctly handle assigning self |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
320 # to a slice of self (the old Lambert Meertens trap). |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
321 b = bytes(range(256)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
322 b[8:] = b |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
323 self.assertEqual(b, bytes(list(range(8)) + list(range(256)))) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
324 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
325 def test_encoding(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
326 sample = u"Hello world\n\u1234\u5678\u9abc\udef0" |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
327 for enc in ("utf8", "utf16"): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
328 b = bytes(sample, enc) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
329 self.assertEqual(b, bytes(map(ord, sample.encode(enc)))) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
330 self.assertRaises(UnicodeEncodeError, bytes, sample, "latin1") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
331 b = bytes(sample, "latin1", "ignore") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
332 self.assertEqual(b, bytes(sample[:-4])) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
333 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
334 def test_decode(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
335 sample = u"Hello world\n\u1234\u5678\u9abc\def0\def0" |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
336 for enc in ("utf8", "utf16"): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
337 b = bytes(sample, enc) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
338 self.assertEqual(b.decode(enc), sample) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
339 sample = u"Hello world\n\x80\x81\xfe\xff" |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
340 b = bytes(sample, "latin1") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
341 self.assertRaises(UnicodeDecodeError, b.decode, "utf8") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
342 self.assertEqual(b.decode("utf8", "ignore"), "Hello world\n") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
343 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
344 def test_from_buffer(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
345 sample = "Hello world\n\x80\x81\xfe\xff" |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
346 buf = buffer(sample) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
347 b = bytes(buf) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
348 self.assertEqual(b, bytes(map(ord, sample))) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
349 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
350 def test_to_str(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
351 sample = "Hello world\n\x80\x81\xfe\xff" |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
352 b = bytes(sample) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
353 self.assertEqual(str(b), sample) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
354 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
355 def test_from_int(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
356 b = bytes(0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
357 self.assertEqual(b, bytes()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
358 b = bytes(10) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
359 self.assertEqual(b, bytes([0]*10)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
360 b = bytes(10000) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
361 self.assertEqual(b, bytes([0]*10000)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
362 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
363 def test_concat(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
364 b1 = bytes("abc") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
365 b2 = bytes("def") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
366 self.assertEqual(b1 + b2, bytes("abcdef")) |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
367 self.assertEqual(b1 + "def", bytes("abcdef")) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
368 self.assertEqual("def" + b1, bytes("defabc")) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
369 self.assertRaises(TypeError, lambda: b1 + u"def") |
|
41451
e2d7a4c39360
Disallow u"..." + b"..." and b"..." + u"...".
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
370 self.assertRaises(TypeError, lambda: u"abc" + b2) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
371 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
372 def test_repeat(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
373 b = bytes("abc") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
374 self.assertEqual(b * 3, bytes("abcabcabc")) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
375 self.assertEqual(b * 0, bytes()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
376 self.assertEqual(b * -1, bytes()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
377 self.assertRaises(TypeError, lambda: b * 3.14) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
378 self.assertRaises(TypeError, lambda: 3.14 * b) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
379 self.assertRaises(MemoryError, lambda: b * sys.maxint) |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
380 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
381 def test_repeat_1char(self): |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
382 self.assertEqual(bytes('x')*100, bytes('x'*100)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
383 |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
384 def test_iconcat(self): |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
385 b = bytes("abc") |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
386 b1 = b |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
387 b += bytes("def") |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
388 self.assertEqual(b, bytes("abcdef")) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
389 self.assertEqual(b, b1) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
390 self.failUnless(b is b1) |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
391 b += "xyz" |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
392 self.assertEqual(b, b"abcdefxyz") |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
393 try: |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
394 b += u"" |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
395 except TypeError: |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
396 pass |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
397 else: |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
398 self.fail("bytes += unicode didn't raise TypeError") |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
399 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
400 def test_irepeat(self): |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
401 b = bytes("abc") |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
402 b1 = b |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
403 b *= 3 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
404 self.assertEqual(b, bytes("abcabcabc")) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
405 self.assertEqual(b, b1) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
406 self.failUnless(b is b1) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
407 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
408 def test_irepeat_1char(self): |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
409 b = bytes("x") |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
410 b1 = b |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
411 b *= 100 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
412 self.assertEqual(b, bytes("x"*100)) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
413 self.assertEqual(b, b1) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
414 self.failUnless(b is b1) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
415 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
416 def test_contains(self): |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
417 b = bytes("abc") |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
418 self.failUnless(ord('a') in b) |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40472
diff
changeset
|
419 self.failUnless(int(ord('a')) in b) |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
420 self.failIf(200 in b) |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40472
diff
changeset
|
421 self.failIf(200 in b) |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
422 self.assertRaises(ValueError, lambda: 300 in b) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
423 self.assertRaises(ValueError, lambda: -1 in b) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
424 self.assertRaises(TypeError, lambda: None in b) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
425 self.assertRaises(TypeError, lambda: float(ord('a')) in b) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
426 self.assertRaises(TypeError, lambda: "a" in b) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
427 self.failUnless(bytes("") in b) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
428 self.failUnless(bytes("a") in b) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
429 self.failUnless(bytes("b") in b) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
430 self.failUnless(bytes("c") in b) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
431 self.failUnless(bytes("ab") in b) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
432 self.failUnless(bytes("bc") in b) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
433 self.failUnless(bytes("abc") in b) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
434 self.failIf(bytes("ac") in b) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
435 self.failIf(bytes("d") in b) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
436 self.failIf(bytes("dab") in b) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
437 self.failIf(bytes("abd") in b) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
438 |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
439 def test_alloc(self): |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
440 b = bytes() |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
441 alloc = b.__alloc__() |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
442 self.assert_(alloc >= 0) |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
443 seq = [alloc] |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
444 for i in range(100): |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
445 b += bytes("x") |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
446 alloc = b.__alloc__() |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
447 self.assert_(alloc >= len(b)) |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
448 if alloc not in seq: |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
449 seq.append(alloc) |
|
38398
b1896656f22e
Comment out debug print.
Guido van Rossum <guido@python.org>
parents:
38117
diff
changeset
|
450 #print seq |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
451 |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
452 def test_fromhex(self): |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
453 self.assertRaises(TypeError, bytes.fromhex) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
454 self.assertRaises(TypeError, bytes.fromhex, 1) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
455 self.assertEquals(bytes.fromhex(''), bytes()) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
456 b = bytes([0x1a, 0x2b, 0x30]) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
457 self.assertEquals(bytes.fromhex('1a2B30'), b) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
458 self.assertEquals(bytes.fromhex(' 1A 2B 30 '), b) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
459 self.assertEquals(bytes.fromhex(buffer('')), bytes()) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
460 self.assertEquals(bytes.fromhex(buffer('0000')), bytes([0, 0])) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
461 self.assertRaises(ValueError, bytes.fromhex, 'a') |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
462 self.assertRaises(ValueError, bytes.fromhex, 'rt') |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
463 self.assertRaises(ValueError, bytes.fromhex, '1a b cd') |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
464 self.assertRaises(ValueError, bytes.fromhex, '\x00') |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
465 self.assertRaises(ValueError, bytes.fromhex, '12 \x00 34') |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
466 |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
467 def test_join(self): |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
468 self.assertEqual(bytes.join([]), bytes()) |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
469 self.assertEqual(bytes.join([bytes()]), bytes()) |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
470 for part in [("abc",), ("a", "bc"), ("ab", "c"), ("a", "b", "c")]: |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
471 lst = map(bytes, part) |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
472 self.assertEqual(bytes.join(lst), bytes("abc")) |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
473 self.assertEqual(bytes.join(tuple(lst)), bytes("abc")) |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
474 self.assertEqual(bytes.join(iter(lst)), bytes("abc")) |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
475 # XXX more... |
| 40894 | 476 |
| 477 def test_literal(self): | |
| 478 tests = [ | |
| 479 (b"Wonderful spam", u"Wonderful spam"), | |
| 480 (br"Wonderful spam too", u"Wonderful spam too"), | |
| 481 (b"\xaa\x00\000\200", u"\xaa\x00\000\200"), | |
| 482 (br"\xaa\x00\000\200", ur"\xaa\x00\000\200"), | |
| 483 ] | |
| 484 for b, s in tests: | |
| 485 self.assertEqual(b, bytes(s, 'latin-1')) | |
| 486 for c in range(128, 256): | |
| 487 self.assertRaises(SyntaxError, eval, | |
| 488 'b"%s"' % chr(c)) | |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
489 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
490 def test_extend(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
491 orig = b'hello' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
492 a = bytes(orig) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
493 a.extend(a) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
494 self.assertEqual(a, orig + orig) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
495 self.assertEqual(a[5:], orig) |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
496 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
497 def test_remove(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
498 b = b'hello' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
499 b.remove(ord('l')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
500 self.assertEqual(b, b'helo') |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
501 b.remove(ord('l')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
502 self.assertEqual(b, b'heo') |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
503 self.assertRaises(ValueError, lambda: b.remove(ord('l'))) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
504 self.assertRaises(ValueError, lambda: b.remove(400)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
505 self.assertRaises(ValueError, lambda: b.remove('e')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
506 # remove first and last |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
507 b.remove(ord('o')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
508 b.remove(ord('h')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
509 self.assertEqual(b, b'e') |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
510 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
511 def test_pop(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
512 b = b'world' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
513 self.assertEqual(b.pop(), ord('d')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
514 self.assertEqual(b.pop(0), ord('w')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
515 self.assertEqual(b.pop(-2), ord('r')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
516 self.assertRaises(IndexError, lambda: b.pop(10)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
517 self.assertRaises(OverflowError, lambda: bytes().pop()) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
518 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
519 def test_nosort(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
520 self.assertRaises(AttributeError, lambda: bytes().sort()) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
521 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
522 def test_index(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
523 b = b'parrot' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
524 self.assertEqual(b.index('p'), 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
525 self.assertEqual(b.index('rr'), 2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
526 self.assertEqual(b.index('t'), 5) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
527 self.assertRaises(ValueError, lambda: b.index('w')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
528 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
529 def test_count(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
530 b = b'mississippi' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
531 self.assertEqual(b.count('i'), 4) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
532 self.assertEqual(b.count('ss'), 2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
533 self.assertEqual(b.count('w'), 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
534 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
535 def test_append(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
536 b = b'hell' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
537 b.append(ord('o')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
538 self.assertEqual(b, b'hello') |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
539 self.assertEqual(b.append(100), None) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
540 b = bytes() |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
541 b.append(ord('A')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
542 self.assertEqual(len(b), 1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
543 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
544 def test_insert(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
545 b = b'msssspp' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
546 b.insert(1, ord('i')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
547 b.insert(4, ord('i')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
548 b.insert(-2, ord('i')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
549 b.insert(1000, ord('i')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
550 self.assertEqual(b, b'mississippi') |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
551 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
552 def test_startswith(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
553 b = b'hello' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
554 self.assertFalse(bytes().startswith("anything")) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
555 self.assertTrue(b.startswith("hello")) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
556 self.assertTrue(b.startswith("hel")) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
557 self.assertTrue(b.startswith("h")) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
558 self.assertFalse(b.startswith("hellow")) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
559 self.assertFalse(b.startswith("ha")) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
560 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
561 def test_endswith(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
562 b = b'hello' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
563 self.assertFalse(bytes().endswith("anything")) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
564 self.assertTrue(b.endswith("hello")) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
565 self.assertTrue(b.endswith("llo")) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
566 self.assertTrue(b.endswith("o")) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
567 self.assertFalse(b.endswith("whello")) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
568 self.assertFalse(b.endswith("no")) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
569 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
570 def test_find(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
571 b = b'mississippi' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
572 self.assertEqual(b.find('ss'), 2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
573 self.assertEqual(b.find('ss', 3), 5) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
574 self.assertEqual(b.find('ss', 1, 7), 2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
575 self.assertEqual(b.find('ss', 1, 3), -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
576 self.assertEqual(b.find('w'), -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
577 self.assertEqual(b.find('mississippian'), -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
578 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
579 def test_rfind(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
580 b = b'mississippi' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
581 self.assertEqual(b.rfind('ss'), 5) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
582 self.assertEqual(b.rfind('ss', 3), 5) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
583 self.assertEqual(b.rfind('ss', 0, 6), 2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
584 self.assertEqual(b.rfind('w'), -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
585 self.assertEqual(b.rfind('mississippian'), -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
586 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
587 def test_index(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
588 b = b'world' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
589 self.assertEqual(b.index('w'), 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
590 self.assertEqual(b.index('orl'), 1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
591 self.assertRaises(ValueError, lambda: b.index('worm')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
592 self.assertRaises(ValueError, lambda: b.index('ldo')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
593 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
594 def test_rindex(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
595 # XXX could be more rigorous |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
596 b = b'world' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
597 self.assertEqual(b.rindex('w'), 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
598 self.assertEqual(b.rindex('orl'), 1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
599 self.assertRaises(ValueError, lambda: b.rindex('worm')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
600 self.assertRaises(ValueError, lambda: b.rindex('ldo')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
601 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
602 def test_replace(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
603 b = b'mississippi' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
604 self.assertEqual(b.replace('i', 'a'), b'massassappa') |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
605 self.assertEqual(b.replace('ss', 'x'), b'mixixippi') |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
606 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
607 def test_translate(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
608 b = b'hello' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
609 rosetta = bytes(range(0, 256)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
610 rosetta[ord('o')] = ord('e') |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
611 c = b.translate(rosetta, b'l') |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
612 self.assertEqual(b, b'hello') |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
613 self.assertEqual(c, b'hee') |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
614 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
615 def test_split(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
616 b = b'mississippi' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
617 self.assertEqual(b.split('i'), [b'm', b'ss', b'ss', b'pp', b'']) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
618 self.assertEqual(b.split('ss'), [b'mi', b'i', b'ippi']) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
619 self.assertEqual(b.split('w'), [b]) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
620 # require an arg (no magic whitespace split) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
621 self.assertRaises(TypeError, lambda: b.split()) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
622 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
623 def test_rsplit(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
624 b = b'mississippi' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
625 self.assertEqual(b.rsplit('i'), [b'm', b'ss', b'ss', b'pp', b'']) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
626 self.assertEqual(b.rsplit('ss'), [b'mi', b'i', b'ippi']) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
627 self.assertEqual(b.rsplit('w'), [b]) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
628 # require an arg (no magic whitespace split) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
629 self.assertRaises(TypeError, lambda: b.rsplit()) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
630 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
631 def test_partition(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
632 b = b'mississippi' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
633 self.assertEqual(b.partition(b'ss'), (b'mi', b'ss', b'issippi')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
634 self.assertEqual(b.rpartition(b'w'), (b'', b'', b'mississippi')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
635 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
636 def test_rpartition(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
637 b = b'mississippi' |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
638 self.assertEqual(b.rpartition(b'ss'), (b'missi', b'ss', b'ippi')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
639 self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b'')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
640 |
|
41425
0486deaaade3
An example of action-at-a-distance: fix the problems I had in test_io.py
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
641 def test_pickling(self): |
|
0486deaaade3
An example of action-at-a-distance: fix the problems I had in test_io.py
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
642 for pm in pickle, cPickle: |
|
0486deaaade3
An example of action-at-a-distance: fix the problems I had in test_io.py
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
643 for proto in range(pm.HIGHEST_PROTOCOL): |
|
0486deaaade3
An example of action-at-a-distance: fix the problems I had in test_io.py
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
644 for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0": |
|
0486deaaade3
An example of action-at-a-distance: fix the problems I had in test_io.py
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
645 ps = pm.dumps(b, proto) |
|
0486deaaade3
An example of action-at-a-distance: fix the problems I had in test_io.py
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
646 q = pm.loads(ps) |
|
0486deaaade3
An example of action-at-a-distance: fix the problems I had in test_io.py
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
647 self.assertEqual(b, q) |
|
0486deaaade3
An example of action-at-a-distance: fix the problems I had in test_io.py
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
648 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
649 def test_strip(self): |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
650 b = b'mississippi' |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
651 self.assertEqual(b.strip(b'i'), b'mississipp') |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
652 self.assertEqual(b.strip(b'm'), b'ississippi') |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
653 self.assertEqual(b.strip(b'pi'), b'mississ') |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
654 self.assertEqual(b.strip(b'im'), b'ssissipp') |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
655 self.assertEqual(b.strip(b'pim'), b'ssiss') |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
656 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
657 def test_lstrip(self): |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
658 b = b'mississippi' |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
659 self.assertEqual(b.lstrip(b'i'), b'mississippi') |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
660 self.assertEqual(b.lstrip(b'm'), b'ississippi') |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
661 self.assertEqual(b.lstrip(b'pi'), b'mississippi') |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
662 self.assertEqual(b.lstrip(b'im'), b'ssissippi') |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
663 self.assertEqual(b.lstrip(b'pim'), b'ssissippi') |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
664 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
665 def test_rstrip(self): |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
666 b = b'mississippi' |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
667 self.assertEqual(b.rstrip(b'i'), b'mississipp') |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
668 self.assertEqual(b.rstrip(b'm'), b'mississippi') |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
669 self.assertEqual(b.rstrip(b'pi'), b'mississ') |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
670 self.assertEqual(b.rstrip(b'im'), b'mississipp') |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
671 self.assertEqual(b.rstrip(b'pim'), b'mississ') |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
672 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
673 # Optimizations: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
674 # __iter__? (optimization) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
675 # __reversed__? (optimization) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
676 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
677 # XXX More string methods? (Those that don't use character properties) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
678 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
679 # There are tests in string_tests.py that are more |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
680 # comprehensive for things like split, partition, etc. |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
681 # Unfortunately they are all bundled with tests that |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
682 # are not appropriate for bytes |
|
37930
e1252cea439c
Fix a leak and a buglet discovered by Thomas.
Guido van Rossum <guido@python.org>
parents:
37928
diff
changeset
|
683 |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
684 |
|
41038
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
685 class BytesAsStringTest(test.string_tests.BaseTest): |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
686 type2test = bytes |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
687 |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
688 def checkequal(self, result, object, methodname, *args): |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
689 object = bytes(object) |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
690 realresult = getattr(bytes, methodname)(object, *args) |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
691 self.assertEqual( |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
692 self.fixtype(result), |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
693 realresult |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
694 ) |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
695 |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
696 def checkraises(self, exc, object, methodname, *args): |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
697 object = bytes(object) |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
698 self.assertRaises( |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
699 exc, |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
700 getattr(bytes, methodname), |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
701 object, |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
702 *args |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
703 ) |
|
41038
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
704 |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
705 # Currently the bytes containment testing uses a single integer |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
706 # value. This may not be the final design, but until then the |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
707 # bytes section with in a bytes containment not valid |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
708 def test_contains(self): |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
709 pass |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
710 def test_find(self): |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
711 pass |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
712 |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
713 |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
714 def test_main(): |
|
37928
ae6f50600c46
Fix test_main function so test_bytes can be run by regrtest.py.
Thomas Wouters <thomas@python.org>
parents:
37927
diff
changeset
|
715 test.test_support.run_unittest(BytesTest) |
|
41038
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
716 test.test_support.run_unittest(BytesAsStringTest) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
717 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
718 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
719 if __name__ == "__main__": |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
720 test_main() |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
721 ##unittest.main() |
