Skip to content

Commit 0c21de9

Browse files
authored
Merge pull request #4627 from mwichmann/maint/st_mode
Modernize stat usage
2 parents 393792c + f906d01 commit 0c21de9

29 files changed

+317
-306
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER
182182
Only object-like macros are replaced (not function-like), and
183183
only on a whole-word basis; recursion is limited to five levels
184184
and does not error out if that limit is reached (issue #4523).
185+
- Minor modernization: make use of stat object's st_mode, st_mtime
186+
and other attributes rather than indexing into stat return.
185187
- The update-release-info test is adapted to accept changed help output
186188
introduced in Python 3.12.8/3.13.1.
187189

RELEASE.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ FIXES
164164
only on a whole-word basis; recursion is limited to five levels
165165
and does not error out if that limit is reached (issue #4523).
166166

167+
- Minor modernization: make use of stat object's st_mode, st_mtime
168+
and other attributes rather than indexing into stat return.
169+
170+
167171
IMPROVEMENTS
168172
------------
169173

SCons/CacheDir.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def CacheRetrieveFunc(target, source, env) -> int:
7171
except OSError:
7272
pass
7373
st = fs.stat(cachefile)
74-
fs.chmod(t.get_internal_path(), stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE)
74+
fs.chmod(t.get_internal_path(), stat.S_IMODE(st.st_mode) | stat.S_IWRITE)
7575
return 0
7676

7777
def CacheRetrieveString(target, source, env) -> str:

SCons/Node/FS.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,8 @@ def getmtime(self):
762762
st = self.stat()
763763

764764
if st:
765+
# TODO: switch to st.st_mtime, however this changes granularity
766+
# (ST_MTIME is an int for backwards compat, st_mtime is float)
765767
return st[stat.ST_MTIME]
766768
else:
767769
return None

SCons/Node/FSTests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,7 @@ def test_update(self) -> None:
774774

775775
ni.update(fff)
776776

777+
# TODO: flip this to st.st_mtime when Node/FS.py does
777778
mtime = st[stat.ST_MTIME]
778779
assert ni.timestamp == mtime, (ni.timestamp, mtime)
779780
size = st.st_size
@@ -786,6 +787,7 @@ def test_update(self) -> None:
786787

787788
st = os.stat('fff')
788789

790+
# TODO: flip this to st.st_mtime when Node/FS.py does
789791
mtime = st[stat.ST_MTIME]
790792
assert ni.timestamp != mtime, (ni.timestamp, mtime)
791793
size = st.st_size

SCons/Tool/install.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def copyFunc(dest, source, env) -> int:
176176
else:
177177
copy2(source, dest)
178178
st = os.stat(source)
179-
os.chmod(dest, stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE)
179+
os.chmod(dest, stat.S_IMODE(st.st_mode) | stat.S_IWRITE)
180180

181181
return 0
182182

@@ -204,7 +204,7 @@ def copyFuncVersionedLib(dest, source, env) -> int:
204204
pass
205205
copy2(source, dest)
206206
st = os.stat(source)
207-
os.chmod(dest, stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE)
207+
os.chmod(dest, stat.S_IMODE(st.st_mode) | stat.S_IWRITE)
208208
installShlibLinks(dest, source, env)
209209

210210
return 0

site_scons/Utilities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def whereis(filename):
3131
st = os.stat(f_ext)
3232
except:
3333
continue
34-
if stat.S_IMODE(st[stat.ST_MODE]) & 0o111:
34+
if stat.S_IMODE(st.st_mode) & stat.S_IXUSR:
3535
return f_ext
3636
return None
3737

test/Actions/append.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def after(env, target, source):
6363

6464
test.run(arguments='.')
6565
test.must_match('before.txt', 'Bar\n')
66-
os.chmod(after_exe, os.stat(after_exe)[stat.ST_MODE] | stat.S_IXUSR)
66+
os.chmod(after_exe, os.stat(after_exe).st_mode | stat.S_IXUSR)
6767
test.run(program=after_exe, stdout="Foo\n")
6868
test.pass_test()
6969

test/Actions/pre-post.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def before(env, target, source):
5050
a=str(target[0])
5151
with open(a, "wb") as f:
5252
f.write(b"Foo\\n")
53-
os.chmod(a, os.stat(a)[stat.ST_MODE] | stat.S_IXUSR)
53+
os.chmod(a, os.stat(a).st_mode | stat.S_IXUSR)
5454
with open("before.txt", "ab") as f:
5555
f.write((os.path.splitext(str(target[0]))[0] + "\\n").encode())
5656
@@ -59,7 +59,7 @@ def after(env, target, source):
5959
a = "after_" + t
6060
with open(t, "rb") as fin, open(a, "wb") as fout:
6161
fout.write(fin.read())
62-
os.chmod(a, os.stat(a)[stat.ST_MODE] | stat.S_IXUSR)
62+
os.chmod(a, os.stat(a).st_mode | stat.S_IXUSR)
6363
6464
foo = env.Program(source='foo.c', target='foo')
6565
AddPreAction(foo, before)

test/Chmod.py

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def cat(env, source, target):
5151
f.write(infp.read())
5252
5353
Cat = Action(cat)
54+
DefaultEnvironment(tools=[]) # test speedup
5455
env = Environment()
5556
env.Command(
5657
'bar.out',
@@ -154,92 +155,92 @@ def cat(env, source, target):
154155
""")
155156
test.run(options = '-n', arguments = '.', stdout = expect)
156157

157-
s = stat.S_IMODE(os.stat(test.workpath('f1'))[stat.ST_MODE])
158+
s = stat.S_IMODE(os.stat(test.workpath('f1')).st_mode)
158159
test.fail_test(s != 0o444)
159-
s = stat.S_IMODE(os.stat(test.workpath('f1-File'))[stat.ST_MODE])
160+
s = stat.S_IMODE(os.stat(test.workpath('f1-File')).st_mode)
160161
test.fail_test(s != 0o444)
161-
s = stat.S_IMODE(os.stat(test.workpath('d2'))[stat.ST_MODE])
162+
s = stat.S_IMODE(os.stat(test.workpath('d2')).st_mode)
162163
test.fail_test(s != 0o555)
163-
s = stat.S_IMODE(os.stat(test.workpath('d2-Dir'))[stat.ST_MODE])
164+
s = stat.S_IMODE(os.stat(test.workpath('d2-Dir')).st_mode)
164165
test.fail_test(s != 0o555)
165166
test.must_not_exist('bar.out')
166-
s = stat.S_IMODE(os.stat(test.workpath('f3'))[stat.ST_MODE])
167+
s = stat.S_IMODE(os.stat(test.workpath('f3')).st_mode)
167168
test.fail_test(s != 0o444)
168-
s = stat.S_IMODE(os.stat(test.workpath('d4'))[stat.ST_MODE])
169+
s = stat.S_IMODE(os.stat(test.workpath('d4')).st_mode)
169170
test.fail_test(s != 0o555)
170-
s = stat.S_IMODE(os.stat(test.workpath('f5'))[stat.ST_MODE])
171+
s = stat.S_IMODE(os.stat(test.workpath('f5')).st_mode)
171172
test.fail_test(s != 0o444)
172173
test.must_not_exist('f6.out')
173174
test.must_not_exist('f7.out')
174-
s = stat.S_IMODE(os.stat(test.workpath('Chmod-f7.in'))[stat.ST_MODE])
175+
s = stat.S_IMODE(os.stat(test.workpath('Chmod-f7.in')).st_mode)
175176
test.fail_test(s != 0o444)
176-
s = stat.S_IMODE(os.stat(test.workpath('f7.out-Chmod'))[stat.ST_MODE])
177+
s = stat.S_IMODE(os.stat(test.workpath('f7.out-Chmod')).st_mode)
177178
test.fail_test(s != 0o444)
178179
test.must_not_exist('f8.out')
179-
s = stat.S_IMODE(os.stat(test.workpath('f9'))[stat.ST_MODE])
180+
s = stat.S_IMODE(os.stat(test.workpath('f9')).st_mode)
180181
test.fail_test(s != 0o444)
181-
s = stat.S_IMODE(os.stat(test.workpath('f10'))[stat.ST_MODE])
182+
s = stat.S_IMODE(os.stat(test.workpath('f10')).st_mode)
182183
test.fail_test(s != 0o444)
183-
s = stat.S_IMODE(os.stat(test.workpath('d11'))[stat.ST_MODE])
184+
s = stat.S_IMODE(os.stat(test.workpath('d11')).st_mode)
184185
test.fail_test(s != 0o555)
185-
s = stat.S_IMODE(os.stat(test.workpath('d12'))[stat.ST_MODE])
186+
s = stat.S_IMODE(os.stat(test.workpath('d12')).st_mode)
186187
test.fail_test(s != 0o555)
187-
s = stat.S_IMODE(os.stat(test.workpath('f13'))[stat.ST_MODE])
188+
s = stat.S_IMODE(os.stat(test.workpath('f13')).st_mode)
188189
test.fail_test(s != 0o444)
189-
s = stat.S_IMODE(os.stat(test.workpath('f14'))[stat.ST_MODE])
190+
s = stat.S_IMODE(os.stat(test.workpath('f14')).st_mode)
190191
test.fail_test(s != 0o444)
191-
s = stat.S_IMODE(os.stat(test.workpath('f15'))[stat.ST_MODE])
192+
s = stat.S_IMODE(os.stat(test.workpath('f15')).st_mode)
192193
test.fail_test(s != 0o444)
193-
s = stat.S_IMODE(os.stat(test.workpath('d16'))[stat.ST_MODE])
194+
s = stat.S_IMODE(os.stat(test.workpath('d16')).st_mode)
194195
test.fail_test(s != 0o555)
195-
s = stat.S_IMODE(os.stat(test.workpath('d17'))[stat.ST_MODE])
196+
s = stat.S_IMODE(os.stat(test.workpath('d17')).st_mode)
196197
test.fail_test(s != 0o555)
197-
s = stat.S_IMODE(os.stat(test.workpath('d18'))[stat.ST_MODE])
198+
s = stat.S_IMODE(os.stat(test.workpath('d18')).st_mode)
198199
test.fail_test(s != 0o555)
199200

200201
test.run()
201202

202-
s = stat.S_IMODE(os.stat(test.workpath('f1'))[stat.ST_MODE])
203+
s = stat.S_IMODE(os.stat(test.workpath('f1')).st_mode)
203204
test.fail_test(s != 0o666)
204-
s = stat.S_IMODE(os.stat(test.workpath('f1-File'))[stat.ST_MODE])
205+
s = stat.S_IMODE(os.stat(test.workpath('f1-File')).st_mode)
205206
test.fail_test(s != 0o666)
206-
s = stat.S_IMODE(os.stat(test.workpath('d2'))[stat.ST_MODE])
207+
s = stat.S_IMODE(os.stat(test.workpath('d2')).st_mode)
207208
test.fail_test(s != 0o777)
208-
s = stat.S_IMODE(os.stat(test.workpath('d2-Dir'))[stat.ST_MODE])
209+
s = stat.S_IMODE(os.stat(test.workpath('d2-Dir')).st_mode)
209210
test.fail_test(s != 0o777)
210211
test.must_match('bar.out', "bar.in\n")
211-
s = stat.S_IMODE(os.stat(test.workpath('f3'))[stat.ST_MODE])
212+
s = stat.S_IMODE(os.stat(test.workpath('f3')).st_mode)
212213
test.fail_test(s != 0o666)
213-
s = stat.S_IMODE(os.stat(test.workpath('d4'))[stat.ST_MODE])
214+
s = stat.S_IMODE(os.stat(test.workpath('d4')).st_mode)
214215
test.fail_test(s != 0o777)
215-
s = stat.S_IMODE(os.stat(test.workpath('f5'))[stat.ST_MODE])
216+
s = stat.S_IMODE(os.stat(test.workpath('f5')).st_mode)
216217
test.fail_test(s != 0o666)
217218
test.must_match('f6.out', "f6.in\n")
218219
test.must_match('f7.out', "f7.in\n")
219-
s = stat.S_IMODE(os.stat(test.workpath('Chmod-f7.in'))[stat.ST_MODE])
220+
s = stat.S_IMODE(os.stat(test.workpath('Chmod-f7.in')).st_mode)
220221
test.fail_test(s != 0o666)
221-
s = stat.S_IMODE(os.stat(test.workpath('f7.out-Chmod'))[stat.ST_MODE])
222+
s = stat.S_IMODE(os.stat(test.workpath('f7.out-Chmod')).st_mode)
222223
test.fail_test(s != 0o666)
223224
test.must_match('f8.out', "f8.in\n")
224-
s = stat.S_IMODE(os.stat(test.workpath('f9'))[stat.ST_MODE])
225+
s = stat.S_IMODE(os.stat(test.workpath('f9')).st_mode)
225226
test.fail_test(s != 0o666)
226-
s = stat.S_IMODE(os.stat(test.workpath('f10'))[stat.ST_MODE])
227+
s = stat.S_IMODE(os.stat(test.workpath('f10')).st_mode)
227228
test.fail_test(s != 0o666)
228-
s = stat.S_IMODE(os.stat(test.workpath('d11'))[stat.ST_MODE])
229+
s = stat.S_IMODE(os.stat(test.workpath('d11')).st_mode)
229230
test.fail_test(s != 0o777)
230-
s = stat.S_IMODE(os.stat(test.workpath('d12'))[stat.ST_MODE])
231+
s = stat.S_IMODE(os.stat(test.workpath('d12')).st_mode)
231232
test.fail_test(s != 0o777)
232-
s = stat.S_IMODE(os.stat(test.workpath('f13'))[stat.ST_MODE])
233+
s = stat.S_IMODE(os.stat(test.workpath('f13')).st_mode)
233234
test.fail_test(s != 0o444)
234-
s = stat.S_IMODE(os.stat(test.workpath('f14'))[stat.ST_MODE])
235+
s = stat.S_IMODE(os.stat(test.workpath('f14')).st_mode)
235236
test.fail_test(s != 0o666)
236-
s = stat.S_IMODE(os.stat(test.workpath('f15'))[stat.ST_MODE])
237+
s = stat.S_IMODE(os.stat(test.workpath('f15')).st_mode)
237238
test.fail_test(s != 0o666)
238-
s = stat.S_IMODE(os.stat(test.workpath('d16'))[stat.ST_MODE])
239+
s = stat.S_IMODE(os.stat(test.workpath('d16')).st_mode)
239240
test.fail_test(s != 0o777)
240-
s = stat.S_IMODE(os.stat(test.workpath('d17'))[stat.ST_MODE])
241+
s = stat.S_IMODE(os.stat(test.workpath('d17')).st_mode)
241242
test.fail_test(s != 0o777)
242-
s = stat.S_IMODE(os.stat(test.workpath('d18'))[stat.ST_MODE])
243+
s = stat.S_IMODE(os.stat(test.workpath('d18')).st_mode)
243244
test.fail_test(s != 0o777)
244245

245246
test.pass_test()

0 commit comments

Comments
 (0)