Skip to content
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

Allow PathLib objects in sources #4400

Merged
merged 2 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER
- More tweaking of test framework overview (which is duplicated onto
the website, but not in the regular documentation section).
- Extend range of recognized Java versions to 20.
- Builder calls now accept PathLike objects in source lists. Fixes #4398.
- The Help() function now takes an additional keyword argument
keep_local: when starting to build a help message, you can now
retain help from AddOption calls, but omit help for SCons' own
Expand Down
3 changes: 2 additions & 1 deletion RELEASE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ CHANGED/ENHANCED EXISTING FUNCTIONALITY
architecture combination. For example, when using VS2022 on arm64, the arm64 native
tools are only installed for the 14.3x toolsets.
- Extend range of recognized Java versions to 20.
The Help() function now takes an additional keyword argument keep_local:
- Builder calls (like Program()) now accept pathlib objects in source lists.
- The Help() function now takes an additional keyword argument keep_local:
when starting to build a help message, you can now retain help from AddOption
calls (options added for the project_, but omit help for SCons' own cmdline
options with "Help(newtext, append=True, local_only=True)".
Expand Down
7 changes: 7 additions & 0 deletions SCons/Builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@

"""

import os
from collections import UserDict, UserList
from contextlib import suppress

import SCons.Action
import SCons.Debug
Expand Down Expand Up @@ -479,6 +481,11 @@ def _adjustixes(self, files, pre, suf, ensure_suffix: bool=False):
files = [files]

for f in files:
# fspath() is to catch PathLike paths. We avoid the simpler
# str(f) so as not to "lose" files that are already Nodes:
# TypeError: expected str, bytes or os.PathLike object, not File
with suppress(TypeError):
f = os.fspath(f)
if SCons.Util.is_String(f):
f = SCons.Util.adjustixes(f, pre, suf, ensure_suffix)
result.append(f)
Expand Down
34 changes: 34 additions & 0 deletions SCons/BuilderTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,40 @@ def my_emit(env, sources):
tgt = builder(my_env, target = None, source = 'f6.zzz')[0]
assert tgt.get_internal_path() == 'f6.emit', tgt.get_internal_path()

def test__adjustixes(self) -> None:
"""Test the _adjustixes() method"""
from pathlib import Path

builder = SCons.Builder.Builder()

# path without suffix should have both added
with self.subTest():
r = builder._adjustixes('file', 'pre-', '-suf')
self.assertEqual(r, ['pre-file-suf'])

# path with a suffix should not have one added
with self.subTest():
r = builder._adjustixes('file.o', 'pre-', '-suf')
self.assertEqual(r, ['pre-file.o'])

# unless ensure_suffix is True
with self.subTest():
r = builder._adjustixes('file.o', 'pre-', '-suf', ensure_suffix=True)
self.assertEqual(r, ['pre-file.o-suf'])

# a PathLike object should be modified
with self.subTest():
r = builder._adjustixes(Path('file'), 'pre-', '-suf')
self.assertEqual(r, ['pre-file-suf'])

# make sure a non-str, non-PathLike is just left alone
with self.subTest():
env = Environment()
mynode = env.fs.File('file')
r = builder._adjustixes(mynode, 'pre-', '-suf')
self.assertEqual(r, [mynode])
self.assertEqual(r[0].path, 'file')

def test_single_source(self) -> None:
"""Test Builder with single_source flag set"""
def func(target, source, env) -> None:
Expand Down