diff --git a/CHANGES.txt b/CHANGES.txt index ca52cc4a7b..2d6a97b921 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -27,6 +27,9 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER - runtest.py once again finds "external" tests, such as the tests for tools in scons-contrib. An earlier rework had broken this. Fixes #4699. - Clarify how pre/post actions on an alias work. + - Replace use of old conditional expression idioms with the official + one from PEP 308 introduced in Python 2.5 (2006). The idiom being + replaced (using and/or) is regarded as error prone. RELEASE 4.9.1 - Thu, 27 Mar 2025 11:40:20 -0700 diff --git a/RELEASE.txt b/RELEASE.txt index 2219bf7866..24d4a698fe 100644 --- a/RELEASE.txt +++ b/RELEASE.txt @@ -31,6 +31,10 @@ CHANGED/ENHANCED EXISTING FUNCTIONALITY - Nodes are now treated as PathLike objects. +- Replace use of old conditional expression idioms with the official + one from PEP 308 introduced in Python 2.5 (2006). The idiom being + replaced (using and/or) is regarded as error prone. + FIXES ----- diff --git a/SCons/Builder.py b/SCons/Builder.py index 870596f0c7..0e8d337521 100644 --- a/SCons/Builder.py +++ b/SCons/Builder.py @@ -701,7 +701,7 @@ def set_src_suffix(self, src_suffix) -> None: src_suffix = [] elif not SCons.Util.is_List(src_suffix): src_suffix = [ src_suffix ] - self.src_suffix = [callable(suf) and suf or self.adjust_suffix(suf) for suf in src_suffix] + self.src_suffix = [suf if callable(suf) else self.adjust_suffix(suf) for suf in src_suffix] def get_src_suffix(self, env): """Get the first src_suffix in the list of src_suffixes.""" diff --git a/SCons/Script/Main.py b/SCons/Script/Main.py index 79cf8321b2..c1335cc4b9 100644 --- a/SCons/Script/Main.py +++ b/SCons/Script/Main.py @@ -465,19 +465,28 @@ def __init__(self, derived: bool=False, prune: bool=False, status: bool=False, s self.prune = prune self.status = status self.sLineDraw = sLineDraw + def get_all_children(self, node): return node.all_children() + def get_derived_children(self, node): children = node.all_children(None) return [x for x in children if x.has_builder()] + def display(self, t) -> None: if self.derived: func = self.get_derived_children else: func = self.get_all_children - s = self.status and 2 or 0 - SCons.Util.print_tree(t, func, prune=self.prune, showtags=s, lastChild=True, singleLineDraw=self.sLineDraw) - + s = 2 if self.status else 0 + SCons.Util.print_tree( + t, + func, + prune=self.prune, + showtags=s, + lastChild=True, + singleLineDraw=self.sLineDraw, + ) def python_version_string(): return sys.version.split()[0] diff --git a/SCons/Tool/linkCommon/SharedLibrary.py b/SCons/Tool/linkCommon/SharedLibrary.py index 30170f8bb8..04de7fac02 100644 --- a/SCons/Tool/linkCommon/SharedLibrary.py +++ b/SCons/Tool/linkCommon/SharedLibrary.py @@ -202,7 +202,7 @@ def setup_shared_lib_logic(env) -> None: # Note this is gnu style env["SHLIBSONAMEFLAGS"] = "-Wl,-soname=$_SHLIBSONAME" - env["_SHLIBVERSION"] = "${SHLIBVERSION and '.'+SHLIBVERSION or ''}" + env["_SHLIBVERSION"] = "${'.' + SHLIBVERSION if SHLIBVERSION else ''}" env["_SHLIBVERSIONFLAGS"] = "$SHLIBVERSIONFLAGS -Wl,-soname=$_SHLIBSONAME" env["SHLIBEMITTER"] = [lib_emitter, shlib_symlink_emitter] diff --git a/SCons/Tool/msvc.py b/SCons/Tool/msvc.py index b823752b50..e73869bc4e 100644 --- a/SCons/Tool/msvc.py +++ b/SCons/Tool/msvc.py @@ -72,12 +72,12 @@ def msvc_set_PCHPDBFLAGS(env) -> None: if env.get('MSVC_VERSION',False): maj, min = msvc_version_to_maj_min(env['MSVC_VERSION']) if maj < 8: - env['PCHPDBFLAGS'] = SCons.Util.CLVar(['${(PDB and "/Yd") or ""}']) + env['PCHPDBFLAGS'] = SCons.Util.CLVar(['${"/Yd" if PDB else ""}']) else: env['PCHPDBFLAGS'] = '' else: # Default if we can't determine which version of MSVC we're using - env['PCHPDBFLAGS'] = SCons.Util.CLVar(['${(PDB and "/Yd") or ""}']) + env['PCHPDBFLAGS'] = SCons.Util.CLVar(['${"/Yd" if PDB else ""}']) def pch_emitter(target, source, env): @@ -143,7 +143,7 @@ def gen_ccpchflags(env, target, source, for_signature): pch_node = get_pch_node(env, target, source) if not pch_node: return '' - + return SCons.Util.CLVar(["/Yu$PCHSTOP", "/Fp%s" % pch_node]) pch_action = SCons.Action.Action('$PCHCOM', '$PCHCOMSTR') @@ -256,7 +256,7 @@ def generate(env) -> None: static_obj.add_emitter(suffix, static_object_emitter) shared_obj.add_emitter(suffix, shared_object_emitter) - env['CCPDBFLAGS'] = SCons.Util.CLVar(['${(PDB and "/Z7") or ""}']) + env['CCPDBFLAGS'] = SCons.Util.CLVar(['${"/Z7" if PDB else ""}']) env['CCPCHFLAGS'] = gen_ccpchflags env['_MSVC_OUTPUT_FLAG'] = msvc_output_flag env['_CCCOMCOM'] = '$CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS $CCPCHFLAGS $CCPDBFLAGS' diff --git a/bin/scons-diff.py b/bin/scons-diff.py index b1b2eec4ed..a60337afeb 100644 --- a/bin/scons-diff.py +++ b/bin/scons-diff.py @@ -90,8 +90,10 @@ def simple_diff(a, b, fromfile='', tofile='', output like the simple, unadorned 'diff" command. """ sm = difflib.SequenceMatcher(None, a, b) + def comma(x1, x2): - return x1+1 == x2 and str(x2) or '%s,%s' % (x1+1, x2) + return x1 + 1 == str(x2) if x2 else '%s,%s' % (x1 + 1, x2) + result = [] for op, a1, a2, b1, b2 in sm.get_opcodes(): if op == 'delete': diff --git a/bin/update-release-info.py b/bin/update-release-info.py index c911fca38f..4f7cf40035 100644 --- a/bin/update-release-info.py +++ b/bin/update-release-info.py @@ -173,7 +173,7 @@ def set_new_date(self): Mon, 05 Jun 2010 21:17:15 -0700 NEW DATE WILL BE INSERTED HERE """ - min = (time.daylight and time.altzone or time.timezone) // 60 + min = (time.altzone if time.daylight else time.timezone) // 60 hr = min // 60 min = -(min % 60 + hr * 100) self.new_date = (time.strftime('%a, %d %b %Y %X', self.release_date + (0, 0, 0)) diff --git a/site_scons/BuildCommandLine.py b/site_scons/BuildCommandLine.py index 5d00f6eb8e..41d4cf68c7 100644 --- a/site_scons/BuildCommandLine.py +++ b/site_scons/BuildCommandLine.py @@ -104,7 +104,7 @@ def set_date(self): NEW DATE WILL BE INSERTED HERE """ - min = (time.daylight and time.altzone or time.timezone) // 60 + min = (time.altzone if time.daylight else time.timezone) // 60 hr = min // 60 min = -(min % 60 + hr * 100) # TODO: is it better to take the date of last rev? Externally: diff --git a/test/Actions/function.py b/test/Actions/function.py index fd5d5f0257..9b584d807e 100644 --- a/test/Actions/function.py +++ b/test/Actions/function.py @@ -122,7 +122,7 @@ def foo(b=b): def runtest(arguments, expectedOutFile, expectedRebuild=True, stderr=""): test.run( arguments=arguments, - stdout=expectedRebuild and rebuildstr or nobuildstr, + stdout=rebuildstr if expectedRebuild else nobuildstr, stderr="", ) diff --git a/test/SPAWN.py b/test/SPAWN.py index ca9d3448d0..5939f4fca2 100644 --- a/test/SPAWN.py +++ b/test/SPAWN.py @@ -42,7 +42,7 @@ ofp.write(ifp.read()) """) -test.write('SConstruct', """ +test.write('SConstruct', """\ import subprocess import sys @@ -56,16 +56,19 @@ def my_spawn2(sh, escape, cmd, args, env): cp = subprocess.run(s, shell=True) return cp.returncode -env = Environment(MY_SPAWN1 = my_spawn1, - MY_SPAWN2 = my_spawn2, - COMMAND = r'%(_python_)s cat.py $TARGET $SOURCES') -env1 = env.Clone(SPAWN = my_spawn1) +DefaultEnvironment() +env = Environment( + MY_SPAWN1=my_spawn1, + MY_SPAWN2=my_spawn2, + COMMAND=r'%(_python_)s cat.py $TARGET $SOURCES', +) +env1 = env.Clone(SPAWN=my_spawn1) env1.Command('file1.out', 'file1.in', '$COMMAND') -env2 = env.Clone(SPAWN = '$MY_SPAWN2') +env2 = env.Clone(SPAWN='$MY_SPAWN2') env2.Command('file2.out', 'file2.in', '$COMMAND') -env3 = env.Clone(SPAWN = '${USE_TWO and MY_SPAWN2 or MY_SPAWN1}') +env3 = env.Clone(SPAWN='${MY_SPAWN2 if USE_TWO else MY_SPAWN1}') env3.Command('file3.out', 'file3.in', '$COMMAND', USE_TWO=0) env3.Command('file4.out', 'file4.in', '$COMMAND', USE_TWO=1) """ % locals()) diff --git a/test/option/help-options.py b/test/option/help-options.py index e430572a7f..c84961f83d 100644 --- a/test/option/help-options.py +++ b/test/option/help-options.py @@ -54,9 +54,9 @@ lines = stdout.split('\n') lines = [x for x in lines if x[:3] == ' -'] lines = [x[3:] for x in lines] -lines = [x[0] == '-' and x[1:] or x for x in lines] +lines = [x[1:] if x.startswith('-') else x for x in lines] options = [x.split()[0] for x in lines] -options = [x[-1] == ',' and x[:-1] or x for x in options] +options = [x[:-1] if x.endswith(',') else x for x in options] lowered = [x.lower() for x in options] ordered = sorted(lowered) if lowered != ordered: @@ -65,7 +65,7 @@ test.fail_test() test.pass_test() - + # Local Variables: # tab-width:4 diff --git a/testing/framework/TestCmd.py b/testing/framework/TestCmd.py index 059ad608b6..6ac1275fc3 100644 --- a/testing/framework/TestCmd.py +++ b/testing/framework/TestCmd.py @@ -699,7 +699,7 @@ def simple_diff( sm = difflib.SequenceMatcher(None, a, b) def comma(x1, x2): - return x1 + 1 == x2 and str(x2) or f'{x1 + 1},{x2}' + return str(x2) if x1 + 1 == x2 else f'{x1 + 1},{x2}' for op, a1, a2, b1, b2 in sm.get_opcodes(): if op == 'delete': diff --git a/testing/framework/TestCommon.py b/testing/framework/TestCommon.py index 2cd3c490af..9a0cb8beb4 100644 --- a/testing/framework/TestCommon.py +++ b/testing/framework/TestCommon.py @@ -321,7 +321,7 @@ def must_be_writable(self, *files) -> None: them. Exits FAILED if any of the files does not exist or is not writable. """ - flist = [is_List(x) and os.path.join(*x) or x for x in files] + flist = [os.path.join(*x) if is_List(x) else x for x in files] existing, missing = separate_files(flist) unwritable = [x for x in existing if not is_writable(x)] if missing: @@ -531,7 +531,7 @@ def must_exist(self, *files, message: str = "") -> None: pathname will be constructed by concatenating them. Exits FAILED if any of the files does not exist. """ - flist = [is_List(x) and os.path.join(*x) or x for x in files] + flist = [os.path.join(*x) if is_List(x) else x for x in files] missing = [x for x in flist if not os.path.exists(x) and not os.path.islink(x)] if missing: print("Missing files: `%s'" % "', `".join(missing)) @@ -550,7 +550,7 @@ def must_exist_one_of(self, files, message: str = "") -> None: if is_List(x) or is_Tuple(x): xpath = os.path.join(*x) else: - xpath = is_Sequence(x) and os.path.join(x) or x + xpath = os.path.join(x) if is_Sequence(x) else x if glob.glob(xpath): return missing.append(xpath) @@ -669,7 +669,7 @@ def must_not_exist(self, *files) -> None: which case the pathname will be constructed by concatenating them. Exits FAILED if any of the files exists. """ - flist = [is_List(x) and os.path.join(*x) or x for x in files] + flist = [os.path.join(*x) if is_List(x) else x for x in files] existing = [x for x in flist if os.path.exists(x) or os.path.islink(x)] if existing: print("Unexpected files exist: `%s'" % "', `".join(existing)) @@ -688,7 +688,7 @@ def must_not_exist_any_of(self, files) -> None: if is_List(x) or is_Tuple(x): xpath = os.path.join(*x) else: - xpath = is_Sequence(x) and os.path.join(x) or x + xpath = os.path.join(x) if is_Sequence(x) else x if glob.glob(xpath): existing.append(xpath) if existing: @@ -719,7 +719,7 @@ def must_not_be_writable(self, *files) -> None: them. Exits FAILED if any of the files does not exist or is writable. """ - flist = [is_List(x) and os.path.join(*x) or x for x in files] + flist = [os.path.join(*x) if is_List(x) else x for x in files] existing, missing = separate_files(flist) writable = [file for file in existing if is_writable(file)] if missing: