gh-138804: Check type in shlex.quote (GH-138809)

This commit is contained in:
Christoph Walcher
2025-09-12 20:26:21 +02:00
committed by GitHub
parent 35e6138d1f
commit 220c0a8156
3 changed files with 9 additions and 0 deletions

View File

@@ -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_'

View File

@@ -330,6 +330,7 @@ class ShlexTest(unittest.TestCase):
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 @@ class ShlexTest(unittest.TestCase):
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 [

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.