Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/shlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_'
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_shlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 [
Expand Down
Original file line number Diff line number Diff line change
@@ -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