]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45221: Fix handling of LDFLAGS and CPPFLAGS options in setup.py (GH-29031)
authorandrei kulakov <andrei.avk@gmail.com>
Mon, 18 Oct 2021 18:26:23 +0000 (14:26 -0400)
committerGitHub <noreply@github.com>
Mon, 18 Oct 2021 18:26:23 +0000 (14:26 -0400)
Misc/NEWS.d/next/Build/2021-10-18-10-25-56.bpo-45221.rnulhf.rst [new file with mode: 0644]
setup.py

diff --git a/Misc/NEWS.d/next/Build/2021-10-18-10-25-56.bpo-45221.rnulhf.rst b/Misc/NEWS.d/next/Build/2021-10-18-10-25-56.bpo-45221.rnulhf.rst
new file mode 100644 (file)
index 0000000..cb981d9
--- /dev/null
@@ -0,0 +1,3 @@
+Fixed regression in handling of ``LDFLAGS`` and ``CPPFLAGS`` options
+where :meth:`argparse.parse_known_args` could interpret an option as
+one of the built-in command line argument, for example ``-h`` for help.
index 24365ef3d927ba68b618d263fe390d28a90687ac..5428cbde1cc9b0902dc5ab62d97202cd64b7168e 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -801,6 +801,18 @@ class PyBuildExt(build_ext):
             if env_val:
                 parser = argparse.ArgumentParser()
                 parser.add_argument(arg_name, dest="dirs", action="append")
+
+                # To prevent argparse from raising an exception about any
+                # options in env_val that it mistakes for known option, we
+                # strip out all double dashes and any dashes followed by a
+                # character that is not for the option we are dealing with.
+                #
+                # Please note that order of the regex is important!  We must
+                # strip out double-dashes first so that we don't end up with
+                # substituting "--Long" to "-Long" and thus lead to "ong" being
+                # used for a library directory.
+                env_val = re.sub(r'(^|\s+)-(-|(?!%s))' % arg_name[1],
+                                 ' ', env_val)
                 options, _ = parser.parse_known_args(env_val.split())
                 if options.dirs:
                     for directory in reversed(options.dirs):