[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:
Miss Islington (bot) 2025-09-13 20:29:50 +02:00 committed by GitHub
parent fbfbfdf33c
commit 18dfc1d6c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 0 deletions

View file

@ -322,6 +322,9 @@ def quote(s):
if not s:
return "''"
if not isinstance(s, str):
raise TypeError(f"expected string object, got {type(s).__name__!r}")
# Use bytes.translate() for performance
safe_chars = (b'%+,-./0123456789:=@'
b'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'

View file

@ -330,6 +330,7 @@ def testQuote(self):
unsafe = '"`$\\!' + unicode_sample
self.assertEqual(shlex.quote(''), "''")
self.assertEqual(shlex.quote(None), "''")
self.assertEqual(shlex.quote(safeunquoted), safeunquoted)
self.assertEqual(shlex.quote('test file name'), "'test file name'")
for u in unsafe:
@ -338,6 +339,8 @@ def testQuote(self):
for u in unsafe:
self.assertEqual(shlex.quote("test%s'name'" % u),
"'test%s'\"'\"'name'\"'\"''" % u)
self.assertRaises(TypeError, shlex.quote, 42)
self.assertRaises(TypeError, shlex.quote, b"abc")
def testJoin(self):
for split_command, command in [

View file

@ -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.