Skip to content

Fails on empty files #246

@danvk

Description

@danvk

webdiff/webdiff/diff.py

Lines 41 to 45 in d8606b1

def fast_num_lines(path: str) -> int:
# See https://stackoverflow.com/q/9629179/388951 for the idea to use a Unix command.
# Unfortunately `wc -l` ignores the last line if there is no trailing newline. So
# instead, see https://stackoverflow.com/a/38870057/388951
return int(subprocess.check_output(['grep', '-c', '', path]))

grep -c returns an error code on empty files, which subprocess.check_output turns into an exception.

$ touch blank.txt
$ grep -c '' blank.txt
0
$ echo $?
1

The grep manpage say an exit code of 1 means "no match" while an exit code of 2+ means there was an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions