diff --git a/CHANGELOG.md b/CHANGELOG.md index e606435e..4833034a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `scp_alias_hosts` fuzzy rule canonicalising the SCP wiki's alias hosts (`scpwiki.com`, `www.scpwiki.com`, `www.scp-wiki.net`) onto `scp-wiki.wikidot.com` ; all of them serve the same wiki with HTTP 200 rather than a redirect, so without it the same article lands at several ZIM paths and cross-host links resolve to nothing (#423, openzim/zim-requests#402) +### Fixed + +- `Metadata` (and `CustomMetadata`/`XCustomMetadata`) now rejects an empty `io.BytesIO` or file-like value when empty is not allowed, like it already did for empty `bytes` + ## [5.6.0] - 2026-11-20 ### Added diff --git a/src/zimscraperlib/zim/metadata.py b/src/zimscraperlib/zim/metadata.py index 79e13907..d88cbdd3 100644 --- a/src/zimscraperlib/zim/metadata.py +++ b/src/zimscraperlib/zim/metadata.py @@ -240,7 +240,7 @@ def get_binary_from( bvalue = value.read() if isinstance(value, SupportsSeekableRead) and value.seekable(): value.seek(last_pos) - if not self.empty_allowed and not value: + if not self.empty_allowed and not bvalue: raise ValueError("Missing value (empty not allowed)") return bvalue diff --git a/tests/zim/test_metadata.py b/tests/zim/test_metadata.py index a361b4af..20301141 100644 --- a/tests/zim/test_metadata.py +++ b/tests/zim/test_metadata.py @@ -232,6 +232,21 @@ def test_blank_metadata(): metadata.Metadata(name="Blank", value=b"") +def test_blank_metadata_from_bytesio(): + with pytest.raises(ValueError, match=r"Missing value \(empty not allowed\)"): + metadata.Metadata(name="Blank", value=io.BytesIO(b"")) + + +def test_blank_metadata_from_file(tmp_path: pathlib.Path): + fpath = tmp_path / "empty" + fpath.write_bytes(b"") + with ( + open(fpath, "rb") as fh, + pytest.raises(ValueError, match=r"Missing value \(empty not allowed\)"), + ): + metadata.Metadata(name="Blank", value=fh) + + class MetadataInitConfig(NamedTuple): a_type: type nb_args: int