Test that an 'invalid continuation byte' error is raised when the continuation byte(s) of a 3-bytes sequence are invalid. When errors='replace', if the first continuation byte is valid, the first two bytes (start byte + 1st cb) are replaced by a single U+FFFD and th
(self)
| 2104 | 'invalid continuation byte') |
| 2105 | |
| 2106 | def test_invalid_cb_for_3bytes_seq(self): |
| 2107 | """ |
| 2108 | Test that an 'invalid continuation byte' error is raised when the |
| 2109 | continuation byte(s) of a 3-bytes sequence are invalid. When |
| 2110 | errors='replace', if the first continuation byte is valid, the first |
| 2111 | two bytes (start byte + 1st cb) are replaced by a single U+FFFD and the |
| 2112 | third byte is handled separately, otherwise only the start byte is |
| 2113 | replaced with a U+FFFD and the other continuation bytes are handled |
| 2114 | separately. |
| 2115 | E.g. in the sequence <E1 80 41>, E1 is the start byte of a 3-bytes |
| 2116 | sequence, 80 is a valid continuation byte, but 41 is not a valid cb |
| 2117 | because it's the ASCII letter 'A'. |
| 2118 | Note: when the start byte is E0 or ED, the valid ranges for the first |
| 2119 | continuation byte are limited to A0..BF and 80..9F respectively. |
| 2120 | Python 2 used to consider all the bytes in range 80..BF valid when the |
| 2121 | start byte was ED. This is fixed in Python 3. |
| 2122 | """ |
| 2123 | FFFD = '\ufffd' |
| 2124 | FFFDx2 = FFFD * 2 |
| 2125 | sequences = [ |
| 2126 | ('E0 00', FFFD+'\x00'), ('E0 7F', FFFD+'\x7f'), ('E0 80', FFFDx2), |
| 2127 | ('E0 9F', FFFDx2), ('E0 C0', FFFDx2), ('E0 FF', FFFDx2), |
| 2128 | ('E0 A0 00', FFFD+'\x00'), ('E0 A0 7F', FFFD+'\x7f'), |
| 2129 | ('E0 A0 C0', FFFDx2), ('E0 A0 FF', FFFDx2), |
| 2130 | ('E0 BF 00', FFFD+'\x00'), ('E0 BF 7F', FFFD+'\x7f'), |
| 2131 | ('E0 BF C0', FFFDx2), ('E0 BF FF', FFFDx2), ('E1 00', FFFD+'\x00'), |
| 2132 | ('E1 7F', FFFD+'\x7f'), ('E1 C0', FFFDx2), ('E1 FF', FFFDx2), |
| 2133 | ('E1 80 00', FFFD+'\x00'), ('E1 80 7F', FFFD+'\x7f'), |
| 2134 | ('E1 80 C0', FFFDx2), ('E1 80 FF', FFFDx2), |
| 2135 | ('E1 BF 00', FFFD+'\x00'), ('E1 BF 7F', FFFD+'\x7f'), |
| 2136 | ('E1 BF C0', FFFDx2), ('E1 BF FF', FFFDx2), ('EC 00', FFFD+'\x00'), |
| 2137 | ('EC 7F', FFFD+'\x7f'), ('EC C0', FFFDx2), ('EC FF', FFFDx2), |
| 2138 | ('EC 80 00', FFFD+'\x00'), ('EC 80 7F', FFFD+'\x7f'), |
| 2139 | ('EC 80 C0', FFFDx2), ('EC 80 FF', FFFDx2), |
| 2140 | ('EC BF 00', FFFD+'\x00'), ('EC BF 7F', FFFD+'\x7f'), |
| 2141 | ('EC BF C0', FFFDx2), ('EC BF FF', FFFDx2), ('ED 00', FFFD+'\x00'), |
| 2142 | ('ED 7F', FFFD+'\x7f'), |
| 2143 | ('ED A0', FFFDx2), ('ED BF', FFFDx2), # see note ^ |
| 2144 | ('ED C0', FFFDx2), ('ED FF', FFFDx2), ('ED 80 00', FFFD+'\x00'), |
| 2145 | ('ED 80 7F', FFFD+'\x7f'), ('ED 80 C0', FFFDx2), |
| 2146 | ('ED 80 FF', FFFDx2), ('ED 9F 00', FFFD+'\x00'), |
| 2147 | ('ED 9F 7F', FFFD+'\x7f'), ('ED 9F C0', FFFDx2), |
| 2148 | ('ED 9F FF', FFFDx2), ('EE 00', FFFD+'\x00'), |
| 2149 | ('EE 7F', FFFD+'\x7f'), ('EE C0', FFFDx2), ('EE FF', FFFDx2), |
| 2150 | ('EE 80 00', FFFD+'\x00'), ('EE 80 7F', FFFD+'\x7f'), |
| 2151 | ('EE 80 C0', FFFDx2), ('EE 80 FF', FFFDx2), |
| 2152 | ('EE BF 00', FFFD+'\x00'), ('EE BF 7F', FFFD+'\x7f'), |
| 2153 | ('EE BF C0', FFFDx2), ('EE BF FF', FFFDx2), ('EF 00', FFFD+'\x00'), |
| 2154 | ('EF 7F', FFFD+'\x7f'), ('EF C0', FFFDx2), ('EF FF', FFFDx2), |
| 2155 | ('EF 80 00', FFFD+'\x00'), ('EF 80 7F', FFFD+'\x7f'), |
| 2156 | ('EF 80 C0', FFFDx2), ('EF 80 FF', FFFDx2), |
| 2157 | ('EF BF 00', FFFD+'\x00'), ('EF BF 7F', FFFD+'\x7f'), |
| 2158 | ('EF BF C0', FFFDx2), ('EF BF FF', FFFDx2), |
| 2159 | ] |
| 2160 | for seq, res in sequences: |
| 2161 | self.assertCorrectUTF8Decoding(bytes.fromhex(seq), res, |
| 2162 | 'invalid continuation byte') |
| 2163 |
nothing calls this directly
no test coverage detected