Skip to content

Commit 220c0a8

Browse files
authored
gh-138804: Check type in shlex.quote (GH-138809)
1 parent 35e6138 commit 220c0a8

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

Lib/shlex.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@ def quote(s):
322322
if not s:
323323
return "''"
324324

325+
if not isinstance(s, str):
326+
raise TypeError(f"expected string object, got {type(s).__name__!r}")
327+
325328
# Use bytes.translate() for performance
326329
safe_chars = (b'%+,-./0123456789:=@'
327330
b'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'

Lib/test/test_shlex.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ def testQuote(self):
330330
unsafe = '"`$\\!' + unicode_sample
331331

332332
self.assertEqual(shlex.quote(''), "''")
333+
self.assertEqual(shlex.quote(None), "''")
333334
self.assertEqual(shlex.quote(safeunquoted), safeunquoted)
334335
self.assertEqual(shlex.quote('test file name'), "'test file name'")
335336
for u in unsafe:
@@ -338,6 +339,8 @@ def testQuote(self):
338339
for u in unsafe:
339340
self.assertEqual(shlex.quote("test%s'name'" % u),
340341
"'test%s'\"'\"'name'\"'\"''" % u)
342+
self.assertRaises(TypeError, shlex.quote, 42)
343+
self.assertRaises(TypeError, shlex.quote, b"abc")
341344

342345
def testJoin(self):
343346
for split_command, command in [
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Raise :exc:`TypeError` instead of :exc:`AttributeError` when an argument of
2+
incorrect type is passed to :func:`shlex.quote`. This restores the behavior of
3+
the function prior to 3.14.

0 commit comments

Comments
 (0)