]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-112438: Fix support of format units with the "e" prefix in nested tuples...
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 27 Nov 2023 18:11:09 +0000 (20:11 +0200)
committerGitHub <noreply@github.com>
Mon, 27 Nov 2023 18:11:09 +0000 (18:11 +0000)
(cherry picked from commit 4eea1e82369fbf7a795d1956e7a8212a1b58009f)

Lib/test/test_capi/test_getargs.py
Misc/NEWS.d/next/C API/2023-11-27-09-44-16.gh-issue-112438.GdNZiI.rst [new file with mode: 0644]
Python/getargs.c

index 27675533d1bb4b8f300949677b45f62cfcd76bb5..1ce1ec3b41c954d5c56aa1252675c4e856171a94 100644 (file)
@@ -1322,6 +1322,33 @@ class ParseTupleAndKeywords_Test(unittest.TestCase):
         with self.assertRaisesRegex(SystemError, 'Empty keyword'):
             parse((1,), {}, 'O|OO', ['', 'a', ''])
 
+    def test_nested_tuple(self):
+        parse = _testcapi.parse_tuple_and_keywords
+
+        parse(((1, 2, 3),), {}, '(OOO)', ['a'])
+        parse((1, (2, 3), 4), {}, 'O(OO)O', ['a', 'b', 'c'])
+        parse(((1, 2, 3),), {}, '(iii)', ['a'])
+
+        with self.assertRaisesRegex(TypeError,
+                "argument 1 must be sequence of length 2, not 3"):
+            parse(((1, 2, 3),), {}, '(ii)', ['a'])
+        with self.assertRaisesRegex(TypeError,
+                "argument 1 must be sequence of length 2, not 1"):
+            parse(((1,),), {}, '(ii)', ['a'])
+        with self.assertRaisesRegex(TypeError,
+                "argument 1 must be 2-item sequence, not int"):
+            parse((1,), {}, '(ii)', ['a'])
+        with self.assertRaisesRegex(TypeError,
+                "argument 1 must be 2-item sequence, not bytes"):
+            parse((b'ab',), {}, '(ii)', ['a'])
+
+        for f in 'es', 'et', 'es#', 'et#':
+            with self.assertRaises(LookupError):  # empty encoding ""
+                parse((('a',),), {}, '(' + f + ')', ['a'])
+            with self.assertRaisesRegex(TypeError,
+                    "argument 1 must be sequence of length 1, not 0"):
+                parse(((),), {}, '(' + f + ')', ['a'])
+
 
 class Test_testcapi(unittest.TestCase):
     locals().update((name, getattr(_testcapi, name))
diff --git a/Misc/NEWS.d/next/C API/2023-11-27-09-44-16.gh-issue-112438.GdNZiI.rst b/Misc/NEWS.d/next/C API/2023-11-27-09-44-16.gh-issue-112438.GdNZiI.rst
new file mode 100644 (file)
index 0000000..113119e
--- /dev/null
@@ -0,0 +1,2 @@
+Fix support of format units "es", "et", "es#", and "et#" in nested tuples in
+:c:func:`PyArg_ParseTuple`-like functions.
index 3105bd556c1bf1c49134a5d30918156d718ca824..e18d77199291612889bee7c79ff8f1a2b1090d7f 100644 (file)
@@ -522,7 +522,7 @@ converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
         }
         else if (c == ':' || c == ';' || c == '\0')
             break;
-        else if (level == 0 && Py_ISALPHA(c))
+        else if (level == 0 && Py_ISALPHA(c) && c != 'e')
             n++;
     }