diff options
author | Mats Wichmann <mats@linux.com> | 2022-03-03 17:46:19 (GMT) |
---|---|---|
committer | Mats Wichmann <mats@linux.com> | 2022-03-03 18:11:45 (GMT) |
commit | 778e5f341584b00c0ccde052505a13ffa68dc1d9 (patch) | |
tree | 81256e6ad6134fada6e175f22bb5327025bd79ee /SCons/Scanner | |
parent | b16f31c99f8828e0ef8fa47d28718e114e81c8ba (diff) | |
download | SCons-778e5f341584b00c0ccde052505a13ffa68dc1d9.zip SCons-778e5f341584b00c0ccde052505a13ffa68dc1d9.tar.gz SCons-778e5f341584b00c0ccde052505a13ffa68dc1d9.tar.bz2 |
Fix dictify_CPPDEFINES exeception
In converting CPPDEFINES to a dict, if an element is a single-item
sequence c, it would take an IndexError trying to access c[1]. This
could happen if AppendUnique has been called as it converts to tuples.
Fixes #4108
Signed-off-by: Mats Wichmann <mats@linux.com>
Diffstat (limited to 'SCons/Scanner')
-rw-r--r-- | SCons/Scanner/C.py | 9 | ||||
-rw-r--r-- | SCons/Scanner/CTests.py | 12 |
2 files changed, 19 insertions, 2 deletions
diff --git a/SCons/Scanner/C.py b/SCons/Scanner/C.py index 8506fe6..a066104 100644 --- a/SCons/Scanner/C.py +++ b/SCons/Scanner/C.py @@ -61,7 +61,8 @@ class SConsCPPScanner(SCons.cpp.PreProcessor): self.missing.append((file, self.current_file)) return '' -def dictify_CPPDEFINES(env): +def dictify_CPPDEFINES(env) -> dict: + """Returns CPPDEFINES converted to a dict.""" cppdefines = env.get('CPPDEFINES', {}) if cppdefines is None: return {} @@ -69,7 +70,11 @@ def dictify_CPPDEFINES(env): result = {} for c in cppdefines: if SCons.Util.is_Sequence(c): - result[c[0]] = c[1] + try: + result[c[0]] = c[1] + except IndexError: + # it could be a one-item sequence + result[c[0]] = None else: result[c] = None return result diff --git a/SCons/Scanner/CTests.py b/SCons/Scanner/CTests.py index 67ff312..14e156e 100644 --- a/SCons/Scanner/CTests.py +++ b/SCons/Scanner/CTests.py @@ -490,6 +490,17 @@ class CConditionalScannerTestCase3(unittest.TestCase): headers = ['d1/f1.h'] deps_match(self, deps, headers) +class dictify_CPPDEFINESTestCase(unittest.TestCase): + def runTest(self): + """Make sure single-item tuples convert correctly. + + This is a regression test: AppendUnique turns sequences into + lists of tuples, and dictify could gack on these. + """ + env = DummyEnvironment(CPPDEFINES=(("VALUED_DEFINE", 1), ("UNVALUED_DEFINE", ))) + d = SCons.Scanner.C.dictify_CPPDEFINES(env) + expect = {'VALUED_DEFINE': 1, 'UNVALUED_DEFINE': None} + assert d == expect def suite(): suite = unittest.TestSuite() @@ -510,6 +521,7 @@ def suite(): suite.addTest(CConditionalScannerTestCase1()) suite.addTest(CConditionalScannerTestCase2()) suite.addTest(CConditionalScannerTestCase3()) + suite.addTest(dictify_CPPDEFINESTestCase()) return suite if __name__ == "__main__": |