text stringlengths 0 828 |
|---|
if v == 0: |
break |
return num_bytes" |
637,"def decode_varint(f, max_bytes=4): |
""""""Decode variable integer using algorithm similar to that described |
in MQTT Version 3.1.1 line 297. |
Parameters |
---------- |
f: file |
Object with a read method. |
max_bytes: int or None |
If a varint cannot be constructed using `max_bytes` or fewer |
from f then raises a `DecodeError`. If None then there is no |
maximum number of bytes. |
Raises |
------- |
DecodeError |
When length is greater than max_bytes. |
UnderflowDecodeError |
When file ends before enough bytes can be read to construct the |
varint. |
Returns |
------- |
int |
Number of bytes consumed. |
int |
Value extracted from `f`. |
"""""" |
num_bytes_consumed = 0 |
value = 0 |
m = 1 |
while True: |
buf = f.read(1) |
if len(buf) == 0: |
raise UnderflowDecodeError() |
(u8,) = FIELD_U8.unpack(buf) |
value += (u8 & 0x7f) * m |
m *= 0x80 |
num_bytes_consumed += 1 |
if u8 & 0x80 == 0: |
# No further bytes |
break |
elif max_bytes is not None and num_bytes_consumed >= max_bytes: |
raise DecodeError('Variable integer contained more than maximum bytes ({}).'.format(max_bytes)) |
return num_bytes_consumed, value" |
638,"def unpack(self, struct): |
""""""Read as many bytes as are required to extract struct then |
unpack and return a tuple of the values. |
Raises |
------ |
UnderflowDecodeError |
Raised when a read failed to extract enough bytes from the |
underlying stream to extract the bytes. |
Parameters |
---------- |
struct: struct.Struct |
Returns |
------- |
tuple |
Tuple of extracted values. |
"""""" |
v = struct.unpack(self.read(struct.size)) |
return v" |
639,"def unpack_utf8(self): |
""""""Decode a utf-8 string encoded as described in MQTT Version |
3.1.1 section 1.5.3 line 177. This is a 16-bit unsigned length |
followed by a utf-8 encoded string. |
Raises |
------ |
UnderflowDecodeError |
Raised when a read failed to extract enough bytes from the |
underlying stream to decode the string. |
DecodeError |
When any code point in the utf-8 string is invalid. |
Returns |
------- |
int |
Number of bytes consumed. |
str |
A string utf-8 decoded from the underlying stream. |
"""""" |
num_bytes_consumed, s = decode_utf8(self.__f) |
self.__num_bytes_consumed += num_bytes_consumed |
return num_bytes_consumed, s" |
640,"def unpack_bytes(self): |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.