From ea71fbcd46a67538d0129e53f9ce847e447a72aa Mon Sep 17 00:00:00 2001 From: Jacob Champion Date: Wed, 19 Jul 2023 11:08:56 -0700 Subject: [PATCH 1/2] setup.py: handle more corner cases for pg_config - Differentiate between unexpected empty values and execution failure. - Accept empty --cppflags and --ldflags output. Fixes #1599. - Accept UTF-8 output from pg_config, for alternative client locales. --- setup.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/setup.py b/setup.py index 9561a9f7..7c9c7091 100644 --- a/setup.py +++ b/setup.py @@ -105,24 +105,23 @@ For further information please check the 'doc/src/install.rst' file (also at """) sys.exit(1) - def query(self, attr_name): + def query(self, attr_name, *, empty_ok=False): """Spawn the pg_config executable, querying for the given config name, and return the printed value, sanitized. """ try: - pg_config_process = subprocess.Popen( + pg_config_process = subprocess.run( [self.pg_config_exe, "--" + attr_name], - stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except OSError: raise Warning( f"Unable to find 'pg_config' file in '{self.pg_config_exe}'") - pg_config_process.stdin.close() - result = pg_config_process.stdout.readline().strip() - if not result: - raise Warning(pg_config_process.stderr.readline()) - if not isinstance(result, str): - result = result.decode('ascii') + if pg_config_process.returncode: + err = pg_config_process.stderr.decode(errors='backslashreplace') + raise Warning(f"pg_config --{attr_name} failed: {err}") + result = pg_config_process.stdout.decode().strip() + if not result and not empty_ok: + raise Warning(f"pg_config --{attr_name} is empty") return result def find_on_path(self, exename, path_directories=None): @@ -378,12 +377,14 @@ For further information please check the 'doc/src/install.rst' file (also at self.include_dirs.append(pg_config_helper.query("includedir")) self.include_dirs.append(pg_config_helper.query("includedir-server")) - # add includedirs from cppflags, libdirs from ldflags - for token in pg_config_helper.query("ldflags").split(): + # if present, add includedirs from cppflags, libdirs from ldflags + tokens = pg_config_helper.query("ldflags", empty_ok=True).split() + for token in tokens: if token.startswith("-L"): self.library_dirs.append(token[2:]) - for token in pg_config_helper.query("cppflags").split(): + tokens = pg_config_helper.query("cppflags", empty_ok=True).split() + for token in tokens: if token.startswith("-I"): self.include_dirs.append(token[2:]) From ef7053c070d6752ca14cdc8d498eeb9be2c7b4a1 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Fri, 4 Aug 2023 16:48:11 +0100 Subject: [PATCH 2/2] docs: add pg_config improvement to news file --- NEWS | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS b/NEWS index 24518e7d..841e051c 100644 --- a/NEWS +++ b/NEWS @@ -4,6 +4,7 @@ Current release What's new in psycopg 2.9.7 (unreleased) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +- Fix building when pg_config returns an empty string (:ticket:`#1599`). - Wheel package compiled against OpenSSL 1.1.1v.