Skip to content

Improve and extend the _read() method availability for its users and related methods. Part 2: Response format validator#711

Open
belono wants to merge 8 commits into
python-escpos:masterfrom
belono:status_validator
Open

Improve and extend the _read() method availability for its users and related methods. Part 2: Response format validator#711
belono wants to merge 8 commits into
python-escpos:masterfrom
belono:status_validator

Conversation

@belono
Copy link
Copy Markdown
Contributor

@belono belono commented Oct 4, 2025

Description

This is part 2 of the series to improve and extend the availability of the _read() method.

Part 1: #709

Part 2: Add a simple response format validator when querying for status:

As stated at the Epson ESC/POS reference guide:

Each status consists of 1 byte, and the value is 0xx1xx10b.
The real time status can be differentiated by the bits 0, 1, 4, and 7 from other transmission data, except for data in block data (Header – NUL).

So a status response consists of a byte which bits 0 and 7 are fixed to 0, and bits 1 and 4 are fixed to 1. The remaining bits contain the response to the query according to the tables given in the aforementioned reference guide.

This PR tries to reduce the possibility of false positives when interpreting data from the printer buffer as status responses by checking that the data read is at least in the right response format:

  • Add a simple validator method to validate every status format before return it as a response.
  • Log an error if validation fails.
  • Optionally raise a (new) ValidationError exception if validation fails, or return an empty response (default).
  • Treat an empty response as a negative response for the query_status() users: is_online() and paper_status() methods.
  • Add tests for the status read functions.

Part 3: #712

Part 4: #714

Tested with

TM-U210 Parallel port printer

@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 4, 2025

Codecov Report

❌ Patch coverage is 95.65217% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 82.20%. Comparing base (11d46fd) to head (e37e39e).

Files with missing lines Patch % Lines
src/escpos/exceptions.py 85.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #711      +/-   ##
==========================================
+ Coverage   81.39%   82.20%   +0.81%     
==========================================
  Files          21       21              
  Lines        1693     1714      +21     
  Branches      267      270       +3     
==========================================
+ Hits         1378     1409      +31     
+ Misses        235      221      -14     
- Partials       80       84       +4     
Flag Coverage Δ
unittests 82.14% <95.65%> (+0.93%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/escpos/escpos.py 78.59% <100.00%> (+3.19%) ⬆️
src/escpos/exceptions.py 67.21% <85.71%> (+1.12%) ⬆️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@belono belono force-pushed the status_validator branch from 3742883 to 0fa5b53 Compare April 21, 2026 21:48
@belono belono force-pushed the status_validator branch from 151bbac to e37e39e Compare April 21, 2026 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant