r""" >>> import io >>> read_string4(io.BytesIO(b"\x00\x00\x00\x00abc")) '' >>> read_string4(io.BytesIO(b"\x03\x00\x00\x00abcdef")) 'abc' >>> read_string4(io.BytesIO(b"\x00\x00\x00\x03abcdef")) Traceback (most recent call last): ... ValueError: expected 50331648 by
(f)
| 436 | |
| 437 | |
| 438 | def read_string4(f): |
| 439 | r""" |
| 440 | >>> import io |
| 441 | >>> read_string4(io.BytesIO(b"\x00\x00\x00\x00abc")) |
| 442 | '' |
| 443 | >>> read_string4(io.BytesIO(b"\x03\x00\x00\x00abcdef")) |
| 444 | 'abc' |
| 445 | >>> read_string4(io.BytesIO(b"\x00\x00\x00\x03abcdef")) |
| 446 | Traceback (most recent call last): |
| 447 | ... |
| 448 | ValueError: expected 50331648 bytes in a string4, but only 6 remain |
| 449 | """ |
| 450 | |
| 451 | n = read_int4(f) |
| 452 | if n < 0: |
| 453 | raise ValueError("string4 byte count < 0: %d" % n) |
| 454 | data = f.read(n) |
| 455 | if len(data) == n: |
| 456 | return data.decode("latin-1") |
| 457 | raise ValueError("expected %d bytes in a string4, but only %d remain" % |
| 458 | (n, len(data))) |
| 459 | |
| 460 | string4 = ArgumentDescriptor( |
| 461 | name="string4", |