summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2018-12-17 17:47:24 (GMT)
committerGitHub <noreply@github.com>2018-12-17 17:47:24 (GMT)
commit0af9c33262fb43f39a6558e3f155689e83e10706 (patch)
tree5cce3e7ae9bafb77c733a2ff2410c074c73d218d
parent94cf308ee231bfbfaa9ddc50b9764545a1318773 (diff)
downloadcpython-0af9c33262fb43f39a6558e3f155689e83e10706.zip
cpython-0af9c33262fb43f39a6558e3f155689e83e10706.tar.gz
cpython-0af9c33262fb43f39a6558e3f155689e83e10706.tar.bz2
bpo-35348: Fix platform.architecture() (GH-11159)
Make platform.architecture() parsing of "file" command output more reliable: * Add the "-b" option to the "file" command to omit the filename; * Force the usage of the C locale; * Search also the "shared object" pattern. Co-Authored-By: Serhiy Storchaka <storchaka@gmail.com>
-rwxr-xr-xLib/platform.py16
-rw-r--r--Misc/NEWS.d/next/Library/2018-12-14-13-27-45.bpo-35348.u3Y2an.rst3
2 files changed, 15 insertions, 4 deletions
diff --git a/Lib/platform.py b/Lib/platform.py
index 0fe841c..9dd3f47 100755
--- a/Lib/platform.py
+++ b/Lib/platform.py
@@ -608,13 +608,21 @@ def _syscmd_file(target, default=''):
import subprocess
target = _follow_symlinks(target)
+ # "file" output is locale dependent: force the usage of the C locale
+ # to get deterministic behavior.
+ env = dict(os.environ, LC_ALL='C')
try:
- output = subprocess.check_output(['file', target],
+ # -b: do not prepend filenames to output lines (brief mode)
+ output = subprocess.check_output(['file', '-b', target],
stderr=subprocess.DEVNULL,
- encoding='latin-1')
+ env=env)
except (OSError, subprocess.CalledProcessError):
return default
- return (output or default)
+ if not output:
+ return default
+ # With the C locale, the output should be mostly ASCII-compatible.
+ # Decode from Latin-1 to prevent Unicode decode error.
+ return output.decode('latin-1')
### Information about the used architecture
@@ -672,7 +680,7 @@ def architecture(executable=sys.executable, bits='', linkage=''):
linkage = l
return bits, linkage
- if 'executable' not in fileout:
+ if 'executable' not in fileout and 'shared object' not in fileout:
# Format not supported
return bits, linkage
diff --git a/Misc/NEWS.d/next/Library/2018-12-14-13-27-45.bpo-35348.u3Y2an.rst b/Misc/NEWS.d/next/Library/2018-12-14-13-27-45.bpo-35348.u3Y2an.rst
new file mode 100644
index 0000000..190db31
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-12-14-13-27-45.bpo-35348.u3Y2an.rst
@@ -0,0 +1,3 @@
+Make :func:`platform.architecture` parsing of ``file`` command output more
+reliable: add the ``-b`` option to the ``file`` command to omit the filename,
+force the usage of the C locale, and search also the "shared object" pattern.