fix: make _nuitka_astropy_patch a no-op outside Nuitka builds (fixes #151) - #152
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.pycallsget_caller_module_dict(2)directly and its_patch_ply_modulewrapper 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 bothwalk(2)andwalk(4)coming back withouttokens.The substituted vendored grammar was also never registered in
sys.modules, so PLY'svalidate_pfunctions→inspect.getsourcefile(None)died withTypeError ... got NoneTypewhile importingastropy.units(Unit("m / (s)")). With a faithful Nuitka simulation (emptyf_localson the PLY walks), the angle parser inastropy.coordinates.angles.formatsgets 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__.pypassescompiled="__compiled__" in globals()(evaluated in the module guaranteed compiled in the binary).levels + 1, restoring the exact frame the unpatched walk would inspect — the fallback only fires when the walk genuinely lacks the grammar (compiled frames withoutf_locals). Verified: with the patch force-installed under CPython, the fallback fires 0 times and parsing works.sys.modulesand 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).astropy.units.format.generic; angle/cds/ogip builds now fail loudly instead of silently parsing with the wrong grammar.Verification
SkyCoord("18h36m56.33634889s +38d47m01.28024248s", frame=ICRS, unit=(u.hour, u.deg))afterpatch_generic_unit_parser()) passes from source.ruffandblackclean.