From: Christoph Walcher Date: Fri, 12 Sep 2025 18:26:21 +0000 (+0200) Subject: gh-138804: Check type in `shlex.quote` (GH-138809) X-Git-Tag: v3.15.0a1~392 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=220c0a8156ad5d33a85fda3cc2b80a3e3d424195;p=thirdparty%2FPython%2Fcpython.git gh-138804: Check type in `shlex.quote` (GH-138809) --- diff --git a/Lib/shlex.py b/Lib/shlex.py index 5bf6e0d70e00..5959f52dd126 100644 --- a/Lib/shlex.py +++ b/Lib/shlex.py @@ -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_' diff --git a/Lib/test/test_shlex.py b/Lib/test/test_shlex.py index a13ddcb76b7b..2a355abdeeb3 100644 --- a/Lib/test/test_shlex.py +++ b/Lib/test/test_shlex.py @@ -330,6 +330,7 @@ class ShlexTest(unittest.TestCase): 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 @@ class ShlexTest(unittest.TestCase): 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 [ diff --git a/Misc/NEWS.d/next/Library/2025-09-12-01-01-05.gh-issue-138804.46ZukT.rst b/Misc/NEWS.d/next/Library/2025-09-12-01-01-05.gh-issue-138804.46ZukT.rst new file mode 100644 index 000000000000..5d403f8cf034 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-09-12-01-01-05.gh-issue-138804.46ZukT.rst @@ -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.