diff --git a/src/commoncode/hash.py b/src/commoncode/hash.py index 5e71fde..b97dd89 100644 --- a/src/commoncode/hash.py +++ b/src/commoncode/hash.py @@ -217,6 +217,8 @@ def checksum(location, name, base64=False): """ Return a checksum from the content of the file at ``location`` using the ``name`` checksum algorithm. The checksum is a string as a hexdigest or is base64-encoded is ``base64`` is True. + + Return None if ``location`` is not a file or an empty file. """ if not filetype.is_file(location): return @@ -290,6 +292,9 @@ def multi_checksums(location, checksum_names=("md5", "sha1", "sha256", "sha512", if not filetype.is_file(location): return {name: None for name in checksum_names} file_size = get_file_size(location) + if file_size == 0: + return {name: None for name in checksum_names} + hashers = { name: get_hasher_instance_by_name(name=name, total_length=file_size) for name in checksum_names diff --git a/tests/data/hash/empty b/tests/data/hash/empty new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_hash.py b/tests/test_hash.py index 52e36bf..b1ab88e 100644 --- a/tests/test_hash.py +++ b/tests/test_hash.py @@ -194,3 +194,8 @@ def test_checksum_from_chunks_from_stream_is_same_as_plain(self): for _ in range(100): result2.update(chunk) assert result1 == result2.hexdigest() + + def test_checksum_empty_file(self): + test_file = self.get_test_loc("hash/empty") + checksums = multi_checksums(location=test_file, checksum_names=("sha1",)) + assert checksums == {"sha1": None}