Skip to content

fix: write StarDict metadata as bytes on Python 3 - #149

Merged
skywind3000 merged 1 commit into
skywind3000:masterfrom
JesstLe:fix/stardict-python3-export-20261102
Nov 2, 2026
Merged

skywind3000 merged 1 commit into
skywind3000:masterfrom
JesstLe:fix/stardict-python3-export-20261102

Conversation

@JesstLe

@JesstLe JesstLe commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Fixes #91.

export_stardict() opens its .ifo file in binary mode but writes text strings to it. On Python 3 even a single ASCII entry raises TypeError: a bytes-like object is required, not 'str', leaving an incomplete export.

Change

Write the metadata with byte-string templates, preserving the existing binary file mode and LF output. Keep the book title encoded as UTF-8 and encode the generated date as ASCII. The word ordering and .idx/.dict paths are unchanged.

Add two standard-library regression tests covering an empty dictionary and a multilingual export. They check the UTF-8 title, case-sensitive sorting tie-break, binary offsets/lengths, actual index size, and metadata contents. No test dependency or dictionary download is needed.

Verification

python -B -m unittest discover -s tests -p test_stardict_export.py -v
  • Before the fix: FAILED (errors=2), both at the first .ifo.write().
  • After the fix: Ran 2 tests / OK on Python 3.12.14, Python 3.14.7, and Linux Python 2.7.18 (python:2.7-slim).
  • Reverting only the production file in an independent copy restores the two original errors; the regression tests remain present.
  • git diff --check passes.

The reproducer and tests above were executed locally.

@skywind3000
skywind3000 merged commit 39cfb96 into skywind3000:master Nov 2, 2026
@skywind3000

Copy link
Copy Markdown
Owner

thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

export_stardict 方法写文件错误

2 participants