-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
build: better support for python3 systems #14737
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m okay with this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of suggestions
Also branch name should have been I-shpy-with-my-little-eye-something-beginning-with-python2
@@ -1,4 +1,14 @@ | |||
#!/usr/bin/env python | |||
#!/bin/sh |
There was a problem hiding this comment.
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.
configure
Outdated
which python2 >/dev/null && exec python2 "$0" "$@" | ||
exec python "$0" "$@" | ||
''' "$0" "$@" | ||
] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a del _
or _ = None
so that the _
variable doesn't propagate through the script. I doubt it'll conflict with anything (or cause a performance hit), but it would at least clarify that the python _
variable creation is a byproduct of the shell script.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do that if you insist but my thinking was that this snippet will be copy/pasted across a number of files and should therefore be as succinct as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're copy pasting an extra line doesn't matter right?
I find it clearer, so yes please.
# 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' ''' |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
# the ] goes on a new line. | ||
_=[ 'exec' '/bin/sh' '-c' ''' | ||
which python2.7 >/dev/null && exec python2.7 "$0" "$@" | ||
which python2 >/dev/null && exec python2 "$0" "$@" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
- I wasn't sure if it should go before or after python2, and
- We supported python2.6 because of centos 5 but since we no longer support that platform...
There was a problem hiding this comment.
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.
Ben this is witchcraft, did you just come up with this? I can't get over how many python specific things it relies on. |
I got the idea from a perl script. :-) It's a lot easier in perl though because just about any shell syntax is also valid perl. |
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.
Added a |
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. PR-URL: #14737 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Landed in c6da5c8 |
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. PR-URL: nodejs/node#14737 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
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. PR-URL: nodejs/node#14737 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
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. PR-URL: #14737 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
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. PR-URL: #14737 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
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. PR-URL: #14737 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
I've landed on v6.x. Please let me know if it should be backed out |
exec python "$0" "$@" | ||
''' "$0" "$@" | ||
] | ||
del _ | ||
|
There was a problem hiding this comment.
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 _
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Improve support for systems where
python
is actuallypython3
.Not all systems have a
python2
binary, so simply updating the shebangwon'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.
This would need to be applied to other python scripts as well but I figured
I'd start with a single script in case everyone hates the approach.
CI: https://ci.nodejs.org/job/node-test-pull-request/9594/