Skip to content

bytes-and-bytearray figure renders doubled backslashes #18

Description

@adewale

What

The `bytes-vs-bytearray` figure on https://www.pythonbyexample.dev/examples/bytes-and-bytearray renders its byte literals with doubled backslashes:

```
b'\x63\x61\x66'
bytearray(b'\x63\x61')
```

The example's own code and output show single backslashes, so the figure contradicts the cell it sits under.

Why

`bytes_vs_bytearray` in `src/marginalia.py` writes the labels as Python string literals with four backslashes (`"b'\\\\x63…'"`). Each pair collapses to one literal backslash, so the rendered text carries two. The grammar then XML-escapes the text correctly, so the doubled characters reach the browser as-is.

Why it shipped

The figure contracts in `tests/test_marginalia_geometry.py` check geometry (clipping, collision, palette, stroke, accent count) but never check label content against the example. Nothing compares a figure's mono text with the cell's code or output.

Fix

  • Use two backslashes per escape in the paint strings (or a raw string).
  • Add a content contract: no rendered figure text may contain a doubled backslash, and mono labels that contain an escape sequence must appear verbatim in the example's code or output.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions