119119
120120### Globals & Constants
121121
122- # Determine the platform's /dev/null device
123- try :
124- DEV_NULL = os .devnull
125- except AttributeError :
126- # os.devnull was added in Python 2.4, so emulate it for earlier
127- # Python versions
128- if sys .platform in ('dos' , 'win32' , 'win16' ):
129- # Use the old CP/M NUL as device name
130- DEV_NULL = 'NUL'
131- else :
132- # Standard Unix uses /dev/null
133- DEV_NULL = '/dev/null'
134-
135122# Helper for comparing two version number strings.
136123# Based on the description of the PHP's version_compare():
137124# http://php.net/manual/en/function.version-compare.php
@@ -288,16 +275,15 @@ def _syscmd_ver(system='', release='', version='',
288275 return system , release , version
289276
290277 # Try some common cmd strings
278+ import subprocess
291279 for cmd in ('ver' , 'command /c ver' , 'cmd /c ver' ):
292280 try :
293- pipe = os .popen (cmd )
294- info = pipe .read ()
295- if pipe .close ():
296- raise OSError ('command failed' )
297- # XXX How can I suppress shell errors from being written
298- # to stderr ?
299- except OSError as why :
300- #print 'Command %s failed: %s' % (cmd, why)
281+ info = subprocess .check_output (cmd ,
282+ stderr = subprocess .DEVNULL ,
283+ text = True ,
284+ shell = True )
285+ except (OSError , subprocess .CalledProcessError ) as why :
286+ #print('Command %s failed: %s' % (cmd, why))
301287 continue
302288 else :
303289 break
@@ -602,16 +588,15 @@ def _syscmd_uname(option, default=''):
602588 if sys .platform in ('dos' , 'win32' , 'win16' ):
603589 # XXX Others too ?
604590 return default
591+
592+ import subprocess
605593 try :
606- f = os .popen ('uname %s 2> %s' % (option , DEV_NULL ))
607- except (AttributeError , OSError ):
608- return default
609- output = f .read ().strip ()
610- rc = f .close ()
611- if not output or rc :
594+ output = subprocess .check_output (('uname' , option ),
595+ stderr = subprocess .DEVNULL ,
596+ text = True )
597+ except (OSError , subprocess .CalledProcessError ):
612598 return default
613- else :
614- return output
599+ return (output .strip () or default )
615600
616601def _syscmd_file (target , default = '' ):
617602
@@ -629,17 +614,12 @@ def _syscmd_file(target, default=''):
629614 import subprocess
630615 target = _follow_symlinks (target )
631616 try :
632- proc = subprocess .Popen (['file' , target ],
633- stdout = subprocess .PIPE ,
634- stderr = subprocess . STDOUT )
635- except (AttributeError , OSError ):
617+ output = subprocess .check_output (['file' , target ],
618+ stderr = subprocess .DEVNULL ,
619+ encoding = 'latin-1' )
620+ except (OSError , subprocess . CalledProcessError ):
636621 return default
637- output = proc .communicate ()[0 ].decode ('latin-1' )
638- rc = proc .wait ()
639- if not output or rc :
640- return default
641- else :
642- return output
622+ return (output or default )
643623
644624### Information about the used architecture
645625
0 commit comments