Builds one message at a time, from make.
Functions#
__init__#
__init__() -> Nonedef __init__(self) -> None:
self._buf = bytearray()
# The offsets of the length placeholders of the values not joined yet.
self._forks: list[int] = []finish#
finish() -> _BytesThe bytes written, which the caller owns; the writer starts over.
Returns _Bytes
def finish(self) -> _Bytes:
"""The bytes written, which the caller owns; the writer starts over."""
if self._forks:
raise RuntimeError("ProtobufWriter.finish with a fork that is not joined")
out = _Bytes(self._buf)
self._buf = bytearray()
return outfork#
fork() -> SelfStarts a length-delimited value; join ends it.
Returns Self
def fork(self) -> Self:
"""Starts a length-delimited value; ``join`` ends it."""
self._forks.append(len(self._buf))
self._buf.append(0)
return selfjoin#
join() -> SelfEnds the value of the last fork, writing its length before it.
Returns Self
def join(self) -> Self:
"""Ends the value of the last ``fork``, writing its length before it."""
if not self._forks:
raise RuntimeError("ProtobufWriter.join without a fork")
start = self._forks.pop()
length = len(self._buf) - start - 1
if length < 0x80:
self._buf[start] = length
else:
self._buf[start : start + 1] = _varint(length)
return selftag#
tag(field_number, wire_type) -> SelfA field's tag. Raises ValueError for a field number outside
1 through 536870911 or a wire type outside 0 through 5.
| Parameter | Type | Description |
|---|---|---|
field_number | int | |
wire_type | WireType | int |
Returns Self
def tag(self, field_number: int, wire_type: WireType | int) -> Self:
"""A field's tag. Raises ``ValueError`` for a field number outside
1 through 536870911 or a wire type outside 0 through 5."""
if (
isinstance(field_number, _Bool)
or not isinstance(cast("object", field_number), int)
or not 1 <= field_number <= _MAX_FIELD_NUMBER
):
raise ValueError(
"ProtobufWriter.tag field_number must be an integer from 1 through 536870911"
)
if (
isinstance(wire_type, _Bool)
or not isinstance(cast("object", wire_type), int)
or not 0 <= wire_type <= _MAX_WIRE_TYPE
):
raise ValueError("ProtobufWriter.tag wire_type must be an integer from 0 through 5")
return self.uint32(field_number * 8 + wire_type)uint32#
uint32(n) -> Self| Parameter | Type | Description |
|---|---|---|
n | int | _Float |
Returns Self
def uint32(self, n: int | _Float) -> Self:
self._buf += _varint(_to_uint32(n))
return selfint32#
int32(n) -> SelfA negative value takes ten bytes, as its 64-bit two's complement.
| Parameter | Type | Description |
|---|---|---|
n | int | _Float |
Returns Self
def int32(self, n: int | _Float) -> Self:
"""A negative value takes ten bytes, as its 64-bit two's complement."""
self._buf += _varint(_to_int32(n) % _UINT64)
return selfsint32#
sint32(n) -> Self| Parameter | Type | Description |
|---|---|---|
n | int | _Float |
Returns Self
def sint32(self, n: int | _Float) -> Self:
n = _to_int32(n)
return self.uint32((n << 1) ^ (n >> 31))uint64#
uint64(n) -> Self| Parameter | Type | Description |
|---|---|---|
n | int | _Float | str |
Returns Self
def uint64(self, n: int | _Float | str) -> Self:
return self.int64(n)int64#
int64(n) -> Self| Parameter | Type | Description |
|---|---|---|
n | int | _Float | str |
Returns Self
def int64(self, n: int | _Float | str) -> Self:
self._buf += _varint(_to_uint64(n))
return selfsint64#
sint64(n) -> Self| Parameter | Type | Description |
|---|---|---|
n | int | _Float | str |
Returns Self
def sint64(self, n: int | _Float | str) -> Self:
n = _to_uint64(n)
signed = n - _UINT64 if n >= 1 << 63 else n
self._buf += _varint(((signed << 1) ^ (signed >> 63)) % _UINT64)
return selfbool#
bool(value) -> Self| Parameter | Type | Description |
|---|---|---|
value | _Bool |
Returns Self
def bool(self, value: _Bool) -> Self:
self._buf.append(1 if value else 0)
return selffixed32#
fixed32(n) -> Self| Parameter | Type | Description |
|---|---|---|
n | int | _Float |
Returns Self
def fixed32(self, n: int | _Float) -> Self:
self._buf += _to_uint32(n).to_bytes(4, "little")
return selfsfixed32#
sfixed32(n) -> Self| Parameter | Type | Description |
|---|---|---|
n | int | _Float |
Returns Self
def sfixed32(self, n: int | _Float) -> Self:
return self.fixed32(n)fixed64#
fixed64(n) -> Self| Parameter | Type | Description |
|---|---|---|
n | int | _Float | str |
Returns Self
def fixed64(self, n: int | _Float | str) -> Self:
self._buf += _to_uint64(n).to_bytes(8, "little")
return selfsfixed64#
sfixed64(n) -> Self| Parameter | Type | Description |
|---|---|---|
n | int | _Float | str |
Returns Self
def sfixed64(self, n: int | _Float | str) -> Self:
return self.fixed64(n)float#
float(n) -> Selfn as a 32-bit float; one too large for it is an infinity, as
in JavaScript.
| Parameter | Type | Description |
|---|---|---|
n | _Float |
Returns Self
def float(self, n: _Float) -> Self:
"""``n`` as a 32-bit float; one too large for it is an infinity, as
in JavaScript."""
try:
self._buf += _F32.pack(n)
except OverflowError:
self._buf += _F32.pack(math.copysign(math.inf, n))
return selfdouble#
double(n) -> Self| Parameter | Type | Description |
|---|---|---|
n | _Float |
Returns Self
def double(self, n: _Float) -> Self:
self._buf += _F64.pack(n)
return selfbytes#
bytes(data) -> Selfdata with its length before it.
| Parameter | Type | Description |
|---|---|---|
data | _Bytes | bytearray | memoryview |
Returns Self
def bytes(self, data: _Bytes | bytearray | memoryview) -> Self:
"""``data`` with its length before it."""
self.uint32(len(data))
return self.raw(data)raw#
raw(data) -> Selfdata as it is, with no length.
| Parameter | Type | Description |
|---|---|---|
data | _Bytes | bytearray | memoryview |
Returns Self
def raw(self, data: _Bytes | bytearray | memoryview) -> Self:
"""``data`` as it is, with no length."""
self._buf += data
return selfstring#
string(text) -> Selftext as UTF-8 with its length before it; a lone surrogate is
written as U+FFFD, as TextEncoder writes it.
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns Self
def string(self, text: str) -> Self:
"""``text`` as UTF-8 with its length before it; a lone surrogate is
written as U+FFFD, as ``TextEncoder`` writes it."""
return self.bytes(text_encode(text))