Skip to content

Commit 93033b3

Browse files
authored
Merge pull request #4696 from Repiteo/pathlike
Allow parsing Nodes as `PathLike`
2 parents 48ca5f9 + 54b843a commit 93033b3

File tree

121 files changed

+269
-291
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+269
-291
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ RELEASE 4.9.1 - Thu, 27 Mar 2025 11:40:20 -0700
3434
- Fixed a hang in `wait_for_process_to_die()` on Windows, affecting
3535
clean-up of the SCons daemon used for Ninja builds.
3636

37+
From Thaddeus Crews:
38+
- Nodes are now treated as PathLike objects.
39+
3740
From Mats Wichmann:
3841
- Fix typos in CCFLAGS test. Didn't affect the test itself, but
3942
didn't correctly apply the DefaultEnvironment speedup.

RELEASE.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ CHANGED/ENHANCED EXISTING FUNCTIONALITY
2929
- List modifications to existing features, where the previous behavior
3030
wouldn't actually be considered a bug
3131

32+
- Nodes are now treated as PathLike objects.
33+
3234
FIXES
3335
-----
3436

SCons/Builder.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
from SCons.Debug import logInstanceCreation
115115
from SCons.Errors import InternalError, UserError
116116
from SCons.Executor import Executor
117+
from SCons.Node import Node
117118

118119
class _Null:
119120
pass
@@ -487,10 +488,11 @@ def _adjustixes(self, files, pre, suf, ensure_suffix: bool=False):
487488
# fspath() is to catch PathLike paths. We avoid the simpler
488489
# str(f) so as not to "lose" files that are already Nodes:
489490
# TypeError: expected str, bytes or os.PathLike object, not File
490-
with suppress(TypeError):
491-
f = os.fspath(f)
492-
if SCons.Util.is_String(f):
493-
f = SCons.Util.adjustixes(f, pre, suf, ensure_suffix)
491+
if not isinstance(f, Node):
492+
with suppress(TypeError):
493+
f = os.fspath(f)
494+
if SCons.Util.is_String(f):
495+
f = SCons.Util.adjustixes(f, pre, suf, ensure_suffix)
494496
result.append(f)
495497
return result
496498

SCons/BuilderTests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ def test_single_source(self) -> None:
702702
"""Test Builder with single_source flag set"""
703703
def func(target, source, env) -> None:
704704
"""create the file"""
705-
with open(str(target[0]), "w"):
705+
with open(target[0], "w"):
706706
pass
707707
if len(source) == 1 and len(target) == 1:
708708
env['CNT'][0] = env['CNT'][0] + 1
@@ -759,7 +759,7 @@ def test_lists(self) -> None:
759759
"""Testing handling lists of targets and source"""
760760
def function2(target, source, env, tlist = [outfile, outfile2], **kw) -> int:
761761
for t in target:
762-
with open(str(t), 'w') as f:
762+
with open(t, 'w') as f:
763763
f.write("function2\n")
764764
for t in tlist:
765765
if t not in list(map(str, target)):
@@ -790,7 +790,7 @@ def function2(target, source, env, tlist = [outfile, outfile2], **kw) -> int:
790790

791791
def function3(target, source, env, tlist = [sub1_out, sub2_out]) -> int:
792792
for t in target:
793-
with open(str(t), 'w') as f:
793+
with open(t, 'w') as f:
794794
f.write("function3\n")
795795
for t in tlist:
796796
if t not in list(map(str, target)):

SCons/Environment.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ env.Command(
12481248

12491249
import os
12501250
def rename(env, target, source):
1251-
os.rename('.tmp', str(target[0]))
1251+
os.rename('.tmp', target[0])
12521252

12531253

12541254
env.Command(
@@ -3626,7 +3626,7 @@ def create(target, source, env):
36263626

36273627
Writes 'prefix=$SOURCE' into the file name given as $TARGET.
36283628
"""
3629-
with open(str(target[0]), 'wb') as f:
3629+
with open(target[0], 'wb') as f:
36303630
f.write(b'prefix=' + source[0].get_contents() + b'\n')
36313631

36323632
# Fetch the prefix= argument, if any, from the command line.

SCons/Node/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,9 @@ def __init__(self) -> None:
622622
# what line in what file created the node, for example).
623623
Annotate(self)
624624

625+
def __fspath__(self) -> str:
626+
return str(self)
627+
625628
def disambiguate(self, must_exist: bool = False):
626629
return self
627630

SCons/SConfTests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def test_TryAction(self) -> None:
295295
"""Test SConf.TryAction
296296
"""
297297
def actionOK(target, source, env):
298-
with open(str(target[0]), "w") as f:
298+
with open(target[0], "w") as f:
299299
f.write("RUN OK\n")
300300
return None
301301
def actionFAIL(target, source, env) -> int:

bin/SConsExamples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ def process(source_file, ofp):
547547
elif line[:11] != "STRIP CCCOM":
548548
ofp.write(line)
549549
550-
with open(str(target[0]), "w") as fp:
550+
with open(target[0], "w") as fp:
551551
for src in map(str, source):
552552
process(src, fp)
553553
fp.write('debug = ' + ARGUMENTS.get('debug', '0') + '\\n')

test/Actions/append.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@
4545
env=Environment()
4646
4747
def before(env, target, source):
48-
with open(str(target[0]), "wb") as f:
48+
with open(target[0], "wb") as f:
4949
f.write(b"Foo\\n")
5050
with open("before.txt", "wb") as f:
5151
f.write(b"Bar\\n")
5252
5353
def after(env, target, source):
54-
with open(str(target[0]), "rb") as fin, open("after%s", "wb") as fout:
54+
with open(target[0], "rb") as fin, open("after%s", "wb") as fout:
5555
fout.write(fin.read())
5656
5757
env.Prepend(LINKCOM=Action(before))

test/Actions/exitstatfunc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def always_succeed(s):
3838
return 0
3939
4040
def copy_fail(target, source, env):
41-
with open(str(source[0]), 'rb') as infp, open(str(target[0]), 'wb') as f:
41+
with open(source[0], 'rb') as infp, open(target[0], 'wb') as f:
4242
f.write(infp.read())
4343
return 2
4444

0 commit comments

Comments
 (0)