]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-138804: Check type in `shlex.quote` (GH-138809)
authorChristoph Walcher <christoph-wa@gmx.de>
Fri, 12 Sep 2025 18:26:21 +0000 (20:26 +0200)
committerGitHub <noreply@github.com>
Fri, 12 Sep 2025 18:26:21 +0000 (14:26 -0400)
Lib/shlex.py
Lib/test/test_shlex.py
Misc/NEWS.d/next/Library/2025-09-12-01-01-05.gh-issue-138804.46ZukT.rst [new file with mode: 0644]

index 5bf6e0d70e001210c5934c82383fd00bba36ca4d..5959f52dd12639de2afd6f2f1a6bc312ab794163 100644 (file)
@@ -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_'
index a13ddcb76b7bcb8ef2d8bb6eae094dc55e0e1210..2a355abdeeb30fbcf27a2ae6316c11def3b286dd 100644 (file)
@@ -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 (file)
index 0000000..5d403f8
--- /dev/null
@@ -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.