| 1121 | self.assertEqual(d.decode(s.encode("utf-8-sig")), s) |
| 1122 | |
| 1123 | def test_stream_bom(self): |
| 1124 | unistring = "ABC\u00A1\u2200XYZ" |
| 1125 | bytestring = codecs.BOM_UTF8 + b"ABC\xC2\xA1\xE2\x88\x80XYZ" |
| 1126 | |
| 1127 | reader = codecs.getreader("utf-8-sig") |
| 1128 | for sizehint in [None] + list(range(1, 11)) + \ |
| 1129 | [64, 128, 256, 512, 1024]: |
| 1130 | istream = reader(io.BytesIO(bytestring)) |
| 1131 | ostream = io.StringIO() |
| 1132 | while 1: |
| 1133 | if sizehint is not None: |
| 1134 | data = istream.read(sizehint) |
| 1135 | else: |
| 1136 | data = istream.read() |
| 1137 | |
| 1138 | if not data: |
| 1139 | break |
| 1140 | ostream.write(data) |
| 1141 | |
| 1142 | got = ostream.getvalue() |
| 1143 | self.assertEqual(got, unistring) |
| 1144 | |
| 1145 | def test_stream_bare(self): |
| 1146 | unistring = "ABC\u00A1\u2200XYZ" |