| 1319 | self.assertRaises(TypeError, hash, bytearray()) |
| 1320 | |
| 1321 | def test_bytearray_api(self): |
| 1322 | short_sample = b"Hello world\n" |
| 1323 | sample = short_sample + b"\0"*(20 - len(short_sample)) |
| 1324 | tfn = tempfile.mktemp() |
| 1325 | try: |
| 1326 | # Prepare |
| 1327 | with open(tfn, "wb") as f: |
| 1328 | f.write(short_sample) |
| 1329 | # Test readinto |
| 1330 | with open(tfn, "rb") as f: |
| 1331 | b = bytearray(20) |
| 1332 | n = f.readinto(b) |
| 1333 | self.assertEqual(n, len(short_sample)) |
| 1334 | self.assertEqual(list(b), list(sample)) |
| 1335 | # Test writing in binary mode |
| 1336 | with open(tfn, "wb") as f: |
| 1337 | f.write(b) |
| 1338 | with open(tfn, "rb") as f: |
| 1339 | self.assertEqual(f.read(), sample) |
| 1340 | # Text mode is ambiguous; don't test |
| 1341 | finally: |
| 1342 | try: |
| 1343 | os.remove(tfn) |
| 1344 | except OSError: |
| 1345 | pass |
| 1346 | |
| 1347 | def test_reverse(self): |
| 1348 | b = bytearray(b'hello') |