https://github.com/python/cpython/commit/18dfc1d6c4ba9e1cc8fd5c824095abc9bf6918c6 commit: 18dfc1d6c4ba9e1cc8fd5c824095abc9bf6918c6 branch: 3.14 author: Miss Islington (bot) <[email protected]> committer: hugovk <[email protected]> date: 2025-09-13T21:29:50+03:00 summary:
[3.14] gh-138804: Check type in `shlex.quote` (GH-138809) (#138846) Co-authored-by: Christoph Walcher <[email protected]> files: A Misc/NEWS.d/next/Library/2025-09-12-01-01-05.gh-issue-138804.46ZukT.rst M Lib/shlex.py M Lib/test/test_shlex.py diff --git a/Lib/shlex.py b/Lib/shlex.py index 5bf6e0d70e0012..5959f52dd12639 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 a13ddcb76b7bcb..2a355abdeeb30f 100644 --- a/Lib/test/test_shlex.py +++ b/Lib/test/test_shlex.py @@ -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 [ 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 00000000000000..5d403f8cf034ad --- /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. _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
