Skip to content

Fix pyphen hook: use original NVDA approach#244

Merged
albertosottile merged 2 commits into
py2exe:masterfrom
LeonarddeR:pyphenTest
Jun 4, 2026
Merged

Fix pyphen hook: use original NVDA approach#244
albertosottile merged 2 commits into
py2exe:masterfrom
LeonarddeR:pyphenTest

Conversation

@LeonarddeR

@LeonarddeR LeonarddeR commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

The original hook added in #242 patched pyphen.dictionaries after importing pyphen via bootcode, but pyphen.__init__ populates LANGUAGES at module level on import — so the patch always ran too late.

Root cause

importlib.resources.files('pyphen.dictionaries') requires a module in sys.modules with a loader implementing get_resource_reader().files(). Inside a frozen binary, pyphen.dictionaries is never imported so no such module exists.

Fix

Bootcode injects a minimal fake pyphen.dictionaries module into sys.modules before pyphen is imported. Its loader's get_resource_reader() returns an object whose files() yields Path(pyphenDictionaries/) next to the executable — exactly what resources.files() expects. pyphen.LANGUAGES then populates correctly at import time.

Adds tests/functional/pyphen_test/ to verify end-to-end in a frozen binary.

The original bootcode patched pyphen.dictionaries after importing pyphen,
but pyphen.__init__ populates LANGUAGES at module level during import,
so the patch was always too late.

Fix: inject a fake pyphen.dictionaries module into sys.modules before
pyphen is imported. Uses a _DicLoader whose .path tricks FileReader into
resolving importlib.resources.files('pyphen.dictionaries') to the
pyphenDictionaries/ directory next to the frozen executable.

Add tests/functional/pyphen_test/ to verify the hook works end-to-end
in a frozen binary.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@albertosottile

Copy link
Copy Markdown
Member

I appreciate the test. However, the hook now seems fairly convoluted. I wonder if patching the pyphen code before freezing it, similarly to what we do in the hook for matplotlib would now be a better approach.

@albertosottile albertosottile added the enhancement New feature or request label Jun 2, 2026
@LeonarddeR

Copy link
Copy Markdown
Contributor Author

Replaced bootcode + fake module injection with AST transformation, matching the hook_matplotlib pattern. This approach is also used in NVDA's py2exe setup.

@LeonarddeR LeonarddeR changed the title Fix pyphen hook: inject fake pyphen.dictionaries into sys.modules Fix pyphen hook: use original NVDA approach Jun 4, 2026
@albertosottile albertosottile merged commit ad075ac into py2exe:master Jun 4, 2026
22 checks passed
@albertosottile

Copy link
Copy Markdown
Member

Thanks for this, I will try to prepare a patch release over the weekend.

@albertosottile

Copy link
Copy Markdown
Member

@LeonarddeR I just released this fix in version 0.14.1.1

@LeonarddeR

Copy link
Copy Markdown
Contributor Author

Thank you very much!

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants