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.
This commit is contained in:
Jacob Champion 2023-07-19 11:08:56 -07:00 committed by Daniele Varrazzo
parent 0c5b5f4ec3
commit ea71fbcd46

View File

@ -105,24 +105,23 @@ For further information please check the 'doc/src/install.rst' file (also at
""") """)
sys.exit(1) 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 """Spawn the pg_config executable, querying for the given config
name, and return the printed value, sanitized. """ name, and return the printed value, sanitized. """
try: try:
pg_config_process = subprocess.Popen( pg_config_process = subprocess.run(
[self.pg_config_exe, "--" + attr_name], [self.pg_config_exe, "--" + attr_name],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
except OSError: except OSError:
raise Warning( raise Warning(
f"Unable to find 'pg_config' file in '{self.pg_config_exe}'") f"Unable to find 'pg_config' file in '{self.pg_config_exe}'")
pg_config_process.stdin.close() if pg_config_process.returncode:
result = pg_config_process.stdout.readline().strip() err = pg_config_process.stderr.decode(errors='backslashreplace')
if not result: raise Warning(f"pg_config --{attr_name} failed: {err}")
raise Warning(pg_config_process.stderr.readline()) result = pg_config_process.stdout.decode().strip()
if not isinstance(result, str): if not result and not empty_ok:
result = result.decode('ascii') raise Warning(f"pg_config --{attr_name} is empty")
return result return result
def find_on_path(self, exename, path_directories=None): 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"))
self.include_dirs.append(pg_config_helper.query("includedir-server")) self.include_dirs.append(pg_config_helper.query("includedir-server"))
# add includedirs from cppflags, libdirs from ldflags # if present, add includedirs from cppflags, libdirs from ldflags
for token in pg_config_helper.query("ldflags").split(): tokens = pg_config_helper.query("ldflags", empty_ok=True).split()
for token in tokens:
if token.startswith("-L"): if token.startswith("-L"):
self.library_dirs.append(token[2:]) 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"): if token.startswith("-I"):
self.include_dirs.append(token[2:]) self.include_dirs.append(token[2:])