Skip to content
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
10 changes: 10 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER
falls back to the <fm:Branch> field, which names the XSL-NS branch
outright.

From Adam Matthews:
- SConstruct and SConscript files (including one read from stdin),
site_init.py and variables files are no longer compiled with the
future flags of the SCons module that runs them. Since
"from __future__ import annotations" was added to SConscript.py,
Main.py and Variables/__init__.py, annotations in those files had
become strings, which among other things stopped dataclasses from
recognising InitVar fields ("__post_init__() missing 1 required
positional argument").

From Andrew Morrow:
- Remove the legacy job scheduler and the "legacy_sched_deprecated"
experimental feature flag.
Expand Down
5 changes: 5 additions & 0 deletions NEWS.d/changes/NNNN.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SConstruct and SConscript files, site_init.py and variables files are no
longer compiled with the future flags of the SCons modules that run them.
Since SCons 4.11 those modules use "from __future__ import annotations",
which made annotations in build scripts strings and stopped dataclasses
from recognising InitVar fields.
6 changes: 6 additions & 0 deletions RELEASE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ FIXES
CC="ccache clang". Generating the ninja file failed with "Could not find
tool $CC".

- SConstruct and SConscript files, site_init.py and variables files are no
longer compiled with the future flags of the SCons modules that run them.
Since SCons 4.11 those modules use "from __future__ import annotations",
which made annotations in build scripts strings and, for example, stopped
dataclasses from recognising InitVar fields.

IMPROVEMENTS
------------

Expand Down
3 changes: 2 additions & 1 deletion SCons/Script/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,8 @@ def _load_site_scons_dir(topdir, site_dir_name=None):
with open(spec.origin) as f:
code = f.read()
try:
codeobj = compile(code, spec.name, "exec")
# dont_inherit: site_init.py must not pick up this module's future flags
codeobj = compile(code, spec.name, "exec", dont_inherit=True)
exec(codeobj, site_m)
except KeyboardInterrupt:
raise
Expand Down
12 changes: 10 additions & 2 deletions SCons/Script/SConscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,12 @@ def _SConscript(fs: FS, *files: str | Node, **kw) -> Any | list[Any]:
try:
SCons.Script.sconscript_reading = SCons.Script.sconscript_reading + 1
if fn == "-":
exec(sys.stdin.read(), call_stack[-1].globals)
# dont_inherit: SConscripts must not pick up this module's
# "from __future__ import annotations" (or any other future flag)
exec(
compile(sys.stdin.read(), "<stdin>", 'exec', dont_inherit=True),
call_stack[-1].globals,
)
else:
if isinstance(fn, SCons.Node.Node):
f = fn
Expand Down Expand Up @@ -281,7 +286,10 @@ def _SConscript(fs: FS, *files: str | Node, **kw) -> Any | list[Any]:
_file_.close()
if SCons.Debug.sconscript_trace:
print("scons: Entering "+str(scriptname))
exec(compile(scriptdata, scriptname, 'exec'), call_stack[-1].globals)
exec(
compile(scriptdata, scriptname, 'exec', dont_inherit=True),
call_stack[-1].globals,
)
if SCons.Debug.sconscript_trace:
print("scons: Exiting "+str(scriptname))
except SConscriptReturn:
Expand Down
8 changes: 7 additions & 1 deletion SCons/Variables/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,13 @@ def Update(self, env, args: dict | None = None) -> None:
temp_values['__name__'] = filename
with open(filename) as f:
contents = f.read()
exec(contents, {}, temp_values)
# dont_inherit: variables files must not pick up this
# module's future flags
exec(
compile(contents, filename, 'exec', dont_inherit=True),
{},
temp_values,
)
finally:
if dirname:
del sys.path[0]
Expand Down
86 changes: 86 additions & 0 deletions test/SConscript/future-flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env python
#
# MIT License
#
# Copyright The SCons Foundation
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""
Test that SConstruct, SConscript, site_init.py and variables files are not
compiled with the future flags of the SCons modules that run them.

SCons/Script/SConscript.py has "from __future__ import annotations". If a
script inherits that flag, its annotations become strings, which among
other things stops dataclasses from recognising InitVar fields.
"""

import TestSCons

test = TestSCons.TestSCons()

# reports whether an annotation in the script is a type or a string
CHECK = """\
def _f(x: int):
pass
print("%s:", type(_f.__annotations__['x']).__name__)
"""

INITVAR = """\
from dataclasses import InitVar, dataclass

@dataclass
class _C:
env: InitVar[int]
name: str

def __post_init__(self, env):
self.seen = env

print("InitVar:", _C(env=1, name="n").seen)
"""

test.subdir('site_scons', 'sub')
test.write(['site_scons', 'site_init.py'], CHECK % "site_init")
test.write('vars.py', CHECK % "variables")
test.write(['sub', 'SConscript'], CHECK % "SConscript" + INITVAR)
test.write('SConstruct', CHECK % "SConstruct" + """\
Environment(tools=[], variables=Variables('vars.py'))
SConscript('sub/SConscript')
""")

# site_init.py is read before the SConscript files
expect = "site_init: type\n" + test.wrap_stdout(
read_str="SConstruct: type\n"
"variables: type\n"
"SConscript: type\n"
"InitVar: 1\n",
build_str="scons: `.' is up to date.\n",
)
test.run(arguments='.', stdout=expect)

# an SConstruct read from standard input
expect = "site_init: type\n" + test.wrap_stdout(
read_str="stdin: type\n",
build_str="scons: `.' is up to date.\n",
)
test.run(arguments='-f - .', stdin=CHECK % "stdin", stdout=expect)

test.pass_test()
Loading