Fix X-Stitch-Count off-by-one (issue #10) #18
No reviewers
Labels
No labels
blocked:needs-attention
blocked:needs-decision
bug
feature-request
in-progress
needs-decompose
quickfix
triage-approved
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Mark/easystitch-service!18
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "fix/10-stitch-count-off-by-one"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Fixes the off-by-one between
X-Stitch-Countand the on-disk stitch record count reported in Mark/easystitch-service#10.The header was previously computed from
sum(len(obj.stitches) for obj in req.stitch_objects)(pre-fixapp.py:242), which only counted the stitch points in the request payload. pyembroidery's writer unconditionally appends a trailingENDrecord on disk (and PES injects additionalJUMP/TRIMmarkers around color changes viaFULL_JUMP = True), so the on-disk record count is strictly greater than the request-payload point count.Fix
X-Stitch-Countis now derived from a round-trip read of the encoded bytes: afterwrite_<fmt>(pattern, buf)returns, the buffer is re-decoded through the matching reader and the header is set tolen(roundtrip.stitches). This makes the header authoritative — by construction it equals whatpyembroidery.read(...)would return from the file bytes — without changing the on-disk file format.Reproduction (before fix)
For the same digitize response,
X-Stitch-Count: 139916vslen(pyembroidery.read(file).stitches) == 139917(off by 1; the trailingENDrecord). See parent cardt_6acb4d16investigation report for the full PES/DST/EXP/JEF single-color and multi-color breakdown.Diff
Tests
test_export_header_matches_pes_byte_countasserts the header equals the round-tripped file's stitch count for PES. Fails onmain(off-by-one), passes after the fix.test_export_round_trips_each_formatandtest_export_multi_color_inserts_color_changesnow assert againstlen(pyembroidery.read(path).stitches)instead of the request-payload sum.ruff check app.py tests/unit/test_export.py: clean.Closes Mark/easystitch-service#10
Pull request closed