Skip to content

Commit 2b7dc40

Browse files
rpluemvstinner
authored andcommitted
bpo-38347: find pathfix for Python scripts whose name contain a '-' (pythonGH-16536)
pathfix.py: Assume all files that end on '.py' are Python scripts when working recursively.
1 parent cbb5481 commit 2b7dc40

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

Lib/test/test_tools/test_pathfix.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,34 @@ class TestPathfixFunctional(unittest.TestCase):
1414
script = os.path.join(scriptsdir, 'pathfix.py')
1515

1616
def setUp(self):
17-
self.temp_file = support.TESTFN
1817
self.addCleanup(support.unlink, support.TESTFN)
1918

20-
def pathfix(self, shebang, pathfix_flags, exitcode=0, stdout='', stderr=''):
21-
with open(self.temp_file, 'w', encoding='utf8') as f:
19+
def pathfix(self, shebang, pathfix_flags, exitcode=0, stdout='', stderr='',
20+
directory=''):
21+
if directory:
22+
# bpo-38347: Test filename should contain lowercase, uppercase,
23+
# "-", "_" and digits.
24+
filename = os.path.join(directory, 'script-A_1.py')
25+
pathfix_arg = directory
26+
else:
27+
filename = support.TESTFN
28+
pathfix_arg = filename
29+
30+
with open(filename, 'w', encoding='utf8') as f:
2231
f.write(f'{shebang}\n' + 'print("Hello world")\n')
2332

2433
proc = subprocess.run(
2534
[sys.executable, self.script,
26-
*pathfix_flags, '-n', self.temp_file],
35+
*pathfix_flags, '-n', pathfix_arg],
2736
capture_output=True, text=1)
2837

2938
if stdout == '' and proc.returncode == 0:
30-
stdout = f'{self.temp_file}: updating\n'
39+
stdout = f'{filename}: updating\n'
3140
self.assertEqual(proc.returncode, exitcode, proc)
3241
self.assertEqual(proc.stdout, stdout, proc)
3342
self.assertEqual(proc.stderr, stderr, proc)
3443

35-
with open(self.temp_file, 'r', encoding='utf8') as f:
44+
with open(filename, 'r', encoding='utf8') as f:
3645
output = f.read()
3746

3847
lines = output.split('\n')
@@ -44,6 +53,19 @@ def pathfix(self, shebang, pathfix_flags, exitcode=0, stdout='', stderr=''):
4453

4554
return new_shebang
4655

56+
def test_recursive(self):
57+
tmpdir = support.TESTFN + '.d'
58+
self.addCleanup(support.rmtree, tmpdir)
59+
os.mkdir(tmpdir)
60+
expected_stderr = f"recursedown('{os.path.basename(tmpdir)}')\n"
61+
self.assertEqual(
62+
self.pathfix(
63+
'#! /usr/bin/env python',
64+
['-i', '/usr/bin/python3'],
65+
directory=tmpdir,
66+
stderr=expected_stderr),
67+
'#! /usr/bin/python3')
68+
4769
def test_pathfix(self):
4870
self.assertEqual(
4971
self.pathfix(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pathfix.py: Assume all files that end on '.py' are Python scripts when working recursively.

Tools/scripts/pathfix.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,8 @@ def main():
8989
sys.exit(bad)
9090

9191

92-
ispythonprog = re.compile(r'^[a-zA-Z0-9_]+\.py$')
93-
94-
9592
def ispython(name):
96-
return bool(ispythonprog.match(name))
93+
return name.endswith('.py')
9794

9895

9996
def recursedown(dirname):

0 commit comments

Comments
 (0)