[Python-checkins] r80166 - python/trunk/Lib/platform.py (original) (raw)
M.-A. Lemburg mal at egenix.com
Sun Apr 18 13:35:16 CEST 2010
- Previous message: [Python-checkins] r80166 - python/trunk/Lib/platform.py
- Next message: [Python-checkins] r80166 - python/trunk/Lib/platform.py
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
victor.stinner wrote:
Author: victor.stinner Date: Sun Apr 18 11:07:49 2010 New Revision: 80166
Log: platform: use subprocess.Popen() instead of os.popen() in syscmdfile() * 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
Viktor, before making such changes to platform.py, please coordinate with me, as I am the maintainer of that module.
The subprocess change is not OK, since platform.py has to stay compatible with Python 2.3 which doesn't have subprocess. Please either revert the change or make it fallback to os.popen().
Modified: python/trunk/Lib/platform.py
Modified: python/trunk/Lib/platform.py ============================================================================== --- python/trunk/Lib/platform.py (original) +++ python/trunk/Lib/platform.py Sun Apr 18 11:07:49 2010 @@ -113,7 +113,7 @@ version = '1.0.7' -import sys,string,os,re +import sys, string, os, re, subprocess ### Globals & Constants @@ -966,13 +966,19 @@ if sys.platform in ('dos','win32','win16','os2'): # XXX Others too ? return default - target = followsymlinks(target).replace('"', '\"') + target = followsymlinks(target) try: - f = os.popen('file "%s" 2> %s' % (target, DEVNULL)) + proc = subprocess.Popen( + ['file', target], + stdout=subprocess.PIPE, + stderr=open(DEVNULL, '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 @@ 'dos': ('','MSDOS'), } -architecturesplit = 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 @@ # Get data from the 'file' system command if executable: - output = syscmdfile(executable, '') + fileout = syscmdfile(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 @@ linkage = l return bits, linkage - # Split the output into a list of strings omitting the filename - fileout = architecturesplit(output)[1:] - if 'executable' not in fileout: # Format not supported return bits,linkage
-- Marc-Andre Lemburg eGenix.com
Professional Python Services directly from the Source (#1, Apr 18 2010)
Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/
- Previous message: [Python-checkins] r80166 - python/trunk/Lib/platform.py
- Next message: [Python-checkins] r80166 - python/trunk/Lib/platform.py
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]