Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-50333: Deprecate support of non-tuple sequences in PyArg_ParseTuple()#128374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
serhiy-storchaka merged 9 commits into python:main from serhiy-storchaka:pyarg-deprecate-sequencesApr 8, 2025
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
9 commits Select commit Hold shift + click to select a range
6ad1ff6 gh-50333: Deprecate support of non-tuple sequences in PyArg_ParseTuple()
serhiy-storchaka 9878b37 Apply suggestions from code review
serhiy-storchaka f8bc0e0 Merge branch 'main' into pyarg-deprecate-sequences
serhiy-storchaka e7198bc Duplicate "a borrowed".
serhiy-storchaka fac1989 Re-order format units.
serhiy-storchaka ab1ea18 Update Python/getargs.c
serhiy-storchaka 44fd700 Update docs.
serhiy-storchaka 82ee6d9 Merge branch 'main' into pyarg-deprecate-sequences
serhiy-storchaka d700cad Minimize refcount changes.
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions 5 Misc/NEWS.d/next/C_API/2024-12-31-15-28-14.gh-issue-50333.KxQUXa.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Non-tuple sequences are deprecated as argument for the ``(items)`` format | ||
| unit in :c:func:`PyArg_ParseTuple` and other :ref:`argument parsing | ||
| <arg-parsing>` functions if *items* contains format units which store | ||
| a :ref:`borrowed buffer <c-arg-borrowed-buffer>` or | ||
| a :term:`borrowed reference`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -466,6 +466,8 @@ converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags, | ||
| const char *format = *p_format; | ||
| int i; | ||
| Py_ssize_t len; | ||
| int istuple = PyTuple_Check(arg); | ||
| int mustbetuple = istuple; | ||
| for (;){ | ||
| int c = *format++; | ||
| @@ -481,51 +483,104 @@ 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) && c != 'e') | ||
| n++; | ||
| else{ | ||
| if (level == 0 && Py_ISALPHA(c)){ | ||
| n++; | ||
| } | ||
| if (c == 'e' && (*format == 's' || *format == 't')){ | ||
| format++; | ||
| continue; | ||
| } | ||
| if (!mustbetuple){ | ||
| switch (c){ | ||
| case 'y': | ||
| case 's': | ||
| case 'z': | ||
| if (*format != '*'){ | ||
| mustbetuple = 1; | ||
| } | ||
| break; | ||
| case 'S': | ||
| case 'Y': | ||
| case 'U': | ||
| mustbetuple = 1; | ||
| break; | ||
| case 'O': | ||
| if (*format != '&'){ | ||
| mustbetuple = 1; | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if (!PySequence_Check(arg) || PyBytes_Check(arg)){ | ||
| if (istuple){ | ||
| /* fallthrough */ | ||
| } | ||
| else if (!PySequence_Check(arg) || | ||
| PyUnicode_Check(arg) || PyBytes_Check(arg) || PyByteArray_Check(arg)) | ||
| { | ||
| levels[0] = 0; | ||
| PyOS_snprintf(msgbuf, bufsize, | ||
| "must be %d-item sequence, not %.50s", | ||
| "must be %d-item tuple, not %.50s", | ||
| n, | ||
| arg == Py_None ? "None" : Py_TYPE(arg)->tp_name); | ||
| return msgbuf; | ||
| } | ||
| else{ | ||
| if (mustbetuple){ | ||
| if (PyErr_WarnFormat(PyExc_DeprecationWarning, 0, | ||
| "argument must be %d-item tuple, not %T", n, arg)) | ||
| { | ||
| return msgbuf; | ||
| } | ||
erlend-aasland marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| len = PySequence_Size(arg); | ||
| if (len != n){ | ||
| levels[0] = 0; | ||
| PyOS_snprintf(msgbuf, bufsize, | ||
| "must be %s of length %d, not %zd", | ||
| mustbetuple ? "tuple" : "sequence", n, len); | ||
| return msgbuf; | ||
| } | ||
| arg = PySequence_Tuple(arg); | ||
| if (arg == NULL){ | ||
| return msgbuf; | ||
erlend-aasland marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| } | ||
| len = PySequence_Size(arg); | ||
| len = PyTuple_GET_SIZE(arg); | ||
| if (len != n){ | ||
| levels[0] = 0; | ||
| PyOS_snprintf(msgbuf, bufsize, | ||
| "must be sequence of length %d, not %zd", | ||
| "must be tuple of length %d, not %zd", | ||
| n, len); | ||
| if (!istuple){ | ||
| Py_DECREF(arg); | ||
| } | ||
| return msgbuf; | ||
| } | ||
| format = *p_format; | ||
| for (i = 0; i < n; i++){ | ||
| const char *msg; | ||
| PyObject *item; | ||
| item = PySequence_GetItem(arg, i); | ||
| if (item == NULL){ | ||
| PyErr_Clear(); | ||
| levels[0] = i+1; | ||
| levels[1] = 0; | ||
| strncpy(msgbuf, "is not retrievable", bufsize); | ||
| return msgbuf; | ||
| } | ||
| PyObject *item = PyTuple_GET_ITEM(arg, i); | ||
| msg = convertitem(item, &format, p_va, flags, levels+1, | ||
| msgbuf, bufsize, freelist); | ||
| /* PySequence_GetItem calls tp->sq_item, which INCREFs */ | ||
| Py_XDECREF(item); | ||
| if (msg != NULL){ | ||
| levels[0] = i+1; | ||
| if (!istuple){ | ||
| Py_DECREF(arg); | ||
| } | ||
| return msg; | ||
| } | ||
| } | ||
| *p_format = format; | ||
| if (!istuple){ | ||
| Py_DECREF(arg); | ||
| } | ||
| return NULL; | ||
| } | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.