Skip to content

Commit 18b45e4

Browse files
committed
Allow a Variable to not be substituted
New parameter do_subst added to the variables Add method, if false indicates the variable value should not be substituted by the Variables logic. The default is True. Fixes SCons#4241. Signed-off-by: Mats Wichmann <[email protected]>
1 parent 2566498 commit 18b45e4

File tree

7 files changed

+45
-14
lines changed

7 files changed

+45
-14
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER
8585
Now matches the annotation and docstring (which were prematurely
8686
updated in 4.6). All SCons usage except unit test was already fully
8787
consistent with a bool.
88+
- When a variable is added to a Variables object, it can now be flagged
89+
as "don't perform substitution". This allows variables to contain
90+
characters which would otherwise cause expansion. Fixes #4241.
8891

8992

9093
RELEASE 4.7.0 - Sun, 17 Mar 2024 17:22:20 -0700

RELEASE.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ CHANGED/ENHANCED EXISTING FUNCTIONALITY
5050
Now matches the annotation and docstring (which were prematurely
5151
updated in 4.6). All SCons usage except unit test was already fully
5252
consistent with a bool.
53+
- The Variables object Add method now accepts a do_subst keyword argument
54+
(defaults to True) which can be set to inhibit substitution prior to
55+
calling the variable's converter and validator.
5356

5457
FIXES
5558
-----

SCons/Tool/yacc.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ The value is used only if &cv-YACC_GRAPH_FILE_SUFFIX; is not set.
236236
The default value is <filename>.gv</filename>.
237237
</para>
238238
<para>
239-
<emphasis>Changed in version 4.X.Y</emphasis>: deprecated. The default value
239+
<emphasis>Changed in version 4.6.0</emphasis>: deprecated. The default value
240240
changed from <filename>.vcg</filename> (&bison; stopped generating
241241
<filename>.vcg</filename> output with version 2.4, in 2006).
242242
</para>
@@ -261,7 +261,7 @@ Various yacc tools have emitted various formats
261261
at different times.
262262
Set this to match what your parser generator produces.
263263
</para>
264-
<para><emphasis>New in version 4.X.Y</emphasis>. </para>
264+
<para><emphasis>New in version 4.6.0</emphasis>. </para>
265265
</summary>
266266
</cvar>
267267

SCons/Variables/PathVariable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def PathExists(key, val, env) -> None:
141141

142142
# lint: W0622: Redefining built-in 'help' (redefined-builtin)
143143
def __call__(
144-
self, key, help: str, default, validator: Optional[Callable] = None
144+
self, key: str, help: str, default, validator: Optional[Callable] = None
145145
) -> Tuple[str, str, str, Callable, None]:
146146
"""Return a tuple describing a path list SCons Variable.
147147

SCons/Variables/__init__.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
class Variable:
5050
"""A Build Variable."""
5151

52-
__slots__ = ('key', 'aliases', 'help', 'default', 'validator', 'converter')
52+
__slots__ = ('key', 'aliases', 'help', 'default', 'validator', 'converter', 'do_subst')
5353

5454
def __lt__(self, other):
5555
"""Comparison fuction so Variable instances sort."""
@@ -87,9 +87,9 @@ def __init__(
8787
) -> None:
8888
self.options: List[Variable] = []
8989
self.args = args if args is not None else {}
90-
if not SCons.Util.is_List(files):
90+
if not SCons.Util.is_Sequence(files):
9191
files = [files] if files else []
92-
self.files = files
92+
self.files: Sequence[str] = files
9393
self.unknown: Dict[str, str] = {}
9494

9595
def __str__(self) -> str:
@@ -132,6 +132,7 @@ def _do_add(
132132
option.default = default
133133
option.validator = validator
134134
option.converter = converter
135+
option.do_subst = kwargs.get("subst", True)
135136

136137
self.options.append(option)
137138

@@ -171,8 +172,11 @@ def Add(
171172
value before putting it in the environment. (default: ``None``)
172173
"""
173174
if SCons.Util.is_Sequence(key):
174-
if not (len(args) or len(kwargs)):
175-
return self._do_add(*key)
175+
# If no other positional args (and no fundamental kwargs),
176+
# unpack key, and pass the kwargs on:
177+
known_kw = {'help', 'default', 'validator', 'converter'}
178+
if not args and not known_kw.intersection(kwargs.keys()):
179+
return self._do_add(*key, **kwargs)
176180

177181
return self._do_add(key, *args, **kwargs)
178182

@@ -247,7 +251,10 @@ def Update(self, env, args: Optional[dict] = None) -> None:
247251
# apply converters
248252
for option in self.options:
249253
if option.converter and option.key in values:
250-
value = env.subst(f'${option.key}')
254+
if option.do_subst:
255+
value = env.subst(f'${option.key}')
256+
else:
257+
value = env[option.key]
251258
try:
252259
try:
253260
env[option.key] = option.converter(value)
@@ -262,7 +269,11 @@ def Update(self, env, args: Optional[dict] = None) -> None:
262269
# apply validators
263270
for option in self.options:
264271
if option.validator and option.key in values:
265-
option.validator(option.key, env.subst(f'${option.key}'), env)
272+
if option.do_subst:
273+
value = env.subst('${%s}'%option.key)
274+
else:
275+
value = env[option.key]
276+
option.validator(option.key, value, env)
266277

267278
def UnknownVariables(self) -> dict:
268279
"""Return dict of unknown variables.
@@ -340,7 +351,6 @@ def GenerateHelpText(self, env, sort: Union[bool, Callable] = False) -> str:
340351
# removed so now we have to convert to a key.
341352
if callable(sort):
342353
options = sorted(self.options, key=cmp_to_key(lambda x, y: sort(x.key, y.key)))
343-
344354
elif sort is True:
345355
options = sorted(self.options)
346356
else:

doc/generated/variables.gen

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10668,7 +10668,7 @@ Various yacc tools have emitted various formats
1066810668
at different times.
1066910669
Set this to match what your parser generator produces.
1067010670
</para>
10671-
<para><emphasis>New in version 4.X.Y</emphasis>. </para>
10671+
<para><emphasis>New in version 4.6.0</emphasis>. </para>
1067210672
</listitem>
1067310673
</varlistentry>
1067410674
<varlistentry id="cv-YACC_HEADER_FILE">
@@ -10826,7 +10826,7 @@ The value is used only if &cv-YACC_GRAPH_FILE_SUFFIX; is not set.
1082610826
The default value is <filename>.gv</filename>.
1082710827
</para>
1082810828
<para>
10829-
<emphasis>Changed in version 4.X.Y</emphasis>: deprecated. The default value
10829+
<emphasis>Changed in version 4.6.0</emphasis>: deprecated. The default value
1083010830
changed from <filename>.vcg</filename> (&bison; stopped generating
1083110831
<filename>.vcg</filename> output with version 2.4, in 2006).
1083210832
</para>

doc/man/scons.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4835,7 +4835,7 @@ not to any stored-values files.
48354835

48364836
<variablelist>
48374837
<varlistentry id="v-Add">
4838-
<term><replaceable>vars</replaceable>.<function>Add</function>(<parameter>key, [help, default, validator, converter]</parameter>)</term>
4838+
<term><replaceable>vars</replaceable>.<function>Add</function>(<parameter>key, [help, default, validator, converter, do_subst]</parameter>)</term>
48394839
<listitem>
48404840
<para>Add a customizable &consvar; to the &Variables; object.
48414841
<parameter>key</parameter>
@@ -4887,6 +4887,16 @@ or there is no separate validator
48874887
it can raise a <exceptionname>ValueError</exceptionname>.
48884888
</para>
48894889

4890+
<para>
4891+
Substitution will be performed on the variable value
4892+
as it is added, before the converter and validator are called,
4893+
unless the optional <parameter>do_subst</parameter> parameter
4894+
is false (default <literal>True</literal>).
4895+
Suppressing substitution may be useful if the variable value
4896+
looks like a &consvar; reference (<literal>$VAR</literal>)
4897+
to be expanded later.
4898+
</para>
4899+
48904900
<para>
48914901
As a special case, if <parameter>key</parameter>
48924902
is a sequence and is the <emphasis>only</emphasis>
@@ -4919,6 +4929,11 @@ def valid_color(key, val, env):
49194929

49204930
vars.Add('COLOR', validator=valid_color)
49214931
</programlisting>
4932+
4933+
<para>
4934+
<emphasis>Changed in version 4.8.0:</emphasis>
4935+
added the <parameter>do_subst</parameter> parameter.
4936+
</para>
49224937
</listitem>
49234938
</varlistentry>
49244939

0 commit comments

Comments
 (0)