Purpose
-------
Apply one mutant to a real file and guarantee the pristine bytes come back.
Responsibilities
----------------
- Refuse a target that git cannot restore before anything is written.
- Record the pristine bytes, write the mutant, and restore unconditionally.
- Preserve the target's declared source encoding when writing a mutant.
Diagnostics
-----------
Domain: OCT-MUTATION
Levels:
L2 - lifecycle
L3 - apply and restore details
L4 - deep tracing
Contracts
---------
- Restoration runs in a ``finally``, so it also executes on ``KeyboardInterrupt``
and on any exception raised inside the managed block.
- Restoration writes the recorded bytes back unconditionally, so it repairs a
target that the managed block replaced rather than merely mutated.
- A refusal is raised before the first write, never after a partial one.
- This module makes no claim about processes it did not start: a child that
outlives the managed block can still observe or rewrite the target, which is
a named residual rather than a closed gap.
Dependencies
------------
- oct.core.git (repository discovery and working-tree state, read-only)
- oct.mutation.engine (mutant application)
- tokenize (declared source encodings)
| Iterator[str] oct.mutation.sandbox.mutated_target |
( |
str | Path | path, |
|
|
str | mutant_id, |
|
|
str | relpath ) |
Write one mutant to ``path``, yield its text, and always restore.
The pristine bytes are captured before anything else happens and written
back in a ``finally``, so restoration survives an exception in the body, a
``KeyboardInterrupt``, and a body that replaced the file outright. The
restore is a plain unconditional write rather than a conditional repair:
comparing first would leave a window where a crash between the comparison
and the write loses the original.
Definition at line 145 of file sandbox.py.