Recording and atoms#
Recording#
record#
def record(self, path: str | os.PathLike[str], /, *, flush: bool = False) -> Recording
Records the engine’s fact stream to path with libds’s recorder, and returns the Recording.
The stream’s format is in Fact stream, and RFCS.3.6 gives the atom each event kind records as. The file is created, or truncated if it exists. flush=True flushes it after every atom.
Raises |
Condition |
|---|---|
|
|
|
|
|
the file cannot be opened |
|
a recording is running |
|
the recording’s buffer cannot be allocated |
|
where the engine has had a stream event, |
|
where the engine has had a stream event, |
|
where the engine has had a stream event, |
with engine.record("trace.bin", flush=True) as rec:
engine.pump()
print(rec.closed)
(Source: bindings/python/src/pyds/engine.py, bindings/python/src/_pyds/engine.c, runtime/src/libds/rfcs/codec.c)
Recording#
class Recording
A recording of an engine’s fact stream. Engine.record returns one.
It writes until close, or the end of a with block. Engine.close ends it too, reporting no failure.
(Source: bindings/python/src/pyds/engine.py)
path#
path: Path
The file the recording writes.
(Source: bindings/python/src/pyds/engine.py)
closed#
closed: bool
Whether the recording has stopped.
(Source: bindings/python/src/pyds/engine.py)
close#
def close(self) -> None
Stops the recording and closes its file.
A second close does nothing. It raises Error for the recorder’s first failure, and OSError if closing the file fails. The recording has stopped either way. The same failure is also the status of the call whose event it lost.
(Source: bindings/python/src/pyds/engine.py, bindings/python/src/_pyds/engine.c)
Replay#
replay#
def replay(self, stream: Buffer | Iterable[Buffer] | _Reader, /, *, chunk_size: int = 65536) -> ReplayResult
Replays a recorded fact stream into the engine, checking it as it goes.
stream is a bytes-like object, a binary file read chunk_size bytes at a time, or an iterable of bytes-like chunks. Each chunk is released once consumed. What the engine needs is in Replay settings.
During a replay, on_event and a running Recording hear no event, and neither the call callback nor on_cache_set or on_cache_delete is called. on_event and a running Recording hear events again once the replay ends.
A replay that halts the engine (halt latch) leaves it halted: on_event and a running Recording hear nothing more, and the next atom that calls the engine raises Error with Status.ENGINE_HALTED.
A replay that reaches the end of its recording with every compare clean returns its ReplayResult, for a run that halted too. halted says whether the run halted. Status.ENGINE_HALTED from replay means the replay did not reproduce the run, or met an engine already halted.
Raises |
Condition |
|---|---|
|
a re-derived atom differs from the record, breaks a timetag’s pairing with an earlier atom, or has no recorded twin; a recorded compare atom is not re-derived; a |
|
the stream was written by another DS minor series |
|
the recording’s first event has a |
|
an unknown atom in the inject band |
|
a |
|
the |
|
a |
|
a |
|
a malformed atom; a |
|
a |
|
an allocation fails |
|
|
|
|
|
|
|
|
what |
its |
Status.CONTRACT_BREACH for a FactAssert or a PumpBoundary does not end the replay, nor does Status.ENGINE_HALTED for a halt that the atom’s own assert, retract, delivery or pump raises, nor do Status.NAN_REJECTED and Status.SERIES_NO_BACKING for a PumpBoundary. Atoms applied before a failure stay applied.
(Source: bindings/python/src/pyds/engine.py, bindings/python/src/_pyds/engine.c, runtime/src/libds/rfcs/rfcs.h, runtime/src/libds/rfcs/codec.c, runtime/src/libds/recorder.c, Spec: [NDET.8.2], [NCBK.2.9], [RBND.5.2], [RBND.2.1], [RFCS.7.3], [LDET.1.10], [LTIM.2.8.5], [RFCS.18.8], [RFCS.16.3], [RFCS.19.2], [RFCS.24.2], [RFCS.26.2], [RFCS.5.3], [RFCS.5.4], [RFCS.6.3], [RFCS.6.7], [NCBK.2.11])
ReplayResult#
ReplayResult(asserts, retracts, clocks, rule_fires, calls, withdrawals, skipped, header_verified, tail_bytes)
A frozen dataclass of what replay delivered and checked, counted by atom.
Field |
Holds |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
whether the stream’s header names the artefact this engine loaded |
|
the bytes of a partial last atom from a file or iterable; |
(Source: bindings/python/src/pyds/engine.py, runtime/src/libds/rfcs/codec.c)
decode_fact_body#
def decode_fact_body(self, schema: str, body: Buffer, /) -> dict[str, object]
Reads a FactAssert or FactEmit atom’s fact field against the engine’s schema named schema.
Each value is as Fact.slots holds it.
Raises |
Condition |
|---|---|
|
|
|
the engine has no schema named |
|
the schema has a fact-reference slot; |
|
a malformed |
|
a |
|
an allocation fails |
|
|
(Source: bindings/python/src/pyds/engine.py, bindings/python/src/_pyds/engine.c, runtime/src/libds/rfcs/codec.c, Spec: [LSTR.1.5], [LGRA.5.8.6], [RFCS.6.3])
Atoms#
pyds.atoms#
from pyds import atoms
The module of libds’s atom reader and framing, for a .dsc or a fact stream.
An atom’s field number is an int, or an object with __index__. A number outside 0..2**32-1 raises OverflowError. Malformed input raises InputError with Status.PARSE, naming the byte libds stopped at. The container formats are in Fact stream.
from pyds import atoms
for atom in atoms.iter_atoms(b"", partial=True):
print(atom.kind, atom.offset)
(Source: bindings/python/src/pyds/atoms.py, bindings/python/src/_pyds/atoms.c)
AtomKind#
class AtomKind(enum.IntEnum)
An allocated atom.
A member’s value is the atom’s field number, its name is the body message in constant spelling, and its message property is the body message: AtomKind.FACT_ASSERT is 1, and its message is "FactAssert". field in AtomKind is whether a number is allocated. The atoms are in Fact stream.
(Source: bindings/python/src/pyds/_atom_registry.py)
Atom#
Atom(kind, body, offset, end)
A frozen dataclass of one atom of a container, as libds framed it.
kind is an AtomKind, or the int field number of an atom this build has no entry for. body is bytes. offset is where the atom’s frame starts, and end is where the next atom starts.
message() parses body as its kind’s generated class in pyds.proto, and raises LookupError for a kind this build has no entry for.
(Source: bindings/python/src/pyds/atoms.py)
iter_atoms#
def iter_atoms(data: Buffer, /, *, partial: bool = False) -> Iterator[Atom]
Yields a container’s atoms in order, one per step.
A partial trailing atom raises, unless partial: then the iteration ends before it. A frame no append could repair raises either way.
(Source: bindings/python/src/pyds/atoms.py)
frame#
def frame(kind: AtomKind | int, body: Buffer, /) -> bytes
Returns one framed atom.
A number with no registry entry raises ValueError.
(Source: bindings/python/src/pyds/atoms.py)
band#
def band(field: int, /) -> Band
Returns a field number’s band, RFCS.16.2.
(Source: bindings/python/src/pyds/atoms.py)
Band#
class Band(enum.StrEnum)
What band returns.
Its members and their values are NONE "none", INJECT "inject", REJECT "reject" and SKIP "skip".
(Source: bindings/python/src/pyds/atoms.py)
disposition#
def disposition(field: int, /) -> Disposition
Returns what a reader does with an atom it does not know, RFCS.16.3.
It is the whole answer only when field in AtomKind is false.
(Source: bindings/python/src/pyds/atoms.py)
Disposition#
class Disposition(enum.StrEnum)
What disposition returns.
Its members and their values are SKIP "skip", REJECT_INJECT "reject_inject", REJECT_VERSION "reject_version" and MALFORMED "malformed".
(Source: bindings/python/src/pyds/atoms.py)
container_of#
def container_of(data: Buffer, /) -> Container
Returns a container’s type from its first atom’s number, RFCS.19.4.
(Source: bindings/python/src/pyds/atoms.py)
Container#
class Container(enum.StrEnum)
What container_of returns.
Its members and their values are UNKNOWN "unknown", STREAM "stream" and DSC "dsc".
(Source: bindings/python/src/pyds/atoms.py)
wire_fields#
def wire_fields(body: Buffer, /) -> list[tuple[int, int, int | bytes]]
Returns a body’s top-level fields, each as (field, wire, value).
value is bytes for wire type 2, and otherwise an int.
(Source: bindings/python/src/pyds/_pyds.pyi, bindings/python/src/_pyds/atoms.c)
visit_atom#
def visit_atom(field: SupportsIndex, body: Buffer, /) -> list[tuple[str, str | None, int, int, int, str | None, _VisitValue]]
Returns libds’s typed walk of a body, each visited field as (kind, name, number, index, depth, message, value).
kind is "int", "uint", "double", "bool", "enum", "string", "bytes", "enter" or "leave". value is an int, float, bool, str or bytes as kind names it, a (number, name) tuple for "enum", and None for "enter" and "leave".
(Source: bindings/python/src/pyds/_pyds.pyi, bindings/python/src/_pyds/atoms.c)
message_name#
def message_name(field: SupportsIndex, /) -> str | None
Returns the proto message of the atom field names, or None for a number with no entry.
(Source: bindings/python/src/pyds/atoms.py, bindings/python/src/pyds/_pyds.pyi)