Skip to content

Metadata accepts empty BytesIO/file values although empty is not allowed #422

Backlog

Description

@playlikeboy

Metadata.get_binary_from() (src/zimscraperlib/zim/metadata.py) checks the input object instead of the bytes it read:

if not self.empty_allowed and not value:   # value is the BytesIO / file object
    raise ValueError("Missing value (empty not allowed)")
return bvalue

io.BytesIO and file objects are always truthy, so an empty stream passes the check and an empty metadata entry ends up in the ZIM, while the same empty value given as bytes is rejected:

>>> import io
>>> from zimscraperlib.zim.metadata import CustomMetadata
>>> CustomMetadata("Foo", b"")
ValueError: Missing value (empty not allowed)
>>> CustomMetadata("Foo", io.BytesIO(b"")).value
b''

An empty file opened with "rb" is accepted the same way.

Expected: empty streams are rejected like empty bytes. Checking bvalue instead of value fixes it; I have that change with two regression tests (BytesIO and file) ready and can open a PR if that works for you.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions