-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Closed as not planned
Closed as not planned
Copy link
Labels
stdlibPython modules in the Lib dirPython modules in the Lib dir
Description
Bug report
Bug description:
def _syscmd_file(target, default=''):
""" Interface to the system's file command.
The function uses the -b option of the file command to have it
omit the filename in its output. Follow the symlinks. It returns
default in case the command should fail.
"""
if sys.platform in {'dos', 'win32', 'win16', 'ios', 'tvos', 'watchos'}:
# XXX Others too ?
return default
try:
import subprocess
except ImportError:
return default
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:
# -b: do not prepend filenames to output lines (brief mode)
output = subprocess.check_output(['file', '-b', target],
stderr=subprocess.DEVNULL,
env=env)
except (OSError, subprocess.CalledProcessError):
return 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')

When using dashscope in some environments (e.g. Python where subprocess returns str instead of bytes), calling platform.platform() internally causes a crash:
AttributeError: 'str' object has no attribute 'decode'. Did you mean: 'encode'?
This is due to the implementation of _syscmd_file() inside zoneinfo/plateform.py:
output = subprocess.check_output([...])
return output.decode('latin-1') # <-- fails if output is str
In some environments (possibly patched or wrapped subprocess), check_output returns str, so .decode() raises an exception.
Suggested fix: Check type before decoding:
if isinstance(output, bytes):
return output.decode('latin-1')
return output
Would be great if dashscope SDK can gracefully handle this, or avoid relying on platform.platform() in performance-critical paths.
Thanks!
CPython versions tested on:
3.13
Operating systems tested on:
macOS
Metadata
Metadata
Assignees
Labels
stdlibPython modules in the Lib dirPython modules in the Lib dir