Skip to content

platform.platform() crashes in certain environments due to decode() on str in zoneinfo._syscmd_file() #137522

@zgx949

Description

@zgx949

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')
Image

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

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directory

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions