Discussion:
[issue25567] shlex.quote doesn't work on bytestrings
Jonas Thiem
2015-11-06 12:52:23 UTC
Permalink
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 <***@bugs.python.org>
<http://bugs.python.org/issue25567>
_______________________________________
R. David Murray
2015-11-06 13:58:12 UTC
Permalink
This post might be inappropriate. Click to display it.
Nan Wu
2015-11-10 03:27:55 UTC
Permalink
Nan Wu added the comment:

Added a patch for support this in `quote` method. What is a good example or a group of examples to demonstrate the usage in the document?

----------
keywords: +patch
nosy: +Nan Wu
Added file: http://bugs.python.org/file40992/shlex_quote_bytes_support.patch

_______________________________________
Python tracker <***@bugs.python.org>
<http://bugs.python.org/issue25567>
_______________________________________

Loading...