From b365592777245343156e701d311ae5a1ecd10fb5 Mon Sep 17 00:00:00 2001 From: Erwan Le Pape Date: Mon, 17 Sep 2018 18:47:19 +0200 Subject: [PATCH 1/2] bpo-34689: _parse_makefile no longer expands $${} The sysconfig._parse_makefile function expanded $${} formatted text if another variable was present in a value. This change splits values on $$ before searching for variables to avoid that. --- Lib/sysconfig.py | 28 +++++++++++++------ Lib/test/test_sysconfig.py | 2 ++ .../2018-09-17-19-11-17.bpo-34689.RtyOE3.rst | 2 ++ 3 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-09-17-19-11-17.bpo-34689.RtyOE3.rst diff --git a/Lib/sysconfig.py b/Lib/sysconfig.py index e0f9c18531b674..d3b4f300577808 100644 --- a/Lib/sysconfig.py +++ b/Lib/sysconfig.py @@ -254,12 +254,21 @@ def _parse_makefile(filename, vars=None): while len(variables) > 0: for name in tuple(variables): value = notdone[name] - m1 = _findvar1_rx.search(value) - m2 = _findvar2_rx.search(value) - if m1 and m2: - m = m1 if m1.start() < m2.start() else m2 - else: - m = m1 if m1 else m2 + m = None + offset = 0 + for substr in value.split('$$'): + m1 = _findvar1_rx.search(substr) + m2 = _findvar2_rx.search(substr) + if m1 and m2: + m = m1 if m1.start() < m2.start() else m2 + else: + m = m1 if m1 else m2 + if m is not None: + break + + # Add 2 to account for the $$ which were removed by split('$$') + offset += 2 + len(substr) + if m is not None: n = m.group(1) found = True @@ -287,11 +296,12 @@ def _parse_makefile(filename, vars=None): done[n] = item = "" if found: - after = value[m.end():] - value = value[:m.start()] + item + after - if "$" in after: + after = value[offset + m.end():] + value = value[:offset + m.start()] + item.replace('$', '$$') + after + if "$" in after.replace('$$', ''): notdone[name] = value else: + value = value.replace('$$', '$') try: value = int(value) except ValueError: diff --git a/Lib/test/test_sysconfig.py b/Lib/test/test_sysconfig.py index 20252be0a41b00..2ce0e6064d8c10 100644 --- a/Lib/test/test_sysconfig.py +++ b/Lib/test/test_sysconfig.py @@ -419,6 +419,7 @@ def test_parse_makefile(self): print("var5=dollar$$5", file=makefile) print("var6=${var3}/lib/python3.5/config-$(VAR2)$(var5)" "-x86_64-linux-gnu", file=makefile) + print("var7=$${ORIGIN}${var5}", file=makefile) vars = sysconfig._parse_makefile(TESTFN) self.assertEqual(vars, { 'var1': 'ab42', @@ -427,6 +428,7 @@ def test_parse_makefile(self): 'var4': '$/invalid', 'var5': 'dollar$5', 'var6': '42/lib/python3.5/config-b42dollar$5-x86_64-linux-gnu', + 'var7': '${ORIGIN}dollar$5', }) diff --git a/Misc/NEWS.d/next/Library/2018-09-17-19-11-17.bpo-34689.RtyOE3.rst b/Misc/NEWS.d/next/Library/2018-09-17-19-11-17.bpo-34689.RtyOE3.rst new file mode 100644 index 00000000000000..878a6ccdf766a4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-09-17-19-11-17.bpo-34689.RtyOE3.rst @@ -0,0 +1,2 @@ +Split strings prior to parsing in :func:`sysconfig._parse_makefile` so it +doesn't expand variables formatted as ``$${variable}``. From e15e47c724bd861d0a91411408f9650f5e8c1bb7 Mon Sep 17 00:00:00 2001 From: Erwan Le Pape Date: Mon, 6 Jul 2020 21:38:33 +0200 Subject: [PATCH 2/2] Fix formatting issues --- Lib/sysconfig.py | 3 ++- .../next/Library/2018-09-17-19-11-17.bpo-34689.RtyOE3.rst | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/sysconfig.py b/Lib/sysconfig.py index 08e0f06be6da67..f3198d666080b0 100644 --- a/Lib/sysconfig.py +++ b/Lib/sysconfig.py @@ -302,7 +302,8 @@ def _parse_makefile(filename, vars=None): if found: after = value[offset + m.end():] - value = value[:offset + m.start()] + item.replace('$', '$$') + after + value = value[:offset + m.start()] + \ + item.replace('$', '$$') + after if "$" in after.replace('$$', ''): notdone[name] = value else: diff --git a/Misc/NEWS.d/next/Library/2018-09-17-19-11-17.bpo-34689.RtyOE3.rst b/Misc/NEWS.d/next/Library/2018-09-17-19-11-17.bpo-34689.RtyOE3.rst index 878a6ccdf766a4..1751dcb7bb77a6 100644 --- a/Misc/NEWS.d/next/Library/2018-09-17-19-11-17.bpo-34689.RtyOE3.rst +++ b/Misc/NEWS.d/next/Library/2018-09-17-19-11-17.bpo-34689.RtyOE3.rst @@ -1,2 +1,2 @@ -Split strings prior to parsing in :func:`sysconfig._parse_makefile` so it -doesn't expand variables formatted as ``$${variable}``. +Split strings prior to parsing in `sysconfig._parse_makefile` so it doesn't +expand variables formatted as ``$${variable}``.