]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41361: Optimized argument parsing for deque_rotate (GH-24796)
authorDennis Sweeney <36520290+sweeneyde@users.noreply.github.com>
Tue, 16 Mar 2021 04:02:25 +0000 (00:02 -0400)
committerGitHub <noreply@github.com>
Tue, 16 Mar 2021 04:02:25 +0000 (21:02 -0700)
Misc/NEWS.d/next/Library/2021-03-13-08-18-01.bpo-41361.lXDIlr.rst [new file with mode: 0644]
Modules/_collectionsmodule.c

diff --git a/Misc/NEWS.d/next/Library/2021-03-13-08-18-01.bpo-41361.lXDIlr.rst b/Misc/NEWS.d/next/Library/2021-03-13-08-18-01.bpo-41361.lXDIlr.rst
new file mode 100644 (file)
index 0000000..19e08f8
--- /dev/null
@@ -0,0 +1 @@
+:meth:`~collections.deque.rotate` calls are now slightly faster due to faster argument parsing.
\ No newline at end of file
index 90bafb0ea86d9293b2f654cb40ac36cce7d36507..ca63f710cd8648c26d527f624a7b9d6a026c3e6a 100644 (file)
@@ -880,9 +880,20 @@ deque_rotate(dequeobject *deque, PyObject *const *args, Py_ssize_t nargs)
 {
     Py_ssize_t n=1;
 
-    if (!_PyArg_ParseStack(args, nargs, "|n:rotate", &n)) {
+    if (!_PyArg_CheckPositional("deque.rotate", nargs, 0, 1)) {
         return NULL;
     }
+    if (nargs) {
+        PyObject *index = _PyNumber_Index(args[0]);
+        if (index == NULL) {
+            return NULL;
+        }
+        n = PyLong_AsSsize_t(index);
+        Py_DECREF(index);
+        if (n == -1 && PyErr_Occurred()) {
+            return NULL;
+        }
+    }
 
     if (!_deque_rotate(deque, n))
         Py_RETURN_NONE;