Skip to content

CSVReader crashes when reading an empty file #771

Closed

Description

@ckrenslehner

The next statement in the iter function of the class CSVReader is not inside the for loop. There for a Stopiteration error is not catched.

Rewriting the code like this fixes this:

def __iter__(self):
        for (index, line) in enumerate(self.file):
            if index > 0:
                timestamp, arbitration_id, extended, remote, error, dlc, data = line.split(',')

                yield Message(
                    timestamp=float(timestamp),
                    is_remote_frame=(remote == '1'),
                    is_extended_id=(extended == '1'),
                    is_error_frame=(error == '1'),
                    arbitration_id=int(arbitration_id, base=16),
                    dlc=int(dlc),
                    data=b64decode(data),
                )

        self.stop()

Activity

  1. bessman commented on Feb 21, 2020

    @bessman
    Contributor

    This is fixed in develop:

    python-can/can/io/csv.py

    Lines 91 to 96 in b873299

    # skip the header line
    try:
    next(self.file)
    except StopIteration:
    # don't crash on a file with only a header
    return

  2. karlding commented on May 22, 2020

    @karlding
    Collaborator

    Closing since the fix is released as part of v3.3.3.

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

    bugfile-ioabout reading & writing to files

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions