Skip to content

Commit ea71fbc

Browse files
jchampiodvarrazzo
authored andcommitted
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.
1 parent 0c5b5f4 commit ea71fbc

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

setup.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,23 @@ def __init__(self, build_ext):
105105
""")
106106
sys.exit(1)
107107

108-
def query(self, attr_name):
108+
def query(self, attr_name, *, empty_ok=False):
109109
"""Spawn the pg_config executable, querying for the given config
110110
name, and return the printed value, sanitized. """
111111
try:
112-
pg_config_process = subprocess.Popen(
112+
pg_config_process = subprocess.run(
113113
[self.pg_config_exe, "--" + attr_name],
114-
stdin=subprocess.PIPE,
115114
stdout=subprocess.PIPE,
116115
stderr=subprocess.PIPE)
117116
except OSError:
118117
raise Warning(
119118
f"Unable to find 'pg_config' file in '{self.pg_config_exe}'")
120-
pg_config_process.stdin.close()
121-
result = pg_config_process.stdout.readline().strip()
122-
if not result:
123-
raise Warning(pg_config_process.stderr.readline())
124-
if not isinstance(result, str):
125-
result = result.decode('ascii')
119+
if pg_config_process.returncode:
120+
err = pg_config_process.stderr.decode(errors='backslashreplace')
121+
raise Warning(f"pg_config --{attr_name} failed: {err}")
122+
result = pg_config_process.stdout.decode().strip()
123+
if not result and not empty_ok:
124+
raise Warning(f"pg_config --{attr_name} is empty")
126125
return result
127126

128127
def find_on_path(self, exename, path_directories=None):
@@ -378,12 +377,14 @@ def finalize_options(self):
378377
self.include_dirs.append(pg_config_helper.query("includedir"))
379378
self.include_dirs.append(pg_config_helper.query("includedir-server"))
380379

381-
# add includedirs from cppflags, libdirs from ldflags
382-
for token in pg_config_helper.query("ldflags").split():
380+
# if present, add includedirs from cppflags, libdirs from ldflags
381+
tokens = pg_config_helper.query("ldflags", empty_ok=True).split()
382+
for token in tokens:
383383
if token.startswith("-L"):
384384
self.library_dirs.append(token[2:])
385385

386-
for token in pg_config_helper.query("cppflags").split():
386+
tokens = pg_config_helper.query("cppflags", empty_ok=True).split()
387+
for token in tokens:
387388
if token.startswith("-I"):
388389
self.include_dirs.append(token[2:])
389390

0 commit comments

Comments
 (0)