Skip to content

fix: make _nuitka_astropy_patch a no-op outside Nuitka builds (fixes #151) - #152

Merged
thusser merged 1 commit into
developfrom
fix/nuitka-astropy-patch-breaks-source
Aug 28, 2026
Merged

fix: make _nuitka_astropy_patch a no-op outside Nuitka builds (fixes #151)#152
thusser merged 1 commit into
developfrom
fix/nuitka-astropy-patch-breaks-source

Conversation

@thusser

@thusser thusser commented Aug 28, 2026

Copy link
Copy Markdown
Member

Closes #151 — the Nuitka workaround patch was breaking astropy's unit/coordinate parsing when running from source.

Root cause

patch_generic_unit_parser() claimed to be a no-op under a regular interpreter, but the wrapper functions it installs (patched_yacc_dict / patched_lex_dict) add one stack frame, which shifts astropy 7.2.2's frame-level bookkeeping: astropy/utils/parsing.py calls get_caller_module_dict(2) directly and its _patch_ply_module wrapper adds two more levels. The extra frame makes the walk land on astropy's parsing wrapper instead of the grammar-defining frame, so the fallback fired on plain CPython — instrumented runs showed both walk(2) and walk(4) coming back without tokens.

The substituted vendored grammar was also never registered in sys.modules, so PLY's validate_pfunctionsinspect.getsourcefile(None) died with TypeError ... got NoneType while importing astropy.units (Unit("m / (s)")). With a faithful Nuitka simulation (empty f_locals on the PLY walks), the angle parser in astropy.coordinates.angles.formats gets the generic-unit grammar substituted and rejects every coordinate — the reporter's exact "Cannot parse first argument data" / "Invalid coordinates" symptom.

Changes

  • patch_generic_unit_parser() is now a strict no-op outside Nuitka builds. It returns immediately unless running compiled; pyobs_gui/__init__.py passes compiled="__compiled__" in globals() (evaluated in the module guaranteed compiled in the binary).
  • Frame compensation: the patched walks now call the originals with levels + 1, restoring the exact frame the unpatched walk would inspect — the fallback only fires when the walk genuinely lacks the grammar (compiled frames without f_locals). Verified: with the patch force-installed under CPython, the fallback fires 0 times and parsing works.
  • Registered grammar module: the vendored namespace is registered in sys.modules and its __file__ stub is materialized, so a real Nuitka grammar rebuild no longer dies in PLY validation (tables get written to the existing scratch dir).
  • Scoped fallback: the generic-unit grammar is only substituted for astropy.units.format.generic; angle/cds/ogip builds now fail loudly instead of silently parsing with the wrong grammar.

Verification

  • Issue _nuitka_astropy_patch breaks coordinate parsing when running from source #151 repro (SkyCoord("18h36m56.33634889s +38d47m01.28024248s", frame=ICRS, unit=(u.hour, u.deg)) after patch_generic_unit_parser()) passes from source.
  • Forced-compiled install under CPython: parses fine, 0 fallback firings.
  • Simulated Nuitka: generic-unit parsing works, grammar tables rebuilt into the scratch dir; angle parsing fails cleanly (angle grammar is a documented, still-open limitation for the binary).
  • Existing suite: 43/43 tests pass; ruff and black clean.

…151)

The wrapper installed by patch_generic_unit_parser() adds one stack frame,
shifting astropy's frame-level bookkeeping: astropy.utils.parsing calls
get_caller_module_dict(2) directly and its _patch_ply_module wrapper adds two
more levels, so the extra frame made the walk land on astropy's parsing wrapper
instead of the grammar-defining frame. The fallback then fired on plain CPython
and corrupted unit/coordinate parsing when running from source.

- Only install the patch in the Nuitka-compiled standalone binary
  ('__compiled__' in globals()), evaluated in pyobs_gui/__init__.py which is
  guaranteed compiled; otherwise it is a strict no-op.
- Step one level deeper (levels + 1) in the patched walks to restore the
  original frame targeting, so the fallback only fires when the walk genuinely
  lacks the grammar (compiled frames without f_locals).
- Register the vendored grammar namespace in sys.modules and materialize its
  __file__ stub so PLY's inspect.getmodule()/getsourcefile() validation works
  when the grammar has to be rebuilt.
- Scope the fallback to astropy.units.format.generic only: angle/cds/ogip
  grammars are not vendored, so feed the broken-locals pdict back unchanged
  instead of silently parsing with the wrong grammar.
@thusser
thusser merged commit a068087 into develop Aug 28, 2026
3 checks passed
@thusser
thusser deleted the fix/nuitka-astropy-patch-breaks-source branch August 28, 2026 08:15
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