Skip to content

Fix #1194: Fix _remove_circular_references nulling shared non-circular config values - #1195

Open
pamod-madubashana wants to merge 1 commit into
PDFMathTranslate:mainfrom
pamod-madubashana:opencode/issue-1194-35361882715
Open

pamod-madubashana wants to merge 1 commit into
PDFMathTranslate:mainfrom
pamod-madubashana:opencode/issue-1194-35361882715

Conversation

@pamod-madubashana

Copy link
Copy Markdown

Fixes #1194

PR Summary — Fix _remove_circular_references nulling shared non-circular config values (Issue #1194)

What changed

pdf2zh/config.py — ConfigManager._remove_circular_references:

  • The seen set is now scoped to the current recursion (ancestor) path instead of one
    never-shrunk global set for the whole traversal. Container ids are added before
    recursing into children and discarded (seen.discard) in a finally block when
    recursion unwinds, so a diamond (shared but non-circular) reference is preserved
    while a true ancestor-path cycle still collapses to None.
  • Tracking now applies only to dict and list containers. Previously every object
    (including immutable scalars like small ints/strings, which share identity via
    interning) was added to seen, so e.g. [1, 1] would have serialized as
    [1, None]. Scalars are now returned verbatim.

Why it addresses the issue

The issue's repro returned {'x': {'a': 1}, 'y': None} for a shared dict referenced
from two sibling keys (and [[1, 2], None] for a shared list), causing silent data
loss on _save_config. After the fix, both cases round-trip fully, while a genuine
self-referential dict (d["self"] = d) still collapses to {"self": None} as
intended, and nested cycles (e.g. a self-referential list inside a dict) still
collapse at the exact cyclic edge.

Verification

  • Reproduction script covering 5 cases (dict diamond, list diamond, true dict cycle,
    repeated immutable scalars, nested list cycle): all assertions passed.
    Command: python3 _repro_1194.py (temporary script, removed after the run).
    Result: ALL REPRO CHECKS PASSED.
  • Syntax check: python3 -m py_compile pdf2zh/config.py — passed with no output.
  • The project's full suite (pytest . per .github/workflows/python-test.yml) could
    not be run here: neither pytest nor uv is installed in this environment, and the
    suite additionally requires system dependencies (libreoffice) and network-cached
    BabelDOC assets. There are no existing tests covering ConfigManager, so no
    narrower test target exists.

@pamod-madubashana
pamod-madubashana marked this pull request as ready for review September 18, 2026 15:33

This branch has not been deployed

No deployments
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.

Fix _remove_circular_references nulling shared non-circular config values

1 participant