Pants bundling with wrong version of Python?

350 views
Skip to first unread message

Grant Wu

unread,
Jun 9, 2015, 7:43:15 PM6/9/15
to pants...@googlegroups.com
Hello!


Grant here. Member of a team of avid Pants-wearers. Running into an issue for which I've failed to find a solution.

I have Python code that uses 'raise...from' syntax as outlined here. And here is a Gist containing some test code I'm trying to run.

A file named "test.py" containing only the contents of the above Gist fails to run / bundle with Pants. Error:

"Exception message: invalid syntax (test.py, line 4)"

The same test.py runs successfully when invoked with Python3.4.3, and gives the expected syntax error with Python2.

Here is my pants.ini. I'm specifying "interpreter_requirement: CPython>=3.4,<3.5".


I'm specifying a version of Python that should support 'raise...from', so I can't figure out why Pants is giving me a syntax error. I'm wondering if there's something else I need to do to tell Pants which version of Python I'm using. 

Am I missing something? Thanks so much in advance for your help.



Best,
Grant Wu

John Sirois

unread,
Jun 9, 2015, 8:11:22 PM6/9/15
to Grant Wu, pants-devel
Nope - this is a bug.

I did a minimal repo like so:

1.) Setup a pants python repo:
===
jsirois@gill ~ $ mkdir /tmp/test
jsirois@gill ~ $ cd /tmp/test
jsirois@gill /tmp/test $ virtualenv -p python2 build-support/pants.venv
...
jsirois@gill /tmp/test $ source build-support/pants.venv/bin/activate
(pants.venv)jsirois@gill /tmp/test $ pip install pantsbuild.pants
...
(pants.venv)jsirois@gill /tmp/test $ cat << EOF > pants.ini
> [python-setup]
> interpreter_requirement: CPython>=3.4,<3.5
> EOF

2.) Setup your case:
===
(pants.venv)jsirois@gill /tmp/test $ cat << EOF > main.py
> try:
>     print(1 / 0)
> except Exception as exc:
>     raise RuntimeError("Something bad happened") from exc
> EOF
(pants.venv)jsirois@gill /tmp/test $ cat << EOF > BUILD
> python_binary(
>   name='main',
>   source='main.py'
> )
> EOF

3.) Repro the run ... not quite so fast
===
(pants.venv)jsirois@gill /tmp/test $ pants run :main
...
17:50:25 00:00   [resolve]
17:50:25 00:00     [ivy]
                   Failed to resolve target for tool: //:nailgun-server.
                   This target was obtained from option nailgun-server in scope resolve.ivy.
                   You probably need to add this target to your tools BUILD file(s), usually located in the workspace root.
                   
FAILURE
...


4.) Add the nailgun tool dep:
===
(pants.venv)^jsirois@gill /tmp/test $ cat << EOF > BUILD.tools
> jar_library(
>   name='nailgun-server',
>   jars=[
>     jar('com.martiansoftware', 'nailgun-server', '0.9.1')
>   ]
> )
> EOF

5.) Really do the repro:
===
(pants.venv)jsirois@gill /tmp/test $ pants binary :main --print-exception-stacktrace
...
17:52:22 00:00   [binary]
17:52:22 00:00     [python-binary-create]
                   Detected interpreter /home/jsirois/.pyenv/versions/3.4.2/bin/python3.4: CPython-3.4.2
                   Detected interpreter /usr/bin/python3.4: CPython-3.4.3fatal: Not a git repository (or any parent up to mount point /tmp)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

17:52:23 00:01       [chroot]
               FAILURE

Exception caught:
  File "/tmp/test/build-support/pants.venv/bin/pants", line 9, in <module>
    load_entry_point('pantsbuild.pants==0.0.32', 'console_scripts', 'pants')()
  File "/tmp/test/build-support/pants.venv/lib/python2.7/site-packages/pants/bin/pants_exe.py", line 81, in main
    _run(exiter)
  File "/tmp/test/build-support/pants.venv/lib/python2.7/site-packages/pants/bin/pants_exe.py", line 74, in _run
    result = goal_runner.run()
  File "/tmp/test/build-support/pants.venv/lib/python2.7/site-packages/pants/bin/goal_runner.py", line 174, in run
    result = self._do_run()
  File "/tmp/test/build-support/pants.venv/lib/python2.7/site-packages/pants/bin/goal_runner.py", line 230, in _do_run
    return engine.execute(context, self.goals)
  File "/tmp/test/build-support/pants.venv/lib/python2.7/site-packages/pants/engine/engine.py", line 26, in execute
    self.attempt(context, goals)
  File "/tmp/test/build-support/pants.venv/lib/python2.7/site-packages/pants/engine/round_engine.py", line 212, in attempt
    goal_executor.attempt(explain)
  File "/tmp/test/build-support/pants.venv/lib/python2.7/site-packages/pants/engine/round_engine.py", line 45, in attempt
    task.execute()
  File "/tmp/test/build-support/pants.venv/lib/python2.7/site-packages/pants/backend/python/tasks/python_binary_create.py", line 38, in execute
    self.create_binary(binary)
  File "/tmp/test/build-support/pants.venv/lib/python2.7/site-packages/pants/backend/python/tasks/python_binary_create.py", line 51, in create_binary
    with self.temporary_chroot(interpreter=interpreter, pex_info=pexinfo, targets=[binary], platforms=binary.platforms) as chroot:
  File "/usr/lib64/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/tmp/test/build-support/pants.venv/lib/python2.7/site-packages/pants/backend/python/tasks/python_task.py", line 104, in temporary_chroot
    chroot.dump()
  File "/tmp/test/build-support/pants.venv/lib/python2.7/site-packages/pants/backend/python/python_chroot.py", line 178, in dump
    self._dump_library(lib)
  File "/tmp/test/build-support/pants.venv/lib/python2.7/site-packages/pants/backend/python/python_chroot.py", line 112, in _dump_library
    copy_to_chroot(library.target_base, relpath, self._builder.add_source)
  File "/tmp/test/build-support/pants.venv/lib/python2.7/site-packages/pants/backend/python/python_chroot.py", line 107, in copy_to_chroot
    add_function(src, path)
  File "/tmp/test/build-support/pants.venv/lib/python2.7/site-packages/pex/pex_builder.py", line 136, in add_source
    pyc_object = CodeMarshaller.from_py(fp.read(), env_filename)
  File "/tmp/test/build-support/pants.venv/lib/python2.7/site-packages/pex/marshaller.py", line 70, in from_py
    code = compile(py.replace('\r\n', '\n').replace('\r', '\n'), filename, 'exec')

Exception message: invalid syntax (main.py, line 4)

I've filed all this here for more investigation: https://github.com/pantsbuild/pants/issues/1658




Best,
Grant Wu

Reply all
Reply to author
Forward
0 new messages