-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-37064: add option -k to Tools/scripts/pathfix.py #15548
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
Conversation
- with this option it is possible to keep flags when changing shebang line
Tools/scripts/pathfix.py
Outdated
@@ -33,16 +38,20 @@ | |||
new_interpreter = None | |||
preserve_timestamps = False | |||
create_backup = True | |||
add_flag = None |
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 variable looks unused.
Tools/scripts/pathfix.py
Outdated
|
||
def parse_shebang(shebangline): | ||
end = len(shebangline) | ||
start = shebangline.find(b' -', 0, end) # .find() returns index at space |
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.
Please remove the comment, IMHO it's not really helpful.
", 0, end" are the default value and so can be removed.
Tools/scripts/pathfix.py
Outdated
@@ -164,12 +180,28 @@ def fix(filename): | |||
# Return success | |||
return 0 | |||
|
|||
|
|||
def parse_shebang(shebangline): | |||
end = len(shebangline) |
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.
Please add a comment that on Linux, the first whitespace (U+0020) indicates the beginning of options.
Tools/scripts/pathfix.py
Outdated
start = shebangline.find(b' -', 0, end) # .find() returns index at space | ||
if start == -1: | ||
return b'', b'' | ||
print(shebangline[(start + 2):end]) |
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.
Don't commit debug print please (remove it).
Tools/scripts/pathfix.py
Outdated
def fixline(line): | ||
global keep_flags |
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 don't think that this global statement is useful. Variables are global by default.
Tools/scripts/pathfix.py
Outdated
if b"python" not in line: | ||
return line | ||
if keep_flags: | ||
flags = parse_shebang(line) | ||
return b'#! ' + new_interpreter + b" -" + flags | ||
return b'#! ' + new_interpreter + b'\n' |
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 would prefer to have:
if keep_flags:
flags = parse_shebang(line)
else:
flags = b''
return b'#! ' + new_interpreter + flags + b'\n'
So parse_shebang() must remove the trailing newline.
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.
Ping @PatrikKopkan?
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 have zero idea how I deleted. In editor I still have:
return b'#! ' + new_interpreter + b' -' + flags + b'\n'
Lib/test/test_tools/test_pathfix.py
Outdated
subprocess.call([sys.executable, self.script, '-i', '/usr/bin/python', '-n', self.temp_file]) | ||
with open(self.temp_file) as f: | ||
output = f.read() | ||
self.assertEqual(output, '#! /usr/bin/python\n' + 'print("Hello world")\n') |
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.
Please add a test using -k when the shebang does not contain any flag.
Lib/test/test_tools/test_pathfix.py
Outdated
self.temp_file = support.TESTFN | ||
|
||
with open(self.temp_file, 'w') as f: | ||
f.write('#! /usr/bin/env python -R\n' + 'print("Hello world")\n') |
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 would prefer to have an helper method which takes two arguments: (shebang, pathfix_flags) and return the new shebang. To get tests which look like:
def test_keep_flags(self):
shebang = '#! /usr/bin/env python -R'
self.assertEqual(
self.pathfix(shebang, ['-i', '/usr/bin/python']),
'#! /usr/bin/python')
self.assertEqual(
self.pathfix(shebang, ['-i', '/usr/bin/python', '-k']),
'#! /usr/bin/python -R')
Lib/test/test_tools/test_pathfix.py
Outdated
|
||
@classmethod | ||
def setupUpClass(cls): | ||
self.addCleanup(support.unlink, support.TESTFN) |
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.
It doesn't work: self doesn't exist. You may use setUp().
@@ -0,0 +1 @@ | |||
Add flag -k to pathscript.py. This flag preserve flags from shebang before change and adds them after change. |
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 suggest: "Add flag -k to pathscript.py script: preserve shebang flags."
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Lib/test/test_tools/test_pathfix.py
Outdated
@@ -0,0 +1,48 @@ | |||
import unittest, os, subprocess |
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.
Please put one import per line: see PEP 8.
Try also to sort imports.
Lib/test/test_tools/test_pathfix.py
Outdated
subprocess.call([sys.executable, self.script, *pathfix_flags, self.temp_file]) | ||
with open(self.temp_file) as f: | ||
output = f.read() | ||
print(output) |
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.
Don't forget to remove debug print when you submit a PR.
Lib/test/test_tools/test_pathfix.py
Outdated
self.addCleanup(support.unlink, support.TESTFN) | ||
|
||
def pathfix(self, shebang, pathfix_flags): | ||
with open(self.temp_file, 'w') as f: |
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 would prefer to specify the encoding: pass encoding="utf8" to this open, and the other one below.
Lib/test/test_tools/test_pathfix.py
Outdated
self.assertEqual( | ||
self.pathfix( | ||
'#! /usr/bin/env python -R', | ||
['-i', '/usr/bin/python', '-k', '-n']), |
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.
Please remove -n from tests and always put it in pathfix() function.
Lib/test/test_tools/test_pathfix.py
Outdated
self.pathfix( | ||
'#! /usr/bin/env python -R', | ||
['-i', '/usr/bin/python', '-k', '-n']), | ||
'#! /usr/bin/python -R\n' + 'print("Hello world")\n' |
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 don't think that it's useful to check for 'print("Hello world")\n' in each test. I would prefer to test it in pathfix() and just return the first line.
Something like this in pathfix():
lines = output.split('\n')
self.assertEqual(lines[1:], ['print("Hello world")\n']
shebang = lines[0][:-1] # strip the newline
return shebang
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.
Thanks, the code now looks way better! A few more comments.
Lib/test/test_tools/test_pathfix.py
Outdated
shebang = lines[0][:-1] # strip the newline | ||
return shebang | ||
|
||
def test_pathfix_keeping_flags(self): |
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 would prefer:
- put test_pathfix_without_keeping_flags() code into test_pathfix()
- move test_pathfix_keeping_flags() after test_pathfix()
- add a test in test_pathfix_keeping_flags(): use -k but the shebang doesn't contain any flag (the bug that you just fixed)
Tools/scripts/pathfix.py
Outdated
def parse_shebang(shebangline): | ||
shebangline = shebangline.strip() | ||
end = len(shebangline) | ||
start = shebangline.find(b' -', 0, end) |
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.
"end" argument is useless: shebangline.find(b' -') behaves the same and is simpler. Remove 0 and end.
Tools/scripts/pathfix.py
Outdated
if b"python" not in line: | ||
return line | ||
if keep_flags: | ||
flags = parse_shebang(line) | ||
return b'#! ' + new_interpreter + b" -" + flags | ||
return b'#! ' + new_interpreter + b'\n' |
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.
Ping @PatrikKopkan?
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.
LGTM.
Add flag -k to pathscript.py script: preserve shebang flags.
* bpo-37064: Add option -k to Tools/scripts/pathfix.py (GH-15548) Add flag -k to pathscript.py script: preserve shebang flags. (cherry picked from commit 50254ac) * bpo-37064: Add option -a to pathfix.py tool (GH-15717) Add option -a to Tools/Scripts/pathfix.py script: add flags. (cherry picked from commit 1dc1acb)
Add flag -k to pathscript.py script: preserve shebang flags.
Add flag -k to pathscript.py script: preserve shebang flags.
https://bugs.python.org/issue37064