Commit ddfb2c3a authored by Victor Stinner's avatar Victor Stinner

Issue #9560: Use -b option of the file command in _syscmd_file()

Omit the filename to avoid enconding issues, especially with non encodable
characters in the Python full path.
parent 3060c457
...@@ -990,9 +990,8 @@ def _syscmd_file(target,default=''): ...@@ -990,9 +990,8 @@ def _syscmd_file(target,default=''):
""" Interface to the system's file command. """ Interface to the system's file command.
The function uses the -b option of the file command to have it The function uses the -b option of the file command to have it
ommit the filename in its output and if possible the -L option omit the filename in its output. Follow the symlinks. It returns
to have the command follow symlinks. It returns default in default in case the command should fail.
case the command should fail.
""" """
if sys.platform in ('dos','win32','win16','os2'): if sys.platform in ('dos','win32','win16','os2'):
...@@ -1000,7 +999,7 @@ def _syscmd_file(target,default=''): ...@@ -1000,7 +999,7 @@ def _syscmd_file(target,default=''):
return default return default
target = _follow_symlinks(target).replace('"', '\\"') target = _follow_symlinks(target).replace('"', '\\"')
try: try:
f = os.popen('file "%s" 2> %s' % (target, DEV_NULL)) f = os.popen('file -b "%s" 2> %s' % (target, DEV_NULL))
except (AttributeError,os.error): except (AttributeError,os.error):
return default return default
output = f.read().strip() output = f.read().strip()
...@@ -1020,8 +1019,6 @@ _default_architecture = { ...@@ -1020,8 +1019,6 @@ _default_architecture = {
'dos': ('','MSDOS'), 'dos': ('','MSDOS'),
} }
_architecture_split = re.compile(r'[\s,]').split
def architecture(executable=sys.executable,bits='',linkage=''): def architecture(executable=sys.executable,bits='',linkage=''):
""" Queries the given executable (defaults to the Python interpreter """ Queries the given executable (defaults to the Python interpreter
...@@ -1056,11 +1053,11 @@ def architecture(executable=sys.executable,bits='',linkage=''): ...@@ -1056,11 +1053,11 @@ def architecture(executable=sys.executable,bits='',linkage=''):
# Get data from the 'file' system command # Get data from the 'file' system command
if executable: if executable:
output = _syscmd_file(executable, '') fileout = _syscmd_file(executable, '')
else: else:
output = '' fileout = ''
if not output and \ if not fileout and \
executable == sys.executable: executable == sys.executable:
# "file" command did not return anything; we'll try to provide # "file" command did not return anything; we'll try to provide
# some sensible defaults then... # some sensible defaults then...
...@@ -1072,9 +1069,6 @@ def architecture(executable=sys.executable,bits='',linkage=''): ...@@ -1072,9 +1069,6 @@ def architecture(executable=sys.executable,bits='',linkage=''):
linkage = l linkage = l
return bits,linkage return bits,linkage
# Split the output into a list of strings omitting the filename
fileout = _architecture_split(output)[1:]
if 'executable' not in fileout: if 'executable' not in fileout:
# Format not supported # Format not supported
return bits,linkage return bits,linkage
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment