astropy: Importing astropy.coordinates fails with python -OO flag

Description

I am seeing an issue very similar to this: https://github.com/astropy/astropy/issues/10361

Issue 10361 was for astropy.units. I now see the same issue with astropy.coordinates.

Expected behavior

I expect to be able to import astropy.coordinates in a .py file and run the file in python.

How to Reproduce

Steps to Reproduce run python -OO test_script.py, where test_script.py is below:

import astropy.coordinates as apcoord
pass

Versions

Windows-10-10.0.19045-SP0 Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] astropy 5.3 Numpy 1.25.0 pyerfa 2.0.0.3 Scipy 1.11.1 Matplotlib 3.7.2

Problem reproduced on Centos7/ Python3.9

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 17 (16 by maintainers)

Most upvoted comments

I ran a slightly more complex test using the patched Astropy code with the newly patched Nuitka code (not yet in version available form pip, see https://github.com/Nuitka/Nuitka/issues/2313) and got the same error message I got last week. The original instructions I provided to reproduce the issue were too simplified to show the full scope of the problem. Or maybe this is a separate issue.

Slightly more complex test instructions: Create minimalTest.py with the following text:

import sys
print(sys.flags)
import astropy.units
import astropy.coordinates as ascord
print('hello world')

Confirm it works in python. Should output sys.flags optimize=2 and ‘hello world.’ python3 -OO minimalTest.py With the fix https://github.com/astropy/astropy/pull/15037/files, the only change without the -OO flag is that optimize=0.

Compile using Nuitka and run the resulting executable.

python3 -m nuitka --standalone --python-flag=-OO  minimalTest.py
./minimalTest.dist/minimalTest.bin

I observed the same result with and without “–python-flag=-OO,” with the exception of the optimize flag.

Output of minimalTest.bin:

sys.flags(debug=0, inspect=0, interactive=0, optimize=1, dont_write_bytecode=0, no_user_site=1, no_site=1, ignore_environment=0, verbose=0, bytes_warning=0, quiet=0, hash_randomization=1, isolated=0, dev_mode=False, utf8_mode=1)
ERROR: No token list is defined
Traceback (most recent call last):
  File "/Testing/minimalTest.dist/astropy/units/format/generic.py", line 587, in _do_parse
  File "/Testing/minimalTest.dist/astropy/units/format/generic.py", line 483, in _parse_unit
ValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Testing/minimalTest.dist/astropy/units/core.py", line 2098, in __call__
  File "/Testing/minimalTest.dist/astropy/units/format/generic.py", line 571, in parse
  File "/Testing/minimalTest.dist/astropy/units/format/generic.py", line 590, in _do_parse
  File "/Testing/minimalTest.dist/astropy/utils/decorators.py", line 747, in __get__
  File "/Testing/minimalTest.dist/astropy/units/format/generic.py", line 72, in _parser
  File "/Testing/minimalTest.dist/astropy/units/format/generic.py", line 448, in _make_parser
  File "/Testing/minimalTest.dist/astropy/utils/parsing.py", line 148, in yacc
  File "/Testing/minimalTest.dist/astropy/extern/ply/yacc.py", line 3282, in yacc
astropy.extern.ply.yacc.YaccError: Unable to build parser

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Testing/minimalTest.dist/minimalTest.py", line 3, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "/Testing/minimalTest.dist/astropy/units/__init__.py", line 25, in <module astropy.units>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "/Testing/minimalTest.dist/astropy/units/astrophys.py", line 66, in <module astropy.units.astrophys>
  File "/Testing/minimalTest.dist/astropy/constants/constant.py", line 42, in wrapper
  File "/Testing/minimalTest.dist/astropy/units/quantity.py", line 1011, in unit
  File "/Testing/minimalTest.dist/astropy/utils/decorators.py", line 837, in __get__
  File "/Testing/minimalTest.dist/astropy/constants/constant.py", line 208, in _unit
  File "/Testing/minimalTest.dist/astropy/units/core.py", line 2121, in __call__
ValueError: 'm / (s)' did not parse as unit: Unable to build parser If this is meant to be a custom unit, define it with 'u.def_unit'. To have it recognized inside a file reader or other code, enable it with 'u.add_enabled_units'. For details, see https://docs.astropy.org/en/latest/units/combining_and_defining.html

This error message looks very similar to https://github.com/astropy/astropy/issues/10361, but I think today’s fix should have resolved the related issue.

At this point, I don’t know whether the problem is in Astropy or Nuitka. Does Astropy require optimize flag=2 when python-flag = -OO? Currently Nuitka sets optimize=1 for flag -OO. (See https://github.com/Nuitka/Nuitka/issues/2313)

Note that we discussed upgrading PLY before, but apparently this is not trivial because they changed their implementation quite drastically: #13399. Given more recent changes in PLY, it may be less work than before to adapt our usage to it (but that’s better for discussion in #13399 itself).

Let me see if this works before committing to an upgrade. I don’t have high hopes that this will fix my problem, but I’m out of ideas of things to try that I know how to implement.

Yes there is: sys.flags.optimize.