Skip to content

Fix ASC reader dropping the first frame after header comments - #2137

Merged
zariiii9003 merged 2 commits into
hardbyte:mainfrom
zhaojiale2021:fix/asc-first-data-line
Nov 22, 2026
Merged

zariiii9003 merged 2 commits into
hardbyte:mainfrom
zhaojiale2021:fix/asc-first-data-line

Conversation

@zhaojiale2021

Copy link
Copy Markdown
Contributor

Summary

ASCReader._extract_header() currently consumes the first data line when it follows header metadata or a version comment. The line is not an ASC header field, but it is not rewound for __iter__(), so the first frame is silently lost.

Changes

  • Rewind the text stream to the first non-header data line.
  • Add a regression test for a CANoe-style version comment immediately followed by a frame.

Testing

  • python -m pytest -q test/logformats_test.py -k "first_message_after_header_comment"

Preserve the file position for a first data line encountered while parsing ASC header metadata so __iter__ processes it instead of consuming it. Add a regression test with CANoe-style version comments.
@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review: Request changes

Thanks for tracking this down. The bug is real: when the ASC header has no internal events logged line, _extract_header consumes the first non-header line, and if that line is a frame, the frame is lost. I reproduced it on main with files, StringIO, and .asc.gz via can.LogReader, and the new test catches it.

Blocking: non-seekable streams no longer work

ASCReader accepts any text stream, and reading from a pipe or sys.stdin works on main. With this PR it raises:

io.UnsupportedOperation: underlying stream is not seekable

tell() runs before every header line, so this happens even for normal CANoe files. Those files can't hit the bug at all: per the Vector ASC spec (section 3.1) the header always ends with internal events logged, where the parser already stops. So the common case gains nothing and loses pipe support.

Suggested fix: hand the line back instead of rewinding

TRCReader already uses this pattern. No tell()/seek() is needed:

def _extract_header(self) -> str | None:
    for _line in self.file:
        ...
        if events_match:
            self.internal_events_logged = ...
            return None
        return _line
    return None

def __iter__(self) -> Generator[Message, None, None]:
    first_line = self._extract_header()
    lines = (
        self.file
        if first_line is None
        else itertools.chain([first_line], self.file)
    )
    for _line in lines:
        ...

I tried this locally: all ASC tests pass, including your new one, mypy is clean, and the first frame is read from files, StringIO, gzip, and pipes.

Also needed

  • Changelog fragment: add doc/changelog.d/2137.fixed.rst. Please describe the affected files precisely ("ASC files whose header has no internal events logged line"), so users can tell whether they were affected; files written by CANoe or python-can are not.
  • Behaviour change worth mentioning: in those same files, a Begin Triggerblock line directly after the header is no longer swallowed, so its date now sets the start time for absolute timestamps. That matches how conformant files already behave, so I think it's correct, but the fragment should say so.

@zhaojiale2021

Copy link
Copy Markdown
Contributor Author

Updated the implementation to hand the first non-header line back through itertools.chain instead of rewinding the stream. This avoids seek/tell requirements and keeps pipe, StringIO, file, and gzip inputs compatible.

Also added doc/changelog.d/2137.fixed.rst with the precise affected-file and Begin Triggerblock behavior.

Validation: complete ASC selection passes (32 passed, 1 skipped), and new file/StringIO regressions pass.

@zariiii9003 zariiii9003 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@zariiii9003
zariiii9003 merged commit 890bc87 into hardbyte:main Nov 22, 2026
26 checks passed
zhaojiale2021 added a commit to zhaojiale2021/python-can that referenced this pull request Nov 25, 2026
…te#2137)

* Fix ASC reader dropping first frame after header comments

Preserve the file position for a first data line encountered while parsing ASC header metadata so __iter__ processes it instead of consuming it. Add a regression test with CANoe-style version comments.

* Fix ASC header data line handling

---------

Co-authored-by: Zhao Jiale <zhaojiale2021@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants