Skip to content

Form: fix crash and surface clear errors for malformed XML (bug 11707) - #6

Merged
eduralph merged 1 commit into
maintenance/gramps60from
fix/form-gramps-id-11707
Apr 17, 2026
Merged

Form: fix crash and surface clear errors for malformed XML (bug 11707)#6
eduralph merged 1 commit into
maintenance/gramps60from
fix/form-gramps-id-11707

Conversation

@eduralph

Copy link
Copy Markdown
Owner

Summary

  • Fixes the ValueError: not enough values to unpack (expected 2, got 1) crash in the Forms addon's FamilySection when a <section type='family'> title omits the X/Y separator (gramps-project/gramps#11707).
  • Adds a pure-Python form_validator module so the loader can reject malformed XML up front, with user-facing ErrorDialogs naming the file, form, and rule that failed.
  • Partially addresses #11010 (request for a user error dialog for unsupported elements) by covering its core ask: clear errors for invalid section types, missing/empty role, missing/empty type, and XML syntax errors.

What changed

Loader (Form/form.py)

  • Pre-validates each definition file; syntax errors, missing required attributes, and invalid section types all surface as ErrorDialogs with the file path and a precise message.
  • Partially-broken files still load their well-formed <form> entries — one bad section no longer nukes the whole file.
  • Added diagnostic logging: INFO count of forms loaded per file, DEBUG per file/form trace.

Validator (Form/form_validator.py, new)

  • split_family_title() — belt-and-braces helper so FamilySection never raises on a malformed title.
  • validate_form_element() / validate_form_dom() — returns a list of human-readable errors; free of GTK/Gramps imports so it runs in any CI job.
  • parse_and_validate() — file-level entry point that catches both parse and schema errors cleanly.

Editor (Form/editform.py)

  • Uses split_family_title() instead of the raw title.split('/') that blew up in #11707.
  • Added a module logger with DEBUG on EditForm open and a WARNING in FamilySection if the title lacks the separator (so future regressions are visible in logs, not just as a traceback).

Translations (Form/po/template.pot) — three new translatable dialog titles.

Tests

All green locally (38 passed, 7 subtests):

  • Form/tests/test_form_validator.py — 32 pure-Python unit tests: every branch of split_family_title, every validation rule, parse_and_validate file handling, plus a sanity test that every shipped form_*.xml passes validation.
  • Form/tests/test_integration_form.py — 6 integration tests that patch ErrorDialog and assert: syntax errors, invalid family titles (the exact #11707 repro), missing role, and invalid section types all raise a clear dialog; partially-broken files still load their valid forms; shipped files trigger zero dialogs.

Test plan

  • python3 -m pytest Form/tests/ -v — 38 passed
  • python3 -m pytest Form/tests/test_form_validator.py runs pure Python, no Gramps required
  • ruff check Form/ — clean
  • python3 -m py_compile Form/*.py — clean
  • Manual smoke test in local Gramps 6.0 installation:
    • Shipped forms load with zero dialogs.
    • Dropping a custom.xml with an invalid <section type='Person'> shows the new ErrorDialog naming the file, form, and rule.
    • Dropping a custom.xml with the #11707 family-title repro shows a clear dialog instead of the ValueError traceback.
    • Opening a valid form from an existing source round-trips through the editor without regression.

Fixes gramps-project/gramps#11707
Refs gramps-project/gramps#11010

🤖 Generated with Claude Code

A family section whose title lacked the expected 'X/Y' separator caused
a ValueError: not enough values to unpack when the form editor opened,
crashing the Forms gramplet. The underlying issue was that the addon
trusted the XML definitions and had no schema validation or user-facing
error reporting for broken files.

Split the validation out of form.py into a pure-Python form_validator
module (no GTK/Gramps imports) so it can be unit-tested without a GUI.
The Form loader now:

* parses each file defensively (ExpatError -> ErrorDialog),
* runs the validator before loading (invalid files -> ErrorDialog with
  the file path, offending form id, and the rule that failed),
* skips any <form> element that fails validation while still loading
  sibling well-formed forms from the same file.

split_family_title() in form_validator belt-and-braces the FamilySection
constructor so a missing separator no longer raises, even if validation
is bypassed.

Also adds diagnostic logging:

* INFO log of forms loaded per file,
* DEBUG trace of each file parsed and each form id loaded/skipped,
* DEBUG when EditForm opens (event/citation handles),
* WARNING in FamilySection if its title lacks 'X/Y'.

Tests:

* Form/tests/test_form_validator.py -- 32 pure-Python unit tests,
  covers split_family_title, every validation branch, parse_and_validate
  file handling, and a sanity check that every shipped form_*.xml passes
  validation.
* Form/tests/test_integration_form.py -- 6 integration tests that patch
  ErrorDialog to verify the loader surfaces syntax errors, invalid
  family titles, missing role, and invalid section types; partially
  broken files still load their valid forms; shipped files trigger no
  dialogs.

Partially addresses bug 0011010 (request for user error dialog for
unsupported elements) by covering its core ask: clear errors for
invalid section types, missing/empty role, missing/empty type, and XML
syntax errors.

Fixes gramps-project/gramps#11707
Refs gramps-project/gramps#11010

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@eduralph
eduralph merged commit 8480567 into maintenance/gramps60 Apr 17, 2026
7 checks passed
@eduralph
eduralph deleted the fix/form-gramps-id-11707 branch May 26, 2026 23:34
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.

1 participant