MCPcopy Index your code
hub / github.com/python/cpython / test_bytearray_api

Method test_bytearray_api

Lib/test/test_bytes.py:1397–1421  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

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.

Callers

nothing calls this directly

Calls 8

listClass · 0.85
mktempMethod · 0.80
openFunction · 0.50
writeMethod · 0.45
readintoMethod · 0.45
assertEqualMethod · 0.45
readMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected