diff options
| author | Victor Stinner <victor.stinner@haypocalc.com> | 2010-04-18 09:07:49 (GMT) |
|---|---|---|
| committer | Victor Stinner <victor.stinner@haypocalc.com> | 2010-04-18 09:07:49 (GMT) |
| commit | b2a0a434a83ef94f5b0b72ed1465be2ca967439e (patch) | |
| tree | 74f7d12cd9139c864aeff281c43be2f1af56f41b /Lib/platform.py | |
| parent | 24f09fdb230c346303aef15b25f31fdb8cb431db (diff) | |
| download | cpython-b2a0a434a83ef94f5b0b72ed1465be2ca967439e.zip cpython-b2a0a434a83ef94f5b0b72ed1465be2ca967439e.tar.gz cpython-b2a0a434a83ef94f5b0b72ed1465be2ca967439e.tar.bz2 | |
platform: use subprocess.Popen() instead of os.popen() in _syscmd_file()
* Popen() avoids ugly shell escape: target.replace('"', '\\"')
* Use proc.communicate() instead of f.stdout.read()
* Get output from stdout by splitting with ": " instead of splitting by spaces
to support filename with spaces
Diffstat (limited to 'Lib/platform.py')
| -rwxr-xr-x | Lib/platform.py | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/Lib/platform.py b/Lib/platform.py index 79695ba..495433d 100755 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -113,7 +113,7 @@ __copyright__ = """ __version__ = '1.0.7' -import sys,string,os,re +import sys, string, os, re, subprocess ### Globals & Constants @@ -966,13 +966,19 @@ def _syscmd_file(target,default=''): if sys.platform in ('dos','win32','win16','os2'): # XXX Others too ? return default - target = _follow_symlinks(target).replace('"', '\\"') + target = _follow_symlinks(target) try: - f = os.popen('file "%s" 2> %s' % (target, DEV_NULL)) + proc = subprocess.Popen( + ['file', target], + stdout=subprocess.PIPE, + stderr=open(DEV_NULL, 'wb')) except (AttributeError,os.error): return default - output = string.strip(f.read()) - rc = f.close() + stdout, stderr = proc.communicate() + stdout = stdout.rstrip(b'\n\r') + # get output from "filename: output" + output = stdout.split(b': ', 1)[-1] + rc = proc.wait() if not output or rc: return default else: @@ -988,8 +994,6 @@ _default_architecture = { 'dos': ('','MSDOS'), } -_architecture_split = re.compile(r'[\s,]').split - def architecture(executable=sys.executable,bits='',linkage=''): """ Queries the given executable (defaults to the Python interpreter @@ -1024,11 +1028,11 @@ def architecture(executable=sys.executable,bits='',linkage=''): # Get data from the 'file' system command if executable: - output = _syscmd_file(executable, '') + fileout = _syscmd_file(executable, '') else: - output = '' + fileout = '' - if not output and \ + if not fileout and \ executable == sys.executable: # "file" command did not return anything; we'll try to provide # some sensible defaults then... @@ -1040,9 +1044,6 @@ def architecture(executable=sys.executable,bits='',linkage=''): linkage = l return bits, linkage - # Split the output into a list of strings omitting the filename - fileout = _architecture_split(output)[1:] - if 'executable' not in fileout: # Format not supported return bits,linkage |
