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
PyTuple_GetSlice docs minor inaccuracy #82738
Comments
https://docs.python.org/3/c-api/tuple.html#c.PyTuple_GetSlice In the c-api it says (emphasis mine): PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)¶
Return value: New reference.
But when slicing the entire tuple, CPython will return already existing instance, not a new tuple. https://github.com/python/cpython/blob/3.8/Objects/tupleobject.c#L448-L451 I propose the language in the docs be loosened so as not to tie the hands of implementation |
Verified from Python.
>>> t = (1,2,3)
>>> t2 = t[:]
>>> id(t), id(t2)
(1672756229504, 1672756229504) A partial slice cannot the original tuple, so I presume that the emphasis is about returning a (new) *tuple*, rather than some sort of view of the original. However,
Py_INCREF(a);
return (PyObject *)a;
date back to at least 1997 (GvR), so the optimization is not new. I don't know what should replace 'New reference.' 'Old or new reverence.'? "Take a slice of the tuple pointed to by p from low to high and return it **as a new tuple**." could be replaces with "Return the slice of the tuple point to by p for low to high. If it is a proper subslice, return a new tuple." This leave it undefined when a complete slice. |
The part about "new tuple" can be removed. The documentation olready contains an automatically generated note about a new reference. PR 16925 improves the documentation for getting/setting items/slices of lists and tuples. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: