| 1395 | self.assertRaises(TypeError, hash, bytearray()) |
| 1396 | |
| 1397 | def test_bytearray_api(self): |
| 1398 | short_sample = b"Hello world\n" |
| 1399 | sample = short_sample + b"\0"*(20 - len(short_sample)) |
| 1400 | tfn = tempfile.mktemp() |
| 1401 | try: |
| 1402 | # Prepare |
| 1403 | with open(tfn, "wb") as f: |
| 1404 | f.write(short_sample) |
| 1405 | # Test readinto |
| 1406 | with open(tfn, "rb") as f: |
| 1407 | b = bytearray(20) |
| 1408 | n = f.readinto(b) |
| 1409 | self.assertEqual(n, len(short_sample)) |
| 1410 | self.assertEqual(list(b), list(sample)) |
| 1411 | # Test writing in binary mode |
| 1412 | with open(tfn, "wb") as f: |
| 1413 | f.write(b) |
| 1414 | with open(tfn, "rb") as f: |
| 1415 | self.assertEqual(f.read(), sample) |
| 1416 | # Text mode is ambiguous; don't test |
| 1417 | finally: |
| 1418 | try: |
| 1419 | os.remove(tfn) |
| 1420 | except OSError: |
| 1421 | pass |
| 1422 | |
| 1423 | def test_mod_concurrent_mutation(self): |
| 1424 | # Prevent crash in __mod__ when formatting mutates the bytearray. |