Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
build: better support for python3 systems
Improve support for systems where `python` is actually `python3`.

Not all systems have a `python2` binary, so simply updating the shebang
won't work.

What we can do is apply some cleverness: start life as a shell script,
locate the python binary, then re-execute the script but this time as
python code.

Special care is taken to ensure that spaces in arguments are passed on
verbatim.
  • Loading branch information
bnoordhuis committed Aug 17, 2017
commit e1a700dcdcb3b69fb400bbec30de3cafba006269
13 changes: 12 additions & 1 deletion configure
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
#!/usr/bin/env python
#!/bin/sh
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will stop syntax highlighting from working. Not the end of the world I guess.


# Locate python2 interpreter and re-execute the script. Note that the
# mix of single and double quotes is intentional, as is the fact that
# the ] goes on a new line.
_=[ 'exec' '/bin/sh' '-c' '''
Copy link
Copy Markdown
Member

@gibfahn gibfahn Aug 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is changing $_ definitely okay in all shells? I know it's normally not user-modifiable, but IDK about csh and other arcane shells. I checked zsh, bash, and dash.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/bin/sh is always a Bourne-compatible shell, so it should be okay.

which python2.7 >/dev/null && exec python2.7 "$0" "$@"
which python2 >/dev/null && exec python2 "$0" "$@"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe check for python2.6 as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I deliberately didn't add python2.6 for two reasons:

  1. I wasn't sure if it should go before or after python2, and
  2. We supported python2.6 because of centos 5 but since we no longer support that platform...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I guess dropping python2.6 should be a separate PR.

exec python "$0" "$@"
''' "$0" "$@"
]
del _

Copy link
Copy Markdown

@c9n c9n Jul 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bnoordhuis I'm not very clear how it works. Could you explain a bit more about this block for me? Thanks.

_=[ 'exec' '/bin/sh' '-c' '''
which python2.7 >/dev/null && exec python2.7 "$0" "$@"
which python2 >/dev/null && exec python2 "$0" "$@"
exec python "$0" "$@"
''' "$0" "$@"
]
del _

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c9n The _=[ ... ] is first evaluated as shell script that re-executes the whole script as python code when it finds the appropriate interpreter. As python code, it's effectively a no-op: it assigns to _ an array of strings that isn't otherwise used.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bnoordhuis Got it! Thank you so much.

import sys
if sys.version_info[0] != 2 or sys.version_info[1] not in (6, 7):
Expand Down