Read from a stream safely Raises SerializationError and SerializationTruncationError appropriately. Use this instead of f.read() in your classes stream_(de)serialization() functions.
(f, n)
| 56 | self.padding = padding |
| 57 | |
| 58 | def ser_read(f, n): |
| 59 | """Read from a stream safely |
| 60 | |
| 61 | Raises SerializationError and SerializationTruncationError appropriately. |
| 62 | Use this instead of f.read() in your classes stream_(de)serialization() |
| 63 | functions. |
| 64 | """ |
| 65 | if n > MAX_SIZE: |
| 66 | raise SerializationError('Asked to read 0x%x bytes; MAX_SIZE exceeded' % n) |
| 67 | r = f.read(n) |
| 68 | if len(r) < n: |
| 69 | raise SerializationTruncationError('Asked to read %i bytes, but only got %i' % (n, len(r))) |
| 70 | return r |
| 71 | |
| 72 | |
| 73 | class Serializable(object): |
no test coverage detected