1818from . import string as pg_str
1919
2020# Get the output from the given command.
21- # *args are transformed into "long options", '--' + x
22- def get_command_output (exe , * args ):
21+ # Variable arguments are transformed into "long options", '--' + x
22+ def get_command_output (exe , * args , encoding = 'utf-8' , timeout = 8 ):
2323 pa = list (exe ) + [
2424 '--' + x .strip () for x in args if x is not None
2525 ]
2626 p = subprocess .Popen (pa ,
2727 close_fds = close_fds ,
2828 stdout = subprocess .PIPE ,
29- stderr = subprocess . PIPE ,
30- stdin = subprocess . PIPE ,
29+ stderr = None ,
30+ stdin = None ,
3131 shell = False
3232 )
33- p .stdin .close ()
34- p .stderr .close ()
35- while True :
36- try :
37- rv = p .wait ()
38- break
39- except OSError as e :
40- if e .errno != errno .EINTR :
41- raise
42- if rv != 0 :
33+
34+ try :
35+ stdout , stderr = p .communicate (timeout = timeout )
36+ except subprocess .TimeoutExpired :
37+ p .kill ()
38+ stdout , stderr = p .communicate (timeout = 2 )
39+
40+ if p .returncode != 0 :
4341 return None
44- with p .stdout , io .TextIOWrapper (p .stdout ) as txt :
45- return txt .read ()
4642
47- def pg_config_dictionary (* pg_config_path ):
43+ return stdout .decode (encoding )
44+
45+ def pg_config_dictionary (* pg_config_path , encoding = 'utf-8' , timeout = 8 ):
4846 """
4947 Create a dictionary of the information available in the given
5048 pg_config_path. This provides a one-shot solution to fetching information
5149 from the pg_config binary. Returns a dictionary object.
5250 """
53- default_output = get_command_output (pg_config_path )
51+ default_output = get_command_output (pg_config_path , encoding = encoding , timeout = timeout )
5452 if default_output is not None :
5553 d = {}
5654 for x in default_output .splitlines ():
@@ -67,7 +65,7 @@ def pg_config_dictionary(*pg_config_path):
6765 # Second, all the -- options except version.
6866 # Third, --version as it appears to be exclusive in some cases.
6967 opt = []
70- for l in get_command_output (pg_config_path , 'help' ).splitlines ():
68+ for l in get_command_output (pg_config_path , 'help' , encoding = encoding , timeout = timeout ).splitlines ():
7169 dash_pos = l .find ('--' )
7270 if dash_pos == - 1 :
7371 continue
@@ -79,8 +77,8 @@ def pg_config_dictionary(*pg_config_path):
7977 if 'version' in opt :
8078 opt .remove ('version' )
8179
82- d = dict (zip (opt , get_command_output (pg_config_path , * opt ).splitlines ()))
83- d ['version' ] = get_command_output (pg_config_path , 'version' ).strip ()
80+ d = dict (zip (opt , get_command_output (pg_config_path , * opt , encoding = encoding , timeout = timeout ).splitlines ()))
81+ d ['version' ] = get_command_output (pg_config_path , 'version' , encoding = encoding , timeout = timeout ).strip ()
8482 return d
8583
8684##
0 commit comments