[New-bugs-announce] [issue25567] shlex.quote doesn't work on bytestrings

Jonas Thiem report at bugs.python.org
Fri Nov 6 07:52:23 EST 2015

New submission from Jonas Thiem:


>>> import shlex
>>> shlex.quote(b"abc")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.4/shlex.py", line 285, in quote
    if _find_unsafe(s) is None:
TypeError: can't use a string pattern on a bytes-like object

Your question is now probably, why would anyone not want to use unicode strings here?

The reason is that for some operations (e.g. file access to some known paths) decoding and encoding from/to any sort of unicode interpretation can be lossy, specifically when the file path on the filesystem has broken/mixed encoding characters. In such a case, the shell command might need to be supplied as bytestring to ensure it is sent exactly as-is so such broken files can still be dealt with, without the Unicode interpretation possibly deforming the path in some bytes.

Since shlex.quote seems targeted at shell usage, it should therefore support this.

components: Library (Lib)
messages: 254186
nosy: Jonas Thiem, The Compiler
priority: normal
severity: normal
status: open
title: shlex.quote doesn't work on bytestrings
type: behavior
versions: Python 3.4

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list