Astunparse does not work in Python 3.9, use ast.unparse instead?
This is in relation to the issue https://github.com/serge-sans-paille/gast/issues/48, that transonic fails to build with Python 3.9. While trying to install FluidSim we get:
setup traceback
writing fluidsim.egg-info/PKG-INFO
writing dependency_links to fluidsim.egg-info/dependency_links.txt
writing entry points to fluidsim.egg-info/entry_points.txt
writing requirements to fluidsim.egg-info/requires.txt
writing top-level names to fluidsim.egg-info/top_level.txt
Traceback (most recent call last):
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/site-packages/pip/_vendor/pep517/_in_process.py", line 280, in <module>
main()
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/site-packages/pip/_vendor/pep517/_in_process.py", line 263, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/site-packages/pip/_vendor/pep517/_in_process.py", line 204, in build_wheel
return _build_backend().build_wheel(wheel_directory, config_settings,
File "/tmp/pip-build-env-gvs8yeep/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 216, in build_wheel
return self._build_with_temp_dir(['bdist_wheel'], '.whl',
File "/tmp/pip-build-env-gvs8yeep/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 202, in _build_with_temp_dir
self.run_setup()
File "/tmp/pip-build-env-gvs8yeep/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 145, in run_setup
exec(compile(code, __file__, 'exec'), locals())
File "setup.py", line 172, in <module>
setup(
File "/tmp/pip-build-env-gvs8yeep/overlay/lib/python3.9/site-packages/setuptools/__init__.py", line 153, in setup
return distutils.core.setup(**attrs)
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/distutils/core.py", line 148, in setup
dist.run_commands()
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/distutils/dist.py", line 966, in run_commands
self.run_command(cmd)
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/tmp/pip-build-env-gvs8yeep/overlay/lib/python3.9/site-packages/wheel/bdist_wheel.py", line 335, in run
self.run_command('install')
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/tmp/pip-build-env-gvs8yeep/overlay/lib/python3.9/site-packages/setuptools/command/install.py", line 61, in run
return orig.install.run(self)
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/distutils/command/install.py", line 558, in run
self.run_command(cmd_name)
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/tmp/pip-build-env-gvs8yeep/overlay/lib/python3.9/site-packages/setuptools/command/install_egg_info.py", line 34, in run
self.run_command('egg_info')
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/tmp/pip-build-env-gvs8yeep/overlay/lib/python3.9/site-packages/setuptools/command/egg_info.py", line 299, in run
self.find_sources()
File "/tmp/pip-build-env-gvs8yeep/overlay/lib/python3.9/site-packages/setuptools/command/egg_info.py", line 306, in find_sources
mm.run()
File "/tmp/pip-build-env-gvs8yeep/overlay/lib/python3.9/site-packages/setuptools/command/egg_info.py", line 541, in run
self.add_defaults()
File "/tmp/pip-build-env-gvs8yeep/overlay/lib/python3.9/site-packages/setuptools/command/egg_info.py", line 577, in add_defaults
sdist.add_defaults(self)
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/distutils/command/sdist.py", line 228, in add_defaults
self._add_defaults_ext()
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/distutils/command/sdist.py", line 312, in _add_defaults_ext
self.filelist.extend(build_ext.get_source_files())
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/distutils/command/build_ext.py", line 426, in get_source_files
filenames.extend(ext.sources)
File "/tmp/pip-build-env-gvs8yeep/normal/lib/python3.9/site-packages/pythran/dist.py", line 156, in sources
tc.compile_pythranfile(source, output_file,
File "/tmp/pip-build-env-gvs8yeep/normal/lib/python3.9/site-packages/pythran/toolchain.py", line 472, in compile_pythranfile
output_file = compile_pythrancode(module_name, open(file_path).read(),
File "/tmp/pip-build-env-gvs8yeep/normal/lib/python3.9/site-packages/pythran/toolchain.py", line 394, in compile_pythrancode
module, error_checker = generate_cxx(module_name, pythrancode, specs, opts,
File "/tmp/pip-build-env-gvs8yeep/normal/lib/python3.9/site-packages/pythran/toolchain.py", line 138, in generate_cxx
pm, ir, docstrings = front_middle_end(module_name, code, optimizations,
File "/tmp/pip-build-env-gvs8yeep/normal/lib/python3.9/site-packages/pythran/toolchain.py", line 96, in front_middle_end
ir, docstrings = frontend.parse(pm, code)
File "/tmp/pip-build-env-gvs8yeep/normal/lib/python3.9/site-packages/pythran/frontend.py", line 23, in parse
ir = raw_parse(code)
File "/tmp/pip-build-env-gvs8yeep/normal/lib/python3.9/site-packages/pythran/frontend.py", line 17, in raw_parse
return ast.parse(code)
File "/tmp/pip-build-env-gvs8yeep/overlay/lib/python3.9/site-packages/gast/gast.py", line 300, in parse
return ast_to_gast(_ast.parse(*args, **kwargs))
File "/opt/hostedtoolcache/Python/3.9.2/x64/lib/python3.9/ast.py", line 50, in parse
return compile(source, filename, mode, flags,
File "<unknown>", line 22
inc_var = (var[(:, irx:n1)] - var[(:, 0:n1new)])
This has to do with astunparse
's compatibility with Python 3.9, which is clear when we use ast
directly. The good news is the standard library now has an ast.unparse
method - so why not use that?
In [18]: import ast
...: from ast import unparse, dump
...: # from astunparse import unparse, dump
...:
...: code = "foo[:, b]"
...: tree = ast.parse(code)
...: code_back = unparse(tree).strip()
...:
...: node = tree.body[0]
...: subscript = node.value
...: print(dump.__module__, "dump: (subscript.slice):\n" + dump(subscript.slice))
...:
...: assert code == code_back, (code, code_back)
ast dump: (subscript.slice):
Tuple(elts=[Slice(), Name(id='b', ctx=Load())], ctx=Load())
In [19]: import ast
...: # from ast import unparse, dump
...: from astunparse import unparse, dump
...:
...: code = "foo[:, b]"
...: tree = ast.parse(code)
...: code_back = unparse(tree).strip()
...:
...: node = tree.body[0]
...: subscript = node.value
...: print(dump.__module__, "dump: (subscript.slice):\n" + dump(subscript.slice))
...:
...: assert code == code_back, (code, code_back)
astunparse dump: (subscript.slice):
Tuple(
elts=[
Slice(
lower=None,
upper=None,
step=None),
Name(
id='b',
ctx=Load())],
ctx=Load())
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-19-020b140fd5fe> in <module>
11 print(dump.__module__, "dump: (subscript.slice):\n" + dump(subscript.slice))
12
---> 13 assert code == code_back, (code, code_back)
AssertionError: ('foo[:, b]', 'foo[(:, b)]')