mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[3.14] gh-138804: Check type in shlex.quote (GH-138809) (#138846)
Co-authored-by: Christoph Walcher <christoph-wa@gmx.de>
This commit is contained in:
parent
fbfbfdf33c
commit
18dfc1d6c4
3 changed files with 9 additions and 0 deletions
|
|
@ -322,6 +322,9 @@ def quote(s):
|
||||||
if not s:
|
if not s:
|
||||||
return "''"
|
return "''"
|
||||||
|
|
||||||
|
if not isinstance(s, str):
|
||||||
|
raise TypeError(f"expected string object, got {type(s).__name__!r}")
|
||||||
|
|
||||||
# Use bytes.translate() for performance
|
# Use bytes.translate() for performance
|
||||||
safe_chars = (b'%+,-./0123456789:=@'
|
safe_chars = (b'%+,-./0123456789:=@'
|
||||||
b'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'
|
b'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'
|
||||||
|
|
|
||||||
|
|
@ -330,6 +330,7 @@ def testQuote(self):
|
||||||
unsafe = '"`$\\!' + unicode_sample
|
unsafe = '"`$\\!' + unicode_sample
|
||||||
|
|
||||||
self.assertEqual(shlex.quote(''), "''")
|
self.assertEqual(shlex.quote(''), "''")
|
||||||
|
self.assertEqual(shlex.quote(None), "''")
|
||||||
self.assertEqual(shlex.quote(safeunquoted), safeunquoted)
|
self.assertEqual(shlex.quote(safeunquoted), safeunquoted)
|
||||||
self.assertEqual(shlex.quote('test file name'), "'test file name'")
|
self.assertEqual(shlex.quote('test file name'), "'test file name'")
|
||||||
for u in unsafe:
|
for u in unsafe:
|
||||||
|
|
@ -338,6 +339,8 @@ def testQuote(self):
|
||||||
for u in unsafe:
|
for u in unsafe:
|
||||||
self.assertEqual(shlex.quote("test%s'name'" % u),
|
self.assertEqual(shlex.quote("test%s'name'" % u),
|
||||||
"'test%s'\"'\"'name'\"'\"''" % u)
|
"'test%s'\"'\"'name'\"'\"''" % u)
|
||||||
|
self.assertRaises(TypeError, shlex.quote, 42)
|
||||||
|
self.assertRaises(TypeError, shlex.quote, b"abc")
|
||||||
|
|
||||||
def testJoin(self):
|
def testJoin(self):
|
||||||
for split_command, command in [
|
for split_command, command in [
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
Raise :exc:`TypeError` instead of :exc:`AttributeError` when an argument of
|
||||||
|
incorrect type is passed to :func:`shlex.quote`. This restores the behavior of
|
||||||
|
the function prior to 3.14.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue