Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Keep URL-encoded `#` characters of the path or querystring when rewriting document URIs, instead of dropping everything after them (#341)
- Rewrite the `poster` attribute (e.g. of `<video>`) when rewriting HTML, like `src` and `href` (#339)
- Do not collapse consecutive slashes `//` into a single `/` when normalizing URLs into ZIM paths, in both Python and JS rewriting (#340)
- `Creator.finish()` no longer silently discards errors raised by libzim while finalizing the ZIM, so a failure during finalization is no longer reported as a successful run (openzim/warc2zim#462)

## [5.4.1] - 2026-07-31

Expand Down
11 changes: 7 additions & 4 deletions src/zimscraperlib/zim/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def __init__(
self.__indexing_configured = False
self.__indexing_value: bool = False
self.can_finish = True
self._finish_called = False

self.set_mainpath(main_path)

Expand Down Expand Up @@ -475,10 +476,12 @@ def finish(
"""Triggers finalization of ZIM creation and create final ZIM file."""
if not getattr(self, "can_finish", False):
return
try:
super().__exit__(None, None, None)
except RuntimeError:
pass
# finishing an already-finished creator makes libzim raise a RuntimeError;
# make this a no-op instead of calling into libzim again
if getattr(self, "_finish_called", False):
return
self._finish_called = True
super().__exit__(None, None, None)

def __enter__(self):
self.start()
Expand Down
18 changes: 18 additions & 0 deletions tests/zim/test_zim_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import Any, NamedTuple
from unittest.mock import call, patch

import libzim.writer # pyright: ignore[reportMissingModuleSource]
import pytest
from libzim.writer import Compression # pyright: ignore[reportMissingModuleSource]

Expand Down Expand Up @@ -268,6 +269,23 @@ def test_cannot_finish(tmp_path: pathlib.Path):
creator.finish()


def test_finish_error_propagates(tmp_path: pathlib.Path):
fpath = tmp_path / "test.zim"
creator = Creator(fpath, "welcome").config_dev_metadata()
creator.start()
creator.add_item(StaticItem(path="welcome", content="hello"))

with (
patch.object(
libzim.writer.Creator,
"__exit__",
side_effect=RuntimeError("boom"),
),
pytest.raises(RuntimeError, match="boom"),
):
creator.finish()


def test_sourcefile_removal(tmp_path: pathlib.Path, html_file: pathlib.Path):
fpath = tmp_path / "test.zim"
with Creator(fpath, "").config_dev_metadata() as creator:
Expand Down